Skip to content

An Objection.js plugin for using convenient dynamic finders

License

Notifications You must be signed in to change notification settings

snlamm/objection-dynamic-finder

Repository files navigation

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.

About

An Objection.js plugin for using convenient dynamic finders

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published