This Bookshelf.js plugin enables you to define which model columns have JSON format, stringifying its values before saving and parsing afterwards. It's recommended to use it with the postgres
client, since bookshelf does not have schema information and doesn't know when to use ::json
building the knex queries. Through a simple configuration, there's no need to manually define hooks 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'
});
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