@@ -68,16 +68,20 @@ module.exports = class {
68
68
create ( body , version = false ) {
69
69
return this . validate ( body )
70
70
. then ( data => {
71
+ const res = Object . assign ( { } , data )
71
72
const vals = [ ]
72
73
const cols = Object . keys ( data ) . reduce ( ( acc , key ) => {
74
+ if ( typeof data [ key ] === 'object' && data [ key ] !== null ) {
75
+ data [ key ] = JSON . stringify ( data [ key ] )
76
+ }
73
77
vals . push ( `'${ data [ key ] } '` )
74
78
acc . push ( key )
75
79
return acc
76
80
} , [ ] )
77
81
const query = `INSERT INTO ${ this . tableName } (${ cols . join ( ',' ) } ) VALUES (${ vals . join ( ',' ) } )`
78
82
return this . query ( query )
79
- . then ( res => {
80
- if ( res . rowCount ) return data
83
+ . then ( data => {
84
+ if ( data . rowCount ) return res
81
85
throw new Error ( 'Unable to create record' )
82
86
} )
83
87
} )
@@ -108,17 +112,26 @@ module.exports = class {
108
112
update ( query , body , version = false ) {
109
113
return this . validate ( body )
110
114
. then ( data => {
115
+ const res = Object . assign ( { } , data )
111
116
let i = 1
112
117
let changes = ''
113
118
let len = Object . keys ( data ) . length
114
- for ( let prop in data ) {
115
- if ( { } . hasOwnProperty . call ( data , prop ) ) {
119
+ for ( let key in data ) {
120
+ /* istanbul ignore else: should not happen */
121
+ if ( { } . hasOwnProperty . call ( data , key ) ) {
122
+ if ( typeof data [ key ] === 'object' && data [ key ] !== null ) {
123
+ data [ key ] = JSON . stringify ( data [ key ] )
124
+ }
116
125
let comma = ( i !== len ) ? ', ' : ''
117
- changes += `${ prop } ='${ data [ prop ] } '${ comma } `
126
+ changes += `${ key } ='${ data [ key ] } '${ comma } `
118
127
i ++
119
128
}
120
129
}
121
130
return this . query ( `UPDATE ${ this . tableName } SET ${ changes } WHERE ${ query } ` )
131
+ . then ( data => {
132
+ if ( data . rowCount ) return res
133
+ throw new Error ( 'Unable to update record(s)' )
134
+ } )
122
135
} )
123
136
}
124
137
0 commit comments