-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
36 lines (30 loc) · 1.16 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const Pool = require("pg").Pool;
const dotenv = require("dotenv");
dotenv.config({ path: "./.env", encoding: "utf-8" });
const devConfig = {
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
};
const prodConfig = {
connectionString: process.env.POSTGRES_URL,
ssl: {
require: true,
},
};
/**
* XXX Next.js FORCES the NODE_ENV to be either "development" or "production" at build time.
* Because of this, we have a difference between the process.env.NODE_ENV given by Express and the on given by Next
* In order to avoid this huge issue, we stored the real NODE_ENV in env.SERVER_ENV
* And this config property must be used to get the NODE_ENV instead of process.env.NODE_ENV
*
* This function is compatible with Express/Next, and can be used anywhere, on the client and server.
*
* @returns {string}
* @see XXX https://github.com/zeit/next.js/issues/3605#issuecomment-370255754
*/
const realNodeEnv = process.env.SERVER_ENV || process.env.NODE_ENV;
const pool = new Pool(realNodeEnv === 'production' ? prodConfig : devConfig);
module.exports = pool;