Skip to content

Commit 0065e5e

Browse files
committed
Add boolean plugin
1 parent 71c38f9 commit 0065e5e

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

src/storage/db/boolean-plugin.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
type KyselyPlugin,
3+
OperationNodeTransformer,
4+
type PluginTransformQueryArgs,
5+
type PluginTransformResultArgs,
6+
PrimitiveValueListNode,
7+
type QueryResult,
8+
type RootOperationNode,
9+
type UnknownRow,
10+
type ValueNode,
11+
} from "kysely";
12+
13+
// Source:
14+
// https://github.com/kysely-org/kysely/issues/123#issuecomment-1194184342
15+
16+
export class SqliteBooleanPlugin implements KyselyPlugin {
17+
readonly #transformer = new SqliteBooleanTransformer();
18+
19+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
20+
return this.#transformer.transformNode(args.node);
21+
}
22+
23+
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>> {
24+
return Promise.resolve(args.result);
25+
}
26+
}
27+
28+
class SqliteBooleanTransformer extends OperationNodeTransformer {
29+
override transformValue(node: ValueNode): ValueNode {
30+
return {
31+
...super.transformValue(node),
32+
value: this.serialize(node.value),
33+
};
34+
}
35+
36+
transformPrimitiveValueList(node: PrimitiveValueListNode): PrimitiveValueListNode {
37+
return {
38+
...super.transformPrimitiveValueList(node),
39+
values: node.values.map(value => this.serialize(value)),
40+
};
41+
}
42+
43+
private serialize(value: unknown) {
44+
return typeof value === "boolean" ? (value ? 1 : 0) : value;
45+
}
46+
}

src/storage/db/db.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { captureException, startInactiveSpan } from "@sentry/node";
88

99
import type { Database } from "./model.js";
1010
import datePlugin from "./date-plugin.js";
11+
import { SqliteBooleanPlugin } from "./boolean-plugin.js";
1112
import assertNever from "@/utils/assertNever.js";
1213
import log from "@log";
1314

@@ -22,7 +23,7 @@ export async function connectToDb(databasePath: string) {
2223

2324
const nativeDb = new SQLite(databasePath);
2425
const db = new Kysely<Database>({
25-
plugins: [datePlugin],
26+
plugins: [datePlugin, new SqliteBooleanPlugin()],
2627
dialect: new SqliteDialect({
2728
database: nativeDb,
2829
}),

src/utils/smoke.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ describe("database smoke", () => {
4343
// TODO: Check if there is a builtin way of handling DB lifecycle
4444
await createDatabase();
4545
await using _ = defer(closeDatabase);
46+
4647
await db()
4748
.insertInto("emoteUse")
4849
.values({
49-
channelId: "0",
50-
emoteId: 0,
51-
messageGuildId: "0",
50+
channelId: "13",
51+
emoteId: 37,
52+
messageGuildId: "42",
5253
isReaction: false,
5354
})
5455
.execute();

0 commit comments

Comments
 (0)