Skip to content

Commit d74dce5

Browse files
committed
refactor: replace DIY sql template with kysely
1 parent e329aef commit d74dce5

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

src/db/sql.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/rpc/server.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { RequestHandler } from "@hattip/compose";
22
import { type TinyRpcRoutes, createTinyRpcHandler } from "@hiogawa/tiny-rpc";
33
import { zodFn } from "@hiogawa/tiny-rpc/dist/zod";
44
import { tinyassert } from "@hiogawa/utils";
5+
import { sql } from "kysely";
56
import { z } from "zod";
6-
import { sql } from "../db/sql";
7+
import { db } from "../db";
78
import { env } from "../utils/worker-env";
89

910
export type RpcRoutes = typeof rpcRoutes;
@@ -47,17 +48,19 @@ const counterD1 = {
4748
id: 1,
4849

4950
async get() {
50-
const rows = await sql<{
51+
const { rows } = await sql<{
5152
value: number;
52-
}>`SELECT value from counter where id = ${this.id}`;
53+
}>`SELECT value from counter where id = ${this.id}`.execute(db);
5354
tinyassert(rows.length === 1);
5455
return rows[0].value;
5556
},
5657

5758
async update(delta: number) {
58-
const rows = await sql<{
59+
const { rows } = await sql<{
5960
value: number;
60-
}>`UPDATE Counter SET value = value + ${delta} WHERE id = ${this.id} RETURNING value`;
61+
}>`UPDATE Counter SET value = value + ${delta} WHERE id = ${this.id} RETURNING value`.execute(
62+
db,
63+
);
6164
tinyassert(rows.length === 1);
6265
return rows[0].value;
6366
},

0 commit comments

Comments
 (0)