Skip to content

Commit 11e5aba

Browse files
committed
feat: handle aggregate queries
1 parent d543de1 commit 11e5aba

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ ${query.text.trim()}`,
181181
}
182182

183183
let argIface = undefined;
184-
let returnIface = undefined;
184+
let returnIface: string | undefined;
185185
if (query.params.length > 0) {
186186
argIface = `${nameWithoutFileNs}Args`;
187187
nodesToPush.push(argsDecl(argIface, driver, query.params));
@@ -190,6 +190,9 @@ ${query.text.trim()}`,
190190
returnIface = driver.parseDatabaseType(
191191
query.columns[0].type?.name ?? "",
192192
);
193+
if (query.columns[0].isArray) {
194+
returnIface += "[]";
195+
}
193196
} else if (query.columns.length > 1) {
194197
returnIface = `${nameWithoutFileNs}Row`;
195198
nodesToPush.push(rowDecl(returnIface, driver, query.columns, embeds));

test/cases/aggregate.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect, it, describe } from "bun:test";
2+
import { db, prepare, sql } from "../case";
3+
import { aggEmails } from "./aggregate_sql";
4+
5+
await prepare(sql`
6+
-- name: AggEmails :one
7+
SELECT array_agg(e.id::text)::text[] AS emails FROM emails e;
8+
`);
9+
10+
describe("aggregation", () => {
11+
it("returns an array type", async () => {
12+
// this should not have any LSP error
13+
const result = (await aggEmails(db)) as string[];
14+
15+
expect(result).toHaveLength(3);
16+
expect(Array.isArray(result)).toBe(true);
17+
});
18+
});

0 commit comments

Comments
 (0)