-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
152 lines (119 loc) · 4.95 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// mongoose-multi connections
var fs = require('fs');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
// Register plugins
mongoose.plugin(require('./plugin/findMinOne'));
mongoose.plugin(require('./plugin/findExactOne'));
// logging
var log;
try { // try using rf-log
log = require(require.resolve('rf-log'));
} catch (e) {
log = {
error: console.log,
info: console.log,
success: console.log,
critical: console.error
};
}
// public vars
module.exports.db = {};
module.exports.schemaFile = {};
module.exports.connections = {};
module.exports.start = function (connections, schemaFile) {
var db = module.exports.db;
module.exports.connections = connections;
if (typeof schemaFile === 'string') {
var schemaPath = schemaFile;
schemaFile = {};
// extract all schemas from schemafile or folder with schema files
try {
if (fs.statSync(schemaPath).isDirectory()) {
// require all files within folder, extract db schema and sort in corresponding obj
// NOTE: files need to have the same name as the database
fs.readdirSync(schemaPath).forEach(function (fileName) {
var filePath = schemaPath + '/' + fileName;
var words = fileName.split('.');
var dbName = words[0];
var extension = words[words.length - 1];
if (fs.statSync(filePath).isDirectory()) {
log.critical('tried to require ' + fileName + ', but path is a folder! Aborting. Please Check your Schema folder.');
}
if (extension === 'js') {
var dbSchema = require(filePath);
schemaFile[dbName] = dbSchema;
}
});
} else { // path is the schmea file
schemaFile = require(schemaPath);
}
} catch (error) {
log.critical('tried extract schemas, please Check your Schema folder, an error occured: ' + error);
}
} // else => schemaFile is already the complete schema obj
module.exports.schemaFile = schemaFile;
for (var conName in connections) {
var connection = connections[conName];
var options = {}, url = connection;
// obj holding url + options => transform
if (typeof connection !== 'string') {
options = connection.options || {};
url = connection.url;
}
// add mongoose option for newer version to prevent deprecation warning
options.useNewUrlParser = options.useNewUrlParser || true;
startConnection(conName, url, schemaFile[conName], options);
}
function startConnection (name, url, schemas, options) {
// check input data
if (!name) return log.error('[mongoose-multi] Error - no name specified for db');
if (!url) return log.error('[mongoose-multi] Error - no url defined for db ' + name);
if (!schemas) return log.error('[mongoose-multi] Error - no schema found for db ' + name);
// merge options
options = options || {};
options.useUnifiedTopology = true;
// connect database
var opts = JSON.parse(JSON.stringify(options)); delete opts.auto_reconnect; // remove "auto_reconnect" as it is no longer supported
connections[name] = mongoose.createConnection(url, opts);
var dbcon = connections[name];
// assemble the return object
// return pure mongoose connection => use in other modules; use the events above
db[name] = { mongooseConnection: dbcon };
// create connections for this database
for (var schemaName in schemas) {
var pluralAddition = (schemaName[schemaName.length - 1] === 's') ? 'es' : 's';
db[name][schemaName + pluralAddition] = dbcon.model(schemaName, schemas[schemaName]);
}
var prefix = '[mongoose-multi] DB ' + name;
dbcon.on('connecting', function () {
log.info(prefix + ' connecting to ' + url);
});
dbcon.on('error', function (error) {
log.error(prefix + ' connection error: ', error);
mongoose.disconnect();
});
dbcon.on('connected', function () {
log.success(prefix + ' connected');
});
dbcon.once('open', function () {
log.info(prefix + ' connection open');
});
dbcon.on('reconnected', function () {
log.success(prefix + ' reconnected, ' + url);
});
dbcon.on('disconnected', function () {
log.error(prefix + ' disconnected, ' + url);
// there have been several issues with reconnecting
// we simple restart the whole process and try it again
// we assume we will have created our own framework first, before this is fixed reliable in mongoose
if (options.auto_reconnect !== false) {
setTimeout(function () {
log.error('[mongoose-multi] shutting down application for restart: Try to reconnet DB.');
process.exit(0);
}, 10000);
}
});
}
return db;
};