Skip to content

Commit 59299b1

Browse files
committed
fix: correct code for postgres
1 parent 1669fc4 commit 59299b1

File tree

3 files changed

+405
-66
lines changed

3 files changed

+405
-66
lines changed

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,11 @@ export type GetAuthorRowValues = [
2323
];
2424

2525
export async function getAuthor(sql: SQL, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
26-
const rows = await sql.unsafe(getAuthorQuery, [args.id]).values() as GetAuthorRowValues[];
26+
const rows = await sql.unsafe(getAuthorQuery, [args.id]) as GetAuthorRow[];
2727
if (rows.length !== 1) {
2828
return null;
2929
}
30-
const row = rows[0];
31-
if (!row) {
32-
return null;
33-
}
34-
return {
35-
id: Number(row[0]),
36-
name: row[1],
37-
bio: row[2]
38-
};
30+
return rows[0] ?? null;
3931
}
4032

4133
export const listAuthorsQuery = `-- name: ListAuthors :many
@@ -55,11 +47,7 @@ export type ListAuthorsRowValues = [
5547
];
5648

5749
export async function listAuthors(sql: SQL): Promise<ListAuthorsRow[]> {
58-
return (await sql.unsafe(listAuthorsQuery, []).values() as ListAuthorsRowValues[]).map(row => ({
59-
id: Number(row[0]),
60-
name: row[1],
61-
bio: row[2]
62-
}));
50+
return await sql.unsafe(listAuthorsQuery, []) as ListAuthorsRow[];
6351
}
6452

6553
export const createAuthorQuery = `-- name: CreateAuthor :one
@@ -88,19 +76,11 @@ export type CreateAuthorRowValues = [
8876
];
8977

9078
export async function createAuthor(sql: SQL, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
91-
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values() as CreateAuthorRowValues[];
79+
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]) as CreateAuthorRow[];
9280
if (rows.length !== 1) {
9381
return null;
9482
}
95-
const row = rows[0];
96-
if (!row) {
97-
return null;
98-
}
99-
return {
100-
id: Number(row[0]),
101-
name: row[1],
102-
bio: row[2]
103-
};
83+
return rows[0] ?? null;
10484
}
10585

10686
export const deleteAuthorQuery = `-- name: DeleteAuthor :exec

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,11 @@ export type GetAuthorRowValues = [
2323
];
2424

2525
export async function getAuthor(sql: SQL, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
26-
const rows = await sql.unsafe(getAuthorQuery, [args.id]).values() as GetAuthorRowValues[];
26+
const rows = await sql.unsafe(getAuthorQuery, [args.id]) as GetAuthorRow[];
2727
if (rows.length !== 1) {
2828
return null;
2929
}
30-
const row = rows[0];
31-
if (!row) {
32-
return null;
33-
}
34-
return {
35-
id: Number(row[0]),
36-
name: row[1],
37-
bio: row[2]
38-
};
30+
return rows[0] ?? null;
3931
}
4032

4133
export const listAuthorsQuery = `-- name: ListAuthors :many
@@ -55,11 +47,7 @@ export type ListAuthorsRowValues = [
5547
];
5648

5749
export async function listAuthors(sql: SQL): Promise<ListAuthorsRow[]> {
58-
return (await sql.unsafe(listAuthorsQuery, []).values() as ListAuthorsRowValues[]).map(row => ({
59-
id: Number(row[0]),
60-
name: row[1],
61-
bio: row[2]
62-
}));
50+
return await sql.unsafe(listAuthorsQuery, []) as ListAuthorsRow[];
6351
}
6452

6553
export const createAuthorQuery = `-- name: CreateAuthor :one
@@ -88,19 +76,11 @@ export type CreateAuthorRowValues = [
8876
];
8977

9078
export async function createAuthor(sql: SQL, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
91-
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values() as CreateAuthorRowValues[];
79+
const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]) as CreateAuthorRow[];
9280
if (rows.length !== 1) {
9381
return null;
9482
}
95-
const row = rows[0];
96-
if (!row) {
97-
return null;
98-
}
99-
return {
100-
id: Number(row[0]),
101-
name: row[1],
102-
bio: row[2]
103-
};
83+
return rows[0] ?? null;
10484
}
10585

10686
export const deleteAuthorQuery = `-- name: DeleteAuthor :exec

0 commit comments

Comments
 (0)