88Object . 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
0 commit comments