2023-11-19 02:25:32 +09:00
|
|
|
import fs from "node:fs/promises";
|
2023-11-20 01:22:15 +09:00
|
|
|
import path from "node:path";
|
2023-11-19 02:25:32 +09:00
|
|
|
import sqlite3 from "sqlite3";
|
|
|
|
import { Database, open } from "sqlite";
|
|
|
|
|
|
|
|
export { Database };
|
|
|
|
|
|
|
|
export async function createDatabase(file: string): Promise<Database> {
|
2023-11-20 01:22:15 +09:00
|
|
|
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
|
|
|
2023-11-19 02:25:32 +09:00
|
|
|
const db = await open({
|
|
|
|
filename: file,
|
|
|
|
driver: sqlite3.cached.Database,
|
|
|
|
});
|
|
|
|
|
|
|
|
const migrationsPath = new URL("./migrations", import.meta.url).pathname;
|
|
|
|
await fs.chmod(file, 0o600);
|
|
|
|
await db.migrate({ migrationsPath });
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|