bookshelf-cast
Plugin for Bookshelf that makes it easy to convert model values fetched from a database.
Custom or built-in cast functions for properties can be set in a casts
object
on a model.
Installation
Install the plugin with npm install bookshelf-cast
and load it in your bookshelf
instance.
const bookshelf = ; bookshelf;
Usage
Configure the cast functions in the casts
object by using built-in converters (see below) or custom functions.
Each function is applied in the model context (this
= model), passed the value and meant to return
a value. Attributes that do not have a cast function assigned will be ignored.
const Model = bookshelfModel; Model ;
Built-in cast functions
boolean
Converts 0
and 'false'
into false
and everything else into true
.