Add docs on `WasmDatabase`

This commit is contained in:
Simon Binder 2022-04-02 20:02:37 +02:00
parent 87848e58dd
commit 9c31a17926
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,21 @@
import 'package:drift/drift.dart';
import 'package:drift/wasm.dart';
import 'package:http/http.dart' as http;
import 'package:sqlite3/wasm.dart';
QueryExecutor connect() {
return LazyDatabase(() async {
// Load wasm bundle
final response = await http.get(Uri.parse('sqlite3.wasm'));
// Create a virtual file system backed by IndexedDb with everything in
// `/drift/my_app/` being persisted.
final fs = await IndexedDbFileSystem.load('/drift/my_app/');
final sqlite3 = await WasmSqlite3.load(
response.bodyBytes,
SqliteEnvironment(fileSystem: fs),
);
// Then, open a database inside that persisted folder.
return WasmDatabase(sqlite3: sqlite3, path: '/drift/my_app/app.db');
});
}

View File

@ -270,3 +270,28 @@ DatabaseConnection connectToWorker(String databaseName) {
```
You can pass that DatabaseConnection to your database by enabling the `generate_connect_constructor` build option.
## New web backend {#drift-wasm}
__Warning__: This new backend is currently in a very experimental state and not suitable for production use.
Starting from version `1.6.0`, drift supports the new `package:drift/wasm.dart` backend for web apps.
It binds to a sqlite3 WebAssembly module directly, without requireing an external JavaScript library.
Unlike sql.js, which uses Emscripten, the new backend uses a custom [sqlite3 VFS](https://www.sqlite.org/vfs.html)
and doesn't require any dependencies apart from sqlite3 itself.
The intention is that this build can be optimized for Dart-typical use-cases. For instance, the sql.js backend
needs to close and re-open the database connection for every save. The new backend can just use a custom
virtual file system that sqlite3 will then use internally.
As this version of sqlite3 was compiled with a custom VFS, you can't re-use the WebAssembly module from sql.js.
Instead, grab a sqlite3.wasm file from the [releases](https://github.com/simolus3/sqlite3.dart/releases) of the
`sqlite3` pub package and put this file in your `web/` folder.
With this setup, sqlite3 can be used on the web without an external library:
{% assign snippets = 'package:moor_documentation/snippets/engines/web_wasm.dart.excerpt.json' | readString | json_decode %}
{% include "blocks/snippet" snippets = snippets %}
This snippet also works in a service worker.
If you're running into any issues with the new backend, please post them [here](https://github.com/simolus3/sqlite3.dart/issues).

View File

@ -25,6 +25,7 @@ This table list all supported drift implementations and on which platforms they
| `SqfliteQueryExecutor` from `package:drift_sqflite` | Android, iOS | Uses platform channels, Flutter only, no isolate support, doesn't support `flutter test`. Formerly known as `moor_flutter` |
| `NativeDatabase` from `package:drift/native.dart` | Android, iOS, Windows, Linux, macOS | No further setup is required for Flutter users. For support outside of Flutter, or in `flutter test`, see the [desktop](#desktop) section below. Usage in a [isolate]({{ 'Advanced Features/isolates.md' | pageUrl }}) is recommended. Formerly known as `package:moor/ffi.dart`. |
| `WebDatabase` from `package:drift/web.dart` | Web | Works with or without Flutter. A bit of [additional setup]({{ 'Other engines/web.md' | pageUrl }}) is required. |
| `WasmDatabase` from `package:drift/web.dart` | Web | Potentially faster than a `WebDatabase`, but still experimental and not yet production ready. See [this]({{ 'Other engines/web.md#drift-wasm' | pageUrl }}) for details. |
To support all platforms in a shared codebase, you only need to change how you open your database, all other usages can stay the same.
[This repository](https://github.com/rodydavis/moor_shared) gives an example on how to do that with conditional imports.
@ -81,7 +82,7 @@ read the main article for details.
## Desktop
Drift also supports all major Desktop operating systems where Dart runs on by using the
Drift also supports all major Desktop operating systems where Dart runs on by using the
`NativeDatabase` from `package:drift/native.dart`. Depending on your operating system, further
setup might be required: