diff --git a/database/scripts/deduce.ts b/database/scripts/deduce.ts index e984a65d..f56ebdc3 100644 --- a/database/scripts/deduce.ts +++ b/database/scripts/deduce.ts @@ -11,7 +11,7 @@ import { deduce() /** - * Makes deductions for categories and functors. + * Makes deductions for categories, functors, and other categorical structures. */ function deduce() { // --- categories --- diff --git a/database/scripts/setup.ts b/database/scripts/setup.ts index eab8551f..13c29f79 100644 --- a/database/scripts/setup.ts +++ b/database/scripts/setup.ts @@ -1,6 +1,6 @@ import fs from 'node:fs' import path from 'node:path' -import { get_client } from '$shared/db' +import { delete_database_file, get_client } from '$shared/db' import { create_schema_hash, write_schema_hash } from './utils/schema' import { devlog } from '$shared/utils' @@ -9,11 +9,13 @@ const schema_folder = path.resolve('database', 'schema') setup() /** - * Creates the tables, indexes, triggers, and views. + * Creates the tables, indexes, triggers, and views in the database. */ function setup() { console.info('\n--- Setup CatDat database ---') + delete_database_file() + const db = get_client({ readonly: false }) const files = fs diff --git a/database/scripts/snapshot.ts b/database/scripts/snapshot.ts new file mode 100644 index 00000000..f4a2d309 --- /dev/null +++ b/database/scripts/snapshot.ts @@ -0,0 +1,12 @@ +import fs from 'node:fs' +import path from 'node:path' + +/** + * For the download feature, the local database file is copied + * to the folder with static assets. + */ + +const source = path.resolve('database', 'catdat.db') +const target = path.join(path.resolve('static', 'databases'), 'catdat-snapshot.db') + +fs.copyFileSync(source, target) diff --git a/database/scripts/watch.ts b/database/scripts/watch.ts index 6d3b202a..fcc0e857 100644 --- a/database/scripts/watch.ts +++ b/database/scripts/watch.ts @@ -4,6 +4,10 @@ import { exec } from 'node:child_process' const data_folder = path.resolve('database', 'data') +/** + * Script that continuously updates all data when some file is changed. + */ + function onchange(file: string) { console.info(`File changed: ${file}`) diff --git a/package.json b/package.json index 98a9b66a..d7048574 100644 --- a/package.json +++ b/package.json @@ -15,12 +15,11 @@ "lint": "prettier --check .", "prepare": "husky", "db:shell": "sqlite3 database/catdat.db", - "db:kill": "rm -rf database/catdat.db", - "db:setup": "pnpm db:kill && tsx --tsconfig database/tsconfig.json database/scripts/setup.ts", + "db:setup": "tsx --tsconfig database/tsconfig.json database/scripts/setup.ts", "db:seed": "tsx --tsconfig database/tsconfig.json database/scripts/seed.ts", "db:deduce": "tsx --tsconfig database/tsconfig.json database/scripts/deduce.ts", "db:test": "tsx --tsconfig database/tsconfig.json database/scripts/test.ts", - "db:snapshot": "cp database/catdat.db static/databases/catdat-snapshot.db", + "db:snapshot": "tsx --tsconfig database/tsconfig.json database/scripts/snapshot.ts", "db:update": "pnpm db:seed && pnpm db:deduce && pnpm db:test && pnpm db:snapshot", "db:watch": "tsx --tsconfig database/tsconfig.json database/scripts/watch.ts", "db:redundancies": "tsx --tsconfig database/tsconfig.json database/scripts/redundancies.ts", diff --git a/shared/db.ts b/shared/db.ts index c557fa1f..9ce576ba 100644 --- a/shared/db.ts +++ b/shared/db.ts @@ -1,5 +1,6 @@ import Database from 'better-sqlite3' import path from 'node:path' +import fs from 'node:fs' const db_path = path.resolve('database', 'catdat.db') @@ -8,3 +9,7 @@ export function get_client(options: { readonly: boolean }) { db.pragma('foreign_keys = ON') return db } + +export function delete_database_file() { + fs.rmSync(db_path, { force: true }) +}