Skip to content

Build your personal knowledge base with Trilium Notes

3.2 KB
Raw Blame
import type { DatabaseProvider, Statement, Transaction } from "@triliumnext/core";
import Database, { type Database as DatabaseType } from "better-sqlite3";
import { unlinkSync } from "fs";

const dbOpts: Database.Options = {
    nativeBinding: process.env.BETTERSQLITE3_NATIVE_PATH || undefined
};

export default class BetterSqlite3Provider implements DatabaseProvider {

    private dbConnection?: DatabaseType;

    constructor() {
        [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach((eventType) => {
            // closing connection is especially important to fold -wal file into the main DB file
            // (see https://sqlite.org/tempfiles.html for details)
            process.on(eventType, () => this.close());
        });
    }

    loadFromFile(path: string, isReadOnly: boolean) {
        this.dbConnection = new Database(path, {
            ...dbOpts,
            readonly: isReadOnly
        });

        // The `edit-integration-db` tool (TRILIUM_INTEGRATION_TEST=edit) attaches
        // directly to the git-tracked fixture DB. Keep it in the rollback journal
        // mode so it stays a single committable file: WAL is persisted in the DB
        // header and would spawn -wal/-shm sidecars, making the fixture harder to
        // track (see docs Testing/Test database.md).
        const journalMode = process.env.TRILIUM_INTEGRATION_TEST ? "DELETE" : "WAL";
        this.dbConnection.pragma(`journal_mode = ${journalMode}`);
    }

    loadFromMemory() {
        this.dbConnection = new Database(":memory:", dbOpts);
    }

    loadFromBuffer(buffer: NonSharedBuffer) {
        // Close any existing connection so its file handles are released
        // before we replace it. Important for repeated rebuilds in tests
        // (each call would otherwise leak the previous handle).
        this.dbConnection?.close();
        this.dbConnection = new Database(buffer, dbOpts);
    }

    detach() {
        // Closing is what folds the -wal file back into the database and lets the file be replaced;
        // on Windows it cannot even be renamed while a handle is open.
        this.dbConnection?.close();
        this.dbConnection = undefined;
    }

    isAttached() {
        return this.dbConnection !== undefined;
    }

    async backup(destinationFile: string) {
        try {
            unlinkSync(destinationFile);
        } catch (e) { } // unlink throws exception if the file did not exist

        await this.dbConnection?.backup(destinationFile);
    }

    prepare(query: string): Statement {
        if (!this.dbConnection) throw new Error("DB not open.");
        // Cast is safe: better-sqlite3 only returns bigint when safeIntegers() is enabled, which we don't use.
        return this.dbConnection.prepare(query) as unknown as Statement;
    }

    transaction<T>(func: (statement: Statement) => T): Transaction {
        if (!this.dbConnection) throw new Error("DB not open.");
        return this.dbConnection.transaction(func) as any;
    }

    get inTransaction() {
        if (!this.dbConnection) throw new Error("DB not open.");
        return this.dbConnection.inTransaction;
    }

    exec(query: string): void {
        this.dbConnection?.exec(query);
    }

    close() {
        this.dbConnection?.close();
    }

}