-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (139 loc) · 5.84 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const mongoose = require('mongoose');
const {EventEmitter} = require('events');
const fs = require('fs');
const defaultOptions = {
connectionString: '',
connectionOptions: {},
path: '',
inject: [],//inject additional options
debug: false,
log: function(){
return this.debug && console.log.apply(console,arguments);
},
extend: function(mongoose){} //extend mongoose with global plugins, custom types, etc.
};
// taken from https://stackoverflow.com/a/55390173/2401804
const semverGreaterThan = function(versionA, versionB){
var versionsA = versionA.split(/\./g),
versionsB = versionB.split(/\./g)
while (versionsA.length || versionsB.length) {
var a = Number(versionsA.shift()), b = Number(versionsB.shift())
if (a == b)
continue
return (a > b || isNaN(b))
}
return false
}
module.exports = helper;
function helper(app,userOptions){
if(typeof userOptions != 'object'){
throw new Error('`express-mongoose-helper` expects parameter options to be of type object.');
}
const options = {...defaultOptions,...userOptions};
if(typeof options.extend == 'function'){
options.extend(mongoose);
}
function model(name,schema,schemaOptions,callback){
// to keep backwards compatibility accept schemaOptions/callback in either order
switch(true){
case (typeof schemaOptions == 'object' && typeof callback == 'function'):
break;
case (typeof schemaOptions == 'function' && typeof callback == 'object'):
var tmp = schemaOptions;
schemaOptions = callback;
callback = tmp;
break;
case (typeof schemaOptions != 'function' && !schemaOptions):
schemaOptions = {};//could break if internal implementation of option handling changes (unlikely)
break;
case (typeof schemaOptions == 'function' && typeof callback == 'undefined'):
callback = schemaOptions;
schemaOptions = {};
break;
}
var schema = new mongoose.Schema(schema,schemaOptions);
if(typeof callback === 'function'){
callback(schema,mongoose);
}
if(model.hasOwnProperty(name)){
throw new Error(`\`express-mongoose-helper\` \`app.model.${name}\` already exists.`);
}
model[name] = mongoose.model(name,schema);
model[name].mongoose = ()=> mongoose;//give em a function to retrieve the mongoose instance
options.log('`express-mongoose-helper` created model `app.model.'+name+'`');
return model[name];// in case they need to do something with the model
};
model.exists = function( modelName ){
return model.hasOwnProperty( modelName ) && model[modelName] instanceof mongoose.model;
};
Object.defineProperty(app,'model',{
get: function(){ return model; },
set: function(){ return false; }
});
if(semverGreaterThan(mongoose.version,'4.11.0') && !semverGreaterThan(mongoose.version,'5.0.0')){
options.connectionOptions.useMongoClient = true;// opt into mongoose >4.11 new connection logic, but wait, it can't be over version 5
}
if(semverGreaterThan(mongoose.version,'5.5.5')){
options.connectionOptions.useFindAndModify = false;
}
options.connectionOptions.useNewUrlParser = true;
if(semverGreaterThan(mongoose.version,'5.2.9')){
mongoose.set('useCreateIndex', true);
}
mongoose.connect(options.connectionString,options.connectionOptions);
mongoose.connection.on('connected', function () {
options.log('`express-mongoose-helper` Mongoose connected.');
app.emit('mongoose.connected');
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
options.log('`express-mongoose-helper` error'+err);
app.emit('mongoose.error',err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
app.emit('mongoose.disconnected');
});
if(options.path == ''){
throw new Error('`express-mongoose-helper` option `path` cannot be blank.');
}
const inject = [
app,
mongoose.Schema.Types
];
for(var i=0;i<options.inject.length;i++){
inject.push(options.inject[i]);//now allow user supplied variables to be injected.
}
options.log('`express-mongoose-helper` is loading files from '+options.path);
fs.readdir(options.path,(err,files)=>{
options.log(files);
if(err) {
throw err;// throw it up throw it up
}
if(!Array.isArray(files)) {
options.log('`express-mongoose-helper` no models to load');
}else{
files.forEach((file)=>{
const ext = file.slice((file.lastIndexOf(".") - 1 >>> 0) + 2);
if(ext=='js'){
options.log('`express-mongoose-helper` loading file `'+file+'`');
var f = require(options.path+file);
if(typeof f !== 'function'){
throw new Error('`express-mongoose-helper` expects required modules to be a function');
}
f(...inject);
}
if(ext==='mjs'){
//var f = import(()=>options.path+file);
options.log('`express-mongoose-helper` does not support loading EcmaScript Modules at this time. file `'+file+'` is ignored and not loaded.');
}
});
}
app.emit('mongoose.models.ready');
});
return app;
}
helper.standalone = function(options){
const app = new EventEmitter;
return helper(app,options);
}