UNPKG

alinea

Version:

[![npm](https://img.shields.io/npm/v/alinea.svg)](https://npmjs.org/package/alinea) [![install size](https://packagephobia.com/badge?p=alinea)](https://packagephobia.com/result?p=alinea)

130 lines (126 loc) 4.03 kB
import { prettyMilliseconds } from "../../chunks/chunk-7YXWNKGS.js"; import { connect } from "../../chunks/chunk-QV233D56.js"; import "../../chunks/chunk-FLZ4KUMA.js"; import { create, create2 } from "../../chunks/chunk-O6EXLFU2.js"; import "../../chunks/chunk-4JLFL6LD.js"; import "../../chunks/chunk-U5RRZUYZ.js"; // src/dashboard/util/PersistentStore.ts import sqlInit from "@alinea/sqlite-wasm"; // node_modules/lib0/indexeddb.js var rtop = (request) => create2((resolve, reject) => { request.onerror = (event) => reject(new Error(event.target.error)); request.onsuccess = (event) => resolve(event.target.result); }); var openDB = (name, initDB) => create2((resolve, reject) => { const request = indexedDB.open(name); request.onupgradeneeded = (event) => initDB(event.target.result); request.onerror = (event) => reject(create(event.target.error)); request.onsuccess = (event) => { const db = event.target.result; db.onversionchange = () => { db.close(); }; if (typeof addEventListener !== "undefined") { addEventListener("unload", () => db.close()); } resolve(db); }; }); var createStores = (db, definitions) => definitions.forEach( (d) => ( // @ts-ignore db.createObjectStore.apply(db, d) ) ); var transact = (db, stores, access = "readwrite") => { const transaction = db.transaction(stores, access); return stores.map((store) => getStore(transaction, store)); }; var get = (store, key) => rtop(store.get(key)); var del = (store, key) => rtop(store.delete(key)); var put = (store, item, key) => rtop(store.put(item, key)); var getStore = (t, store) => t.objectStore(store); // src/dashboard/util/PersistentStore.ts var STORAGE_NAME = "@alinea/peristent.store"; async function createPersistentStore() { const storagePromise = openDB( STORAGE_NAME, (db2) => createStores(db2, [[STORAGE_NAME, { autoIncrement: true }]]) ); const sqlitePromise = sqlInit(); const [storage, { Database }] = await Promise.all([ storagePromise, sqlitePromise ]); const [store] = transact(storage, [STORAGE_NAME], "readonly"); const buffer = await get(store, "db"); const init = ArrayBuffer.isView(buffer) ? buffer : void 0; let db = new Database(init); const driverOptions = { logQuery(stmt, duration) { if (!stmt.sql.startsWith("SELECT")) return; if (duration < 10) return; const icon = duration < 100 ? "\u26A1" : "\u26A0\uFE0F"; console.groupCollapsed( `${icon} Local query (${prettyMilliseconds(duration)})` ); console.groupCollapsed("SQL"); console.log(stmt.sql); console.groupEnd(); console.groupCollapsed("Params"); console.log(stmt.params()); console.groupEnd(); console.groupCollapsed("Query plan"); const explain = db.prepare( `explain query plan ${stmt.sql}`, stmt.params() ); const plan = []; while (explain.step()) plan.push(explain.getAsObject()); explain.free(); renderQueryPlan(plan); console.groupEnd(); console.groupEnd(); } }; const persistent = { store: connect(db, driverOptions).toAsync(), async flush() { const [store2] = transact(storage, [STORAGE_NAME], "readwrite"); await put(store2, db.export(), "db"); }, clone() { const clone = new Database(db.export()); return connect(clone, driverOptions).toAsync(); }, async clear() { const [store2] = transact(storage, [STORAGE_NAME], "readwrite"); await del(store2, "db"); db = new Database(); persistent.store = connect(db, driverOptions).toAsync(); } }; return persistent; } function renderQueryPlan(plan) { const depth = /* @__PURE__ */ new Map(); for (const line of plan) { const parentDepth = depth.get(line.parent) || 0; depth.set(line.id, parentDepth + 1); const indent = " ".repeat(parentDepth * 2); console.log(`${indent}${line.detail}`); } } export { createPersistentStore };