-
Notifications
You must be signed in to change notification settings - Fork 8
/
db.js
74 lines (72 loc) · 2.04 KB
/
db.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
const debug = require('./debug');
const { MongoClient, ObjectID } = require('mongodb');
const assert = require('assert');
const url = 'mongodb://localhost:27017/test';
module.exports = {
id(id) {
return new ObjectID(id);
},
connect(cb) {
assert(!this.connected);
MongoClient.connect(url, (err, db) => {
assert.equal(null, err);
this.connected = true;
this.db = db;
cb(err);
});
},
disconnect() {
assert(this.connected);
this.db.close();
this.connected = false;
this.db = null;
},
insert(coll, obj, cb) {
debug('%s.insert %s', coll, JSON.stringify(obj));
return this.db.collection(coll).insert(obj, cb);
},
find(coll, query, sort, cb) {
if (!cb) {
cb = sort;
sort = null;
}
const cursor = this.db.collection(coll).find(query);
if (sort) cursor.sort(sort);
cursor.toArray(cb);
debug(`${coll}.find ${JSON.stringify(query)}`);
},
update(coll, query, set, cb = null) {
debug(`${coll}.update ${JSON.stringify(query)}: set ${JSON.stringify(set)}`);
this.db.collection(coll).update(
query,
{
$set: set,
$currentDate: { "lastModified": true },
},
cb
);
},
upsert(coll, query, set, cb = null) {
debug(`${coll}.upsert ${JSON.stringify(query)}: set ${JSON.stringify(set)}`);
this.db.collection(coll).update(
query,
{
$set: set,
$currentDate: { "lastModified": true },
},
{
upsert: true,
},
cb
);
},
remove(coll, query, cb = null) {
debug(`${coll}.remove ${JSON.stringify(query)}`);
this.db.collection(coll).remove(query, (err, results) => {
if (cb)
cb(err, results);
else
assert.equal(null, err);
});
},
};