Uh oh!
There was an error while loading. Please reload this page.
This repository was 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
Expand file tree
/
Copy pathdatabase.js
More file actions
Latest commit
133 lines (110 loc) · 3.9 KB
/
Copy pathdatabase.js
File metadata and controls
133 lines (110 loc) · 3.9 KB
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
constpath=require('path');
constfs=require('fs');
varcontext;
module.exports={
start: function(callback=()=>{}){
// Imports
constpg=require('pg');
constSequelize=require('sequelize');
console.log('Initializing context');
// Initialize the context
context={
fs: fs,
pg: pg,
path: path,
Sequelize: Sequelize,
constants: {},
models: {}
};
callback(context);
returncontext;
},
connect: function(postgresCS,callback=()=>{},logging){
constcontext=this.createContext(postgresCS,logging);
returnthis.loadModels()
.then(()=>{
returncontext.sequelize.authenticate()
})
.then(()=>{
returncontext.sequelize.sync()
})
.then(()=>{
console.log('Connection has been established successfully.');
returncallback(context);
},(err)=>{
console.error(err);
returnprocess.exit(1);
});
},
loadModels: function(){
constmodelBasePath=path.join(__dirname,'api','models');
returnnewPromise(function(resolve,reject){
fs.readdir(modelBasePath,(err,files)=>{
if(err){
reject(err);
return;
}
files.forEach((file)=>{
constmodel=context.sequelize.import(path.join(modelBasePath,file));
context.models[model.name]=model;
});
// initialize relationships
Object.keys(context.models).forEach(function(modelName){
if("associate"incontext.models[modelName]){
context.models[modelName].associate(context.models);
}
});
resolve();
});
});
},
createContext: function(postgresCS,logging){
context=this.start();
vardbConfig=require('./config.js.template');
//Create the DB connection string
vardatabaseParams=dbConfig.database;
context.config=databaseParams;
if(logging===undefined){
logging=dbConfig.general.logging;
}
varconfigDB={
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
};
constdatabaseURI=postgresCS||process.env.databaseuri;
if(databaseURI){
//logging is enabled by default
dbConfig.logging=logging;
dbConfig.pool={
max: 1,
min: 0
};
context.sequelize=newcontext.Sequelize(databaseURI,dbConfig);
}else{
context.sequelize=newcontext.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
});
}
returncontext;
},
getContext: function(){
if(context){
returncontext;
}
console.log('Failed to retrieve context: context doesn\'t exist.');
}
};