mirror of https://github.com/AMT-Cheif/drift.git
Add synchronised persistence to WasmDatabase
This commit is contained in:
parent
a1f5ede720
commit
0af392d63f
|
@ -132,12 +132,12 @@ class NativeDatabase extends DelegatedDatabase {
|
|||
class _NativeDelegate extends Sqlite3Delegate<Database> {
|
||||
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() {
|
||||
|
|
|
@ -17,15 +17,18 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
|
|||
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<DB extends CommonDatabase>
|
|||
stmt.dispose();
|
||||
}
|
||||
|
||||
if (_syncPersistence) {
|
||||
await _db.flush();
|
||||
}
|
||||
|
||||
return Future.value();
|
||||
}
|
||||
|
||||
|
@ -99,6 +106,10 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
|
|||
stmt.execute(args);
|
||||
stmt.dispose();
|
||||
}
|
||||
|
||||
if (_syncPersistence) {
|
||||
await _db.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -132,6 +143,9 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
|
|||
if (_closeUnderlyingWhenClosed) {
|
||||
beforeClose(_db);
|
||||
_db.dispose();
|
||||
if (_syncPersistence) {
|
||||
await _db.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<CommonDatabase> {
|
|||
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() {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue