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

Update to @feathersjs/adapter-commons and drop Node 6 #90

Merged
merged 4 commits into from
Dec 17, 2018
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ language: node_js
install: npm install
node_js:
- node
- '6'
- '8'
48 changes: 0 additions & 48 deletions example/app.js

This file was deleted.

160 changes: 70 additions & 90 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
const crypto = require('crypto');
const omit = require('lodash.omit');
const Proto = require('uberproto');
const errors = require('@feathersjs/errors');
const { select, filterQuery } = require('@feathersjs/commons');
const { _ } = require('@feathersjs/commons');
const { select, AdapterService } = require('@feathersjs/adapter-commons');

const { nfcall, getSelect, multiOptions } = require('./utils');
const { nfcall, getSelect } = require('./utils');

// Create the service.
class Service {
class Service extends AdapterService {
constructor (options) {
if (!options) {
throw new Error('NeDB options have to be provided');
}

if (!options.Model) {
if (!options || !options.Model) {
throw new Error('NeDB datastore `Model` needs to be provided');
}

this.Model = options.Model;
this.events = options.events || [];
this.id = options.id || '_id';
this.paginate = options.paginate || {};
super(Object.assign({ id: '_id' }, options));
}

extend (obj) {
return Proto.extend(obj, this);
getModel (params) {
return this.options.Model;
}

getModel (params) {
return this.Model;
multiOptions (id, params) {
const { query } = this.filterQuery(params);
const options = Object.assign({ multi: true }, params.nedb || params.options);

if (id !== null) {
options.multi = false;
query[this.id] = id;
}

return { query, options };
}

_find (params, count, getFilter = filterQuery) {
async _find (params) {
// Start with finding all, and limit when necessary.
let { filters, query } = getFilter(params.query || {});

const { filters, query, paginate } = this.filterQuery(params);
let q = this.getModel(params).find(query);

// $select uses a specific find syntax, so it has to come first.
Expand All @@ -57,16 +56,14 @@ class Service {
q.skip(filters.$skip);
}

let runQuery = total => {
return nfcall(q, 'exec').then(data => {
return {
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data
};
});
};
let runQuery = total => nfcall(q, 'exec').then(data => {
return {
total,
limit: filters.$limit,
skip: filters.$skip || 0,
data
};
});

if (filters.$limit === 0) {
runQuery = total => {
Expand All @@ -79,27 +76,18 @@ class Service {
};
}

if (count) {
if (paginate && paginate.default) {
return nfcall(this.getModel(params), 'count', query).then(runQuery);
}

return runQuery();
return runQuery().then(page => page.data);
}

find (params) {
const paginate = (params && typeof params.paginate !== 'undefined') ? params.paginate : this.paginate;
const result = this._find(params, !!paginate.default,
query => filterQuery(query, paginate));
async _get (id, params) {
const { query } = this.filterQuery(params);
const findOptions = Object.assign({ [this.id]: id }, query);

if (!paginate.default) {
return result.then(page => page.data);
}

return result;
}

_get (id, params) {
return nfcall(this.getModel(params), 'findOne', { [this.id]: id })
return nfcall(this.getModel(params), 'findOne', findOptions)
.then(doc => {
if (!doc) {
throw new errors.NotFound(`No record found for id '${id}'`);
Expand All @@ -110,19 +98,17 @@ class Service {
.then(select(params, this.id));
}

get (id, params) {
return this._get(id, params);
}

_findOrGet (id, params) {
async _findOrGet (id, params) {
if (id === null) {
return this._find(params).then(page => page.data);
return this._find(_.extend({}, params, {
paginate: false
}));
}

return this._get(id, params);
}

create (raw, params) {
_create (raw, params) {
const addId = item => {
if (this.id !== '_id' && item[this.id] === undefined) {
return Object.assign({
Expand All @@ -138,61 +124,55 @@ class Service {
.then(select(params, this.id));
}

patch (id, data, params) {
const { query, options } = multiOptions(id, this.id, params);
const mapIds = page => page.data.map(current => current[this.id]);
_patch (id, data, params) {
const { query, options } = this.multiOptions(id, params);
const mapIds = data => data.map(current => current[this.id]);

// By default we will just query for the one id. For multi patch
// we create a list of the ids of all items that will be changed
// to re-query them after the update
const ids = id === null ? this._find(params)
.then(mapIds) : Promise.resolve([ id ]);
const ids = id === null ? this._find(Object.assign({}, params, {
paginate: false
})).then(mapIds) : Promise.resolve([ id ]);

// Run the query
return ids
.then(idList => {
// Create a new query that re-queries all ids that
// were originally changed
const findParams = Object.assign({}, params, {
query: {
[this.id]: { $in: idList }
}
});
const updateData = { $set: {} };

Object.keys(data).forEach(key => {
if (key.indexOf('$') === 0) {
updateData[key] = data[key];
} else if (key !== '_id' && key !== this.id) {
updateData.$set[key] = data[key];
}
});
return ids.then(idList => {
// Create a new query that re-queries all ids that
// were originally changed
const findParams = Object.assign({}, params, {
query: Object.assign({
[this.id]: { $in: idList }
}, query)
});
const updateData = Object.keys(data).reduce((result, key) => {
if (key.indexOf('$') === 0) {
result[key] = data[key];
} else if (key !== '_id' && key !== this.id) {
result.$set[key] = data[key];
}
return result;
}, { $set: {} });

return nfcall(this.getModel(params), 'update', query, updateData, options)
.then(() => this._findOrGet(id, findParams));
})
.then(select(params, this.id));
return nfcall(this.getModel(params), 'update', query, updateData, options)
.then(() => this._findOrGet(id, findParams));
}).then(select(params, this.id));
}

update (id, data, params) {
if (Array.isArray(data) || id === null) {
return Promise.reject(new errors.BadRequest('Not replacing multiple records. Did you mean `patch`?'));
}

const { query, options } = multiOptions(id, this.id, params);
const entry = omit(data, '_id');
_update (id, data, params) {
const { query, options } = this.multiOptions(id, params);
const entry = _.omit(data, '_id');

if (this.id !== '_id' || (params.nedb && params.nedb.upsert)) {
entry[this.id] = id;
}

return nfcall(this.getModel(params), 'update', query, entry, options)
.then(() => this._findOrGet(id))
.then(() => this._findOrGet(id, params))
.then(select(params, this.id));
}

remove (id, params) {
const { query, options } = multiOptions(id, this.id, params);
_remove (id, params) {
const { query, options } = this.multiOptions(id, params);

return this._findOrGet(id, params).then(items =>
nfcall(this.getModel(params), 'remove', query, options)
Expand Down
14 changes: 0 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
const { filterQuery } = require('@feathersjs/commons');

exports.multiOptions = function multiOptions (id, idField, params) {
let query = filterQuery(params.query || {}).query;
let options = Object.assign({ multi: true }, params.nedb || params.options);

if (id !== null) {
options.multi = false;
query[idField] = id;
}

return { query, options };
};

exports.getSelect = function getSelect (select) {
if (Array.isArray(select)) {
const result = { _id: 0 };
Expand Down
Loading