Skip to content

Commit

Permalink
Release 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Nogueira committed Jun 7, 2016
1 parent de0c008 commit 97c3848
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 21 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# Changelog

## [1.0.1](https://github.com/seegno/bookshelf-json-columns/tree/1.0.1)
## [1.1.0](https://github.com/seegno/bookshelf-json-columns/tree/1.1.0)

[Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.0.1...1.1.0)

**Closed issues:**

- What is this plugin for [\#15](https://github.com/seegno/bookshelf-json-columns/issues/15)

**Merged pull requests:**

- Add master branch to travis image url [\#18](https://github.com/seegno/bookshelf-json-columns/pull/18) ([ricardogama](https://github.com/ricardogama))
- Add JSON columns parsing with SQLite client [\#17](https://github.com/seegno/bookshelf-json-columns/pull/17) ([ricardogama](https://github.com/ricardogama))
- Update coveralls image badge to square [\#14](https://github.com/seegno/bookshelf-json-columns/pull/14) ([ruimarinho](https://github.com/ruimarinho))

## [1.0.1](https://github.com/seegno/bookshelf-json-columns/tree/1.0.1) (2016-04-21)
[Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.0.0...1.0.1)

**Merged pull requests:**
Expand Down
80 changes: 61 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,88 @@

/**
* Export `bookshelf-json-columns` plugin.
* Stringify JSON columns.
*/

'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});
function stringify() {
var _this = this;

this.jsonColumns.forEach(function (column) {
if (_this.attributes[column]) {
_this.attributes[column] = JSON.stringify(_this.attributes[column]);
}
});
}

/**
* Parse JSON columns.
*/

function parse() {
var _this2 = this;

this.jsonColumns.forEach(function (column) {
if (_this2.attributes[column]) {
_this2.attributes[column] = JSON.parse(_this2.attributes[column]);
}
});
}

/**
* Export `bookshelf-json-columns` plugin.
*/

exports['default'] = function (Bookshelf) {
var Model = Bookshelf.Model.prototype;
var client = Bookshelf.knex.client.config.client;

Bookshelf.Model = Bookshelf.Model.extend({
initialize: function initialize() {
var _this = this;

if (!this.jsonColumns) {
return Model.initialize.apply(this, arguments);
}

// Stringify JSON columns before saving.
this.on('saving', function () {
_this.jsonColumns.forEach(function (column) {
if (_this.attributes[column]) {
_this.attributes[column] = JSON.stringify(_this.attributes[column]);
}
});
});
// Stringify JSON columns before model is saved.
this.on('saving', stringify.bind(this));

// Parse JSON columns after saving.
this.on('saved', function () {
_this.jsonColumns.forEach(function (column) {
if (_this.attributes[column]) {
_this.attributes[column] = JSON.parse(_this.attributes[column]);
}
});
});
// Parse JSON columns after model is saved.
this.on('saved', parse.bind(this));

if (client === 'sqlite') {
// Parse JSON columns after model is fetched.
this.on('fetched', parse.bind(this));
}

return Model.initialize.apply(this, arguments);
}
});

if (client === 'sqlite') {
(function () {
var Collection = Bookshelf.Collection.prototype;

Bookshelf.Collection = Bookshelf.Collection.extend({
initialize: function initialize() {
if (!this.model.prototype.jsonColumns) {
return Collection.initialize.apply(this, arguments);
}

// Parse JSON columns after collection is fetched.
this.on('fetched', function (collection) {
collection.models.forEach(function (model) {
parse.apply(model);
});
});

return Collection.initialize.apply(this, arguments);
}
});
})();
}
};

module.exports = exports['default'];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bookshelf-json-columns",
"version": "1.0.1",
"version": "1.1.0",
"description": "Parse JSON columns with Bookshelf.js",
"license": "MIT",
"author": {
Expand Down

0 comments on commit 97c3848

Please # to comment.