This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatabase.js
133 lines (110 loc) · 3.9 KB
/
database.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const path = require('path');
const fs = require('fs');
var context;
module.exports = {
start: function(callback = () => {}) {
// Imports
const pg = require('pg');
const Sequelize = require('sequelize');
console.log('Initializing context');
// Initialize the context
context = {
fs: fs,
pg: pg,
path: path,
Sequelize: Sequelize,
constants: {},
models: {}
};
callback(context);
return context;
},
connect: function(postgresCS, callback = () => {}, logging) {
const context = this.createContext(postgresCS, logging);
return this.loadModels()
.then(() => {
return context.sequelize.authenticate()
})
.then(() => {
return context.sequelize.sync()
})
.then(() => {
console.log('Connection has been established successfully.');
return callback(context);
}, (err) => {
console.error(err);
return process.exit(1);
});
},
loadModels: function() {
const modelBasePath = path.join(__dirname, 'api', 'models');
return new Promise(function(resolve, reject) {
fs.readdir(modelBasePath, (err, files) => {
if(err) {
reject(err);
return;
}
files.forEach((file) => {
const model = context.sequelize.import(path.join(modelBasePath, file));
context.models[model.name] = model;
});
// initialize relationships
Object.keys(context.models).forEach(function(modelName) {
if ("associate" in context.models[modelName]) {
context.models[modelName].associate(context.models);
}
});
resolve();
});
});
},
createContext: function(postgresCS, logging) {
context = this.start();
var dbConfig = require('./config.js.template');
//Create the DB connection string
var databaseParams = dbConfig.database;
context.config = databaseParams;
if(logging === undefined) {
logging = dbConfig.general.logging;
}
var configDB = {
database: databaseParams.collection, //env var: PGDATABASE
username: databaseParams.username,
password: databaseParams.password,
host: databaseParams.host, // Server hosting the postgres database
port: databaseParams.port, //env var: PGPORT
max: 10, // max number of clients in the pool
min: 0,
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};
const databaseURI = postgresCS || process.env.databaseuri;
if (databaseURI) {
//logging is enabled by default
dbConfig.logging = logging;
dbConfig.pool = {
max: 1,
min: 0
};
context.sequelize = new context.Sequelize(databaseURI, dbConfig);
} else {
context.sequelize = new context.Sequelize(configDB.database, configDB.username, configDB.password, {
host: configDB.host,
dialect: 'postgres',
port: configDB.port,
pool: {
max: configDB.max,
min: configDB.min,
idle: configDB.idleTimeoutMillis
},
logging: logging
});
}
return context;
},
getContext: function() {
if(context) {
return context;
}
console.log('Failed to retrieve context: context doesn\'t exist.');
}
};