Build search queries for objection.js models using HTTP query parameters.
Build search queries for objection.js models using HTTP query parameters.
Note: since Objection.js (which this library is based on) now requires Node 6.0.0 as the minimum, objection-find will not work on node 5 console.log(persons[0].children); console.log(persons[0].movie); console.log(persons[0].parent.movies);
// Total size of the result if the range wasn't given. console.log(res.data.total); });
In our example `Person` model had a one-to-one relation `parent`, a many-to-many relation `movies` and one-to-many
relation `children`. This example used the `$http` module of [AngularJS](https://angularjs.org/) but you can use
objection-find with anything that can send an HTTP request.
Documentation on the supported query parameters can be found [here](#query-parameters) and API documentation
[here](API.md).
It is recommended to use [query builder](https://www.npmjs.com/package/objection-find-query-builder) for constructing query parameters on the client side.
# Installation
```sh
npm install objection objection-findEasiest way to get started is to use the objection.js example project
and copy paste this to the api.js file:
const findQuery = require('objection-find');
app.get('/persons/search', function (req, res, next) {
findQuery(Person).build(req.query).then(function (persons) {
res.send(persons);
}).catch(next);
});You also need to run this in the root of the example project to install objection-find:
npm install --save objection-findNow you can start bombing the /persons/search route. Documentation on the supported query parameters can be found
here.
Objection-find understands two kinds of query parameters: filters and special parameters.
A filter parameter has the following format:
||...:=
…
firstName:eq=Jennifer&parent.age:lt=60&children.firstName:like=%rad%
…
js
const findQuery = require('objection-find');
app.get('/persons', function (req, res, next) {
const { page, ...query } = req.query;
findQuery(Person)
.build(query, Person.query().page(page, 12))
.then(function (persons) {
res.send(persons);
})
.catch(next);
});No open issues yet, or sync has not completed.