Skip to content

Commit a2789f9

Browse files
committed
fix: specify missing numeric types as numbers
1 parent 4e5ad1f commit a2789f9

File tree

7 files changed

+52
-16
lines changed

7 files changed

+52
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ WHERE id = $1 LIMIT 1;
4545
const name = await Authors.get(sql, {id: 1});
4646
typeof name // string
4747
48-
await Authors.get(sql, {id: 9999}); // error thrown
48+
await Authors.get(sql, {id: nonExistingId}); // error
4949
```
5050

5151
### Export specific queries in a module

examples/authors/postgresql/query1.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ ORDER BY name;
1414
SELECT name FROM authors
1515
where id = $1 limit 1;
1616

17+
-- name: GetCount :one
18+
SELECT count(id) FROM authors;
19+
1720
-- name: Read_Nested_List :many
1821
SELECT * FROM authors
1922
ORDER BY name;

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export module Read {
77
SELECT id, name, bio FROM authors
88
WHERE id = $1 LIMIT 1`;
99
export interface GetAuthorArgs {
10-
id: string;
10+
id: number;
1111
}
1212
export interface GetAuthorRow {
13-
id: string;
13+
id: number;
1414
name: string;
1515
bio: string | null;
1616
}
@@ -33,7 +33,7 @@ WHERE id = $1 LIMIT 1`;
3333
SELECT id, name, bio FROM authors
3434
ORDER BY name`;
3535
export interface ListAuthorsRow {
36-
id: string;
36+
id: number;
3737
name: string;
3838
bio: string | null;
3939
}
@@ -48,7 +48,7 @@ ORDER BY name`;
4848
SELECT name FROM authors
4949
where id = $1 limit 1`;
5050
export interface GetNameByIdArgs {
51-
id: string;
51+
id: number;
5252
}
5353
export async function getNameById(sql: Sql, args: GetNameByIdArgs): Promise<string> {
5454
const rows = await sql.unsafe(getNameByIdQuery, [args.id]).values();
@@ -61,12 +61,25 @@ where id = $1 limit 1`;
6161
}
6262
return row[0];
6363
}
64+
const getCountQuery = `-- name: GetCount :one
65+
SELECT count(id) FROM authors`;
66+
export async function getCount(sql: Sql): Promise<number> {
67+
const rows = await sql.unsafe(getCountQuery, []).values();
68+
if (rows.length !== 1) {
69+
throw new Error(`expected 1 row, got ${rows.length}`);
70+
}
71+
const row = rows[0];
72+
if (!row) {
73+
throw new Error("query returned empty row");
74+
}
75+
return row[0];
76+
}
6477
export module Nested {
6578
const listQuery = `-- name: Read_Nested_List :many
6679
SELECT id, name, bio FROM authors
6780
ORDER BY name`;
6881
export interface ListRow {
69-
id: string;
82+
id: number;
7083
name: string;
7184
bio: string | null;
7285
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export module Update {
77
DELETE FROM authors
88
WHERE id = $1`;
99
export interface DeleteAuthorArgs {
10-
id: string;
10+
id: number;
1111
}
1212
export async function deleteAuthor(sql: Sql, args: DeleteAuthorArgs): Promise<void> {
1313
await sql.unsafe(deleteAuthorQuery, [args.id]);
@@ -24,7 +24,7 @@ RETURNING id, name, bio`;
2424
bio: string | null;
2525
}
2626
export interface CreateAuthorRow {
27-
id: string;
27+
id: number;
2828
name: string;
2929
bio: string | null;
3030
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export module Read {
77
SELECT id, name, bio FROM authors
88
WHERE id = $1 LIMIT 1`;
99
export interface GetAuthorArgs {
10-
id: string;
10+
id: number;
1111
}
1212
export interface GetAuthorRow {
13-
id: string;
13+
id: number;
1414
name: string;
1515
bio: string | null;
1616
}
@@ -33,7 +33,7 @@ WHERE id = $1 LIMIT 1`;
3333
SELECT id, name, bio FROM authors
3434
ORDER BY name`;
3535
export interface ListAuthorsRow {
36-
id: string;
36+
id: number;
3737
name: string;
3838
bio: string | null;
3939
}
@@ -48,7 +48,7 @@ ORDER BY name`;
4848
SELECT name FROM authors
4949
where id = $1 limit 1`;
5050
export interface GetNameByIdArgs {
51-
id: string;
51+
id: number;
5252
}
5353
export async function getNameById(sql: Sql, args: GetNameByIdArgs): Promise<string> {
5454
const rows = await sql.unsafe(getNameByIdQuery, [args.id]).values();
@@ -61,12 +61,25 @@ where id = $1 limit 1`;
6161
}
6262
return row[0];
6363
}
64+
const getCountQuery = `-- name: GetCount :one
65+
SELECT count(id) FROM authors`;
66+
export async function getCount(sql: Sql): Promise<number> {
67+
const rows = await sql.unsafe(getCountQuery, []).values();
68+
if (rows.length !== 1) {
69+
throw new Error(`expected 1 row, got ${rows.length}`);
70+
}
71+
const row = rows[0];
72+
if (!row) {
73+
throw new Error("query returned empty row");
74+
}
75+
return row[0];
76+
}
6477
export module Nested {
6578
const listQuery = `-- name: Read_Nested_List :many
6679
SELECT id, name, bio FROM authors
6780
ORDER BY name`;
6881
export interface ListRow {
69-
id: string;
82+
id: number;
7083
name: string;
7184
bio: string | null;
7285
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export module Update {
77
DELETE FROM authors
88
WHERE id = $1`;
99
export interface DeleteAuthorArgs {
10-
id: string;
10+
id: number;
1111
}
1212
export async function deleteAuthor(sql: Sql, args: DeleteAuthorArgs): Promise<void> {
1313
await sql.unsafe(deleteAuthorQuery, [args.id]);
@@ -24,7 +24,7 @@ RETURNING id, name, bio`;
2424
bio: string | null;
2525
}
2626
export interface CreateAuthorRow {
27-
id: string;
27+
id: number;
2828
name: string;
2929
bio: string | null;
3030
}

src/drivers/postgres.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { argName, colName } from "./utlis";
1313
const typeMapping = {
1414
string: [
1515
"aclitem",
16-
"bigserial",
1716
"bit",
1817
"box",
1918
"bpchar",
@@ -49,6 +48,14 @@ const typeMapping = {
4948
"xml",
5049
],
5150
number: [
51+
"bigint",
52+
"bigserial",
53+
"smallserial",
54+
"double precision",
55+
"real",
56+
"decimal",
57+
"integer",
58+
"smallint",
5259
"int2",
5360
"int4",
5461
"int8",

0 commit comments

Comments
 (0)