24 lines
741 B
TypeScript
24 lines
741 B
TypeScript
/**
|
|
* Creates the database from schema.sql when {@link DB_PATH} doesn't exist yet,
|
|
* and leaves an existing one alone. `deno task start` runs it before the
|
|
* server, which then runs every migration on the new file: schema.sql already
|
|
* holds their changes, so each one finds nothing to do.
|
|
*
|
|
* @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.");
|
|
}
|