Skip to content

Commit 30b7b46

Browse files
committed
Stable Version 1.0.0
1 parent 1de2de3 commit 30b7b46

File tree

7 files changed

+142
-56
lines changed

7 files changed

+142
-56
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
##### 0.1.0 - 28 September 2015
1+
##### 1.0.0 - 30 September 2015
22

33
- Initial release

Gruntfile.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ module.exports = function (grunt) {
4545
library: 'js-data-nedb'
4646
},
4747
externals: [
48-
'mout/string/underscore',
49-
'mout/random/guid',
5048
'mout/array/map',
5149
'mout/array/unique',
5250
'js-data',

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ var store = new JSData.DS();
2323
store.registerAdapter('nedb', adapter, { default: true });
2424

2525
// "store" will now use the nedb adapter for all async operations
26+
var User = store.defineResource({
27+
name: 'user',
28+
// path where you want the table file for this resource created
29+
filepath: 'path/to/userTable.db'
30+
});
31+
32+
// nedb handler is available here
33+
User.db; // don't run custom queries unless you know what you're doing
34+
35+
// one method you might want to call is User.db.persistence.compactDatafile()
36+
37+
// we don't specify a filepath here,
38+
// so it's automatically created at "./post.db"
39+
var Post = store.defineResource({
40+
name: 'post'
41+
});
2642
```
2743

2844
### Changelog

dist/js-data-nedb.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,9 @@ module.exports =
6363

6464
var reserved = ['orderBy', 'sort', 'limit', 'offset', 'skip', 'where'];
6565

66-
var Defaults = function Defaults() {
67-
_classCallCheck(this, Defaults);
68-
};
69-
7066
var DSNedbAdapter = (function () {
71-
function DSNedbAdapter(options) {
67+
function DSNedbAdapter() {
7268
_classCallCheck(this, DSNedbAdapter);
73-
74-
options = options || {};
75-
this.defaults = new Defaults();
76-
DSUtils.deepMixIn(this.defaults, options);
7769
}
7870

7971
_createClass(DSNedbAdapter, [{
@@ -192,34 +184,40 @@ module.exports =
192184
}
193185
}, {
194186
key: 'getQueryOptions',
195-
value: function getQueryOptions(resourceConfig, params) {
187+
value: function getQueryOptions(resourceConfig, params, query) {
196188
params = params || {};
197189
params.orderBy = params.orderBy || params.sort;
198190
params.skip = params.skip || params.offset;
199191

200-
var queryOptions = {};
201-
202192
if (params.orderBy) {
203-
if (DSUtils.isString(params.orderBy)) {
204-
params.orderBy = [[params.orderBy, 'asc']];
205-
}
206-
for (var i = 0; i < params.orderBy.length; i++) {
207-
if (DSUtils.isString(params.orderBy[i])) {
208-
params.orderBy[i] = [params.orderBy[i], 'asc'];
193+
var i;
194+
195+
(function () {
196+
if (DSUtils.isString(params.orderBy)) {
197+
params.orderBy = [[params.orderBy, 'asc']];
209198
}
210-
}
211-
queryOptions.sort = params.orderBy;
199+
for (i = 0; i < params.orderBy.length; i++) {
200+
if (DSUtils.isString(params.orderBy[i])) {
201+
params.orderBy[i] = [params.orderBy[i], 'asc'];
202+
}
203+
}
204+
var orderByOptions = {};
205+
DSUtils.forEach(params.orderBy, function (order) {
206+
orderByOptions[order[0]] = order[1].toUpperCase() === 'ASC' ? 1 : -1;
207+
});
208+
query = query.sort(orderByOptions);
209+
})();
212210
}
213211

214212
if (params.skip) {
215-
queryOptions.skip = params.skip;
213+
query = query.skip(params.skip);
216214
}
217215

218216
if (params.limit) {
219-
queryOptions.limit = params.limit;
217+
query = query.limit(params.limit);
220218
}
221219

222-
return queryOptions;
220+
return query;
223221
}
224222
}, {
225223
key: 'getDb',
@@ -254,12 +252,14 @@ module.exports =
254252
}
255253
}, {
256254
key: 'FIND',
257-
value: function FIND(resourceConfig, query) {
255+
value: function FIND(resourceConfig, params) {
258256
var _this2 = this;
259257

260-
query = query || {};
258+
params = params || {};
261259
return new DSUtils.Promise(function (resolve, reject) {
262-
_this2.getDb(resourceConfig).find(query, function (err, doc) {
260+
var query = _this2.getDb(resourceConfig).find(_this2.getQuery(resourceConfig, params));
261+
query = _this2.getQueryOptions(resourceConfig, params, query);
262+
query.exec(function (err, doc) {
263263
if (err) {
264264
reject(err);
265265
} else {
@@ -419,9 +419,7 @@ module.exports =
419419
var items = null;
420420
options = this.origify(options ? DSUtils.copy(options) : {});
421421
options['with'] = options['with'] || [];
422-
DSUtils.deepMixIn(options, this.getQueryOptions(resourceConfig, params));
423-
var query = this.getQuery(resourceConfig, params);
424-
return this.FIND(resourceConfig, query).then(function (_items) {
422+
return this.FIND(resourceConfig, params).then(function (_items) {
425423
items = _items;
426424
var tasks = [];
427425
DSUtils.forEach(resourceConfig.relationList, function (def) {

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "js-data-nedb",
33
"description": "nedb adapter for js-data.",
4-
"version": "0.1.0",
4+
"version": "1.0.0",
55
"homepage": "http://www.js-data.io/docs/dsnedbadapter",
66
"repository": {
77
"type": "git",
@@ -18,14 +18,16 @@
1818
"store",
1919
"database",
2020
"adapter",
21-
"nedb"
21+
"nedb",
22+
"embedded",
23+
"filesystem"
2224
],
2325
"main": "./dist/js-data-nedb.js",
2426
"devDependencies": {
2527
"babel-core": "5.8.25",
2628
"babel-eslint": "4.1.3",
2729
"babel-loader": "5.3.2",
28-
"bluebird": "^2.10.1",
30+
"bluebird": "2.10.1",
2931
"chai": "3.3.0",
3032
"grunt": "0.4.5",
3133
"grunt-contrib-clean": "0.6.0",

src/index.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,8 @@ let reserved = [
1313
'where'
1414
]
1515

16-
class Defaults {
17-
18-
}
19-
2016
class DSNedbAdapter {
21-
constructor (options) {
22-
options = options || {}
23-
this.defaults = new Defaults()
24-
DSUtils.deepMixIn(this.defaults, options)
17+
constructor () {
2518
}
2619

2720
getQuery (resourceConfig, params) {
@@ -137,13 +130,11 @@ class DSNedbAdapter {
137130
return query
138131
}
139132

140-
getQueryOptions (resourceConfig, params) {
133+
getQueryOptions (resourceConfig, params, query) {
141134
params = params || {}
142135
params.orderBy = params.orderBy || params.sort
143136
params.skip = params.skip || params.offset
144137

145-
let queryOptions = {}
146-
147138
if (params.orderBy) {
148139
if (DSUtils.isString(params.orderBy)) {
149140
params.orderBy = [
@@ -155,18 +146,22 @@ class DSNedbAdapter {
155146
params.orderBy[i] = [params.orderBy[i], 'asc']
156147
}
157148
}
158-
queryOptions.sort = params.orderBy
149+
let orderByOptions = {}
150+
DSUtils.forEach(params.orderBy, function (order) {
151+
orderByOptions[order[0]] = order[1].toUpperCase() === 'ASC' ? 1 : -1
152+
})
153+
query = query.sort(orderByOptions)
159154
}
160155

161156
if (params.skip) {
162-
queryOptions.skip = params.skip
157+
query = query.skip(params.skip)
163158
}
164159

165160
if (params.limit) {
166-
queryOptions.limit = params.limit
161+
query = query.limit(params.limit)
167162
}
168163

169-
return queryOptions
164+
return query
170165
}
171166

172167
getDb (resourceConfig) {
@@ -196,10 +191,12 @@ class DSNedbAdapter {
196191
})
197192
}
198193

199-
FIND (resourceConfig, query) {
200-
query = query || {}
194+
FIND (resourceConfig, params) {
195+
params = params || {}
201196
return new DSUtils.Promise((resolve, reject) => {
202-
this.getDb(resourceConfig).find(query, function (err, doc) {
197+
let query = this.getDb(resourceConfig).find(this.getQuery(resourceConfig, params))
198+
query = this.getQueryOptions(resourceConfig, params, query)
199+
query.exec(function (err, doc) {
203200
if (err) {
204201
reject(err)
205202
} else {
@@ -343,9 +340,7 @@ class DSNedbAdapter {
343340
let items = null
344341
options = this.origify(options ? DSUtils.copy(options) : {})
345342
options.with = options.with || []
346-
DSUtils.deepMixIn(options, this.getQueryOptions(resourceConfig, params))
347-
let query = this.getQuery(resourceConfig, params)
348-
return this.FIND(resourceConfig, query).then(_items => {
343+
return this.FIND(resourceConfig, params).then(_items => {
349344
items = _items
350345
let tasks = []
351346
DSUtils.forEach(resourceConfig.relationList, def => {

test/findAll.spec.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,81 @@ describe('DSNedbAdapter#findAll', function() {
194194
assert.isDefined(posts[1].user);
195195
});
196196
});
197+
it('should sort, skip, and limit', function () {
198+
return adapter.create(User, {
199+
age: 10,
200+
height: 65
201+
}).then(function () {
202+
return adapter.create(User, {
203+
age: 20,
204+
height: 64
205+
})
206+
}).then(function () {
207+
return adapter.create(User, {
208+
age: 30,
209+
height: 65
210+
})
211+
}).then(function () {
212+
return adapter.create(User, {
213+
age: 30,
214+
height: 61
215+
})
216+
}).then(function () {
217+
return adapter.create(User, {
218+
age: 30,
219+
height: 69
220+
})
221+
}).then(function () {
222+
return adapter.create(User, {
223+
age: 40,
224+
height: 99
225+
})
226+
}).then(function () {
227+
return adapter.create(User, {
228+
age: 50,
229+
height: 88
230+
})
231+
}).then(function () {
232+
return adapter.findAll(User, {
233+
orderBy: [
234+
['age', 'asc'],
235+
['height', 'desc']
236+
],
237+
skip: 1,
238+
limit: 4
239+
})
240+
}).then(function (users) {
241+
assert.equalObjects(users.map(function (user) {
242+
return user.age;
243+
}), [20, 30, 30, 30])
244+
assert.equalObjects(users.map(function (user) {
245+
return user.height;
246+
}), [64, 69, 65, 61])
247+
248+
return adapter.findAll(User, {
249+
age: 30,
250+
orderBy: 'height'
251+
})
252+
}).then(function (users) {
253+
assert.equalObjects(users.map(function (user) {
254+
return user.age;
255+
}), [30, 30, 30])
256+
assert.equalObjects(users.map(function (user) {
257+
return user.height;
258+
}), [61, 65, 69])
259+
260+
return adapter.findAll(User, {
261+
orderBy: [['age', 'desc']],
262+
limit: 1,
263+
offset: 1
264+
})
265+
}).then(function (users) {
266+
assert.equalObjects(users.map(function (user) {
267+
return user.age;
268+
}), [40])
269+
assert.equalObjects(users.map(function (user) {
270+
return user.height;
271+
}), [99])
272+
});
273+
})
197274
});

0 commit comments

Comments
 (0)