-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
43 lines (34 loc) · 1.13 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
var NedbDatastore = require('nedb')
var thenify = require('thenify')
function fromInstance(nedbInstance) {
var newDB = { nedb: nedbInstance }
var methods = [ 'loadDatabase', 'insert', 'find', 'findOne', 'count', 'update', 'remove', 'ensureIndex', 'removeIndex' ]
for (var i = 0; i < methods.length; ++i) {
var m = methods[i]
newDB[m] = thenify(nedbInstance[m].bind(nedbInstance))
}
newDB.cfind = function(query, projections) {
var cursor = nedbInstance.find(query, projections)
cursor.exec = thenify(cursor.exec.bind(cursor))
return cursor
}
newDB.cfindOne = function(query, projections) {
var cursor = nedbInstance.findOne(query, projections)
cursor.exec = thenify(cursor.exec.bind(cursor))
return cursor
}
newDB.ccount = function(query) {
var cursor = nedbInstance.count(query)
cursor.exec = thenify(cursor.exec.bind(cursor))
return cursor
}
return newDB
}
function datastore(options) {
var nedbInstance = new NedbDatastore(options)
return fromInstance(nedbInstance)
}
// so that import { datastore } still works:
datastore.datastore = datastore
datastore.fromInstance = fromInstance
module.exports = datastore