diff --git a/CHANGELOG.md b/CHANGELOG.md index cb6fbdf..a057d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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:** diff --git a/dist/index.js b/dist/index.js index 5193c95..6813134 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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]); @@ -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]); @@ -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; + }); } }); diff --git a/package.json b/package.json index fa53b94..f1800bb 100644 --- a/package.json +++ b/package.json @@ -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": {