Add synchronised persistence to WasmDatabase

This commit is contained in:
westito 2022-05-21 17:58:54 +02:00
parent a1f5ede720
commit 0af392d63f
4 changed files with 38 additions and 9 deletions

View File

@ -132,12 +132,12 @@ class NativeDatabase extends DelegatedDatabase {
class _NativeDelegate extends Sqlite3Delegate<Database> { class _NativeDelegate extends Sqlite3Delegate<Database> {
final File? file; final File? file;
_NativeDelegate(this.file, DatabaseSetup? setup) : super(setup); _NativeDelegate(this.file, DatabaseSetup? setup) : super(setup, false);
_NativeDelegate.opened( _NativeDelegate.opened(
Database db, DatabaseSetup? setup, bool closeUnderlyingWhenClosed) Database db, DatabaseSetup? setup, bool closeUnderlyingWhenClosed)
: file = null, : file = null,
super.opened(db, setup, closeUnderlyingWhenClosed); super.opened(db, setup, closeUnderlyingWhenClosed, false);
@override @override
Database openDatabase() { Database openDatabase() {

View File

@ -17,15 +17,18 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
bool _hasCreatedDatabase = false; bool _hasCreatedDatabase = false;
bool _isOpen = false; bool _isOpen = false;
final bool _syncPersistence;
final void Function(DB)? _setup; final void Function(DB)? _setup;
final bool _closeUnderlyingWhenClosed; final bool _closeUnderlyingWhenClosed;
/// A delegate that will call [openDatabase] to open the database. /// 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. /// been opened.
Sqlite3Delegate.opened(this._db, this._setup, this._closeUnderlyingWhenClosed) Sqlite3Delegate.opened(this._db, this._setup, this._closeUnderlyingWhenClosed,
this._syncPersistence)
: _hasCreatedDatabase = true { : _hasCreatedDatabase = true {
_initializeDatabase(); _initializeDatabase();
} }
@ -88,6 +91,10 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
stmt.dispose(); stmt.dispose();
} }
if (_syncPersistence) {
await _db.flush();
}
return Future.value(); return Future.value();
} }
@ -99,6 +106,10 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
stmt.execute(args); stmt.execute(args);
stmt.dispose(); stmt.dispose();
} }
if (_syncPersistence) {
await _db.flush();
}
} }
@override @override
@ -132,6 +143,9 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
if (_closeUnderlyingWhenClosed) { if (_closeUnderlyingWhenClosed) {
beforeClose(_db); beforeClose(_db);
_db.dispose(); _db.dispose();
if (_syncPersistence) {
await _db.flush();
}
} }
} }
} }

View File

@ -38,13 +38,21 @@ class WasmDatabase extends DelegatedDatabase {
/// Creates a wasm database at [path] in the virtual file system of the /// Creates a wasm database at [path] in the virtual file system of the
/// [sqlite3] module. /// [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({ factory WasmDatabase({
required CommmonSqlite3 sqlite3, required CommmonSqlite3 sqlite3,
required String path, required String path,
WasmDatabaseSetup? setup, WasmDatabaseSetup? setup,
bool synchronizedPersistence = false,
bool logStatements = 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. /// Creates an in-memory database in the loaded [sqlite3] database.
@ -53,7 +61,8 @@ class WasmDatabase extends DelegatedDatabase {
WasmDatabaseSetup? setup, WasmDatabaseSetup? setup,
bool logStatements = false, 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 CommmonSqlite3 _sqlite3;
final String? _path; final String? _path;
_WasmDelegate(this._sqlite3, this._path, WasmDatabaseSetup? setup) _WasmDelegate(
: super(setup); this._sqlite3, this._path, WasmDatabaseSetup? setup, bool syncPersistence)
: super(setup, syncPersistence);
@override @override
CommonDatabase openDatabase() { CommonDatabase openDatabase() {

View File

@ -36,3 +36,8 @@ dependency_overrides:
path: ../drift_dev path: ../drift_dev
sqlparser: sqlparser:
path: ../sqlparser path: ../sqlparser
sqlite3:
git:
url: https://github.com/westito/sqlite3.dart.git
ref: indexeddb-improvements
path: sqlite3