Skip to content

Commit

Permalink
Update feathers-query-filters to version 2.0.0 🚀 (#56)
Browse files Browse the repository at this point in the history
* chore(package): update feathers-query-filters to version 2.0.0

* Update feathers-query-filters usage
  • Loading branch information
greenkeeperio-bot authored and daffl committed Jul 10, 2016
1 parent f5e276e commit 0dd4943
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@
"lib": "lib"
},
"dependencies": {
"babel-polyfill": "^6.3.14",
"feathers-errors": "^2.0.1",
"feathers-query-filters": "^1.5.1",
"feathers-query-filters": "^2.0.0",
"is-plain-object": "^2.0.1",
"uberproto": "^1.2.0"
},
Expand Down
20 changes: 9 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
if(!global._babelPolyfill) { require('babel-polyfill'); }

import Proto from 'uberproto';
import filter from 'feathers-query-filters';
import isPlainObject from 'is-plain-object';
Expand Down Expand Up @@ -79,36 +77,36 @@ class Service {
}

_find(params, count, getFilter = filter) {
let query = this.db().select(['*']);
let filters = getFilter(params.query || {});
let q = this.db().select(['*']);
let { filters, query } = getFilter(params.query || {});

// $select uses a specific find syntax, so it has to come first.
if (filters.$select) {
let fields = filters.$select;
query = this.db().select(... fields);
q = this.db().select(... fields);
}

// build up the knex query out of the query params
this.knexify(query, params.query);
this.knexify(q, query);

// Handle $sort
if (filters.$sort) {
Object.keys(filters.$sort).forEach(key =>
query = query.orderBy(key, parseInt(filters.$sort[key], 10) === 1 ? 'asc' : 'desc'));
q = q.orderBy(key, parseInt(filters.$sort[key], 10) === 1 ? 'asc' : 'desc'));
}

// Handle $limit
if (filters.$limit) {
query.limit(filters.$limit);
q.limit(filters.$limit);
}

// Handle $skip
if (filters.$skip) {
query.offset(filters.$skip);
q.offset(filters.$skip);
}

const executeQuery = total => {
return query.then(data => {
return q.then(data => {
return {
total,
limit: filters.$limit,
Expand All @@ -121,7 +119,7 @@ class Service {
if(count) {
let countQuery = this.db().count(`${this.id} as total`);

this.knexify(countQuery, params.query);
this.knexify(countQuery, query);

return countQuery.then(count => count[0].total).then(executeQuery);
}
Expand Down

0 comments on commit 0dd4943

Please sign in to comment.