22 lines
589 B
TypeScript
22 lines
589 B
TypeScript
/**
|
|
* Creates the database from schema.sql when {@link DB_PATH} doesn't exist yet;
|
|
* run once before the server starts (`deno task start`).
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { DatabaseSync } from "node:sqlite";
|
|
import { DB_PATH } from "../config.ts";
|
|
|
|
try {
|
|
await Deno.stat(DB_PATH);
|
|
console.log("Database already exists, skipping initialization.");
|
|
} catch {
|
|
console.log("Initializing database from schema...");
|
|
const schema = Deno.readTextFileSync("api/db/schema.sql");
|
|
const db = new DatabaseSync(DB_PATH);
|
|
db.exec(schema);
|
|
db.close();
|
|
console.log("Database initialized.");
|
|
}
|