Update docs on code sharing between native/web

This commit is contained in:
Simon Binder 2021-09-28 22:31:33 +02:00
parent 90e179643c
commit 8c933ba2f2
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 42 additions and 5 deletions

View File

@ -46,8 +46,12 @@ Also, make sure that your webserver serves the `.wasm` file as `application/wasm
won't accept it otherwise. won't accept it otherwise.
## Sharing code between native apps and web ## Sharing code between native apps and web
If you want to share your database code between native applications and webapps, just import the If you want to share your database code between native applications and webapps, just import the
basic `moor` library and make the `QueryExecutor` configurable: basic `moor` library into your databas file.
And instead of passing a `VmDatabase` or `WebDatabase` to the `super` constructor, make the
`QueryExecutor` customizable:
```dart ```dart
// don't import moor_web.dart or moor_flutter/moor_flutter.dart in shared code // don't import moor_web.dart or moor_flutter/moor_flutter.dart in shared code
import 'package:moor/moor.dart'; import 'package:moor/moor.dart';
@ -57,21 +61,54 @@ class SharedDatabase extends _$MyDatabase {
SharedDatabase(QueryExecutor e): super(e); SharedDatabase(QueryExecutor e): super(e);
} }
``` ```
With native Flutter, you can create an instance of your database with
In native Flutter apps, you can create an instance of your database with
```dart ```dart
import 'package:moor_flutter/moor_flutter.dart'; // native.dart
import 'package:moor/ffi.dart';
SharedDatabase constructDb() { SharedDatabase constructDb() {
return SharedDatabase(FlutterQueryExecutor.inDatabaseFolder(path: 'db.sqlite')); final db = LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
return VmDatabase(file);
});
return SharedDatabase(db);
} }
``` ```
On the web, you can use On the web, you can use
```dart ```dart
// web.dart
import 'package:moor/moor_web.dart'; import 'package:moor/moor_web.dart';
SharedDatabase constructDb() { SharedDatabase constructDb() {
return SharedDatabase(WebDatabase('db')); return SharedDatabase(WebDatabase('db'));
} }
``` ```
Finally, we can use [conditional imports](https://dart.dev/guides/libraries/create-library-packages#conditionally-importing-and-exporting-library-files)
to automatically pick the right `constructDb` function based on where your app runs.
To do this, first create a `unsupported.dart` default implementation:
```dart
// unsupported.dart
SharedDatabase constructDb() => throw UnimplementedError();
```
Now, we can use conditional imports in a `shared.dart` file to export the correct function:
```dart
// shared.dart
export 'unsupported.dart'
if (dart.library.ffi) 'native.dart'
if (dart.library.html) 'web.dart';
```
A ready example of this construct can also be found [here](https://github.com/rodydavis/moor_shared/blob/master/lib/src/database/database/unsupported.dart).
## Debugging ## Debugging
You can see all queries sent from moor to the underlying database engine by enabling the `logStatements` You can see all queries sent from moor to the underlying database engine by enabling the `logStatements`
parameter on the `WebDatabase` - they will appear in the console. parameter on the `WebDatabase` - they will appear in the console.