-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
138 lines (117 loc) · 4.09 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
134
135
136
137
138
const mongoose = require("mongoose");
const logModule = "chassis-database";
class Database {
/**
* Initialise database connection.
*
* @param {Function} callback
* @param {Object} [options]
*/
static init(callback, options) {
let _config = config.getConfigByID("db");
options = _config ? _config.data : undefined;
if (!options) {
callback({message: "Cannot connect to database, no configuration found."});
} else {
if (!mongoose.connection.readyState) { //disconnected
Log.info("Initialising database connection...", logModule);
connect(function(err, DB) {
if (err) {
callback(err);
} else {
Log.info("Established database connection.", logModule);
callback(null, DB);
}
}, options);
} else if (mongoose.connection.readyState === 2) { //connecting
wait(0);
} else if (mongoose.connection.readyState === 1) { //connected
callback(null, DB);
} else {
callback({message: "Failed to establish a connection to the database."});
}
function wait(i) {
Log.info("Waiting for database to complete initialisation...", logModule);
setTimeout(function() {
if (i < 5) {
if (mongoose.connection.readyState) {
callback(null);
} else {
wait(i+1);
}
}
}, 5000);
}
}
}
static getMongoose() {
return mongoose;
}
static getModel() {
}
static getAbstractModel() {
return require("./model/abstract/modelAbstract");
}
static getReadyState() {
return mongoose.connection.readyState;
}
/**
*
* @param {Array} data - Array of instances
* @param {String} method - Method to call on the instance
* @param {Function} callback
* @param {Object} [options] - Optional additional options
*/
static transaction(data, method, callback, options) {
let counter = 0;
let response = {
err: [],
success: []
};
for (let i=0; i<data.length; i++) {
data[i][method](function(err, res) {
if (err) {
response.err.push(err);
} else {
response.success.push(res);
}
reqComplete();
}, options);
}
function reqComplete() {
counter++;
if (counter === data.length) { //if all requests have completed
callback(response);
}
}
}
}
function connect(callback, options) {
options.authSource = options.authSource ? options.authSource : "admin";
options.socketTimeoutMS = options.socketTimeoutMS ? options.socketTimeoutMS : 0;
options.keepAlive = options.keepAlive ? options.keepAlive : true;
options.poolSize = options.poolSize ? options.poolSize : 5;
options.useNewUrlParser = options.useNewUrlParser ? options.useNewUrlParser : true;
options.useUnifiedTopology = options.useUnifiedTopology ? options.useUnifiedTopology : true;
mongoose.connect("mongodb://" + options.host + ":" + options.port + "/" + options.dbName + "?authSource=" + options.authSource, stripUnsupportedOptions(options));
let DB = mongoose.connection;
DB.on("error", function (err) {
callback(err);
});
DB.once("open", function () {
callback(null, DB);
});
}
/**
* Strips unsupported options from the options object before passing to the Mongo client.
* This prevents annoying warnings in the console.
*
* @param {Object} options
*/
function stripUnsupportedOptions(options) {
let _options = {...options};
delete _options.host;
delete _options.port;
return _options;
}
module.exports = Database;