91 lines
3.5 KiB
JavaScript
91 lines
3.5 KiB
JavaScript
// Generated, do not edit. sha256:0c36b25c75165df3de3ba590bc3b8b5e5c39b5db442fe5f91547c9706dc403d0
|
|||
|
|
import { join } from 'node:path';
|
||
|
|
import { CONFIG } from '../shared/config.js';
|
||
|
|
import { activeDir } from './portfolios.mjs';
|
||
|
|
import { openDatabase, closeDatabase, database, pragmaStand, withOtherDatabase, run, all, one } from './db/conn.mjs';
|
||
|
|
import { SCHEMA } from './db/schema.js';
|
||
|
|
import {
|
||
|
|
seedPreferences, seedProviders, seedConnections, seedAssets,
|
||
|
|
seedPriceSource, seedDashboardProfile,
|
||
|
|
} from './db/seeds.js';
|
||
|
|
import { runMigrations, readVersion } from './db/migrations.js';
|
||
|
|
|
||
|
|
export { run, all, one, lastId } from './db/conn.mjs';
|
||
|
|
export { SCHEMA } from './db/schema.js';
|
||
|
|
export { seedPriceSource } from './db/seeds.js';
|
||
|
|
|
||
|
|
export function dbFile() {
|
||
|
|
return process.env.ASSETAPP_DB_FILE || join(activeDir(), 'db.sqlite');
|
||
|
|
}
|
||
|
|
|
||
|
|
export function initDatabase(pad = dbFile(), { baseCurrency = CONFIG.PORTFOLIO.DEFAULT_BASE_CURRENCY } = {}) {
|
||
|
|
assertBaseCurrency(baseCurrency);
|
||
|
|
const bestaat = openBestaand(pad);
|
||
|
|
if (!bestaat) {
|
||
|
|
inrichten(baseCurrency);
|
||
|
|
} else {
|
||
|
|
runMigrations();
|
||
|
|
seedDashboardProfile();
|
||
|
|
}
|
||
|
|
return startregel(pad, bestaat);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createDatabase(pad, { baseCurrency = CONFIG.PORTFOLIO.DEFAULT_BASE_CURRENCY } = {}) {
|
||
|
|
assertBaseCurrency(baseCurrency);
|
||
|
|
withOtherDatabase(pad, () => {
|
||
|
|
if (one(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'Schema'`)) {
|
||
|
|
throw new Error(`createDatabase: ${pad} already holds a database.`);
|
||
|
|
}
|
||
|
|
inrichten(baseCurrency);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function assertBaseCurrency(code) {
|
||
|
|
if (!CONFIG.PORTFOLIO.BASE_CURRENCIES.includes(code)) {
|
||
|
|
throw new Error(`Unknown unit of account '${code}': not in CONFIG.PORTFOLIO.BASE_CURRENCIES.`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function inrichten(baseCurrency) {
|
||
|
|
run(SCHEMA);
|
||
|
|
run(`INSERT INTO Schema (version) VALUES (?)`, [CONFIG.SCHEMA_VERSION]);
|
||
|
|
run(`INSERT INTO Portfolio (id, baseCurrency, createdAt) VALUES (1, ?, ?)`, [baseCurrency, new Date().toISOString()]);
|
||
|
|
seedPreferences();
|
||
|
|
seedProviders();
|
||
|
|
seedConnections();
|
||
|
|
seedAssets();
|
||
|
|
seedPriceSource(null, 'confirmed');
|
||
|
|
seedDashboardProfile();
|
||
|
|
}
|
||
|
|
|
||
|
|
function openBestaand(pad) {
|
||
|
|
openDatabase(pad);
|
||
|
|
return !!one(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'Schema'`);
|
||
|
|
}
|
||
|
|
|
||
|
|
function startregel(pad, bestond) {
|
||
|
|
const p = pragmaStand();
|
||
|
|
const restjes = one(`SELECT COUNT(*) AS n FROM Preference WHERE scope = 'nwc'`);
|
||
|
|
const waarschuwing = restjes && restjes.n > 0
|
||
|
|
? `\n[assetapp] LET OP: er staan nog ${restjes.n} versleutelde NWC-rij(en) in Preference, uit de tijd `
|
||
|
|
+ 'dat geheimen in de database leefden. Ze zijn niet meer te ontsleutelen en niemand leest ze; '
|
||
|
|
+ 'opruimen kan via Systeem → Data.'
|
||
|
|
: '';
|
||
|
|
return `database: ${pad} (${bestond ? 'bestaand' : 'nieuw aangemaakt'}, versie ${readVersion()}) | `
|
||
|
|
+ `FK's: ${p.foreignKeys ? 'aan' : 'UIT'} | journal: ${p.journalMode}` + waarschuwing;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function schemaVersion() { return readVersion(); }
|
||
|
|
|
||
|
|
export function databaseStatus() {
|
||
|
|
const ingericht = one(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'Schema'`);
|
||
|
|
if (!ingericht) return { klaar: false, reden: 'leeg', versie: null };
|
||
|
|
const versie = readVersion();
|
||
|
|
if (versie < CONFIG.SCHEMA_VERSION) return { klaar: false, reden: 'oud', versie };
|
||
|
|
if (versie > CONFIG.SCHEMA_VERSION) return { klaar: false, reden: 'nieuwer', versie };
|
||
|
|
return { klaar: true, reden: null, versie };
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function persist() { }
|
||
|
|
|
||
|
|
export { closeDatabase, database };
|