Skip to content

Commit dea25ee

Browse files
committed
Fix generation for scalar queries
1 parent 7a72980 commit dea25ee

6 files changed

Lines changed: 95 additions & 1 deletion

File tree

examples/authors/postgresql/query.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ RETURNING *;
1717
-- name: DeleteAuthor :exec
1818
DELETE FROM authors
1919
WHERE id = $1;
20+
21+
-- name: CountAuthors :one
22+
SELECT COUNT(*)::bigint
23+
FROM authors;

examples/bun-pg/src/db/query_sql.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,26 @@ export async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Prom
115115
});
116116
}
117117

118+
export const countAuthorsQuery = `-- name: CountAuthors :one
119+
SELECT COUNT(*)::bigint
120+
FROM authors`;
121+
122+
export interface CountAuthorsRow {
123+
col_0: string;
124+
}
125+
126+
export async function countAuthors(client: Client): Promise<CountAuthorsRow | null> {
127+
const result = await client.query({
128+
text: countAuthorsQuery,
129+
values: [],
130+
rowMode: "array"
131+
});
132+
if (result.rows.length !== 1) {
133+
return null;
134+
}
135+
const row = result.rows[0];
136+
return {
137+
col_0: row[0]
138+
};
139+
}
140+

examples/bun-postgres/src/db/query_sql.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,25 @@ export async function deleteAuthor(sql: Sql, args: DeleteAuthorArgs): Promise<vo
9898
await sql.unsafe(deleteAuthorQuery, [args.id]);
9999
}
100100

101+
export const countAuthorsQuery = `-- name: CountAuthors :one
102+
SELECT COUNT(*)::bigint
103+
FROM authors`;
104+
105+
export interface CountAuthorsRow {
106+
col_0: string;
107+
}
108+
109+
export async function countAuthors(sql: Sql): Promise<CountAuthorsRow | null> {
110+
const rows = await sql.unsafe(countAuthorsQuery, []).values();
111+
if (rows.length !== 1) {
112+
return null;
113+
}
114+
const row = rows[0];
115+
if (!row) {
116+
return null;
117+
}
118+
return {
119+
col_0: row[0]
120+
};
121+
}
122+

examples/node-pg/src/db/query_sql.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,26 @@ export async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Prom
115115
});
116116
}
117117

118+
export const countAuthorsQuery = `-- name: CountAuthors :one
119+
SELECT COUNT(*)::bigint
120+
FROM authors`;
121+
122+
export interface CountAuthorsRow {
123+
col_0: string;
124+
}
125+
126+
export async function countAuthors(client: Client): Promise<CountAuthorsRow | null> {
127+
const result = await client.query({
128+
text: countAuthorsQuery,
129+
values: [],
130+
rowMode: "array"
131+
});
132+
if (result.rows.length !== 1) {
133+
return null;
134+
}
135+
const row = result.rows[0];
136+
return {
137+
col_0: row[0]
138+
};
139+
}
140+

examples/node-postgres/src/db/query_sql.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,25 @@ export async function deleteAuthor(sql: Sql, args: DeleteAuthorArgs): Promise<vo
9898
await sql.unsafe(deleteAuthorQuery, [args.id]);
9999
}
100100

101+
export const countAuthorsQuery = `-- name: CountAuthors :one
102+
SELECT COUNT(*)::bigint
103+
FROM authors`;
104+
105+
export interface CountAuthorsRow {
106+
col_0: string;
107+
}
108+
109+
export async function countAuthors(sql: Sql): Promise<CountAuthorsRow | null> {
110+
const rows = await sql.unsafe(countAuthorsQuery, []).values();
111+
if (rows.length !== 1) {
112+
return null;
113+
}
114+
const row = rows[0];
115+
if (!row) {
116+
return null;
117+
}
118+
return {
119+
col_0: row[0]
120+
};
121+
}
122+

src/drivers/utlis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function fieldName(
77
column?: Column
88
): string {
99
let name = `${prefix}_${index}`;
10-
if (column) {
10+
if (column?.name) {
1111
name = column.name;
1212
}
1313
return name

0 commit comments

Comments
 (0)