Skip to content

Commit 2344933

Browse files
committed
fix examples
1 parent 85cd6a8 commit 2344933

6 files changed

Lines changed: 71 additions & 15 deletions

File tree

examples/authors/postgresql/query.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ ORDER BY name;
88

99
-- name: CreateAuthor :one
1010
INSERT INTO authors (
11-
name, bio
11+
name, bio, status
1212
) VALUES (
13-
$1, $2
13+
$1, $2, $3
1414
)
1515
RETURNING *;
1616

17+
-- name: ListAuthorsByStatus :many
18+
SELECT * FROM authors
19+
WHERE status = $1
20+
ORDER BY name;
21+
1722
-- name: DeleteAuthor :exec
1823
DELETE FROM authors
1924
WHERE id = $1;
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
CREATE TYPE author_status AS ENUM ('active', 'inactive', 'pending');
2+
13
CREATE TABLE authors (
2-
id BIGSERIAL PRIMARY KEY,
3-
name text NOT NULL,
4-
bio text
4+
id BIGSERIAL PRIMARY KEY,
5+
name text NOT NULL,
6+
bio text,
7+
status author_status NOT NULL DEFAULT 'active'
58
);

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import type { Sql } from "postgres";
44

5+
export type AuthorStatus = "active" | "inactive" | "pending";
6+
57
export interface GetAuthorArgs {
68
id: number;
79
}
@@ -10,10 +12,11 @@ export interface GetAuthorRow {
1012
id: number;
1113
name: string;
1214
bio: string | null;
15+
status: AuthorStatus;
1316
}
1417

1518
export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
16-
const rows = await sql<GetAuthorRow[]> `SELECT id, name, bio FROM authors
19+
const rows = await sql<GetAuthorRow[]> `SELECT id, name, bio, status FROM authors
1720
WHERE id = ${args.id} LIMIT 1`;
1821
return rows[0] ?? null;
1922
}
@@ -22,34 +25,54 @@ export interface ListAuthorsRow {
2225
id: number;
2326
name: string;
2427
bio: string | null;
28+
status: AuthorStatus;
2529
}
2630

2731
export async function listAuthors(sql: Sql): Promise<ListAuthorsRow[]> {
28-
return await sql<ListAuthorsRow[]> `SELECT id, name, bio FROM authors
32+
return await sql<ListAuthorsRow[]> `SELECT id, name, bio, status FROM authors
2933
ORDER BY name`;
3034
}
3135

3236
export interface CreateAuthorArgs {
3337
name: string;
3438
bio: string | null;
39+
status: AuthorStatus;
3540
}
3641

3742
export interface CreateAuthorRow {
3843
id: number;
3944
name: string;
4045
bio: string | null;
46+
status: AuthorStatus;
4147
}
4248

4349
export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
4450
const rows = await sql<CreateAuthorRow[]> `INSERT INTO authors (
45-
name, bio
51+
name, bio, status
4652
) VALUES (
47-
${args.name}, ${args.bio}
53+
${args.name}, ${args.bio}, ${args.status}
4854
)
49-
RETURNING id, name, bio`;
55+
RETURNING id, name, bio, status`;
5056
return rows[0] ?? null;
5157
}
5258

59+
export interface ListAuthorsByStatusArgs {
60+
status: AuthorStatus;
61+
}
62+
63+
export interface ListAuthorsByStatusRow {
64+
id: number;
65+
name: string;
66+
bio: string | null;
67+
status: AuthorStatus;
68+
}
69+
70+
export async function listAuthorsByStatus(sql: Sql, args: ListAuthorsByStatusArgs): Promise<ListAuthorsByStatusRow[]> {
71+
return await sql<ListAuthorsByStatusRow[]> `SELECT id, name, bio, status FROM authors
72+
WHERE status = ${args.status}
73+
ORDER BY name`;
74+
}
75+
5376
export interface DeleteAuthorArgs {
5477
id: number;
5578
}

examples/bun-postgres/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async function main() {
1414
const author = await createAuthor(sql, {
1515
name: "Seal",
1616
bio: "Kissed from a rose",
17+
status: "active",
1718
});
1819
if (author === null) {
1920
throw new Error("author not created");

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import type { Sql } from "postgres";
44

5+
export type AuthorStatus = "active" | "inactive" | "pending";
6+
57
export interface GetAuthorArgs {
68
id: number;
79
}
@@ -10,10 +12,11 @@ export interface GetAuthorRow {
1012
id: number;
1113
name: string;
1214
bio: string | null;
15+
status: AuthorStatus;
1316
}
1417

1518
export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAuthorRow | null> {
16-
const rows = await sql<GetAuthorRow[]> `SELECT id, name, bio FROM authors
19+
const rows = await sql<GetAuthorRow[]> `SELECT id, name, bio, status FROM authors
1720
WHERE id = ${args.id} LIMIT 1`;
1821
return rows[0] ?? null;
1922
}
@@ -22,34 +25,54 @@ export interface ListAuthorsRow {
2225
id: number;
2326
name: string;
2427
bio: string | null;
28+
status: AuthorStatus;
2529
}
2630

2731
export async function listAuthors(sql: Sql): Promise<ListAuthorsRow[]> {
28-
return await sql<ListAuthorsRow[]> `SELECT id, name, bio FROM authors
32+
return await sql<ListAuthorsRow[]> `SELECT id, name, bio, status FROM authors
2933
ORDER BY name`;
3034
}
3135

3236
export interface CreateAuthorArgs {
3337
name: string;
3438
bio: string | null;
39+
status: AuthorStatus;
3540
}
3641

3742
export interface CreateAuthorRow {
3843
id: number;
3944
name: string;
4045
bio: string | null;
46+
status: AuthorStatus;
4147
}
4248

4349
export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<CreateAuthorRow | null> {
4450
const rows = await sql<CreateAuthorRow[]> `INSERT INTO authors (
45-
name, bio
51+
name, bio, status
4652
) VALUES (
47-
${args.name}, ${args.bio}
53+
${args.name}, ${args.bio}, ${args.status}
4854
)
49-
RETURNING id, name, bio`;
55+
RETURNING id, name, bio, status`;
5056
return rows[0] ?? null;
5157
}
5258

59+
export interface ListAuthorsByStatusArgs {
60+
status: AuthorStatus;
61+
}
62+
63+
export interface ListAuthorsByStatusRow {
64+
id: number;
65+
name: string;
66+
bio: string | null;
67+
status: AuthorStatus;
68+
}
69+
70+
export async function listAuthorsByStatus(sql: Sql, args: ListAuthorsByStatusArgs): Promise<ListAuthorsByStatusRow[]> {
71+
return await sql<ListAuthorsByStatusRow[]> `SELECT id, name, bio, status FROM authors
72+
WHERE status = ${args.status}
73+
ORDER BY name`;
74+
}
75+
5376
export interface DeleteAuthorArgs {
5477
id: number;
5578
}

examples/node-postgres/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async function main() {
1414
const author = await createAuthor(sql, {
1515
name: "Seal",
1616
bio: "Kissed from a rose",
17+
status: "active",
1718
});
1819
if (author === null) {
1920
throw new Error("author not created");

0 commit comments

Comments
 (0)