drift icon indicating copy to clipboard operation
drift copied to clipboard

[Web] Drift not persisting db changes

Open zkrige opened this issue 6 months ago • 2 comments

Future<QueryExecutor> openConnection() async {
  final executor = driftDatabase(
    name: 'database',
    native: DriftNativeOptions(
      databaseDirectory: () async => await getApplicationSupportDirectory(),
    ),
    web: DriftWebOptions(
      sqlite3Wasm: Uri.parse('sqlite3.wasm'),
      driftWorker: Uri.parse('drift_worker.js'),
      onResult: (result) {
        if (result.missingFeatures.isNotEmpty) {
          debugPrint('Drift fallback: ${result.missingFeatures}');
        }
      },
    ),
  );

  return executor.interceptWith(LogInterceptor());
}

class AppDatabase extends _$AppDatabase {
  static AppDatabase? _instance;

  AppDatabase._internal(super.e);

  /// Initializes the singleton instance. Must be called once before using [getInstance].
  static void init({required QueryExecutor executor}) {
    if (_instance != null) return;

    _instance = AppDatabase._internal(executor);
  }

final connection = await openConnection();
AppDatabase.init(executor: connection);

  await _database.delete(_database.users).go();
  _database.select(_database.users).getSingleOrNull();

this shows the db is empty.

But, when I close my browser and run my app again,

  _database.select(_database.users).getSingleOrNull();

returns a record

How do I prevent this from happening?

zkrige avatar May 21 '25 12:05 zkrige

What does the Drift fallback line print? Could you also print the result.chosenImplementation?

How are you running the app, with flutter run or is this a built application?

simolus3 avatar May 21 '25 20:05 simolus3

Using WasmStorageImplementation.sharedIndexedDb due to missing browser features: {MissingBrowserFeature.dedicatedWorkersInSharedWorkers, MissingBrowserFeature.sharedArrayBuffers}

flutter run -d chrome --web-port 5555

zkrige avatar May 22 '25 07:05 zkrige