Skip to content

Commit b09020e

Browse files
committed
Removed 'Async' suffixes from the wrapped object; added fromInstance method
1 parent e6c6efb commit b09020e

File tree

4 files changed

+85
-61
lines changed

4 files changed

+85
-61
lines changed

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ async function doDatabaseStuff() {
2626
autoload: true // so that we don't have to call loadDatabase()
2727
})
2828
29-
await DB.insertAsync([{
29+
await DB.insert([{
3030
num: 1, alpha: 'a'
3131
}, {
3232
num: 2, alpha: 'b'
3333
}])
3434
35-
let document = await DB.findOneAsync({ num: 1 })
35+
let document = await DB.findOne({ num: 1 })
3636
3737
// use NeDB cursors:
3838
let documents = await DB.cfind({})
3939
.projection({ num: 1, _id: 0 })
40-
.execAsync()
40+
.exec()
4141
}
4242
4343
doDatabaseStuff()
@@ -48,18 +48,20 @@ API
4848

4949
## datastore(options)
5050

51-
Returns an extended `nedb.Datastore` instance (`options` are passed through to the NeDB constructor), with promisified methods (suffixed with 'Async').
51+
Returns an object that proxies to an internal `nedb.Datastore` instance (`options` are passed through to the NeDB constructor), with promisified methods.
5252

53-
For example, you may use `findAsync(...)`, `insertAsync(...)`, etc.
54-
55-
It also includes the two extension methods `cfind(...)`, and `cfindOne(...)` that return promisified cursors, so that you may do:
53+
It also includes extension methods `cfind(...)`, `cfindOne(...)`, and `ccount(...)` that return promisified cursors, so that you may do:
5654

5755
```
58-
let results = await myDataStore.cfindA({ moo: 'goo' })
56+
let results = await myDataStore.cfind({ moo: 'goo' })
5957
.projection({ moo: 1, _id: 0 }) // see NeDB cursor methods
60-
.execAsync()
58+
.exec()
6159
```
6260

61+
## datastore.fromInstance(nedbInstance)
62+
63+
Use this method if you already have a NeDB instance that you want to wrap with the promisified methods.
64+
6365
Testing
6466
=======
6567

index.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
var NedbDatastore = require('nedb')
22
var thenify = require('thenify')
33

4-
function datastore(options) {
5-
options = options || { }
6-
var DB = new NedbDatastore(options)
4+
function fromInstance(nedbInstance) {
5+
var newDB = { nedb: nedbInstance }
6+
7+
var methods = [ 'loadDatabase', 'insert', 'find', 'findOne', 'count', 'update', 'remove', 'ensureIndex', 'removeIndex' ]
8+
for (var i = 0; i < methods.length; ++i) {
9+
var m = methods[i]
10+
newDB[m] = thenify(nedbInstance[m].bind(nedbInstance))
11+
}
712

8-
DB.cfind = function(spec, opts) {
9-
var c = DB.find(spec, opts)
10-
c.execAsync = thenify(c.exec.bind(c))
11-
return c
13+
newDB.cfind = function(query, projections) {
14+
var cursor = nedbInstance.find(query, projections)
15+
cursor.exec = thenify(cursor.exec.bind(cursor))
16+
return cursor
1217
}
1318

14-
DB.cfindOne = function(spec, opts) {
15-
var c = DB.findOne(spec, opts)
16-
c.execAsync = thenify(c.exec.bind(c))
17-
return c
19+
newDB.cfindOne = function(query, projections) {
20+
var cursor = nedbInstance.findOne(query, projections)
21+
cursor.exec = thenify(cursor.exec.bind(cursor))
22+
return cursor
1823
}
1924

20-
for (var mbr in DB) {
21-
if (typeof DB[mbr] !== 'function')
22-
continue
23-
DB[mbr + 'Async'] = thenify(DB[mbr])
25+
newDB.ccount = function(query) {
26+
var cursor = nedbInstance.count(query)
27+
cursor.exec = thenify(cursor.exec.bind(cursor))
28+
return cursor
2429
}
2530

26-
return DB
31+
return newDB
32+
}
33+
34+
function datastore(options) {
35+
var nedbInstance = new NedbDatastore(options)
36+
return fromInstance(nedbInstance)
2737
}
2838

2939
// so that import { datastore } still works:
3040
datastore.datastore = datastore
41+
datastore.fromInstance = fromInstance
3142

3243
module.exports = datastore

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nedb-promise",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A promise wrapper around the nedb package",
55
"keywords": [
66
"nedb",

test.js

+46-35
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,16 @@ const datastore = require('./index')
66
const path = require('path')
77
const _ = require('lodash')
88

9-
function makeDB(opts) {
10-
const filename = path.resolve(path.join(__dirname, 'test-db.json'))
11-
opts = _.extend({ filename }, opts || { })
12-
return datastore(opts)
13-
}
14-
159
describe('datastore', () => {
16-
const DB = makeDB({ autoload: true })
10+
const DB = datastore({ autoload: true })
1711

1812
//
1913
// before each: clear and then stage data
2014
//
2115
beforeEach(co.wrap(function * () {
22-
yield DB.removeAsync({ }, { multi: true })
16+
yield DB.remove({ }, { multi: true })
2317

24-
yield DB.insertAsync([ {
18+
yield DB.insert([ {
2519
num: 1,
2620
alpha: 'a'
2721
}, {
@@ -33,19 +27,19 @@ describe('datastore', () => {
3327
} ])
3428
}))
3529

36-
describe('#findAsync()', () => {
30+
describe('#find()', () => {
3731
it('should return them all', co.wrap(function * () {
38-
assert.equal((yield DB.findAsync({})).length, 3)
32+
assert.equal((yield DB.find({})).length, 3)
3933
}))
4034

4135
it('should only return 1', co.wrap(function * () {
42-
assert.equal((yield DB.findAsync({ num: 3 })).length, 1)
36+
assert.equal((yield DB.find({ num: 3 })).length, 1)
4337
}))
4438

4539
it ('should project', co.wrap(function * () {
4640
let doc = yield DB.cfind({ num: 3 })
4741
.projection({ num: 1, _id: 0 })
48-
.execAsync()
42+
.exec()
4943

5044
doc = doc[0]
5145
assert.equal(doc.num, 3)
@@ -55,7 +49,7 @@ describe('datastore', () => {
5549
it('should sort', co.wrap(function * () {
5650
let docs = yield DB.cfind({})
5751
.sort({ num: -1 })
58-
.execAsync()
52+
.exec()
5953

6054
docs = _.chain(docs)
6155
.map(d => d.num)
@@ -66,78 +60,95 @@ describe('datastore', () => {
6660
}))
6761
})
6862

69-
describe('#findOneAsync()', () => {
63+
describe('#findOne()', () => {
7064
it('should only return one', co.wrap(function * () {
71-
let doc = yield DB.findOneAsync({ num: 2 })
65+
let doc = yield DB.findOne({ num: 2 })
7266
assert.equal(doc.num, 2)
7367
}))
7468

7569
it('should project', co.wrap(function * () {
7670
let doc = yield DB.cfindOne({ num: 2 }, true)
7771
.projection({ num: 1, _id: false })
78-
.execAsync()
72+
.exec()
7973

8074
assert.equal(doc.num, 2)
8175
assert.equal(doc.alpha, undefined)
8276
}))
8377
})
8478

85-
describe('#countAsync()', () => {
79+
describe('#count()', () => {
8680
it('should return the number of documents in the database', co.wrap(function * () {
87-
let count = yield DB.countAsync({})
81+
let count = yield DB.count({})
8882
assert.equal(count, 3)
8983
}))
84+
85+
it('should work with cursors', co.wrap(function * () {
86+
const count = yield DB.ccount({}).limit(2).exec()
87+
assert.equal(count, 2)
88+
}))
9089
})
9190

92-
describe('#insertAsync()', () => {
91+
describe('#insert()', () => {
9392
it('should insert two documents', co.wrap(function * () {
94-
let beforeCount = yield DB.countAsync({})
95-
let docs = yield DB.insertAsync([{
93+
let beforeCount = yield DB.count({})
94+
let docs = yield DB.insert([{
9695
num: 4,
9796
alpha: 'd'
9897
}, {
9998
num: 5,
10099
alpha: 'e'
101100
}])
102-
let afterCount = yield DB.countAsync({})
101+
let afterCount = yield DB.count({})
103102

104103
assert.equal(afterCount - beforeCount, 2)
105104
}))
106105
})
107106

108-
describe('#updateAsync()', () => {
107+
describe('#update()', () => {
109108
it('should update a document', co.wrap(function * () {
110-
yield DB.updateAsync({ num: 3 }, { $set: { updated: true } })
111-
let updated = yield DB.findOneAsync({ num: 3 })
109+
yield DB.update({ num: 3 }, { $set: { updated: true } })
110+
let updated = yield DB.findOne({ num: 3 })
112111
assert(updated.updated)
113112
}))
114113

115114
it('should insert a new document', co.wrap(function * () {
116-
let beforeCount = yield DB.countAsync({})
117-
yield DB.updateAsync({ num: 4 }, {
115+
let beforeCount = yield DB.count({})
116+
yield DB.update({ num: 4 }, {
118117
num: 4,
119118
alpha: 'f'
120119
}, { upsert: true })
121-
let afterCount = yield DB.countAsync({})
120+
let afterCount = yield DB.count({})
122121

123122
assert.equal(afterCount - beforeCount, 1)
124123
}))
125124
})
126125

127-
describe('#removeAsync()', () => {
126+
describe('#remove()', () => {
128127
it('should remove a document', co.wrap(function * () {
129-
let beforeCount = yield DB.countAsync({})
130-
yield DB.removeAsync({})
131-
let afterCount = yield DB.countAsync({})
128+
let beforeCount = yield DB.count({})
129+
yield DB.remove({})
130+
let afterCount = yield DB.count({})
132131

133132
assert.equal(beforeCount - afterCount, 1)
134133
}))
135134

136135
it('should remove all documents', co.wrap(function * () {
137-
yield DB.removeAsync({}, { multi: true })
138-
let afterCount = yield DB.countAsync({})
136+
yield DB.remove({}, { multi: true })
137+
let afterCount = yield DB.count({})
139138

140139
assert.equal(afterCount, 0)
141140
}))
142141
})
142+
143+
describe('Cursors', () => {
144+
it('should work with no suffix', co.wrap(function * () {
145+
const db = datastore('')
146+
yield db.insert([ { id: 2, name: 'Tim' }, { id: 1, name: 'Tom' } ])
147+
const res = yield db.cfind().sort({ name: -1 }).projection({ name: 1 }).exec()
148+
149+
assert.equal(res[0].name, 'Tom')
150+
assert.equal(res[1].name, 'Tim')
151+
assert.equal(typeof res[0].id, 'undefined')
152+
}))
153+
})
143154
})

0 commit comments

Comments
 (0)