openConnection method

Future<WebDatabase> openConnection(
  1. SqliteOpenOptions options
)

Currently this only uses the SQLite Web WASM implementation. This provides built in async Web worker functionality and automatic persistence storage selection. Due to being asynchronous, the under laying CommonDatabase is not accessible

Implementation

Future<WebDatabase> openConnection(SqliteOpenOptions options) async {
  final workers = await _initialized;
  final connection = await connectToWorker(workers, path);

  final pragmaStatements = this.pragmaStatements(options);
  if (pragmaStatements.isNotEmpty) {
    // The default implementation doesn't use pragmas on the web, but a
    // subclass might.
    await connection.database.requestLock((token) async {
      for (final stmt in pragmaStatements) {
        await connection.database.execute(stmt, token: token);
      }
    });
  }

  // When the database is hosted in a shared worker, we don't need a local
  // mutex since that worker will hand out leases for us.
  // Additionally, package:sqlite3_web uses navigator locks internally for
  // OPFS databases.
  // Technically, the only other implementation (IndexedDB in a local context
  // or a dedicated worker) is inherently unsafe to use across tabs. But
  // wrapping those in a mutex and flushing the file system helps a little bit
  // (still something we're trying to avoid).
  final hasSqliteWebMutex =
      connection.access == AccessMode.throughSharedWorker ||
          connection.storage == StorageMode.opfs;

  final mutex = hasSqliteWebMutex
      ? null
      : WebMutexImpl(
          identifier: path); // Use the DB path as a mutex identifier

  BroadcastUpdates? broadcastUpdates;
  if (connection.access != AccessMode.throughSharedWorker &&
      connection.storage != StorageMode.inMemory) {
    broadcastUpdates = BroadcastUpdates(path);
  }

  return WebDatabase(
    connection.database,
    mutex,
    broadcastUpdates: broadcastUpdates,
    profileQueries: sqliteOptions.profileQueries,
    updates: updatesFor(connection.database),
  );
}