From b58e97f99c54e72ad82d43c4bed48a551865243a Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 30 Jan 2024 22:48:04 +0100 Subject: [PATCH] Document how to existing databases on web --- docs/lib/snippets/platforms/web.dart | 20 ++++++++++++++++++++ docs/lib/src/external_apis.dart | 10 ++++++++++ docs/pages/docs/Platforms/web.md | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 docs/lib/src/external_apis.dart diff --git a/docs/lib/snippets/platforms/web.dart b/docs/lib/snippets/platforms/web.dart index 499e0ff5..1a4f0eaf 100644 --- a/docs/lib/snippets/platforms/web.dart +++ b/docs/lib/snippets/platforms/web.dart @@ -1,6 +1,7 @@ import 'package:drift/drift.dart'; import 'package:drift/wasm.dart'; import 'package:drift/web.dart'; +import 'package:drift_docs/src/external_apis.dart'; import 'package:sqlite3/wasm.dart'; typedef _$MyWebDatabase = GeneratedDatabase; @@ -44,6 +45,25 @@ class MyWebDatabase extends _$MyWebDatabase { } // #enddocregion connect +DatabaseConnection connectWithExisting() { + // #docregion existing + return DatabaseConnection.delayed(Future(() async { + final result = await WasmDatabase.open( + databaseName: 'my_app_db', // prefer to only use valid identifiers here + sqlite3Uri: Uri.parse('sqlite3.wasm'), + driftWorkerUri: Uri.parse('drift_worker.dart.js'), + initializeDatabase: () async { + final data = await rootBundle.load('my_database'); + return data.buffer.asUint8List(); + }, + ); + + // ... + return result.resolvedExecutor; + })); + // #enddocregion existing +} + // #docregion migrate-wasm // If you've previously opened your database like this Future customDatabase() async { diff --git a/docs/lib/src/external_apis.dart b/docs/lib/src/external_apis.dart new file mode 100644 index 00000000..156ea141 --- /dev/null +++ b/docs/lib/src/external_apis.dart @@ -0,0 +1,10 @@ +// Stubs for external APIs we want to reference in Snippets without being able +// to depend on them. + +import 'dart:typed_data'; + +abstract class AssetBundle { + Future load(String key); +} + +AssetBundle get rootBundle => throw 'stub'; diff --git a/docs/pages/docs/Platforms/web.md b/docs/pages/docs/Platforms/web.md index bbcf59e7..e5617563 100644 --- a/docs/pages/docs/Platforms/web.md +++ b/docs/pages/docs/Platforms/web.md @@ -217,6 +217,18 @@ export 'unsupported.dart' A ready example of this construct can also be found [here](https://github.com/simolus3/drift/blob/develop/examples/app/lib/database/connection/connection.dart). +## Using existing databases + +You can use existing database with Drift on the web by importing the `initializeDatabase` +function on `WasmDatabase.open`. +This function will be called when attempting to open a database that doesn't exist yet, +allowing the initial sqlite3 file to be provided instead: + +{% include "blocks/snippet" snippets = snippets name = "existing" %} + +Please be aware that this only works for the default journal mode, WAL is not supported +on the web and WAL databases can't be imported with `initializeDatabase` either. + ## Examples The drift repository contains a two small web applications using drift on the web: