mirror of https://github.com/AMT-Cheif/drift.git
Use experimental wasm backend in new example
This commit is contained in:
parent
49b215facd
commit
7c2d95fb30
|
@ -31,6 +31,17 @@ class WasmDatabase extends DelegatedDatabase {
|
|||
WasmDatabase._(DatabaseDelegate delegate, bool logStatements)
|
||||
: super(delegate, isSequential: true, logStatements: logStatements);
|
||||
|
||||
/// Creates a wasm database at [path] in the virtual file system of the
|
||||
/// [sqlite3] module.
|
||||
factory WasmDatabase({
|
||||
required CommmonSqlite3 sqlite3,
|
||||
required String path,
|
||||
WasmDatabaseSetup? setup,
|
||||
bool logStatements = false,
|
||||
}) {
|
||||
return WasmDatabase._(_WasmDelegate(sqlite3, path, setup), logStatements);
|
||||
}
|
||||
|
||||
/// Creates an in-memory database in the loaded [sqlite3] database.
|
||||
factory WasmDatabase.inMemory(
|
||||
CommmonSqlite3 sqlite3, {
|
||||
|
|
|
@ -1,13 +1,38 @@
|
|||
import 'dart:async';
|
||||
// ignore: avoid_web_libraries_in_flutter
|
||||
import 'dart:html';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/remote.dart';
|
||||
import 'package:drift/web.dart';
|
||||
import 'package:drift/wasm.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:sqlite3/wasm.dart';
|
||||
|
||||
const _useWorker = true;
|
||||
|
||||
/// Obtains a database connection for running drift on the web.
|
||||
DatabaseConnection connect({bool isInWebWorker = false}) {
|
||||
return DatabaseConnection.delayed(Future.sync(() async {
|
||||
final storage = await DriftWebStorage.indexedDbIfSupported('app_database',
|
||||
inWebWorker: isInWebWorker);
|
||||
final databaseImpl = WebDatabase.withStorage(storage);
|
||||
if (_useWorker && !isInWebWorker) {
|
||||
final worker = SharedWorker('shared_worker.dart.js');
|
||||
return remote(worker.port!.channel());
|
||||
} else {
|
||||
return DatabaseConnection.delayed(Future.sync(() async {
|
||||
// We're using the experimental wasm support in Drift because this gives us
|
||||
// a recent sqlite3 version with fts5 support.
|
||||
// This is still experimental, so consider using the approach described in
|
||||
// https://drift.simonbinder.eu/web/ instead.
|
||||
|
||||
return DatabaseConnection.fromExecutor(databaseImpl);
|
||||
}));
|
||||
final response = await http.get(Uri.parse('sqlite3.wasm'));
|
||||
final fs = await IndexedDbFileSystem.load('/drift/my_app/');
|
||||
final sqlite3 = await WasmSqlite3.load(
|
||||
response.bodyBytes,
|
||||
SqliteEnvironment(fileSystem: fs),
|
||||
);
|
||||
|
||||
final databaseImpl =
|
||||
WasmDatabase(sqlite3: sqlite3, path: '/drift/my_app/app.db');
|
||||
return DatabaseConnection.fromExecutor(databaseImpl);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,6 +93,18 @@ class AppDatabase extends _$AppDatabase {
|
|||
}).watch();
|
||||
}
|
||||
|
||||
Future<void> deleteCategory(Category category) {
|
||||
return transaction(() async {
|
||||
// First, move todo entries that might remain into the default category
|
||||
await (todoEntries.update()
|
||||
..where((todo) => todo.category.equals(category.id)))
|
||||
.write(const TodoEntriesCompanion(category: Value(null)));
|
||||
|
||||
// Then, delete the category
|
||||
await categories.deleteOne(category);
|
||||
});
|
||||
}
|
||||
|
||||
static Provider<AppDatabase> provider = Provider((ref) {
|
||||
final database = AppDatabase();
|
||||
ref.onDispose(database.close);
|
||||
|
|
|
@ -139,7 +139,7 @@ class _CategoryDrawerEntry extends ConsumerWidget {
|
|||
|
||||
// can be null when the dialog is dismissed
|
||||
if (confirmed == true) {
|
||||
ref.read(AppDatabase.provider).categories.deleteOne(category);
|
||||
ref.read(AppDatabase.provider).deleteCategory(category);
|
||||
}
|
||||
},
|
||||
),
|
||||
|
|
|
@ -15,7 +15,9 @@ dependencies:
|
|||
flutter_riverpod: ^1.0.3
|
||||
go_router: ^3.0.6
|
||||
intl: ^0.17.0
|
||||
http: ^0.13.4 # used to load sqlite3 wasm files on the web
|
||||
sqlite3_flutter_libs: ^0.5.5
|
||||
sqlite3: ^1.6.1
|
||||
path_provider: ^2.0.9
|
||||
path: ^1.8.0
|
||||
riverpod: ^1.0.3
|
||||
|
@ -46,4 +48,4 @@ dependency_overrides:
|
|||
drift_dev:
|
||||
path: ../../drift_dev
|
||||
sqlparser:
|
||||
path: ../../sqlparser
|
||||
path: ../../sqlparser
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
../../../extras/integration_tests/web/test/sql-wasm.js
|
|
@ -1 +0,0 @@
|
|||
../../../extras/integration_tests/web/test/sql-wasm.wasm
|
Binary file not shown.
|
@ -7,8 +7,6 @@ import 'package:drift/remote.dart';
|
|||
|
||||
void main() {
|
||||
final self = SharedWorkerGlobalScope.instance;
|
||||
self.importScripts('sql-wasm.js');
|
||||
|
||||
final server = DriftServer(connect(isInWebWorker: true));
|
||||
|
||||
self.onConnect.listen((event) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:html';
|
||||
|
||||
import 'package:drift/web.dart';
|
||||
import 'package:stream_channel/stream_channel.dart';
|
||||
|
||||
StreamChannel<Object?> startWorker(String script) {
|
||||
|
@ -8,13 +9,3 @@ StreamChannel<Object?> startWorker(String script) {
|
|||
|
||||
return worker.port!.channel();
|
||||
}
|
||||
|
||||
extension PortToChannel on MessagePort {
|
||||
StreamChannel<Object?> channel() {
|
||||
final controller = StreamChannelController();
|
||||
onMessage.map((event) => event.data).pipe(controller.local.sink);
|
||||
controller.local.stream.listen(postMessage, onDone: close);
|
||||
|
||||
return controller.foreign;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue