Skip to content

Commit f5bd794

Browse files
committed
feat: handle anyarray type case
1 parent db0a5d2 commit f5bd794

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/app.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import ts, {
1616
createPrinter,
1717
createSourceFile,
1818
factory,
19+
addSyntheticLeadingComment,
1920
} from "typescript";
2021

2122
import {
@@ -187,10 +188,15 @@ ${query.text.trim()}`,
187188
nodesToPush.push(argsDecl(argIface, driver, query.params));
188189
}
189190
if (query.columns.length === 1) {
190-
returnIface = driver.parseDatabaseType(
191-
query.columns[0].type?.name ?? "",
191+
const col = query.columns[0];
192+
returnIface = driver.parseDatabaseType(col.type?.name ?? "");
193+
addSyntheticLeadingComment(
194+
nodesToPush.slice(-1)[0],
195+
SyntaxKind.MultiLineCommentTrivia,
196+
`${col.type.name}, ${col.arrayDims}: ${returnIface}`,
197+
true,
192198
);
193-
if (query.columns[0].isArray) {
199+
if (col.isArray || col.type?.name === "anyarray") {
194200
returnIface += "[]";
195201
}
196202
} else if (query.columns.length > 1) {

src/drivers/postgres.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ export class Driver {
162162
keyword = factory.createKeywordTypeNode(SyntaxKind.StringKeyword);
163163
}
164164

165-
if (column.isArray || column.arrayDims > 0) {
165+
if (
166+
column.type?.name === "anyarray" ||
167+
column.isArray ||
168+
column.arrayDims > 0
169+
) {
166170
const dims = Math.max(column.arrayDims || 1);
167171
for (let i = 0; i < dims; i++) {
168172
keyword = factory.createArrayTypeNode(keyword);

test/cases/aggregate.test.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
11
import { expect, it, describe } from "bun:test";
2-
import { db, gen, prepare, sql } from "../case";
2+
import { db, prepare, sql } from "../case";
33
await prepare(sql`
4-
-- name: AggEmails :one
5-
SELECT array_agg(e.id::text)::text[] AS emails FROM emails e;
4+
-- name: AggEmailsOne :one
5+
SELECT array_agg(e.id)::text[] AS emails FROM emails e;
6+
7+
-- name: AggEmailsWithoutCastOne :one
8+
SELECT array_agg(e.id) AS emails FROM emails e;
9+
10+
-- name: AggEmailsMany :many
11+
SELECT 1 as foo, array_agg(e.id)::text[] AS emails FROM emails e;
12+
13+
-- name: AggEmailsWithoutCastMany :many
14+
SELECT 1 as foo, array_agg(e.id) AS emails FROM emails e;
615
`);
716

817
describe("aggregation", () => {
9-
it("returns an array type", async () => {
10-
const result = await gen().aggEmails(db);
18+
it("returns an array type in One mode", async () => {
19+
const result = await (await import("./aggregate_sql")).aggEmailsOne(db);
20+
21+
result as string[];
22+
23+
expect(result).toHaveLength(3);
24+
expect(Array.isArray(result)).toBe(true);
25+
});
26+
27+
it("returns an array type in Many mode", async () => {
28+
const result = await (await import("./aggregate_sql")).aggEmailsMany(db);
29+
30+
result[0].emails as string[];
31+
32+
expect(result[0].emails).toHaveLength(3);
33+
expect(Array.isArray(result)).toBe(true);
34+
});
35+
36+
it("returns an array type in a query without type cast in One mode", async () => {
37+
const result = await (
38+
await import("./aggregate_sql")
39+
).aggEmailsWithoutCastOne(db);
40+
41+
result as string[];
1142

1243
expect(result).toHaveLength(3);
1344
expect(Array.isArray(result)).toBe(true);
1445
});
46+
47+
it("returns an array type in a query without type cast in Many mode", async () => {
48+
const result = await (
49+
await import("./aggregate_sql")
50+
).aggEmailsWithoutCastMany(db);
51+
52+
result[0].emails as string[];
53+
54+
expect(result[0].emails).toHaveLength(3);
55+
expect(Array.isArray(result)).toBe(true);
56+
});
1557
});

0 commit comments

Comments
 (0)