Skip to content

Commit 22651e3

Browse files
committed
Fix porsager#49 and improve sql.array usage
1 parent 5072079 commit 22651e3

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lib/index.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function Postgres(a, b) {
199199
function nested(first, rest) {
200200
const o = Object.create(notPromise)
201201
o.first = first
202-
o.rest = rest
202+
o.rest = rest.flat()
203203
return o
204204
}
205205

@@ -222,12 +222,10 @@ function Postgres(a, b) {
222222
return connection
223223
}
224224

225-
function array(value) {
226-
return {
227-
type: inferType(value) || 25,
228-
array: true,
229-
value
230-
}
225+
function array(xs) {
226+
const o = Object.create(notPromise)
227+
o.array = xs
228+
return o
231229
}
232230

233231
function json(value) {
@@ -395,10 +393,7 @@ function Postgres(a, b) {
395393

396394
for (let i = 1; i < xs.length; i++) {
397395
arg = args[i - 1]
398-
str += (arg && arg.P === notPromise.P
399-
? parseHelper(str, arg, xargs, types)
400-
: parseValue(arg, xargs, types)
401-
) + xs[i]
396+
str += parseArg(str, arg, xargs, types) + xs[i]
402397
}
403398

404399
return {
@@ -408,6 +403,21 @@ function Postgres(a, b) {
408403
}
409404
}
410405

406+
function parseArg(str, arg, xargs, types) {
407+
return arg && arg.P === notPromise.P
408+
? arg.array
409+
? parseArray(arg.array, xargs, types)
410+
: parseHelper(str, arg, xargs, types)
411+
: parseValue(arg, xargs, types)
412+
}
413+
414+
function parseArray(array, xargs, types) {
415+
return array.length === 0 ? '\'{}\'' : 'array[' + array.map((x) => Array.isArray(x)
416+
? parseArray(x, xargs, types)
417+
: parseValue(x, xargs, types)
418+
).join(',') + ']'
419+
}
420+
411421
function parseHelper(str, { first, rest }, xargs, types) {
412422
xargs.dynamic = true
413423
if (first !== null && typeof first === 'object' && typeof first[0] !== 'string') {
@@ -458,6 +468,8 @@ function Postgres(a, b) {
458468

459469
return Array.isArray(x)
460470
? x.reduce((acc, x) => acc + (acc ? ',' : '') + addValue(x, xargs, types), '')
471+
: x && x.P === notPromise.P
472+
? parseArg('', x, xargs, types)
461473
: addValue(x, xargs, types)
462474
}
463475

@@ -477,7 +489,7 @@ function Postgres(a, b) {
477489
return { type: 0, value: x }
478490

479491
const value = x.type ? x.value : x
480-
, type = x.array ? typeArrayMap[x.type] : (x.type || inferType(value))
492+
, type = x.type || inferType(value)
481493

482494
return {
483495
type,

tests/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ t('Json', async() => {
109109
})
110110

111111
t('Empty array', async() =>
112-
[0, (await sql`select ${ sql.array([]) } as x`)[0].x.length]
112+
[true, Array.isArray((await sql`select ${ sql.array([]) }::int[] as x`)[0].x)]
113113
)
114114

115115
t('Array of Integer', async() =>
@@ -986,3 +986,21 @@ t('Result as arrays', async() => {
986986

987987
return ['1,2', (await sql`select 1 as a, 2 as b`)[0].join(',')]
988988
})
989+
990+
t('Insert empty array', async() => {
991+
await sql`create table tester (ints int[])`
992+
return [
993+
Array.isArray((await sql`insert into tester (ints) values (${ sql.array([]) }) returning *`)[0].ints),
994+
true,
995+
await sql`drop table tester`
996+
]
997+
})
998+
999+
t('Insert array in sql()', async() => {
1000+
await sql`create table tester (ints int[])`
1001+
return [
1002+
Array.isArray((await sql`insert into tester ${ sql({ ints: sql.array([]) })} returning *`)[0].ints),
1003+
true,
1004+
await sql`drop table tester`
1005+
]
1006+
})

0 commit comments

Comments
 (0)