feat: implement prepareSqlitePath function and add tests for database path handling

This commit is contained in:
MythEclipse
2026-05-16 00:12:31 +07:00
parent acd4648d49
commit c6ee7bb8c6
4 changed files with 39 additions and 2 deletions
+23
View File
@@ -0,0 +1,23 @@
import { mkdtemp, rm } from "node:fs/promises";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { describe, expect, test } from "bun:test";
import { prepareSqlitePath } from "./sqlitePath";
describe("prepareSqlitePath", () => {
test("creates parent directory for file database urls", async () => {
const root = await mkdtemp(join(tmpdir(), "booster-role-db-"));
const dbPath = join(root, "nested", "booster-role.sqlite");
expect(existsSync(join(root, "nested"))).toBe(false);
expect(prepareSqlitePath(`file:${dbPath}`)).toBe(dbPath);
expect(existsSync(join(root, "nested"))).toBe(true);
await rm(root, { recursive: true, force: true });
});
test("does not create a directory for in-memory databases", () => {
expect(prepareSqlitePath(":memory:")).toBe(":memory:");
});
});
+12
View File
@@ -0,0 +1,12 @@
import { mkdirSync } from "node:fs";
import { dirname } from "node:path";
export function prepareSqlitePath(databaseUrl: string): string {
const sqlitePath = databaseUrl.replace(/^file:/, "");
if (sqlitePath !== ":memory:") {
mkdirSync(dirname(sqlitePath), { recursive: true });
}
return sqlitePath;
}
+2 -1
View File
@@ -1,6 +1,7 @@
import { Database } from "bun:sqlite";
import type { Client } from "discord.js";
import type { AppConfig } from "../config";
import { prepareSqlitePath } from "../db/sqlitePath";
import { BoosterRoleService } from "../services/boosterRoleService";
import { BunSqliteBoosterRoleStore } from "../services/bunSqliteBoosterRoleStore";
import { DiscordRoleRepository } from "../services/discordRoleRepository";
@@ -8,7 +9,7 @@ import { handleGuildMemberUpdate } from "./events/guildMemberUpdate";
import { handleInteraction } from "./interactionHandler";
export function attachBotHandlers(client: Client, config: AppConfig): void {
const db = new Database(config.databaseUrl.replace(/^file:/, ""));
const db = new Database(prepareSqlitePath(config.databaseUrl));
const store = new BunSqliteBoosterRoleStore(db);
client.on("interactionCreate", async (interaction) => {