A local-first personal finance app
// @ts-strict-ignore
import { createHash } from 'node:crypto';
import { makeViews, schema, schemaConfig } from './aql';
import * as db from './db';
import * as migrations from './migrate/migrations';
// Managing the init/update process
async function runMigrations() {
await migrations.migrate(db.getDatabase());
}
async function updateViews() {
const hashKey = 'view-hash';
const row = await db.first<{ value: string }>(
'SELECT value FROM __meta__ WHERE key = ?',
[hashKey],
);
const { value: hash } = row || {};
const views = makeViews(schema, schemaConfig);
const currentHash = createHash('md5').update(views).digest('hex');
if (hash !== currentHash) {
db.execQuery(views);
db.runQuery('INSERT OR REPLACE INTO __meta__ (key, value) VALUES (?, ?)', [
hashKey,
currentHash,
]);
}
}
export async function updateVersion() {
await runMigrations();
await updateViews();
}