@@ -31,7 +31,10 @@ const {
31
31
createTestTableTwo,
32
32
createDataType,
33
33
} = require ( '../../../util/tableCreatorHelper' ) ;
34
- const { assertNumber } = require ( '../../../util/assertHelper' ) ;
34
+ const {
35
+ assertNumber,
36
+ assertJsonEquals,
37
+ } = require ( '../../../util/assertHelper' ) ;
35
38
36
39
describe ( 'Inserts' , function ( ) {
37
40
getAllDbs ( ) . forEach ( ( db ) => {
@@ -2133,6 +2136,172 @@ describe('Inserts', function () {
2133
2136
) ;
2134
2137
} ) ;
2135
2138
} ) ;
2139
+
2140
+ it ( 'insert json object to json column' , async function ( ) {
2141
+ if ( ! isPostgreSQL ( knex ) ) {
2142
+ return this . skip ( ) ;
2143
+ }
2144
+ const tableName = 'json' ;
2145
+ const jsonObject = {
2146
+ foo : {
2147
+ bar : 'baz' ,
2148
+ } ,
2149
+ } ;
2150
+
2151
+ await knex . schema . dropTableIfExists ( tableName ) ;
2152
+ await knex . schema . createTable ( tableName , ( table ) => {
2153
+ table . increments ( ) ;
2154
+ table . string ( 'name' ) ;
2155
+ table . jsonb ( 'content' ) ;
2156
+ } ) ;
2157
+
2158
+ await knex ( tableName )
2159
+ . insert (
2160
+ {
2161
+ name : 'json_object' ,
2162
+ content : jsonObject ,
2163
+ } ,
2164
+ 'id'
2165
+ )
2166
+ . testSql ( function ( tester ) {
2167
+ tester (
2168
+ 'pg' ,
2169
+ `insert into "${ tableName } " ("content", "name") values (?, ?) returning "id"` ,
2170
+ [ JSON . stringify ( jsonObject ) , 'json_object' ]
2171
+ ) ;
2172
+ } )
2173
+ . then ( ( [ insertResult ] ) =>
2174
+ knex ( tableName ) . where ( 'id' , insertResult . id )
2175
+ )
2176
+ . then ( ( result ) => {
2177
+ expect ( result . length ) . to . equal ( 1 ) ;
2178
+ assertJsonEquals ( result [ 0 ] . content , jsonObject ) ;
2179
+ } ) ;
2180
+ } ) ;
2181
+
2182
+ it ( 'insert number array to integer ARRAY column' , async function ( ) {
2183
+ if ( ! isPostgreSQL ( knex ) ) {
2184
+ return this . skip ( ) ;
2185
+ }
2186
+ const tableName = 'integer_array' ;
2187
+ const integerArrayContent = [ 1 , 2 , 3 , 42 , - 100 ] ;
2188
+
2189
+ await knex . schema . dropTableIfExists ( tableName ) ;
2190
+ await knex . schema . createTable ( tableName , ( table ) => {
2191
+ table . increments ( ) ;
2192
+ table . string ( 'name' ) ;
2193
+ table . specificType ( 'content' , 'integer ARRAY' ) ;
2194
+ } ) ;
2195
+
2196
+ await knex ( tableName )
2197
+ . insert (
2198
+ {
2199
+ name : 'integer_array' ,
2200
+ content : integerArrayContent ,
2201
+ } ,
2202
+ 'id'
2203
+ )
2204
+ . testSql ( function ( tester ) {
2205
+ tester (
2206
+ 'pg' ,
2207
+ `insert into "${ tableName } " ("content", "name") values (?, ?) returning "id"` ,
2208
+ [ integerArrayContent , 'integer_array' ]
2209
+ ) ;
2210
+ } )
2211
+ . then ( ( [ insertResult ] ) =>
2212
+ knex ( tableName ) . where ( 'id' , insertResult . id )
2213
+ )
2214
+ . then ( ( result ) => {
2215
+ expect ( result . length ) . to . equal ( 1 ) ;
2216
+ assertJsonEquals ( result [ 0 ] . content , integerArrayContent ) ;
2217
+ } ) ;
2218
+ } ) ;
2219
+
2220
+ describe ( 'text array' , ( ) => {
2221
+ const tableName = 'text_array' ;
2222
+
2223
+ beforeEach ( async ( ) => {
2224
+ if ( ! isPostgreSQL ( knex ) ) {
2225
+ return true ;
2226
+ }
2227
+ await knex . schema . dropTableIfExists ( tableName ) ;
2228
+ await knex . schema . createTable ( tableName , ( table ) => {
2229
+ table . increments ( ) ;
2230
+ table . string ( 'name' ) ;
2231
+ table . specificType ( 'content' , 'text ARRAY' ) ;
2232
+ } ) ;
2233
+ } ) ;
2234
+
2235
+ it ( '#5365 should insert string array to text ARRAY column' , async function ( ) {
2236
+ if ( ! isPostgreSQL ( knex ) ) {
2237
+ return this . skip ( ) ;
2238
+ }
2239
+
2240
+ const stringArrayContent = [ 'SOME TEXT' , 'SOME OTHER TEXT' ] ;
2241
+
2242
+ await knex ( tableName )
2243
+ . insert (
2244
+ {
2245
+ name : 'array_of_string' ,
2246
+ content : stringArrayContent ,
2247
+ } ,
2248
+ 'id'
2249
+ )
2250
+ . testSql ( function ( tester ) {
2251
+ tester (
2252
+ 'pg' ,
2253
+ `insert into "${ tableName } " ("content", "name") values (?, ?) returning "id"` ,
2254
+ [ stringArrayContent , 'array_of_string' ] ,
2255
+ [ { id : 1 } ]
2256
+ ) ;
2257
+ } )
2258
+ . then ( ( [ insertResult ] ) =>
2259
+ knex ( tableName ) . where ( 'id' , insertResult . id )
2260
+ )
2261
+ . then ( ( result ) => {
2262
+ expect ( result . length ) . to . equal ( 1 ) ;
2263
+ expect ( result [ 0 ] . content ) . to . deep . equal ( stringArrayContent ) ;
2264
+ } ) ;
2265
+ } ) ;
2266
+
2267
+ it ( `#5430 should insert data to text array column if it's an array of object` , async function ( ) {
2268
+ if ( ! isPostgreSQL ( knex ) ) {
2269
+ return this . skip ( ) ;
2270
+ }
2271
+
2272
+ const arrayOfObject = [
2273
+ {
2274
+ foo : {
2275
+ bar : 'baz' ,
2276
+ } ,
2277
+ } ,
2278
+ ] ;
2279
+
2280
+ await knex ( tableName )
2281
+ . insert (
2282
+ {
2283
+ name : 'array_of_object' ,
2284
+ content : arrayOfObject ,
2285
+ } ,
2286
+ 'id'
2287
+ )
2288
+ . testSql ( function ( tester ) {
2289
+ tester (
2290
+ 'pg' ,
2291
+ `insert into "${ tableName } " ("content", "name") values (?, ?) returning "id"` ,
2292
+ [ arrayOfObject , 'array_of_object' ] ,
2293
+ [ { id : 1 } ]
2294
+ ) ;
2295
+ } )
2296
+ . then ( ( [ insertResult ] ) =>
2297
+ knex ( tableName ) . where ( 'id' , insertResult . id )
2298
+ )
2299
+ . then ( ( result ) => {
2300
+ expect ( result . length ) . to . equal ( 1 ) ;
2301
+ assertJsonEquals ( result [ 0 ] . content , arrayOfObject ) ;
2302
+ } ) ;
2303
+ } ) ;
2304
+ } ) ;
2136
2305
} ) ;
2137
2306
} ) ;
2138
2307
} ) ;
0 commit comments