Skip to content

Commit

Permalink
Release 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Gama committed Aug 29, 2016
1 parent eebbd27 commit 1c882b6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# Changelog

## [1.1.1](https://github.com/seegno/bookshelf-json-columns/tree/HEAD)
## [1.2.0](https://github.com/seegno/bookshelf-json-columns/tree/HEAD)

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

**Closed issues:**

- Working for updates? [\#25](https://github.com/seegno/bookshelf-json-columns/issues/25)

**Merged pull requests:**

- Add support for update with patch option [\#27](https://github.com/seegno/bookshelf-json-columns/pull/27) ([ricardogama](https://github.com/ricardogama))

## [1.1.1](https://github.com/seegno/bookshelf-json-columns/tree/1.1.1) (2016-08-23)
[Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.1.0...1.1.1)

**Closed issues:**
Expand Down
58 changes: 56 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
Object.defineProperty(exports, '__esModule', {
value: true
});
function stringify() {

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function stringify(model, attributes, options) {
var _this = this;

// Do not stringify with `patch` option.
if (options && options.patch) {
return;
}

this.jsonColumns.forEach(function (column) {
if (_this.attributes[column]) {
_this.attributes[column] = JSON.stringify(_this.attributes[column]);
Expand All @@ -22,9 +30,14 @@ function stringify() {
* Parse JSON columns.
*/

function parse() {
function parse(model, response, options) {
var _this2 = this;

// Do not parse with `patch` option.
if (options && options.patch) {
return;
}

this.jsonColumns.forEach(function (column) {
if (_this2.attributes[column]) {
_this2.attributes[column] = JSON.parse(_this2.attributes[column]);
Expand Down Expand Up @@ -58,6 +71,47 @@ exports['default'] = function (Bookshelf) {
}

return Model.initialize.apply(this, arguments);
},
save: function save(key, value, options) {
var _this3 = this;

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

// Handle arguments as Bookshelf.
var attributes = undefined;

if (key === null || typeof key === 'object') {
attributes = key || {};
options = value ? _extends({}, value) : {};
} else {
(attributes = {})[key] = value;
options = options ? _extends({}, options) : {};
}

// Only handle arguments with `patch` option.
if (!options.patch) {
return Model.save.apply(this, arguments);
}

// Stringify JSON columns.
Object.keys(attributes).forEach(function (attribute) {
if (_this3.jsonColumns.includes(attribute)) {
attributes[attribute] = JSON.stringify(attributes[attribute]);
}
});

return Model.save.call(this, attributes, options).then(function (model) {
// Parse JSON columns.
Object.keys(attributes).forEach(function (attribute) {
if (_this3.jsonColumns.includes(attribute)) {
model.attributes[attribute] = JSON.parse(model.attributes[attribute]);
}
});

return model;
});
}
});

Expand Down
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.1.1",
"version": "1.2.0",
"description": "Parse JSON columns with Bookshelf.js",
"license": "MIT",
"author": {
Expand Down

0 comments on commit 1c882b6

Please sign in to comment.