This Bookshelf.js plugin enables you to define which model columns have JSON format, preventing manual hook definition for each model with JSON columns.
Install the package via npm
:
$ npm install --save bookshelf-json-columns
Require and register the bookshelf-json-columns plugin:
var bookshelf = require('bookshelf')(knex);
var jsonColumns = require('bookshelf-json-columns');
bookshelf.plugin(jsonColumns);
Define which columns have JSON format with the jsonColumns
prototype property:
bookshelf.Model.extend({
jsonColumns: ['foo', 'bar'],
tableName: 'biz'
});
NOTE: This plugin extends the initialize
method of Bookshelf's Model
, so if you are also extending or overriding it on your models make sure to call its prototype after your work is done:
bookshelf.Model.extend({
initialize: function() {
// Do some stuff.
store.addModel(this);
// Call the initialize prototype method.
bookshelf.Model.prototype.initialize.apply(this, arguments);
},
jsonColumns: ['foo'],
tableName: 'bar'
});
Feel free to fork this repository and submit pull requests. To run the tests, duplicate the test/knexfile.js.dist file, update it to your needs and run:
$ npm test
MIT