-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo_backend.js
73 lines (66 loc) · 2.34 KB
/
mongo_backend.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
var crypto = require('crypto');
module.exports = function (coll_name, backend_options) {
backend_options || (backend_options = {});
if (!backend_options.db) throw new Error('must pass a node-mongodb-native db with backend_options.db');
var db = backend_options.db;
function escapeBase64 (str) {
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}
function hash (id) {
return escapeBase64(crypto.createHash('sha1').update(id).digest('base64'))
}
var coll_path = coll_name;
if (backend_options.key_prefix && backend_options.key_prefix.length) {
coll_path += '.' + backend_options.key_prefix.map(hash).join('.')
}
var coll = db.collection(coll_path);
return {
load: function (id, opts, cb) {
coll.findOne({_id: id}, opts, function (err, doc) {
if (err) return cb(err)
if (doc) delete doc._id
cb(null, doc)
});
},
save: function (id, obj, opts, cb) {
var tmp_obj = JSON.parse(JSON.stringify(obj))
tmp_obj._id = id
coll.save(tmp_obj, function (err, result) {
var doc = result && result.upserted
if (doc) {
delete doc._id
}
cb(err, doc)
})
},
destroy: function (id, opts, cb) {
if (typeof opts.w === 'undefined') opts.w = 1;
this.load(id, {}, function (err, obj) {
if (err) return cb(err);
if (!obj) return cb(null, null);
coll.deleteOne({_id: id}, opts, function (err) {
if (err) return cb(err);
cb(null, obj);
});
});
},
select: function (opts, cb) {
if (typeof opts.query === 'undefined') opts.query = {};
var cursor = coll.find(opts.query);
if (typeof opts.project === 'object') cursor = cursor.project(opts.project);
if (typeof opts.comment === 'string') cursor = cursor.comment(opts.comment);
if (typeof opts.hint === 'object') cursor = cursor.hint(opts.hint);
if (typeof opts.limit === 'number') cursor = cursor.limit(opts.limit);
if (typeof opts.skip === 'number') cursor = cursor.skip(opts.skip);
if (typeof opts.sort === 'object') cursor = cursor.sort(opts.sort);
cursor.toArray(function (err, docs) {
if (err) return cb(err)
docs = docs.map(function (doc) {
delete doc._id
return doc
})
cb(null, docs)
})
}
};
};