@photostructure/sqlite
    Preparing search index...

    Function enhance

    • Ensures that .pragma(), .transaction(), and statement modes (.pluck(), .raw(), .expand()) are available on the given database.

      This function can enhance:

      • node:sqlite DatabaseSync instances (adds the methods)
      • @photostructure/sqlite DatabaseSync instances (adds the methods)
      • Any object with compatible exec(), prepare(), and isTransaction

      The enhancement is done by adding methods directly to the instance, not the prototype, so it won't affect other instances or the original class.

      Type Parameters

      Parameters

      • db: T

        The database instance to enhance

      Returns EnhancedDatabaseSync<T>

      The same instance with .pragma(), .transaction(), and .pluck() / .raw() / .expand() (on prepared statements) guaranteed

      import { DatabaseSync, enhance } from '@photostructure/sqlite';

      const db = enhance(new DatabaseSync(':memory:'));

      // better-sqlite3-style pragma
      db.pragma('journal_mode = wal');

      // better-sqlite3-style transactions
      const insertMany = db.transaction((items) => {
      for (const item of items) insert.run(item);
      });

      // better-sqlite3-style pluck
      const count = db.prepare("SELECT COUNT(*) FROM users").pluck().get();
      const names = db.prepare("SELECT name FROM users").pluck().all();