odf
objection-dynamic-finder
npm i objection-dynamic-finder
odf

objection-dynamic-finder

An Objection.js plugin for using convenient dynamic finders

by Shmuel Lamm

0.1.4 (see all)License:MITTypeScript:Not Found
npm i objection-dynamic-finder
Readme

objection-dynamic-finder

Build Status Coverage Status Dependencies NPM Version License

An Objection.js plugin for using convenient finders inspired by Rails's dynamic finders.

Examples

Person.query().finder.firstNameAndLastName('John', 'Smith')
// => Person.query().where('first_name', 'John').where('last_name', 'Smith')

Person.query().finder.isDisabledOrStatus(true, 'failed')
// => Person.query().where('is_disabled', true).orWhere('status', 'failed')

Person.query().finder.firstNameOrFail('Jane')
// If no model is returned, throws an error (uses throwIfNotFound() in Objection > 0.8.1)

Person.query().finder.firstNameAndNonExistingField('foo')
// => Error 'Querying invalid field: non_existing_field'
// The query fields will be validated against Person model's jsonSchema, if it has one.

Person.query().avg('income').finder.lastNameAndCountry('Smith', 'USA').where('age', '<', 30)
// Finders can be chained with all other QueryBuilder methods.

Installation

Due to Babel not handling the Proxy object, this plugin is only compatible with Node versions >= 6.0.0.

Add the objection-dynamic-finder package via your preferred package manager:

npm install --save objection-dynamic-finder

Usage

See Objection.js docs on using plugins. Once the plugin is applied to a Model class, that class can use .finder in queries.

const Finder = require('objection-dynamic-finder')
const Model = require('objection').Model

class Person extends Finder(Model) {
    // ...
}

Validation Using jsonSchemas

If a class has a jsonSchema property defined then the fields in the finder will be validated against the schema to make sure they exist on the model. Make sure the schema is up to date! CamelCase and snake_case property names are both supported.

Contributing

Contributions are always welcome. You are encouraged to open issues and merge requests.

To run the tests, use npm run test.

Downloads/wk

1

GitHub Stars

14

LAST COMMIT

5yrs ago

MAINTAINERS

1

CONTRIBUTORS

3

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
0.1.4
latest
5yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate