@@ -237,23 +237,47 @@ describe('MySQLTable', () => {
237
237
238
238
it ( 'should insert the specified data into the table with an ON DUPLICATE KEY UPDATE clause' , done => {
239
239
const data = { id : 1 , email : 'one@email.com' } ;
240
- const onDuplicateKey = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'" ;
241
- testTable . insert ( data , onDuplicateKey , ( err , result ) => {
240
+ const onDuplicateKey1 = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'" ;
241
+
242
+ testTable . insert ( data , onDuplicateKey1 , ( err , result1 ) => {
242
243
if ( err ) throw err ;
243
- result . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
244
- result . insertId . should . equal ( 1 ) ;
245
- done ( ) ;
244
+ result1 . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
245
+ result1 . insertId . should . equal ( 1 ) ;
246
+
247
+ const columns = Object . keys ( data ) ;
248
+ const rows = [
249
+ [ data [ columns [ 0 ] ] , data [ columns [ 1 ] ] ] ,
250
+ ] ;
251
+ const onDuplicateKey2 = "ON DUPLICATE KEY UPDATE `email` = 'one2b@email.com'" ;
252
+
253
+ testTable . insert ( [ columns , rows ] , onDuplicateKey2 , ( err , result2 ) => {
254
+ if ( err ) throw err ;
255
+ result2 . affectedRows . should . equal ( 2 ) ;
256
+ result2 . insertId . should . equal ( 1 ) ;
257
+ done ( ) ;
258
+ } ) ;
246
259
} ) ;
247
260
} ) ;
248
261
249
262
it ( 'should insert data with question marks into the table when using the `sqlString` and `values` parameters' , done => {
250
263
const data = { id : 1 , email : '??one?@email.com' } ;
251
264
const onDuplicateKey = 'ON DUPLICATE KEY UPDATE ?? = ?' ;
252
- testTable . insert ( data , onDuplicateKey , [ 'email' , 'one3@email.com' ] , ( err , result ) => {
265
+
266
+ testTable . insert ( data , onDuplicateKey , [ 'email' , 'one3@email.com' ] , ( err , result1 ) => {
253
267
if ( err ) throw err ;
254
- result . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
255
- result . insertId . should . equal ( 1 ) ;
256
- done ( ) ;
268
+ result1 . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
269
+ result1 . insertId . should . equal ( 1 ) ;
270
+
271
+ const columns = Object . keys ( data ) ;
272
+ const rows = [
273
+ [ data [ columns [ 0 ] ] , data [ columns [ 1 ] ] ] ,
274
+ ] ;
275
+ testTable . insert ( [ columns , rows ] , onDuplicateKey , [ 'email' , 'one4@email.com' ] , ( err , result2 ) => {
276
+ if ( err ) throw err ;
277
+ result2 . affectedRows . should . equal ( 2 ) ;
278
+ result2 . insertId . should . equal ( 1 ) ;
279
+ done ( ) ;
280
+ } ) ;
257
281
} ) ;
258
282
} ) ;
259
283
@@ -311,24 +335,41 @@ describe('MySQLTable', () => {
311
335
312
336
it ( 'should insert the specified data into the table with an ON DUPLICATE KEY UPDATE clause' , ( ) => {
313
337
const data = { id : 1 , email : 'one@email.com' } ;
314
- const onDuplicateKey = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'" ;
338
+ const columns = Object . keys ( data ) ;
339
+ const rows = [
340
+ [ data [ columns [ 0 ] ] , data [ columns [ 1 ] ] ] ,
341
+ ] ;
342
+ const onDuplicateKey1 = "ON DUPLICATE KEY UPDATE `email` = 'one2@email.com'" ;
343
+ const onDuplicateKey2 = "ON DUPLICATE KEY UPDATE `email` = 'one2b@email.com'" ;
315
344
316
- return testTable . insert ( data , onDuplicateKey )
317
- . then ( result => {
318
- result . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
319
- result . insertId . should . equal ( 1 ) ;
320
- } ) ;
345
+ return Promise . all ( [
346
+ testTable . insert ( data , onDuplicateKey1 ) ,
347
+ testTable . insert ( [ columns , rows ] , onDuplicateKey2 ) ,
348
+ ] ) . then ( results => {
349
+ results [ 0 ] . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
350
+ results [ 0 ] . insertId . should . equal ( 1 ) ;
351
+ results [ 1 ] . affectedRows . should . equal ( 2 ) ;
352
+ results [ 1 ] . insertId . should . equal ( 1 ) ;
353
+ } ) ;
321
354
} ) ;
322
355
323
356
it ( 'should insert data with question marks into the table when using the `sqlString` and `values` parameters' , ( ) => {
324
357
const data = { id : 1 , email : '??one?@email.com' } ;
358
+ const columns = Object . keys ( data ) ;
359
+ const rows = [
360
+ [ data [ columns [ 0 ] ] , data [ columns [ 1 ] ] ] ,
361
+ ] ;
325
362
const onDuplicateKey = 'ON DUPLICATE KEY UPDATE ?? = ?' ;
326
363
327
- return testTable . insert ( data , onDuplicateKey , [ 'email' , 'one3@email.com' ] )
328
- . then ( result => {
329
- result . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
330
- result . insertId . should . equal ( 1 ) ;
331
- } ) ;
364
+ return Promise . all ( [
365
+ testTable . insert ( data , onDuplicateKey , [ 'email' , 'one3@email.com' ] ) ,
366
+ testTable . insert ( [ columns , rows ] , onDuplicateKey , [ 'email' , 'one3b@email.com' ] ) ,
367
+ ] ) . then ( results => {
368
+ results [ 0 ] . affectedRows . should . equal ( 2 ) ; // Updated rows are affected twice
369
+ results [ 0 ] . insertId . should . equal ( 1 ) ;
370
+ results [ 1 ] . affectedRows . should . equal ( 2 ) ;
371
+ results [ 1 ] . insertId . should . equal ( 1 ) ;
372
+ } ) ;
332
373
} ) ;
333
374
334
375
it ( 'should be able to perform bulk inserts' , ( ) => {
0 commit comments