Skip to content

Commit 1c882b6

Browse files
author
Ricardo Gama
committed
Release 1.2.0
1 parent eebbd27 commit 1c882b6

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
# Changelog
22

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

5+
[Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.1.1...1.2.0)
6+
7+
**Closed issues:**
8+
9+
- Working for updates? [\#25](https://github.com/seegno/bookshelf-json-columns/issues/25)
10+
11+
**Merged pull requests:**
12+
13+
- Add support for update with patch option [\#27](https://github.com/seegno/bookshelf-json-columns/pull/27) ([ricardogama](https://github.com/ricardogama))
14+
15+
## [1.1.1](https://github.com/seegno/bookshelf-json-columns/tree/1.1.1) (2016-08-23)
516
[Full Changelog](https://github.com/seegno/bookshelf-json-columns/compare/1.1.0...1.1.1)
617

718
**Closed issues:**

dist/index.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
Object.defineProperty(exports, '__esModule', {
99
value: true
1010
});
11-
function stringify() {
11+
12+
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; };
13+
14+
function stringify(model, attributes, options) {
1215
var _this = this;
1316

17+
// Do not stringify with `patch` option.
18+
if (options && options.patch) {
19+
return;
20+
}
21+
1422
this.jsonColumns.forEach(function (column) {
1523
if (_this.attributes[column]) {
1624
_this.attributes[column] = JSON.stringify(_this.attributes[column]);
@@ -22,9 +30,14 @@ function stringify() {
2230
* Parse JSON columns.
2331
*/
2432

25-
function parse() {
33+
function parse(model, response, options) {
2634
var _this2 = this;
2735

36+
// Do not parse with `patch` option.
37+
if (options && options.patch) {
38+
return;
39+
}
40+
2841
this.jsonColumns.forEach(function (column) {
2942
if (_this2.attributes[column]) {
3043
_this2.attributes[column] = JSON.parse(_this2.attributes[column]);
@@ -58,6 +71,47 @@ exports['default'] = function (Bookshelf) {
5871
}
5972

6073
return Model.initialize.apply(this, arguments);
74+
},
75+
save: function save(key, value, options) {
76+
var _this3 = this;
77+
78+
if (!this.jsonColumns) {
79+
return Model.save.apply(this, arguments);
80+
}
81+
82+
// Handle arguments as Bookshelf.
83+
var attributes = undefined;
84+
85+
if (key === null || typeof key === 'object') {
86+
attributes = key || {};
87+
options = value ? _extends({}, value) : {};
88+
} else {
89+
(attributes = {})[key] = value;
90+
options = options ? _extends({}, options) : {};
91+
}
92+
93+
// Only handle arguments with `patch` option.
94+
if (!options.patch) {
95+
return Model.save.apply(this, arguments);
96+
}
97+
98+
// Stringify JSON columns.
99+
Object.keys(attributes).forEach(function (attribute) {
100+
if (_this3.jsonColumns.includes(attribute)) {
101+
attributes[attribute] = JSON.stringify(attributes[attribute]);
102+
}
103+
});
104+
105+
return Model.save.call(this, attributes, options).then(function (model) {
106+
// Parse JSON columns.
107+
Object.keys(attributes).forEach(function (attribute) {
108+
if (_this3.jsonColumns.includes(attribute)) {
109+
model.attributes[attribute] = JSON.parse(model.attributes[attribute]);
110+
}
111+
});
112+
113+
return model;
114+
});
61115
}
62116
});
63117

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bookshelf-json-columns",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Parse JSON columns with Bookshelf.js",
55
"license": "MIT",
66
"author": {

0 commit comments

Comments
 (0)