drift_flutter

v0.2.8

Easily set up drift databases across platforms in Flutter apps.

Архив пакета: https://pubdev.letsnova.ru/api/archives/drift_flutter/0.2.8.tar.gz

Установкаdart pub add drift_flutter

README

drift_flutter

drift_flutter is a utility package for drift databases in Flutter apps. The core drift package is Dart-only, meaning that it can't depend on Flutter or any Flutter-only packages. This complicates the setup, as all Flutter apps using drift have to introduce Flutter-specific code to setup drift databases. This package solves that problem by providing drift extensions for Flutter, making it easy to open databases.

Features

This package provides a single method: driftDatabase, which returns a drift database implementation suitable for the current platform. It has a single required parameter, the name of the database to use.

To use this package, use the method to open your database class:

import 'package:drift/drift.dart';
                import 'package:drift_flutter/drift_flutter.dart';
                
                @DriftDatabase(...)
                final class MyAppDatabase extends _$MyAppDatabase {
                  // Keeping a custom constructor is useful for unit tests which may want to
                  // open an in-memory database only.
                  MyAppDatabase(super.e);
                
                  MyAppDatabase.defaults(): super(driftDatabase(name: 'app_db'));
                }
                

Web support

Running sqlite3 on the web requires additional sources which currently have to be downloaded into the web/ folder of your Flutter application. These are the compiled sqlite3.wasm module and a drift_worker.js worker used to share databases across tabs if possible. Obtaining these files is described here.

Sharing databases between isolates

In some setups, for instance when using WorkManager, you may have two independent isolates running your app and a background service. These may have to share a database without blocking each other. This package can internally use the IsolateNameServer to spawn a dedicated database isolates shared between other application isolates. To enable this feature, set shareAcrossIsolates to true in DriftNativeOptions:

  MyAppDatabase.defaults(): super(
                    driftDatabase(
                      name: 'app_db',
                      native: DriftNativeOptions(
                        shareAcrossIsolates: true,
                      ),
                    )
                  );
                

When the option is enabled, drift_flutter will create a dedicated database isolate that starts when the first driftDatabase() call connects and stops when all attached databases have been closed (note that you need to explicitly close() the databases to stop the server, the respective isolates shutting down is not enough).

Behavior

On native platforms (Android, iOS, macOS, Linux and Windows), driftDatabase uses getApplicationDocumentsDirectory() from package:path_provider as the folder to store databases. Inside that folder, a $name.sqlite file is used for the database. To use a custom path, the DriftNativeOptions.databasePath parameter can be used.

On the web, drift's web support is used to open the database.

История изменений

0.2.8

  • Add isolateSetup parameter to DriftNativeOptions to setup isolates spawned to host database connections.

0.2.7

  • Add isolateDebugLog parameter to DriftNativeOptions to debug internal messages between isolates.

0.2.6

  • Add initializeDatabase parameter to DriftWebOptions to enable loading initial database.

0.2.5

  • Fix DriftWebOptions.onResult not being called.
  • Add setup parameter to DriftNativeOptions to customize native database connections.

0.2.4

  • Allow providing a custom temporary directory.
  • Allow providing a custom database directory, making it easier to swap out the default getApplicationDocumentsDirectory().

0.2.3

  • Fix compiling to WebAssembly with Dart 3.6.0.

0.2.2

  • Fix infinite loop in isolate server lookups when using shareAcrossIsolates across hot restarts.

0.2.1

  • Enable serialization between background isolates where necessary.

0.2.0

  • Add DriftNativeOptions with shareAcrossIsolates option that will give multiple isolates access to the same drift database without having to manually set up ports.

0.1.0

  • Initial version.