-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
49 lines (42 loc) · 1.17 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
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @fileoverview Facilitate database connections.
* @author SardonyxApp
* @license MIT
*/
const fs = require('fs');
const mysql = require('mysql');
require('dotenv').config();
const state = {
pool: null
};
/**
* @description Start a database pool
* @param {Function} callback
*/
exports.connect = callback => {
state.pool = process.env.DB_TYPE === 'cloudsql'
? mysql.createPool({
socketPath: `/cloudsql/${process.env.DB_INSTANCE}`,
user: process.env.DB_LOGIN,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
timezone: '+09:00'
})
: mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_LOGIN,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
ssl: (process.env.DB_TYPE === 'ssl' ? {
ca: fs.readFileSync(__dirname + '/certs/ca.pem')
} : {}),
timezone: '+09:00'
});
console.log(`Created pool. User: ${process.env.DB_LOGIN}@${process.env.DB_HOST || '%'} and Database: ${process.env.DB_DATABASE}`);
callback();
};
/**
* @description Use a pre-existing database pool
* @returns Pool
*/
exports.get = () => state.pool;