-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.js
59 lines (50 loc) · 1.92 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
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');
const factory = require('./commons/factory.js');
const tenancy = require('./commons/tenancy.js');
mongoose.Promise = global.Promise;
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
mongoosePaginate.paginate.options = { lean: false };
mongoose.dbs = [];
const sleep = function(ms){ return new Promise(resolve=>{ setTimeout(resolve, ms) }) };
exports.init = async function(mode){
let go = false;
let uri = null;
// Select MongoDb server (in memory version of testing)
if(mode === "test") {
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongod = await MongoMemoryServer.create();
uri = mongod.getUri();
}
else {
if (process.argv.includes('docker')) process.env.DATABASE = process.env.DATABASE.replace('localhost', 'database');
uri = process.env.DATABASE;
}
// Connect to tenants catalogue database
while(!go) {
try {
mongoose.dbs['catalog'] = await mongoose.createConnection(uri);
const tenderSchema = require('./models/tenantSchema.js');
mongoose.dbs['catalog'].model('Tenant', tenderSchema);
console.error('Database connected!');
go = true;
}
catch (error) {
console.error('Database connection error, retry in 3 secs... (' + error + ')');
await sleep(3000);
}
}
// Init tenants
const tenants = await tenancy.getList();
if(tenants.length < 1) {
console.error('Database initialization error, missing default tenants');
process.exit();
}
for(let i=0; i<tenants.length; i++) {
await tenancy.init(tenants[i]);
}
console.error('Database ready!');
}