Skip to content

Commit

Permalink
Release 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Gama committed Oct 25, 2016
1 parent aff964a commit 959262a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 66 deletions.
21 changes: 19 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:**
Expand Down Expand Up @@ -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)*
105 changes: 42 additions & 63 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
});
}
Expand All @@ -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]);
}
});
}
Expand All @@ -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);
}

Expand All @@ -80,29 +61,27 @@ 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));
}

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.
Expand All @@ -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]);
}
});
Expand All @@ -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'];
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.2.2",
"version": "2.0.0",
"description": "Parse JSON columns with Bookshelf.js",
"license": "MIT",
"author": {
Expand Down

0 comments on commit 959262a

Please # to comment.