diff --git a/drift/lib/native.dart b/drift/lib/native.dart index 27b8a512..21ae6fc3 100644 --- a/drift/lib/native.dart +++ b/drift/lib/native.dart @@ -132,12 +132,12 @@ class NativeDatabase extends DelegatedDatabase { class _NativeDelegate extends Sqlite3Delegate { final File? file; - _NativeDelegate(this.file, DatabaseSetup? setup) : super(setup); + _NativeDelegate(this.file, DatabaseSetup? setup) : super(setup, false); _NativeDelegate.opened( Database db, DatabaseSetup? setup, bool closeUnderlyingWhenClosed) : file = null, - super.opened(db, setup, closeUnderlyingWhenClosed); + super.opened(db, setup, closeUnderlyingWhenClosed, false); @override Database openDatabase() { diff --git a/drift/lib/src/sqlite3/database.dart b/drift/lib/src/sqlite3/database.dart index 522b7133..b3867744 100644 --- a/drift/lib/src/sqlite3/database.dart +++ b/drift/lib/src/sqlite3/database.dart @@ -17,15 +17,18 @@ abstract class Sqlite3Delegate bool _hasCreatedDatabase = false; bool _isOpen = false; + final bool _syncPersistence; final void Function(DB)? _setup; final bool _closeUnderlyingWhenClosed; /// A delegate that will call [openDatabase] to open the database. - Sqlite3Delegate(this._setup) : _closeUnderlyingWhenClosed = true; + Sqlite3Delegate(this._setup, this._syncPersistence) + : _closeUnderlyingWhenClosed = true; - /// A delegate using an underlying sqlite3 database object that has alreaddy + /// A delegate using an underlying sqlite3 database object that has already /// been opened. - Sqlite3Delegate.opened(this._db, this._setup, this._closeUnderlyingWhenClosed) + Sqlite3Delegate.opened(this._db, this._setup, this._closeUnderlyingWhenClosed, + this._syncPersistence) : _hasCreatedDatabase = true { _initializeDatabase(); } @@ -88,6 +91,10 @@ abstract class Sqlite3Delegate stmt.dispose(); } + if (_syncPersistence) { + await _db.flush(); + } + return Future.value(); } @@ -99,6 +106,10 @@ abstract class Sqlite3Delegate stmt.execute(args); stmt.dispose(); } + + if (_syncPersistence) { + await _db.flush(); + } } @override @@ -132,6 +143,9 @@ abstract class Sqlite3Delegate if (_closeUnderlyingWhenClosed) { beforeClose(_db); _db.dispose(); + if (_syncPersistence) { + await _db.flush(); + } } } } diff --git a/drift/lib/wasm.dart b/drift/lib/wasm.dart index a2fd0598..268857bd 100644 --- a/drift/lib/wasm.dart +++ b/drift/lib/wasm.dart @@ -38,13 +38,21 @@ class WasmDatabase extends DelegatedDatabase { /// Creates a wasm database at [path] in the virtual file system of the /// [sqlite3] module. + /// If [synchronizedPersistence] is enabled, the data is guaranteed to be + /// stored in the IndexedDB when the request is complete. Attention! + /// Insert/update queries may be slower when this option enabled. If you want + /// to insert more than one rows, be sure you run in a transaction if + /// possible. factory WasmDatabase({ required CommmonSqlite3 sqlite3, required String path, WasmDatabaseSetup? setup, + bool synchronizedPersistence = false, bool logStatements = false, }) { - return WasmDatabase._(_WasmDelegate(sqlite3, path, setup), logStatements); + return WasmDatabase._( + _WasmDelegate(sqlite3, path, setup, synchronizedPersistence), + logStatements); } /// Creates an in-memory database in the loaded [sqlite3] database. @@ -53,7 +61,8 @@ class WasmDatabase extends DelegatedDatabase { WasmDatabaseSetup? setup, bool logStatements = false, }) { - return WasmDatabase._(_WasmDelegate(sqlite3, null, setup), logStatements); + return WasmDatabase._( + _WasmDelegate(sqlite3, null, setup, false), logStatements); } } @@ -61,8 +70,9 @@ class _WasmDelegate extends Sqlite3Delegate { final CommmonSqlite3 _sqlite3; final String? _path; - _WasmDelegate(this._sqlite3, this._path, WasmDatabaseSetup? setup) - : super(setup); + _WasmDelegate( + this._sqlite3, this._path, WasmDatabaseSetup? setup, bool syncPersistence) + : super(setup, syncPersistence); @override CommonDatabase openDatabase() { diff --git a/drift/pubspec.yaml b/drift/pubspec.yaml index 9949ef68..40e259e9 100644 --- a/drift/pubspec.yaml +++ b/drift/pubspec.yaml @@ -36,3 +36,8 @@ dependency_overrides: path: ../drift_dev sqlparser: path: ../sqlparser + sqlite3: + git: + url: https://github.com/westito/sqlite3.dart.git + ref: indexeddb-improvements + path: sqlite3