Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use internal methods instead of service methods directly #12

Merged
merged 1 commit into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"plugins": ["add-module-exports"],
"presets": [ "es2015" ]
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
69 changes: 45 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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}'`);
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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)
);
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 = {};
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down