Skip to content

Plugin: Processor

Ricardo Graça edited this page Mar 18, 2018 · 2 revisions

Introduction

You may need a way to override a setter for specific properties on a model, so that it does some kind of transformation to these properties whenever they are set. For example, in a model with a property like email address, you may want to ensure that when it is set, it always sets the email in lowercase so you can do valid comparisons against values stored in the database.

Getting Started

Load the plugin on your Bookshelf instance using bookshelf.plugin('processor').

You will probably also want to create some processors on a module somewhere on your project that can then be reused in several different models. For example, adding a processor that will cast values into Date objects:

// In a file: processors/to-date.js
module.exports = function castToDate(value) {
  return new Date(value)
})

Then you can use this processor on specific model attributes. You do this by adding the attribute name and a reference to the processor function as a key:value pair in the model's processors property:

var castToDate = require('./processors/to-date')
var MyModel = bookshelf.Model.extend({
  tableName: 'things',
  processors: {
    // loginDate is a model attribute name
    loginDate: castToDate
  }
})

The processor to use can be an existing function or a new anonymous function that accepts a single argument (the value being transformed) and returns the result of the transformation. This means that you can have your processors in their own module if you want and just require that module whenever it is needed on a model, or if you only need a one time processor it's OK to add the function declaration directly to the processors object of the model. Some examples:

var MyModel = bookshelf.Model.extend({
  tableName: 'things',
  processors: {
    loginDate: function(value) {
      return new Date(value);
    }
  }
})

// or

function castToDate(value) {
  return new Date(value);
}

var MyModel = bookshelf.Model.extend({
  tableName: 'things',
  processors: {
    loginDate: castToDate
  }
})

// or

// In a file: my-processors.js
exports.castToDate = function(value) {
  return new Date(value);
}

exports.doSomeStuff = function(value) {
  return value + ' some stuff'
}

// in a model file
var myProcessors = require('./my-processors')
var MyModel = bookshelf.Model.extend({
  tableName: 'things',
  processors: {
    loginDate: myProcessors.castToDate
  }
})

Methods

This plugin defines no methods of its own that are useful to users.

Attributes

Model.processors

An object mapping attribute names to one or more processor functions to apply to that attribute. By default the value of this property is false to indicate that this model doesn't have any processors set.

var bookshelf = Bookshelf(knex)
bookshelf.plugin('processor')
var MyModel = bookshelf.Model.extend({tableName: 'something'})
MyModel.processors // => false

var AnotherModel = bookshelf.Model.extend({
  tableName: 'something',
  processors: {
    name: myProcessors.trim
  }
})
MyModel.processors // => {name: [Function]}
  • Type {Boolean} or {Object}