Convenient fetching methods which allow for compact filtering, relation selection and error handling.
npm install --save bookshelf-ez-fetch
Add the plugin to bookshelf:
const ezFetch = require('bookshelf-ez-fetch')
bookshelf.plugin(ezFetch())
All parameters are optional.
An object containing attributes for filtering. Special filter keys can also be used for different comparison operators:
Can be used instead of .where('column_name', '>', value)
Can be used instead of .where('column_name', '<', value)
Can be used instead of .where('column_name', '>=', value)
Can be used instead of .where('column_name', '<=', value)
Object or array containing relational models.
If it is an object, the value of an entry can be true to fetch all columns of the relation. The value of the entry can also be an array, which specifies which columns to fetch.
Array of columns to fetch from model
Set this to true to prevent calling any "not found" handlers defined in the model or plugin (see below).
ModelA.ezFetchAll({
id-after: 5,
name: 'Test'
}, {
cars: true,
planes: ['id', 'model_a_id', 'name']
},
['id', 'name'])
An optional "not found" handler can be added to the model, which is used to throw errors if .ezFetch()
cannot find data.
Example:
const ModelA = bookshelf.Model.extend({
tableName: 'model_a',
notFoundHandler: function() {
throw new Error('ModelA not found')
}
})
Optional fallback handler can also be specified in the plugin function parameter.
bookshelf.plugin(ezFetchPlugin({
defaultNotFoundHandler: function() {
throw new Error('Model not found')
}
}))