forked from senecajs/seneca-mongo-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-store.js
413 lines (310 loc) · 8.47 KB
/
mongo-store.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Copyright (c) 2010-2015 Richard Rodger, MIT License */
"use strict";
var _ = require('lodash')
var mongo = require('mongodb')
var name = "mongo-store"
/*
native$ = object => use object as query, no meta settings
native$ = array => use first elem as query, second elem as meta settings
*/
function idstr( obj ) {
return ( obj && obj.toHexString ) ? obj.toHexString() : ''+obj
}
function makeid(hexstr) {
if( _.isString(hexstr) && 24 == hexstr.length ) {
return mongo.ObjectID.createFromHexString(hexstr)
}
return hexstr;
}
function fixquery(qent,q) {
var qq = {};
if( !q.native$ ) {
for( var qp in q ) {
if( !qp.match(/\$$/) ) {
qq[qp] = q[qp]
}
}
if( qq.id ) {
qq._id = makeid(qq.id)
delete qq.id
}
}
else {
qq = _.isArray(q.native$) ? q.native$[0] : q.native$
}
return qq
}
function metaquery(qent,q) {
var mq = {}
if( !q.native$ ) {
if( q.sort$ ) {
for( var sf in q.sort$ ) break;
var sd = q.sort$[sf] < 0 ? 'descending' : 'ascending'
mq.sort = [[sf,sd]]
}
if( q.limit$ ) {
mq.limit = q.limit$
}
if( q.skip$ ) {
mq.skip = q.skip$
}
if( q.fields$ ) {
mq.fields = q.fields$
}
}
else {
mq = _.isArray(q.native$) ? q.native$[1] : mq
}
return mq
}
module.exports = function(opts) {
var seneca = this
var desc
var dbinst = null
var collmap = {}
var specifications = null
function error(args,err,cb) {
if( err ) {
seneca.log.error('entity',err,{store:name})
return true;
}
else return false;
}
function configure(spec,cb) {
specifications = spec
// defer connection
// TODO: expose connection action
if( !_.isUndefined(spec.connect) && !spec.connect ) {
return cb()
}
var conf = 'string' == typeof(spec) ? null : spec
if( !conf ) {
conf = {}
var urlM = /^mongo:\/\/((.*?):(.*?)@)?(.*?)(:?(\d+))?\/(.*?)$/.exec(spec);
conf.name = urlM[7]
conf.port = urlM[6]
conf.server = urlM[4]
conf.username = urlM[2]
conf.password = urlM[3]
conf.port = conf.port ? parseInt(conf.port,10) : null
}
conf.host = conf.host || conf.server
conf.username = conf.username || conf.user
conf.password = conf.password || conf.pass
var dbopts = seneca.util.deepextend({
native_parser:false,
auto_reconnect:true,
w:1
},conf.options)
if( conf.replicaset ) {
var rservs = []
for( var i = 0; i < conf.replicaset.servers.length; i++ ) {
var servconf = conf.replicaset.servers[i]
rservs.push(new mongo.Server(servconf.host,servconf.port,dbopts))
}
var rset = new mongo.ReplSetServers(rservs)
dbinst = new mongo.Db( conf.name, rset, dbopts )
}
else {
dbinst = new mongo.Db(
conf.name,
new mongo.Server(
conf.host || conf.server,
conf.port || mongo.Connection.DEFAULT_PORT,
{}
),
dbopts
)
}
dbinst.open(function(err){
if( err ) {
return seneca.die('open',err,conf);
}
if( conf.username ) {
dbinst.authenticate(conf.username,conf.password,function(err){
if( err) {
seneca.log.error('init','db auth failed for '+conf.username,dbopts)
return cb(err);
}
seneca.log.debug('init','db open and authed for '+conf.username,dbopts)
cb(null)
})
}
else {
seneca.log.debug('init','db open',dbopts)
cb(null)
}
})
}
function getcoll(args,ent,cb) {
var canon = ent.canon$({object:true})
var collname = (canon.base?canon.base+'_':'')+canon.name
if( !collmap[collname] ) {
dbinst.collection(collname, function(err,coll){
if( !error(args,err,cb) ) {
collmap[collname] = coll
cb(null,coll);
}
})
}
else {
cb(null,collmap[collname])
}
}
var store = {
name:name,
close: function(args,cb) {
if(dbinst) {
dbinst.close(cb)
}
else return cb();
},
save: function(args,cb) {
var ent = args.ent
var update = !!ent.id;
getcoll(args,ent,function(err,coll){
if( !error(args,err,cb) ) {
var entp = {};
var fields = ent.fields$()
fields.forEach( function(field) {
entp[field] = ent[field]
})
if( !update && void 0 != ent.id$ ) {
entp._id = makeid(ent.id$)
}
if( update ) {
var q = {_id:makeid(ent.id)}
delete entp.id
coll.update(q,{$set: entp}, {upsert:true},function(err,update){
if( !error(args,err,cb) ) {
seneca.log.debug('save/update',ent,desc)
cb(null,ent)
}
})
}
else {
coll.insertOne(entp,function(err,inserts){
if( !error(args,err,cb) ) {
ent.id = idstr( inserts.ops[0]._id )
seneca.log.debug('save/insert',ent,desc)
cb(null,ent)
}
})
}
}
})
},
load: function(args,cb) {
var qent = args.qent
var q = args.q
getcoll(args,qent,function(err,coll){
if( !error(args,err,cb) ) {
var mq = metaquery(qent,q)
var qq = fixquery(qent,q)
coll.findOne(qq,mq,function(err,entp){
if( !error(args,err,cb) ) {
var fent = null;
if( entp ) {
entp.id = idstr( entp._id )
delete entp._id;
fent = qent.make$(entp);
}
seneca.log.debug('load',q,fent,desc)
cb(null,fent);
}
});
}
})
},
list: function(args,cb) {
var qent = args.qent
var q = args.q
getcoll(args,qent,function(err,coll){
if( !error(args,err,cb) ) {
var mq = metaquery(qent,q)
var qq = fixquery(qent,q)
coll.find(qq,mq,function(err,cur){
if( !error(args,err,cb) ) {
var list = []
cur.each(function(err,entp){
if( !error(args,err,cb) ) {
if( entp ) {
var fent = null;
if( entp ) {
entp.id = idstr( entp._id )
delete entp._id;
fent = qent.make$(entp);
}
list.push(fent)
}
else {
seneca.log.debug('list',q,list.length,list[0],desc)
cb(null,list)
}
}
})
}
})
}
})
},
remove: function(args,cb) {
var qent = args.qent
var q = args.q
var all = q.all$ // default false
var load = _.isUndefined(q.load$) ? true : q.load$ // default true
getcoll(args,qent,function(err,coll){
if( !error(args,err,cb) ) {
var qq = fixquery(qent,q)
if( all ) {
coll.remove(qq,function(err){
seneca.log.debug('remove/all',q,desc)
cb(err)
})
}
else {
var mq = metaquery(qent,q)
coll.findOne(qq,mq,function(err,entp){
if( !error(args,err,cb) ) {
if( entp ) {
coll.remove({_id:entp._id},function(err){
seneca.log.debug('remove/one',q,entp,desc)
var ent = load ? entp : null
cb(err,ent)
})
}
else cb(null)
}
})
}
}
})
},
native: function(args,done) {
dbinst.collection('seneca', function(err,coll){
if( !error(args,err,done) ) {
coll.findOne({},{},function(err,entp){
if( !error(args,err,done) ) {
done(null,dbinst)
}
else {
done(err)
}
})
}
else {
done(err)
}
})
}
}
var meta = seneca.store.init(seneca,opts,store)
desc = meta.desc
seneca.add({init:store.name,tag:meta.tag},function(args,done){
configure(opts,function(err){
if( err ) return seneca.die('store',err,{store:store.name,desc:desc});
return done();
})
})
return {name:store.name,tag:meta.tag}
}