diff --git a/.babelrc b/.babelrc index b0ba868..a939bc9 100644 --- a/.babelrc +++ b/.babelrc @@ -1,4 +1,3 @@ - { "plugins": ["add-module-exports"], "presets": [ "es2015" ] diff --git a/README.md b/README.md index 5ae41c6..52e205a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -feathers-nedb -================ +# feathers-nedb [![Build Status](https://travis-ci.org/feathersjs/feathers-nedb.png?branch=master)](https://travis-ci.org/feathersjs/feathers-nedb) [![Code Climate](https://codeclimate.com/github/feathersjs/feathers-nedb.png)](https://codeclimate.com/github/feathersjs/feathers-nedb) @@ -68,6 +67,10 @@ You can run this example by using `node examples/app` and going to [localhost:30 ## Changelog +__2.1.0__ + +- Use internal methods instead of service methods directly + __2.0.0__ - Remove NeDB dependency diff --git a/src/index.js b/src/index.js index 066c51a..34d4796 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ if(!global._babelPolyfill) { require('babel-polyfill'); } import Proto from 'uberproto'; import filter from 'feathers-query-filters'; import errors from 'feathers-errors'; -import { nfcall, getSelect, multiOptions, mapItems } from './utils'; +import { nfcall, getSelect, multiOptions } from './utils'; // Create the service. class Service { @@ -25,12 +25,10 @@ class Service { return Proto.extend(obj, this); } - find(params) { - params.query = params.query || {}; - + _find(params, count, getFilter = filter) { // Start with finding all, and limit when necessary. let query = this.Model.find(params.query); - let filters = filter(params.query, this.paginate); + let filters = getFilter(params.query|| {}); // $select uses a specific find syntax, so it has to come first. if (filters.$select) { @@ -52,24 +50,36 @@ class Service { query.skip(filters.$skip); } - if(this.paginate.default && params.paginate !== false) { - return nfcall(this.Model, 'count', params.query).then(total => { - return nfcall(query, 'exec').then(data => { - return { - total, - limit: filters.$limit, - skip: filters.$skip || 0, - data - }; - }); + const runQuery = total => { + return nfcall(query, 'exec').then(data => { + return { + total, + limit: filters.$limit, + skip: filters.$skip || 0, + data + }; }); + }; + + if(count) { + return nfcall(this.Model, 'count', params.query).then(runQuery); } - - // Execute the query - return nfcall(query, 'exec'); + + return runQuery(); + } + + find(params) { + const paginate = !!this.paginate.default; + const result = this._find(params, paginate, query => filter(query, this.paginate)); + + if(!paginate) { + return result.then(page => page.data); + } + + return result; } - get(_id) { + _get(_id) { return nfcall(this.Model, 'findOne', { _id }).then(doc => { if(!doc) { throw new errors.NotFound(`No record found for id '${_id}'`); @@ -78,6 +88,18 @@ class Service { return doc; }); } + + get(id, params) { + return this._get(id, params); + } + + _findOrGet(id, params) { + if(id === null) { + return this._find(params).then(page => page.data); + } + + return this._get(id, params); + } create(data) { return nfcall(this.Model, 'insert', data); @@ -91,7 +113,7 @@ class Service { // Run the query return nfcall(this.Model, 'update', query, { $set: data }, options) - .then(() => this.find({ query, paginate: false }).then(mapItems(id))); + .then(() => this._findOrGet(id, params)); } update(id, data, params) { @@ -105,16 +127,15 @@ class Service { delete data[this.id]; return nfcall(this.Model, 'update', query, data, options) - .then(() => this.get(id)); + .then(() => this._findOrGet(id)); } remove(id, params) { let { query, options } = multiOptions(id, params); - return this.find({ query, paginate: false }).then(items => + return this._findOrGet(id, params).then(items => nfcall(this.Model, 'remove', query, options) - .then(() => mapItems(id)(items) - ) + .then(() => items) ); } } diff --git a/src/utils.js b/src/utils.js index 60f26fa..0e79216 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,3 @@ -import errors from 'feathers-errors'; - export function multiOptions(id, params) { let query = Object.assign({}, params.query); let options = Object.assign({ multi: true }, params.options); @@ -12,16 +10,6 @@ export function multiOptions(id, params) { return { query, options }; } -export function mapItems(id) { - return function(items) { - if(!items.length) { - throw new errors.NotFound(`No record found for id '${id}'`); - } - - return items.length === 1 ? items[0] : items; - }; -} - export function getSelect(select) { if(Array.isArray(select)) { var result = {}; diff --git a/test/index.test.js b/test/index.test.js index 2098639..1b940cf 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -20,13 +20,13 @@ let counter = 0; const filename = path.join('db-data', 'people'); const db = new NeDB({ filename, autoload: true }); const nedbService = service({ Model: db }).extend({ - find(params) { + _find(params) { params.query = params.query || {}; if(!params.query.$sort) { params.query.$sort = { counter: 1 }; } - return this._super(params); + return this._super.apply(this, arguments); }, create(data, params) {