"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dumpPath = exports.root = void 0;
exports.useLocalKysely = useLocalKysely;
const node_path_1 = require("node:path");
const pglite_1 = require("@electric-sql/pglite");
const kysely_1 = require("kysely");
const kysely_pglite_1 = require("kysely-pglite");
exports.root = (0, node_path_1.join)(__dirname, '../..');
exports.dumpPath = (0, node_path_1.join)(exports.root, '.data.tar');
async function useLocalKysely() {
    let isDestroyed = false;
    const now = performance.now();
    const dump = Bun.file(exports.dumpPath);
    const pglite = new pglite_1.PGlite({ loadDataDir: dump });
    // warm up
    await pglite.sql `SELECT 1`;
    const { dialect } = new kysely_pglite_1.KyselyPGlite(pglite);
    const kysely = new kysely_1.Kysely({
        dialect,
        plugins: [new kysely_1.CamelCasePlugin()],
    });
    const originalDestroy = kysely.destroy.bind(kysely);
    kysely.destroy = async () => {
        if (isDestroyed)
            return;
        isDestroyed = true;
        const destroyTime = performance.now();
        await originalDestroy().catch(() => undefined);
        await pglite.close().catch(() => undefined);
        console.log(`Database destroyed: ${Math.round(performance.now() - destroyTime)}ms`);
    };
    console.log(`Database initialized: ${Math.round(performance.now() - now)}ms`);
    return kysely;
}
//# sourceMappingURL=index.js.map