-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.js
163 lines (132 loc) · 5.71 KB
/
models.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
const Mongo = require( 'pr0mised-m0ng0' ),
Log = require( './lib/methods/log' ),
Events = require( './lib/methods/events' ),
Aggregation = require( './lib/db/Aggregation' ),
local = 'localhost/database';
// Are the env. variables set ?
let url = process.env.DATABASE || process.env.database;
// Try the default rancher config
if ( !url ) {
if (
process.env.MONGODB_MONGO_CLUSTER_1_PORT &&
process.env.MONGODB_MONGO_CLUSTER_2_PORT && process.env.MONGODB_MONGO_CLUSTER_3_PORT
) url = 'mongodb://mongodb_mongo-cluster_1/database';
else {
url = process.env.DATABASE_PORT || process.env.database_port;
if ( url ) {
url += '/database';
if ( ~url.indexOf( '://' ) )
url = url.split( '://', 2 )[ 1 ];
}
}
}
// Otherwise, localhost default config
if ( !url ) url = local;
Log.info( '🗄 Using MongoDB', url );
module.exports = new Proxy( Mongo( url, {
server: {
socketOptions: {
socketTimeoutMS: 30000,
connectTimeoutMS: 30000
}
}
} ), {
get( db, model ) {
if ( typeof model !== 'string' ) return undefined;
if ( ~[ 'inspect', 'valueOf' ].indexOf( model ) )
return undefined;
// Format model name (lower case, plurial)
model = model.toLowerCase();
if ( model.charAt( model.length - 1 ) != 's' )
model += 's';
// English plurial exceptions
if ( model == 'persons' || model == 'peoples' )
model = 'people';
// Sugar access to ObjectID & getCollectionNames
if ( model == 'objectids' )
return db.ObjectId;
if ( model == 'getcollectionnames' )
return db.getCollectionNames;
// Return the Proxy
return new Proxy( db[ model ], {
get( modelORM, method ) {
if ( !method.toLowerCase )
return Log.error( 'Forbidden model access' );
// Aggregation sugar pipeline builder
if ( method.toLowerCase() == 'aggregation' )
return Aggregation( modelORM );
if ( method.toLowerCase() == 'match' )
return ( a ) => Aggregation( modelORM )
.match( a );
if ( method.toLowerCase() == 'search' )
return ( a ) => Aggregation( modelORM )
.search( a );
// Generic method
if ( ~[ 'find', 'count', 'findone' ].indexOf( method ) )
return modelORM[ method ];
// Not a method
if ( typeof modelORM[ method ] != 'function' && method != 'set' )
return modelORM[ method ];
// Shortcuts / Aliases
if ( method == 'create' ) method = 'insert';
if ( method == 'delete' ) method = 'remove';
// Custom methods
return function () {
let args = arguments;
let returnValue = false;
if ( method == 'set' ) {
returnValue = true;
if ( !args[ 2 ] ) args[ 2 ] = {};
if ( args[ 2 ].new === undefined ) args[ 2 ].new = true;
args[ 2 ].query = args[ 0 ];
args[ 2 ].update = {
$set: args[ 1 ]
};
method = 'findAndModify';
args = [ args[ 2 ] ];
}
if ( method == 'insert' || method == 'save' )
if ( Array.isArray( args[ 0 ] ) ) {
let d = new Date();
args[ 0 ].map( entry => {
if ( !entry.createdAt )
entry.createdAt = d;
} );
} else if ( !args[ 0 ].createdAt )
args[ 0 ].createdAt = new Date();
if ( method == 'save' )
args[ 0 ].updatedAt = new Date();
if ( method == 'update' ) {
if ( !args[ 1 ].$set ) args[ 1 ].$set = {};
args[ 1 ].$set.updatedAt = new Date();
returnValue = true;
}
if ( method == 'findAndModify' && args[ 0 ] && args[ 0 ].update && args[ 0 ].update.$set ) {
args[ 0 ].update.$set.updatedAt = new Date();
returnValue = true;
}
return new Promise( ( resolve, reject ) => {
let promise = modelORM[ method ].apply( modelORM, args );
promise.then( result => {
if ( returnValue )
result = result.value;
resolve( result );
if ( method == 'findAndModify' ) method = 'update';
if ( !~[
'insert',
'update',
'save'
].indexOf( method ) )
return;
Events.emit( method + ':' + model, result );
} )
.catch( err => {
reject( err );
Log.warn( `⚠️ ${method}.${model} :`, err );
} );
} );
};
}
} );
}
} );