Skip to content

Commit 7ee9e5e

Browse files
committed
fix: remove parens from cols in buildUnnest()
1 parent 58e9b24 commit 7ee9e5e

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

docs/api.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,23 @@ const unnestUsers = buildUnnest<User>({
240240
type: 'timestamptz',
241241
transform: (user) => new Date(user.dateRegistered), // optional custom transform
242242
},
243-
teamId: {type: 'int4'},
243+
teamId: {type: 'int'},
244244
});
245245

246246
// 3. Build the query
247247

248248
const {cols, unnest, values} = unnestUsers(users);
249249
const text = `
250-
INSERT INTO users ${cols}
250+
INSERT INTO users (${cols})
251251
SELECT * FROM ${unnest}
252252
ON CONFLICT (id) DO NOTHING
253253
`;
254254

255+
// This will generate the following SQL:
256+
// INSERT INTO users ("id", "dateRegistered", "teamId")
257+
// SELECT * FROM UNNEST($1::uuid[], $2::timestamptz[], $3::int4[])
258+
// ON CONFLICT (id) DO NOTHING
259+
255260
await db.query({text, values});
256261
```
257262

src/buildUnnest.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('buildUnnest returns correct cols, unnest, and values', async (assert) => {
2525
return assert.equal(
2626
JSON.stringify({cols, unnest, values}),
2727
JSON.stringify({
28-
cols: '("id", "time", "level")',
28+
cols: '"id", "time", "level"',
2929
unnest: 'UNNEST($1::uuid[], $2::timestamptz[], $3::int[]) AS t("id", "time", "level")',
3030
values: [
3131
['a', 'b'],
@@ -54,7 +54,7 @@ test('buildUnnest works with default accessors', async (assert) => {
5454
return assert.equal(
5555
JSON.stringify({cols, unnest, values}),
5656
JSON.stringify({
57-
cols: '("a", "b")',
57+
cols: '"a", "b"',
5858
unnest: 'UNNEST($1::int[], $2::text[]) AS t("a", "b")',
5959
values: [[1, 2], ['one', 'two']],
6060
})
@@ -79,7 +79,7 @@ test('buildUnnest handles nulls and missing values safely', async (assert) => {
7979
return assert.equal(
8080
JSON.stringify({cols, unnest, values}),
8181
JSON.stringify({
82-
cols: '("name", "note")',
82+
cols: '"name", "note"',
8383
unnest: 'UNNEST($1::text[], $2::text[]) AS t("name", "note")',
8484
values: [['Alice', 'Bob'], [null, 'hi']],
8585
})

src/buildUnnest.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type SchemaEntry<T> = {
88
};
99
type SchemaSpec<T> = Record<keyof T, SchemaEntry<T>>;
1010
type UnnestResult = {
11-
cols: string // e.g. ("id", "time", "level")
11+
cols: string // e.g. "id", "time", "level"
1212
unnest: string // e.g. UNNEST($1::uuid[], ...) AS t("id", "time", ...)
1313
values: any[][] // column-aligned arrays
1414
};
@@ -59,14 +59,11 @@ export function buildUnnest<T>(spec: SchemaSpec<T>): (rows: T[]) => UnnestResult
5959
}
6060

6161
const values: any[][] = keys.map((key) => cols[key]);
62-
63-
const colList = `(${keys.map((k) => `"${k}"`).join(', ')})`;
64-
62+
const colList = `${keys.map((k) => `"${k}"`).join(', ')}`;
6563
const unnestList = keys
6664
.map((key, i) => `$${i + 1}::${types[key]}[]`)
6765
.join(', ');
68-
69-
const unnest = `UNNEST(${unnestList}) AS t${colList}`;
66+
const unnest = `UNNEST(${unnestList}) AS t(${colList})`;
7067

7168
return {
7269
cols: colList,

0 commit comments

Comments
 (0)