Skip to content

Commit

Permalink
Stable Version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Oct 1, 2015
1 parent 1de2de3 commit 30b7b46
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
##### 0.1.0 - 28 September 2015
##### 1.0.0 - 30 September 2015

- Initial release
2 changes: 0 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ module.exports = function (grunt) {
library: 'js-data-nedb'
},
externals: [
'mout/string/underscore',
'mout/random/guid',
'mout/array/map',
'mout/array/unique',
'js-data',
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ var store = new JSData.DS();
store.registerAdapter('nedb', adapter, { default: true });

// "store" will now use the nedb adapter for all async operations
var User = store.defineResource({
name: 'user',
// path where you want the table file for this resource created
filepath: 'path/to/userTable.db'
});

// nedb handler is available here
User.db; // don't run custom queries unless you know what you're doing

// one method you might want to call is User.db.persistence.compactDatafile()

// we don't specify a filepath here,
// so it's automatically created at "./post.db"
var Post = store.defineResource({
name: 'post'
});
```

### Changelog
Expand Down
56 changes: 27 additions & 29 deletions dist/js-data-nedb.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,9 @@ module.exports =

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

var Defaults = function Defaults() {
_classCallCheck(this, Defaults);
};

var DSNedbAdapter = (function () {
function DSNedbAdapter(options) {
function DSNedbAdapter() {
_classCallCheck(this, DSNedbAdapter);

options = options || {};
this.defaults = new Defaults();
DSUtils.deepMixIn(this.defaults, options);
}

_createClass(DSNedbAdapter, [{
Expand Down Expand Up @@ -192,34 +184,40 @@ module.exports =
}
}, {
key: 'getQueryOptions',
value: function getQueryOptions(resourceConfig, params) {
value: function getQueryOptions(resourceConfig, params, query) {
params = params || {};
params.orderBy = params.orderBy || params.sort;
params.skip = params.skip || params.offset;

var queryOptions = {};

if (params.orderBy) {
if (DSUtils.isString(params.orderBy)) {
params.orderBy = [[params.orderBy, 'asc']];
}
for (var i = 0; i < params.orderBy.length; i++) {
if (DSUtils.isString(params.orderBy[i])) {
params.orderBy[i] = [params.orderBy[i], 'asc'];
var i;

(function () {
if (DSUtils.isString(params.orderBy)) {
params.orderBy = [[params.orderBy, 'asc']];
}
}
queryOptions.sort = params.orderBy;
for (i = 0; i < params.orderBy.length; i++) {
if (DSUtils.isString(params.orderBy[i])) {
params.orderBy[i] = [params.orderBy[i], 'asc'];
}
}
var orderByOptions = {};
DSUtils.forEach(params.orderBy, function (order) {
orderByOptions[order[0]] = order[1].toUpperCase() === 'ASC' ? 1 : -1;
});
query = query.sort(orderByOptions);
})();
}

if (params.skip) {
queryOptions.skip = params.skip;
query = query.skip(params.skip);
}

if (params.limit) {
queryOptions.limit = params.limit;
query = query.limit(params.limit);
}

return queryOptions;
return query;
}
}, {
key: 'getDb',
Expand Down Expand Up @@ -254,12 +252,14 @@ module.exports =
}
}, {
key: 'FIND',
value: function FIND(resourceConfig, query) {
value: function FIND(resourceConfig, params) {
var _this2 = this;

query = query || {};
params = params || {};
return new DSUtils.Promise(function (resolve, reject) {
_this2.getDb(resourceConfig).find(query, function (err, doc) {
var query = _this2.getDb(resourceConfig).find(_this2.getQuery(resourceConfig, params));
query = _this2.getQueryOptions(resourceConfig, params, query);
query.exec(function (err, doc) {
if (err) {
reject(err);
} else {
Expand Down Expand Up @@ -419,9 +419,7 @@ module.exports =
var items = null;
options = this.origify(options ? DSUtils.copy(options) : {});
options['with'] = options['with'] || [];
DSUtils.deepMixIn(options, this.getQueryOptions(resourceConfig, params));
var query = this.getQuery(resourceConfig, params);
return this.FIND(resourceConfig, query).then(function (_items) {
return this.FIND(resourceConfig, params).then(function (_items) {
items = _items;
var tasks = [];
DSUtils.forEach(resourceConfig.relationList, function (def) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-data-nedb",
"description": "nedb adapter for js-data.",
"version": "0.1.0",
"version": "1.0.0",
"homepage": "http://www.js-data.io/docs/dsnedbadapter",
"repository": {
"type": "git",
Expand All @@ -18,14 +18,16 @@
"store",
"database",
"adapter",
"nedb"
"nedb",
"embedded",
"filesystem"
],
"main": "./dist/js-data-nedb.js",
"devDependencies": {
"babel-core": "5.8.25",
"babel-eslint": "4.1.3",
"babel-loader": "5.3.2",
"bluebird": "^2.10.1",
"bluebird": "2.10.1",
"chai": "3.3.0",
"grunt": "0.4.5",
"grunt-contrib-clean": "0.6.0",
Expand Down
37 changes: 16 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@ let reserved = [
'where'
]

class Defaults {

}

class DSNedbAdapter {
constructor (options) {
options = options || {}
this.defaults = new Defaults()
DSUtils.deepMixIn(this.defaults, options)
constructor () {
}

getQuery (resourceConfig, params) {
Expand Down Expand Up @@ -137,13 +130,11 @@ class DSNedbAdapter {
return query
}

getQueryOptions (resourceConfig, params) {
getQueryOptions (resourceConfig, params, query) {
params = params || {}
params.orderBy = params.orderBy || params.sort
params.skip = params.skip || params.offset

let queryOptions = {}

if (params.orderBy) {
if (DSUtils.isString(params.orderBy)) {
params.orderBy = [
Expand All @@ -155,18 +146,22 @@ class DSNedbAdapter {
params.orderBy[i] = [params.orderBy[i], 'asc']
}
}
queryOptions.sort = params.orderBy
let orderByOptions = {}
DSUtils.forEach(params.orderBy, function (order) {
orderByOptions[order[0]] = order[1].toUpperCase() === 'ASC' ? 1 : -1
})
query = query.sort(orderByOptions)
}

if (params.skip) {
queryOptions.skip = params.skip
query = query.skip(params.skip)
}

if (params.limit) {
queryOptions.limit = params.limit
query = query.limit(params.limit)
}

return queryOptions
return query
}

getDb (resourceConfig) {
Expand Down Expand Up @@ -196,10 +191,12 @@ class DSNedbAdapter {
})
}

FIND (resourceConfig, query) {
query = query || {}
FIND (resourceConfig, params) {
params = params || {}
return new DSUtils.Promise((resolve, reject) => {
this.getDb(resourceConfig).find(query, function (err, doc) {
let query = this.getDb(resourceConfig).find(this.getQuery(resourceConfig, params))
query = this.getQueryOptions(resourceConfig, params, query)
query.exec(function (err, doc) {
if (err) {
reject(err)
} else {
Expand Down Expand Up @@ -343,9 +340,7 @@ class DSNedbAdapter {
let items = null
options = this.origify(options ? DSUtils.copy(options) : {})
options.with = options.with || []
DSUtils.deepMixIn(options, this.getQueryOptions(resourceConfig, params))
let query = this.getQuery(resourceConfig, params)
return this.FIND(resourceConfig, query).then(_items => {
return this.FIND(resourceConfig, params).then(_items => {
items = _items
let tasks = []
DSUtils.forEach(resourceConfig.relationList, def => {
Expand Down
77 changes: 77 additions & 0 deletions test/findAll.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,81 @@ describe('DSNedbAdapter#findAll', function() {
assert.isDefined(posts[1].user);
});
});
it('should sort, skip, and limit', function () {
return adapter.create(User, {
age: 10,
height: 65
}).then(function () {
return adapter.create(User, {
age: 20,
height: 64
})
}).then(function () {
return adapter.create(User, {
age: 30,
height: 65
})
}).then(function () {
return adapter.create(User, {
age: 30,
height: 61
})
}).then(function () {
return adapter.create(User, {
age: 30,
height: 69
})
}).then(function () {
return adapter.create(User, {
age: 40,
height: 99
})
}).then(function () {
return adapter.create(User, {
age: 50,
height: 88
})
}).then(function () {
return adapter.findAll(User, {
orderBy: [
['age', 'asc'],
['height', 'desc']
],
skip: 1,
limit: 4
})
}).then(function (users) {
assert.equalObjects(users.map(function (user) {
return user.age;
}), [20, 30, 30, 30])
assert.equalObjects(users.map(function (user) {
return user.height;
}), [64, 69, 65, 61])

return adapter.findAll(User, {
age: 30,
orderBy: 'height'
})
}).then(function (users) {
assert.equalObjects(users.map(function (user) {
return user.age;
}), [30, 30, 30])
assert.equalObjects(users.map(function (user) {
return user.height;
}), [61, 65, 69])

return adapter.findAll(User, {
orderBy: [['age', 'desc']],
limit: 1,
offset: 1
})
}).then(function (users) {
assert.equalObjects(users.map(function (user) {
return user.age;
}), [40])
assert.equalObjects(users.map(function (user) {
return user.height;
}), [99])
});
})
});

0 comments on commit 30b7b46

Please sign in to comment.