Skip to content

Commit 69b92b1

Browse files
authored
Merge pull request #1 from maybephilipp/fix-array-type-inference
applied array infer fix
2 parents 6a6c879 + f9de127 commit 69b92b1

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/types.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { Query } from './query.js'
22
import { Errors } from './errors.js'
33

4+
const defaultArrayTypes = {
5+
"boolean": 1000,
6+
"number": 1021,
7+
"string": 1009,
8+
"bigint": 1016,
9+
};
10+
11+
export const inferArrayType = function inferArrayType(x) {
12+
return defaultArrayTypes[typeof x[0]] || inferType(x[0]);
13+
}
14+
415
export const types = {
516
string: {
617
to: 25,
@@ -224,7 +235,7 @@ export const inferType = function inferType(x) {
224235
x instanceof Uint8Array ? 17 :
225236
(x === true || x === false) ? 16 :
226237
typeof x === 'bigint' ? 20 :
227-
Array.isArray(x) ? inferType(x[0]) :
238+
Array.isArray(x) ? inferArrayType(x) :
228239
0
229240
)
230241
}

tests/index.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ t('Boolean false', async() =>
9292
[false, (await sql`select ${ false } as x`)[0].x]
9393
)
9494

95+
t('Boolean[] [true, false]', async() =>
96+
["[true,false]", JSON.stringify((await sql`select ${ [true, false] }::bool[] as x`)[0].x)]
97+
)
98+
99+
t('Number[] [1, 2]', async() =>
100+
["[1,2]", JSON.stringify((await sql`select ${ [1, 2] }::int4[] as x`)[0].x)]
101+
)
102+
103+
t('String[] ["a", "b"]', async() =>
104+
['["a","b"]', JSON.stringify((await sql`select ${ ["a", "b"] }::text[] as x`)[0].x)]
105+
)
106+
107+
t('BigInt[] [BigInt(1), BigInt(2)]', async() =>
108+
['[1,2]', JSON.stringify((await sql`select ${ [BigInt(1), BigInt(2)] }::float4[] as x`)[0].x)]
109+
)
110+
95111
t('Boolean true', async() =>
96112
[true, (await sql`select ${ true } as x`)[0].x]
97113
)
@@ -125,7 +141,7 @@ t('String array', async() =>
125141
)
126142

127143
t('Array of Integer', async() =>
128-
['3', (await sql`select ${ sql.array([1, 2, 3]) } as x`)[0].x[2]]
144+
[3, (await sql`select ${ sql.array([1, 2, 3]) } as x`)[0].x[2]]
129145
)
130146

131147
t('Array of String', async() =>
@@ -143,11 +159,11 @@ t('Array of Box', async() => [
143159
])
144160

145161
t('Nested array n2', async() =>
146-
['4', (await sql`select ${ sql.array([[1, 2], [3, 4]]) } as x`)[0].x[1][1]]
162+
[4, (await sql`select ${ sql.array([[1, 2], [3, 4]]) } as x`)[0].x[1][1]]
147163
)
148164

149165
t('Nested array n3', async() =>
150-
['6', (await sql`select ${ sql.array([[[1, 2]], [[3, 4]], [[5, 6]]]) } as x`)[0].x[2][0][1]]
166+
[6, (await sql`select ${ sql.array([[[1, 2]], [[3, 4]], [[5, 6]]]) } as x`)[0].x[2][0][1]]
151167
)
152168

153169
t('Escape in arrays', async() =>

0 commit comments

Comments
 (0)