Skip to content

Commit

Permalink
Hotfix: Kysely doesn’t parse JSON values from sqslite (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcarl authored Sep 6, 2024
1 parent 2c86275 commit 236df07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/db.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SQLite from "better-sqlite3";
import { Kysely, SqliteDialect } from "kysely";
import { Kysely, ParseJSONResultsPlugin, SqliteDialect } from "kysely";
import type { DB } from "./db";
import { databaseUrl } from "./helpers/env";

Expand All @@ -13,6 +13,7 @@ export const dialect = new SqliteDialect({

const db = new Kysely<DB>({
dialect,
plugins: [new ParseJSONResultsPlugin()],
});

export default db;
Expand Down
7 changes: 5 additions & 2 deletions app/models/guilds.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const fetchSettings = async <T extends keyof typeof SETTINGS>(
guild: DiscordGuild,
keys: T[],
) => {
return (
const result = Object.entries(
(await db
.selectFrom("guilds")
// @ts-expect-error This is broken because of a migration from knex and
Expand All @@ -71,6 +71,9 @@ export const fetchSettings = async <T extends keyof typeof SETTINGS>(
)
.where("id", "=", guild.id)
// This cast is also evidence of the pattern being broken
.executeTakeFirstOrThrow()) as Pick<SettingsRecord, T>
.executeTakeFirstOrThrow()) as Pick<SettingsRecord, T>,
);
return Object.fromEntries(
result.map(([k, v]) => [k, JSON.parse(v as string)]),
);
};

0 comments on commit 236df07

Please sign in to comment.