add DB setup with migrations

This commit is contained in:
2025-05-25 11:06:30 -06:00
parent a48a122224
commit c14c2d173e
6 changed files with 185 additions and 31 deletions

View File

@@ -1,33 +1,6 @@
import initSqlJs from '@jlongster/sql.js';
import { SQLiteFS } from 'absurd-sql';
import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';
import databaseService from './services/database';
async function run() {
console.log("----- initSqlJs");
let SQL = await initSqlJs({
locateFile: file => {
// In Vite, we need to use the full URL to the WASM file
return new URL(`/node_modules/@jlongster/sql.js/dist/${file}`, import.meta.url).href;
}
});
let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());
SQL.register_for_idb(sqlFS);
SQL.FS.mkdir('/sql');
SQL.FS.mount(sqlFS, {}, '/sql');
const path = '/sql/db.sqlite';
if (typeof SharedArrayBuffer === 'undefined') {
let stream = SQL.FS.open(path, 'a+');
await stream.node.contents.readIfFallback();
SQL.FS.close(stream);
}
let db = new SQL.Database(path, { filename: true });
// You might want to try `PRAGMA page_size=8192;` too!
db.exec(`
PRAGMA journal_mode=MEMORY;
`);
console.log("----- db", db);
await databaseService.initialize();
}
run();