-
Notifications
You must be signed in to change notification settings - Fork 59
/
SQL.test.js
646 lines (553 loc) · 19.4 KB
/
SQL.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
'use strict'
const util = require('util')
const { test } = require('node:test')
const assert = require('node:assert')
const SQL = require('./SQL')
const unsafe = SQL.unsafe
const quoteIdent = SQL.quoteIdent
test('SQL helper - build complex query with append', async t => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL`UPDATE teams SET name = ${name}, description = ${description} `
sql.append(SQL`WHERE id = ${teamId} AND org_id = ${organizationId}`)
assert.equal(
sql.text,
'UPDATE teams SET name = $1, description = $2 WHERE id = $3 AND org_id = $4'
)
assert.equal(
sql.sql,
'UPDATE teams SET name = ?, description = ? WHERE id = ? AND org_id = ?'
)
assert.equal(
sql.debug,
`UPDATE teams SET name = '${name}', description = '${description}' WHERE id = ${teamId} AND org_id = '${organizationId}'`
)
assert.deepStrictEqual(sql.values, [name, description, teamId, organizationId])
})
test('SQL helper - multiline', async t => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL`
UPDATE teams SET name = ${name}, description = ${description}
WHERE id = ${teamId} AND org_id = ${organizationId}
`
assert.equal(
sql.text,
'UPDATE teams SET name = $1, description = $2\nWHERE id = $3 AND org_id = $4'
)
assert.equal(
sql.sql,
'UPDATE teams SET name = ?, description = ?\nWHERE id = ? AND org_id = ?'
)
assert.equal(
sql.debug,
`UPDATE teams SET name = '${name}', description = '${description}'\nWHERE id = ${teamId} AND org_id = '${organizationId}'`
)
assert.deepStrictEqual(sql.values, [name, description, teamId, organizationId])
})
test('SQL helper - multiline with emtpy lines', async t => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL`
UPDATE teams SET name = ${name}, description = ${description}
WHERE id = ${teamId} AND org_id = ${organizationId}
RETURNING id
`
assert.equal(
sql.text,
'UPDATE teams SET name = $1, description = $2\nWHERE id = $3 AND org_id = $4\nRETURNING id'
)
assert.equal(
sql.sql,
'UPDATE teams SET name = ?, description = ?\nWHERE id = ? AND org_id = ?\nRETURNING id'
)
assert.equal(
sql.debug,
`UPDATE teams SET name = '${name}', description = '${description}'\nWHERE id = ${teamId} AND org_id = '${organizationId}'\nRETURNING id`
)
assert.deepStrictEqual(sql.values, [name, description, teamId, organizationId])
})
test('SQL helper - build complex query with map', async t => {
const objArray = [{
id: 1,
name: 'name1'
},
{
id: 2,
name: 'name2'
},
{
id: 3,
name: 'name3'
}]
const mapFunction = (objItem) => {
return objItem.id
}
const values = SQL.map(objArray, mapFunction)
assert.equal(values !== null, true)
const sql = SQL`INSERT INTO users (id) VALUES (${values})`
assert.equal(sql.text, 'INSERT INTO users (id) VALUES ($1,$2,$3)')
assert.equal(sql.sql, 'INSERT INTO users (id) VALUES (?,?,?)')
assert.equal(sql.debug, 'INSERT INTO users (id) VALUES (1,2,3)')
assert.deepStrictEqual(sql.values, [1, 2, 3])
})
test('SQL helper - build complex query with map - using default mapper function', async t => {
const ids = [1, 2, 3]
const values = SQL.map(ids)
assert.equal(values !== null, true)
const sql = SQL`INSERT INTO users (id) VALUES (${values})`
assert.equal(sql.text, 'INSERT INTO users (id) VALUES ($1,$2,$3)')
assert.equal(sql.sql, 'INSERT INTO users (id) VALUES (?,?,?)')
assert.equal(sql.debug, 'INSERT INTO users (id) VALUES (1,2,3)')
assert.deepStrictEqual(sql.values, [1, 2, 3])
})
test('SQL helper - build complex query with map - empty array', async t => {
const objArray = []
const mapFunction = (objItem) => {
return objItem.id
}
const values = SQL.map(objArray, mapFunction)
assert.equal(values, null)
})
test('SQL helper - build complex query with map - bad mapper function', async t => {
const objArray = []
const mapFunction = null
const values = SQL.map(objArray, mapFunction)
assert.equal(values, null)
})
test('SQL helper - build complex query with glue', async t => {
const name = 'Team 5'
const description = 'description'
const teamId = 7
const organizationId = 'WONKA'
const sql = SQL` UPDATE teams SET `
const updates = []
updates.push(SQL`name = ${name}`)
updates.push(SQL`description = ${description}`)
sql.append(sql.glue(updates, ' , '))
sql.append(SQL`WHERE id = ${teamId} AND org_id = ${organizationId}`)
assert.equal(
sql.text,
'UPDATE teams SET name = $1 , description = $2 WHERE id = $3 AND org_id = $4'
)
assert.equal(
sql.sql,
'UPDATE teams SET name = ? , description = ? WHERE id = ? AND org_id = ?'
)
assert.equal(
sql.debug,
`UPDATE teams SET name = '${name}' , description = '${description}' WHERE id = ${teamId} AND org_id = '${organizationId}'`
)
assert.deepStrictEqual(sql.values, [name, description, teamId, organizationId])
})
test('SQL helper - build complex query with glue - regression #13', async t => {
const name = 'Team 5'
const ids = [1, 2, 3].map(id => SQL`${id}`)
const sql = SQL`UPDATE teams SET name = ${name} `
sql.append(SQL`WHERE id IN (`)
sql.append(sql.glue(ids, ' , '))
sql.append(SQL`)`)
assert.equal(sql.text, 'UPDATE teams SET name = $1 WHERE id IN ($2 , $3 , $4 )')
assert.equal(sql.sql, 'UPDATE teams SET name = ? WHERE id IN (? , ? , ? )')
assert.equal(
sql.debug,
`UPDATE teams SET name = '${name}' WHERE id IN (1 , 2 , 3 )`
)
assert.deepStrictEqual(sql.values, [name, 1, 2, 3])
})
test('SQL helper - build complex query with glue - regression #17', async t => {
const ids = [1, 2, 3].map(id => SQL`(${id})`)
const sql = SQL`INSERT INTO users (id) VALUES `
sql.append(sql.glue(ids, ' , '))
assert.equal(sql.text, 'INSERT INTO users (id) VALUES ($1) , ($2) , ($3)')
assert.equal(sql.sql, 'INSERT INTO users (id) VALUES (?) , (?) , (?)')
assert.equal(sql.debug, 'INSERT INTO users (id) VALUES (1) , (2) , (3)')
assert.deepStrictEqual(sql.values, [1, 2, 3])
})
test('SQL helper - build complex query with static glue - regression #17', async t => {
const ids = [1, 2, 3].map(id => SQL`(${id})`)
const sql = SQL`INSERT INTO users (id) VALUES `
sql.append(SQL.glue(ids, ' , '))
assert.equal(sql.text, 'INSERT INTO users (id) VALUES ($1) , ($2) , ($3)')
assert.equal(sql.sql, 'INSERT INTO users (id) VALUES (?) , (?) , (?)')
assert.equal(sql.debug, 'INSERT INTO users (id) VALUES (1) , (2) , (3)')
assert.deepStrictEqual(sql.values, [1, 2, 3])
})
test('glue works with quoteIdent - regression #77', async t => {
const sql = SQL.glue([SQL`SELECT * FROM ${quoteIdent('tbl')}`])
assert.equal(sql.text, 'SELECT * FROM "tbl"')
assert.equal(sql.sql, 'SELECT * FROM `tbl`')
assert.equal(sql.debug, 'SELECT * FROM "tbl"')
assert.deepStrictEqual(sql.values, [])
})
test('SQL helper - build complex query with append and glue', async t => {
const updates = []
const v1 = 'v1'
const v2 = 'v2'
const v3 = 'v3'
const v4 = 'v4'
const v5 = 'v5'
const v6 = 'v6'
const v7 = 'v7'
const sql = SQL`TEST QUERY glue pieces FROM `
updates.push(SQL`v1 = ${v1}`)
updates.push(SQL`v2 = ${v2}`)
updates.push(SQL`v3 = ${v3}`)
updates.push(SQL`v4 = ${v4}`)
updates.push(SQL`v5 = ${v5}`)
sql.append(sql.glue(updates, ' , '))
sql.append(SQL`WHERE v6 = ${v6} `)
sql.append(SQL`AND v7 = ${v7}`)
assert.equal(
sql.text,
'TEST QUERY glue pieces FROM v1 = $1 , v2 = $2 , v3 = $3 , v4 = $4 , v5 = $5 WHERE v6 = $6 AND v7 = $7'
)
assert.equal(
sql.sql,
'TEST QUERY glue pieces FROM v1 = ? , v2 = ? , v3 = ? , v4 = ? , v5 = ? WHERE v6 = ? AND v7 = ?'
)
assert.equal(
sql.debug,
"TEST QUERY glue pieces FROM v1 = 'v1' , v2 = 'v2' , v3 = 'v3' , v4 = 'v4' , v5 = 'v5' WHERE v6 = 'v6' AND v7 = 'v7'"
)
assert.deepStrictEqual(sql.values, [v1, v2, v3, v4, v5, v6, v7])
})
test('SQL helper - build complex query with append', async t => {
const v1 = 'v1'
const v2 = 'v2'
const v3 = 'v3'
const v4 = 'v4'
const v5 = 'v5'
const v6 = 'v6'
const v7 = 'v7'
const sql = SQL`TEST QUERY glue pieces FROM `
sql.append(SQL`v1 = ${v1}, `)
sql.append(SQL`v2 = ${v2}, `)
sql.append(SQL`v3 = ${v3}, `)
sql.append(SQL`v4 = ${v4}, `)
sql.append(SQL`v5 = ${v5} `)
sql.append(SQL`WHERE v6 = ${v6} `)
sql.append(SQL`AND v7 = ${v7}`)
assert.equal(
sql.text,
'TEST QUERY glue pieces FROM v1 = $1, v2 = $2, v3 = $3, v4 = $4, v5 = $5 WHERE v6 = $6 AND v7 = $7'
)
assert.equal(
sql.sql,
'TEST QUERY glue pieces FROM v1 = ?, v2 = ?, v3 = ?, v4 = ?, v5 = ? WHERE v6 = ? AND v7 = ?'
)
assert.equal(
sql.debug,
"TEST QUERY glue pieces FROM v1 = 'v1', v2 = 'v2', v3 = 'v3', v4 = 'v4', v5 = 'v5' WHERE v6 = 'v6' AND v7 = 'v7'"
)
assert.deepStrictEqual(sql.values, [v1, v2, v3, v4, v5, v6, v7])
})
test('SQL helper - build complex query with append passing simple strings and template strings', async t => {
const v1 = 'v1'
const v2 = 'v2'
const v3 = 'v3'
const v4 = 'v4'
const v5 = 'v5'
const v6 = 'v6'
const v7 = 'v7'
const sql = SQL`TEST QUERY glue pieces FROM `
sql.append(SQL`v1 = ${v1}, `)
sql.append(SQL`v2 = ${v2}, `)
sql.append(SQL`v3 = ${v3}, `)
sql.append(SQL`v4 = ${v4}, `)
sql.append(SQL`v5 = ${v5}, `)
sql.append(SQL`v6 = v6 `)
sql.append(SQL`WHERE v6 = ${v6} `)
sql.append(SQL`AND v7 = ${v7} `)
sql.append(SQL`AND v8 = v8`)
assert.equal(
sql.text,
'TEST QUERY glue pieces FROM v1 = $1, v2 = $2, v3 = $3, v4 = $4, v5 = $5, v6 = v6 WHERE v6 = $6 AND v7 = $7 AND v8 = v8'
)
assert.equal(
sql.debug,
"TEST QUERY glue pieces FROM v1 = 'v1', v2 = 'v2', v3 = 'v3', v4 = 'v4', v5 = 'v5', v6 = v6 WHERE v6 = 'v6' AND v7 = 'v7' AND v8 = v8"
)
assert.deepStrictEqual(sql.values, [v1, v2, v3, v4, v5, v6, v7])
})
test('SQL helper - will throw an error if append is called without using SQL', async t => {
const sql = SQL`TEST QUERY glue pieces FROM `
try {
sql.append('v1 = v1')
t.fail('showld throw an error when passing strings not prefixed with SQL')
} catch (e) {
assert.equal(
e.message,
'"append" accepts only template string prefixed with SQL (SQL`...`)'
)
}
})
test('SQL helper - build string using append with and without unsafe flag', async t => {
const v2 = 'v2'
const longName = 'whateverThisIs'
const sql = SQL`TEST QUERY glue pieces FROM test WHERE test1 == test2`
sql.append(SQL` AND v1 = v1,`)
sql.append(SQL` AND v2 = ${v2}, `)
sql.append(SQL` AND v3 = ${longName}`, { unsafe: true })
sql.append(SQL` AND v4 = v4`, { unsafe: true })
assert.equal(
sql.text,
'TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = $1, AND v3 = whateverThisIs AND v4 = v4'
)
assert.equal(
sql.debug,
"TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = 'v2', AND v3 = whateverThisIs AND v4 = v4"
)
assert.equal(sql.values.length, 1)
assert.ok(sql.values.includes(v2))
})
test('SQL helper - build string using append and only unsafe', async t => {
const v2 = 'v2'
const longName = 'whateverThisIs'
const sql = SQL`TEST QUERY glue pieces FROM test WHERE test1 == test2`
assert.equal(sql.text, 'TEST QUERY glue pieces FROM test WHERE test1 == test2')
sql.append(SQL` AND v1 = v1,`, { unsafe: true })
assert.equal(
sql.text,
'TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1,'
)
sql.append(SQL` AND v2 = ${v2} AND v3 = ${longName} AND v4 = 'v4'`, {
unsafe: true
})
assert.equal(
sql.text,
"TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = v2 AND v3 = whateverThisIs AND v4 = 'v4'"
)
assert.equal(
sql.debug,
"TEST QUERY glue pieces FROM test WHERE test1 == test2 AND v1 = v1, AND v2 = v2 AND v3 = whateverThisIs AND v4 = 'v4'"
)
})
test('SQL helper - handles js null values as valid `null` sql values', async t => {
const name = null
const id = 123
const sql = SQL`UPDATE teams SET name = ${name} WHERE id = ${id}`
assert.equal(sql.text, 'UPDATE teams SET name = $1 WHERE id = $2')
assert.equal(sql.sql, 'UPDATE teams SET name = ? WHERE id = ?')
assert.equal(sql.debug, `UPDATE teams SET name = null WHERE id = ${id}`)
assert.deepStrictEqual(sql.values, [name, id])
})
test('SQL helper - throws when building an sql string with an `undefined` value', async t => {
assert.throws(() => SQL`UPDATE teams SET name = ${undefined}`)
})
test('empty append', async t => {
const sql = SQL`UPDATE teams SET name = ${'team'}`.append()
assert.equal(sql.text, 'UPDATE teams SET name = $1')
assert.equal(sql.sql, 'UPDATE teams SET name = ?')
assert.equal(sql.debug, "UPDATE teams SET name = 'team'")
assert.deepStrictEqual(sql.values, ['team'])
})
test('inspect', async t => {
const sql = SQL`UPDATE teams SET name = ${'team'}`
assert.equal(util.inspect(sql), "SQL << UPDATE teams SET name = 'team' >>")
})
test('quoteIdent', async t => {
t.test('simple', async t => {
const table = 'teams'
const name = 'name'
const id = 123
const sql = SQL`UPDATE ${quoteIdent(
table
)} SET name = ${name} WHERE id = ${id}`
assert.equal(sql.text, 'UPDATE "teams" SET name = $1 WHERE id = $2')
assert.equal(sql.sql, 'UPDATE `teams` SET name = ? WHERE id = ?')
assert.equal(sql.debug, `UPDATE "teams" SET name = 'name' WHERE id = ${id}`)
assert.deepStrictEqual(sql.values, [name, id])
})
})
test('unsafe', async t => {
const name = 'name'
const id = 123
const sql = SQL`UPDATE teams SET name = '${unsafe(name)}' WHERE id = ${id}`
assert.equal(sql.text, "UPDATE teams SET name = 'name' WHERE id = $1")
assert.equal(sql.sql, "UPDATE teams SET name = 'name' WHERE id = ?")
assert.equal(sql.debug, `UPDATE teams SET name = 'name' WHERE id = ${id}`)
assert.deepStrictEqual(sql.values, [id])
})
test('should be able to append query that is using "{ unsafe: true }"', async t => {
const table = 'teams'
const id = 123
const reusableSql = SQL`SELECT id FROM`
reusableSql.append(SQL` ${table}`, { unsafe: true })
reusableSql.append(SQL` WHERE id = ${id}`)
const sql = SQL`SELECT * FROM`
sql.append(SQL` ${table} `, { unsafe: true })
sql.append(SQL`INNER JOIN (`)
sql.append(reusableSql)
sql.append(SQL`) as t2 ON t2.id = id`)
assert.equal(
sql.text,
'SELECT * FROM teams INNER JOIN (SELECT id FROM teams WHERE id = $1) as t2 ON t2.id = id'
)
assert.equal(
sql.sql,
'SELECT * FROM teams INNER JOIN (SELECT id FROM teams WHERE id = ?) as t2 ON t2.id = id'
)
assert.equal(
sql.debug,
`SELECT * FROM teams INNER JOIN (SELECT id FROM teams WHERE id = ${id}) as t2 ON t2.id = id`
)
assert.deepStrictEqual(sql.values, [id])
})
test('should be able to append query that is using "quoteIdent(...)"', async t => {
const table = 'teams'
const id = 123
const reusableSql = SQL`SELECT id FROM ${quoteIdent(table)} WHERE id = ${id}`
const sql = SQL`SELECT * FROM ${quoteIdent(table)} INNER JOIN (`
sql.append(reusableSql)
sql.append(SQL`) as t2 ON t2.id = id`)
assert.equal(
sql.text,
'SELECT * FROM "teams" INNER JOIN (SELECT id FROM "teams" WHERE id = $1) as t2 ON t2.id = id'
)
assert.equal(
sql.sql,
'SELECT * FROM `teams` INNER JOIN (SELECT id FROM `teams` WHERE id = ?) as t2 ON t2.id = id'
)
assert.equal(
sql.debug,
`SELECT * FROM "teams" INNER JOIN (SELECT id FROM "teams" WHERE id = ${id}) as t2 ON t2.id = id`
)
assert.deepStrictEqual(sql.values, [id])
})
test('should be able to append a SqlStatement within a template literal', t => {
const a = SQL`FROM table`
const selectWithLiteralExpression = SQL`SELECT * ${a}`
assert.equal(selectWithLiteralExpression.text, 'SELECT * FROM table')
assert.equal(selectWithLiteralExpression.sql, 'SELECT * FROM table')
assert.equal(selectWithLiteralExpression.debug, 'SELECT * FROM table')
})
test('should be able to use SQL.glue within template literal', t => {
const pre = 'A'
const ids = [1, '2', 'three']
const idValues = ids.map(id => SQL`${id}`)
const names = ['Bee', 'Cee', 'Dee']
const nameValues = names.map(name => SQL`${name}`)
const post = 'B'
const sql = SQL`UPDATE my_table SET active = FALSE WHERE pre=${pre} AND id IN (${SQL.glue(
idValues,
','
)}) AND name IN (${SQL.glue(nameValues, ',')}) AND post=${post}`
assert.equal(
sql.text,
'UPDATE my_table SET active = FALSE WHERE pre=$1 AND id IN ($2,$3,$4) AND name IN ($5,$6,$7) AND post=$8'
)
assert.equal(
sql.sql,
'UPDATE my_table SET active = FALSE WHERE pre=? AND id IN (?,?,?) AND name IN (?,?,?) AND post=?'
)
assert.equal(
sql.debug,
"UPDATE my_table SET active = FALSE WHERE pre='A' AND id IN (1,'2','three') AND name IN ('Bee','Cee','Dee') AND post='B'"
)
assert.deepStrictEqual(sql.values, ['A', 1, '2', 'three', 'Bee', 'Cee', 'Dee', 'B'])
})
test('should be able to use nested SQLStatements in template literal', t => {
const a = 'A'
const b = 'B'
const c = 'C'
const d = 'D'
const sql = SQL`UPDATE my_table SET active = FALSE WHERE a=${a} AND ${SQL`b=${b} AND ${SQL`c=${c}`}`} AND d=${d}`
assert.equal(
sql.text,
'UPDATE my_table SET active = FALSE WHERE a=$1 AND b=$2 AND c=$3 AND d=$4'
)
assert.equal(
sql.sql,
'UPDATE my_table SET active = FALSE WHERE a=? AND b=? AND c=? AND d=?'
)
assert.equal(
sql.debug,
"UPDATE my_table SET active = FALSE WHERE a='A' AND b='B' AND c='C' AND d='D'"
)
assert.deepStrictEqual(sql.values, ['A', 'B', 'C', 'D'])
})
test('should be able to use the result of SQL.glue([SQL``, SQL``], separator) result with multiple values inside the first element of SQL.glue', t => {
const ids = [1, 2, 3]
const name = 'foo'
const inIds = SQL.glue(
ids.map((id) => SQL`${id}`),
' , '
)
const condition = SQL`tsd.id IN (${inIds})`
const filters = [
condition,
SQL`tsd.name = ${name}`
]
const sql = SQL`SELECT tsd.* FROM data tsd WHERE ${SQL.glue(filters, ' AND ')}`
assert.equal(
sql.text,
'SELECT tsd.* FROM data tsd WHERE tsd.id IN ($1 , $2 , $3) AND tsd.name = $4'
)
assert.deepStrictEqual(
sql.values, [1, 2, 3, 'foo']
)
assert.equal(
sql.debug,
"SELECT tsd.* FROM data tsd WHERE tsd.id IN (1 , 2 , 3) AND tsd.name = 'foo'"
)
})
test('examples in the readme work as expected', t => {
{
const username = 'user1'
const email = '[email protected]'
const userId = 1
const updates = []
updates.push(SQL`name = ${username}`)
updates.push(SQL`email = ${email}`)
const sql = SQL`UPDATE users SET ${SQL.glue(
updates,
' , '
)} WHERE id = ${userId}`
assert.equal(sql.text, 'UPDATE users SET name = $1 , email = $2 WHERE id = $3')
}
{
const ids = [1, 2, 3]
const value = 'test'
const sql = SQL`
UPDATE users
SET property = ${value}
WHERE id
IN (${SQL.glue(
ids.map(id => SQL`${id}`),
' , '
)})
`
assert.equal(
sql.text,
`UPDATE users
SET property = $1
WHERE id
IN ($2 , $3 , $4)`
)
}
{
const users = [
{ id: 1, name: 'something' },
{ id: 2, name: 'something-else' },
{ id: 3, name: 'something-other' }
]
const sql = SQL`INSERT INTO users (id, name) VALUES
${SQL.glue(
users.map(user => SQL`(${user.id},${user.name}})`),
' , '
)}
`
assert.equal(
sql.text,
`INSERT INTO users (id, name) VALUES
($1,$2}) , ($3,$4}) , ($5,$6})`
)
}
})