@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: Buffer, options?: ChangesetApplyOptions): boolean;
        backup(
            path: string | Buffer<ArrayBufferLike> | URL,
            options?: {
                progress?: (
                    info: { remainingPages: number; totalPages: number },
                ) => void;
                rate?: number;
                source?: string;
                target?: string;
            },
        ): Promise<number>;
        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

    • Makes a backup of the database. This method abstracts the sqlite3_backup_init(), sqlite3_backup_step() and sqlite3_backup_finish() functions.

      The backed-up database can be used normally during the backup process. Mutations coming from the same connection will be reflected in the backup right away. However, mutations from other connections will cause the backup process to restart.

      Parameters

      • path: string | Buffer<ArrayBufferLike> | URL

        The path where the backup will be created. If the file already exists, the contents will be overwritten.

      • Optionaloptions: {
            progress?: (
                info: { remainingPages: number; totalPages: number },
            ) => void;
            rate?: number;
            source?: string;
            target?: string;
        }

        Optional configuration for the backup operation.

        • Optionalprogress?: (info: { remainingPages: number; totalPages: number }) => void

          Callback function that will be called with the number of pages copied and the total number of pages.

        • Optionalrate?: number

          Number of pages to be transmitted in each batch of the backup.

        • Optionalsource?: string

          Name of the source database. This can be 'main' (the default primary database) or any other database that have been added with ATTACH DATABASE.

        • Optionaltarget?: string

          Name of the target database. This can be 'main' (the default primary database) or any other database that have been added with ATTACH DATABASE.

      Returns Promise<number>

      A promise that resolves when the backup is completed and rejects if an error occurs.

      100
      
      'main'
      
      'main'
      
      // Basic backup
      await db.backup('./backup.db');
      // Backup with progress
      await db.backup('./backup.db', {
      rate: 10,
      progress: ({ totalPages, remainingPages }) => {
      console.log(`Progress: ${totalPages - remainingPages}/${totalPages}`);
      }
      });
    • 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);