Function createDatabase

  • Create (or auto-resolve) the most appropriate database adapter for the current runtime environment. Priority selection logic:

    1. Explicit options.priority (caller-specified) wins entirely.
    2. Explicit options.type is mapped to a single adapter priority.
    3. Otherwise we auto-detect environment:
      • Browser: ['indexeddb', 'sqljs']: the database is kept in IndexedDB, with in-memory sql.js where IndexedDB cannot open. file: ':memory:' (createMemoryDatabase()) keeps ['sqljs'], in memory.
      • A Capacitor app on a phone: ['capacitor', 'indexeddb', 'sqljs'], as the resolver chooses.
      • Deno: ['sqljs']
      • Node.js with a postgres:// or postgresql:// URL, or Postgres options that name a server: ['postgres']. There is no fallback to SQLite: data meant for Postgres would land in a local file. A URL of another scheme names no server. DATABASE_URL in the environment does not count here.
      • Node.js otherwise: ['better-sqlite3', 'sqljs'] (native first, wasm fallback)

    With strict on Node, a url or postgres option that names no server throws instead of choosing the local default (unless file is given), and a postgres candidate that fails ends resolution.

    The function never throws solely due to a native adapter absence; it falls back to next candidates. Only when no candidate can open will it bubble an error.

    Parameters

    Returns Promise<StorageAdapter>