diff --git a/CHANGELOG.md b/CHANGELOG.md index d3ad4b0..08bccab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ -##### 0.1.0 - 28 September 2015 +##### 1.0.0 - 30 September 2015 - Initial release diff --git a/Gruntfile.js b/Gruntfile.js index b71947d..ce3cda5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', diff --git a/README.md b/README.md index baf5b1f..76b77e3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dist/js-data-nedb.js b/dist/js-data-nedb.js index 8daa549..646d443 100644 --- a/dist/js-data-nedb.js +++ b/dist/js-data-nedb.js @@ -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, [{ @@ -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', @@ -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 { @@ -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) { diff --git a/package.json b/package.json index 716f9b4..2071bcd 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/index.js b/src/index.js index dc4c713..6faa675 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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 = [ @@ -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) { @@ -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 { @@ -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 => { diff --git a/test/findAll.spec.js b/test/findAll.spec.js index 47843f8..1e7f14a 100644 --- a/test/findAll.spec.js +++ b/test/findAll.spec.js @@ -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]) + }); + }) });