@photostructure/sqlite
    Preparing search index...

    Interface DatabaseSyncInstance

    Represents a SQLite database connection. This interface represents an instance of the DatabaseSync class.

    interface DatabaseSyncInstance {
        isOpen: boolean;
        isTransaction: boolean;
        "[dispose]"(): void;
        aggregate(name: string, options: AggregateOptions): void;
        applyChangeset(
            changeset: Uint8Array,
            options?: ChangesetApplyOptions,
        ): boolean;
        close(): void;
        createSession(options?: SessionOptions): Session;
        createTagStore(capacity?: number): SQLTagStoreInstance;
        enableDefensive(active: boolean): void;
        enableLoadExtension(enable: boolean): void;
        exec(sql: string): void;
        function(name: string, func: Function): void;
        function(name: string, options: UserFunctionOptions, func: Function): void;
        loadExtension(path: string, entryPoint?: string): void;
        location(dbName?: string): string | null;
        open(): void;
        prepare(sql: string, options?: StatementOptions): StatementSyncInstance;
        setAuthorizer(
            callback:
                | (
                    (
                        actionCode: number,
                        param1: string | null,
                        param2: string | null,
                        param3: string | null,
                        param4: string | null,
                    ) => number
                )
                | null,
        ): void;
    }
    Index

    Properties

    isOpen: boolean

    Indicates whether the database connection is open.

    isTransaction: boolean

    Indicates whether a transaction is currently active.

    Methods

    • This method creates SQLite aggregate functions, wrapping sqlite3_create_window_function().

      Parameters

      • name: string

        The name of the SQLite aggregate function to create.

      • options: AggregateOptions

        Configuration object containing step function and other settings.

      Returns void

    • Closes the database connection. This method should be called to ensure that the database connection is properly cleaned up. Once a database is closed, it cannot be used again.

      Returns void

    • Create a new SQLTagStore for cached prepared statements via tagged template syntax.

      Parameters

      • Optionalcapacity: number

        Optional maximum number of statements to cache.

      Returns SQLTagStoreInstance

      A SQLTagStore instance.

      1000
      
      const sql = db.createTagStore();
      sql.run`INSERT INTO users VALUES (${id}, ${name})`;
      const user = sql.get`SELECT * FROM users WHERE id = ${id}`;
    • Enables or disables the loading of SQLite extensions.

      Parameters

      • enable: boolean

        If true, enables extension loading. If false, disables it.

      Returns void

    • This method allows one or more SQL statements to be executed without returning any results. This is useful for commands like CREATE TABLE, INSERT, UPDATE, or DELETE.

      Parameters

      • sql: string

        The SQL statement(s) to execute.

      Returns void

    • This method creates SQLite user-defined functions, wrapping sqlite3_create_function_v2().

      Parameters

      • name: string

        The name of the SQLite function to create.

      • func: Function

        The JavaScript function to call when the SQLite function is invoked.

      Returns void

    • This method creates SQLite user-defined functions, wrapping sqlite3_create_function_v2().

      Parameters

      • name: string

        The name of the SQLite function to create.

      • options: UserFunctionOptions

        Optional configuration settings.

      • func: Function

        The JavaScript function to call when the SQLite function is invoked.

      Returns void

    • Loads an SQLite extension from the specified file path.

      Parameters

      • path: string

        The path to the extension library.

      • OptionalentryPoint: string

        Optional entry point function name. If not provided, uses the default entry point.

      Returns void

    • Returns the location of the database file. For attached databases, you can specify the database name. Returns null for in-memory databases.

      Parameters

      • OptionaldbName: string

        The name of the database. Defaults to 'main' (the primary database).

      Returns string | null

      The file path of the database, or null for in-memory databases.

    • Opens a database connection. This method should only be used when the database was created with { open: false }. An exception is thrown if the database is already open.

      Returns void

    • Sets an authorization callback that is invoked before each SQL operation. The authorizer can allow, deny, or ignore each operation.

      Parameters

      • callback:
            | (
                (
                    actionCode: number,
                    param1: string | null,
                    param2: string | null,
                    param3: string | null,
                    param4: string | null,
                ) => number
            )
            | null

        The authorization callback function, or null to remove the authorizer.

        • actionCode: The action being requested (e.g., SQLITE_SELECT, SQLITE_INSERT)
        • param1: First parameter (varies by action, often table name)
        • param2: Second parameter (varies by action, often column name)
        • param3: Third parameter (usually database name like "main")
        • param4: Fourth parameter (trigger or view name if applicable)

        The callback must return one of:

        • constants.SQLITE_OK: Allow the operation
        • constants.SQLITE_DENY: Deny the operation and abort the SQL statement
        • constants.SQLITE_IGNORE: Silently ignore/skip the operation

      Returns void

      // Block all DELETE operations
      db.setAuthorizer((actionCode, table, column, dbName, trigger) => {
      if (actionCode === constants.SQLITE_DELETE) {
      return constants.SQLITE_DENY;
      }
      return constants.SQLITE_OK;
      });

      // Remove the authorizer
      db.setAuthorizer(null);