diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cfcdc5..9f8ba24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,24 @@ # Changelog -## [1.2.2](https://github.com/seegno/bookshelf-json-columns/tree/1.2.2) +## [2.0.0](https://github.com/seegno/bookshelf-json-columns/tree/2.0.0) (2016-10-25) +[Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.2.2...2.0.0) +**Closed issues:** + +- Idea: automatic string to Date conversion [\#34](https://github.com/seegno/bookshelf-json-columns/issues/34) + +**Merged pull requests:** + +- Add .nycrc to .npmignore [\#40](https://github.com/seegno/bookshelf-json-columns/pull/40) ([ricardogama](https://github.com/ricardogama)) +- Remove knex client duplicate condition [\#39](https://github.com/seegno/bookshelf-json-columns/pull/39) ([ricardogama](https://github.com/ricardogama)) +- Add node version badge [\#38](https://github.com/seegno/bookshelf-json-columns/pull/38) ([ricardogama](https://github.com/ricardogama)) +- Add .npmignore [\#37](https://github.com/seegno/bookshelf-json-columns/pull/37) ([abelsoares](https://github.com/abelsoares)) +- Update dependencies versions [\#36](https://github.com/seegno/bookshelf-json-columns/pull/36) ([ricardogama](https://github.com/ricardogama)) +- Update jsonColumns option to be a class property [\#35](https://github.com/seegno/bookshelf-json-columns/pull/35) ([ricardogama](https://github.com/ricardogama)) +- Test against Node.js 6 [\#33](https://github.com/seegno/bookshelf-json-columns/pull/33) ([MarkHerhold](https://github.com/MarkHerhold)) +- Add release script [\#24](https://github.com/seegno/bookshelf-json-columns/pull/24) ([ricardogama](https://github.com/ricardogama)) + +## [1.2.2](https://github.com/seegno/bookshelf-json-columns/tree/1.2.2) (2016-09-20) [Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.2.1...1.2.2) **Closed issues:** @@ -100,4 +117,4 @@ -\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index d40adf5..31704f8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4,39 +4,21 @@ Object.defineProperty(exports, "__esModule", { value: true }); -var _keys = require('babel-runtime/core-js/object/keys'); - -var _keys2 = _interopRequireDefault(_keys); - -var _extends2 = require('babel-runtime/helpers/extends'); - -var _extends3 = _interopRequireDefault(_extends2); - -var _typeof2 = require('babel-runtime/helpers/typeof'); - -var _typeof3 = _interopRequireDefault(_typeof2); - -var _stringify = require('babel-runtime/core-js/json/stringify'); - -var _stringify2 = _interopRequireDefault(_stringify); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +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; }; /** * Stringify JSON columns. */ 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] = (0, _stringify2.default)(_this.attributes[column]); + this.constructor.jsonColumns.forEach(column => { + if (this.attributes[column]) { + this.attributes[column] = JSON.stringify(this.attributes[column]); } }); } @@ -46,16 +28,14 @@ function stringify(model, attributes, options) { */ 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]); + this.constructor.jsonColumns.forEach(column => { + if (this.attributes[column]) { + this.attributes[column] = JSON.parse(this.attributes[column]); } }); } @@ -64,13 +44,14 @@ function parse(model, response, options) { * Export `bookshelf-json-columns` plugin. */ -exports.default = function (Bookshelf) { - var Model = Bookshelf.Model.prototype; - var client = Bookshelf.knex.client.config.client; +exports.default = Bookshelf => { + const Model = Bookshelf.Model.prototype; + const client = Bookshelf.knex.client.config.client; + const parseOnFetch = client === 'sqlite' || client === 'sqlite3'; Bookshelf.Model = Bookshelf.Model.extend({ initialize: function initialize() { - if (!this.jsonColumns) { + if (!this.constructor.jsonColumns) { return Model.initialize.apply(this, arguments); } @@ -80,7 +61,7 @@ exports.default = function (Bookshelf) { // Parse JSON columns after model is saved. this.on('saved', parse.bind(this)); - if (client === 'sqlite' || client === 'sqlite3') { + if (parseOnFetch) { // Parse JSON columns after model is fetched. this.on('fetched', parse.bind(this)); } @@ -88,21 +69,19 @@ exports.default = function (Bookshelf) { return Model.initialize.apply(this, arguments); }, save: function save(key, value, options) { - var _this3 = this; - - if (!this.jsonColumns) { + if (!this.constructor.jsonColumns) { return Model.save.apply(this, arguments); } // Handle arguments as Bookshelf. - var attributes = void 0; + let attributes; - if (key === null || (typeof key === 'undefined' ? 'undefined' : (0, _typeof3.default)(key)) === 'object') { + if (key === null || typeof key === 'object') { attributes = key || {}; - options = value ? (0, _extends3.default)({}, value) : {}; + options = value ? _extends({}, value) : {}; } else { (attributes = {})[key] = value; - options = options ? (0, _extends3.default)({}, options) : {}; + options = options ? _extends({}, options) : {}; } // Only handle arguments with `patch` option. @@ -111,16 +90,16 @@ exports.default = function (Bookshelf) { } // Stringify JSON columns. - (0, _keys2.default)(attributes).forEach(function (attribute) { - if (_this3.jsonColumns.indexOf(attribute) !== -1 && attributes[attribute]) { - attributes[attribute] = (0, _stringify2.default)(attributes[attribute]); + Object.keys(attributes).forEach(attribute => { + if (this.constructor.jsonColumns.indexOf(attribute) !== -1 && attributes[attribute]) { + attributes[attribute] = JSON.stringify(attributes[attribute]); } }); - return Model.save.call(this, attributes, options).then(function (model) { + return Model.save.call(this, attributes, options).then(model => { // Parse JSON columns. - (0, _keys2.default)(attributes).forEach(function (attribute) { - if (_this3.jsonColumns.indexOf(attribute) !== -1) { + Object.keys(attributes).forEach(attribute => { + if (this.constructor.jsonColumns.indexOf(attribute) !== -1) { model.attributes[attribute] = JSON.parse(model.attributes[attribute]); } }); @@ -130,28 +109,28 @@ exports.default = function (Bookshelf) { } }); - if (client === 'sqlite' || client === 'sqlite3') { - (function () { - var Collection = Bookshelf.Collection.prototype; + if (!parseOnFetch) { + return; + } - Bookshelf.Collection = Bookshelf.Collection.extend({ - initialize: function initialize() { - if (!this.model.prototype.jsonColumns) { - return Collection.initialize.apply(this, arguments); - } + const Collection = Bookshelf.Collection.prototype; - // Parse JSON columns after collection is fetched. - this.on('fetched', function (collection) { - collection.models.forEach(function (model) { - parse.apply(model); - }); - }); + Bookshelf.Collection = Bookshelf.Collection.extend({ + initialize: function initialize() { + if (!this.model.jsonColumns) { + return Collection.initialize.apply(this, arguments); + } - return Collection.initialize.apply(this, arguments); - } + // Parse JSON columns after collection is fetched. + this.on('fetched', collection => { + collection.models.forEach(model => { + parse.apply(model); + }); }); - })(); - } + + return Collection.initialize.apply(this, arguments); + } + }); }; module.exports = exports['default']; \ No newline at end of file diff --git a/package.json b/package.json index 8b9b44a..5d8ca2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bookshelf-json-columns", - "version": "1.2.2", + "version": "2.0.0", "description": "Parse JSON columns with Bookshelf.js", "license": "MIT", "author": {