mirror of https://github.com/AMT-Cheif/drift.git
Document how to use moor/ffi.dart
This commit is contained in:
parent
270a29164f
commit
ebb7448d1b
|
@ -13,14 +13,13 @@ how to get started. You can watch it [here](https://youtu.be/zpWsedYMczM).
|
||||||
|
|
||||||
## Adding the dependency
|
## Adding the dependency
|
||||||
First, lets add moor to your project's `pubspec.yaml`.
|
First, lets add moor to your project's `pubspec.yaml`.
|
||||||
At the moment, the current version of `moor` is [](https://pub.dartlang.org/packages/moor),
|
At the moment, the current version of `moor` is [](https://pub.dartlang.org/packages/moor)
|
||||||
`moor_ffi` is at [](https://pub.dartlang.org/packages/moor_ffi)
|
|
||||||
and the latest version of `moor_generator` is [](https://pub.dartlang.org/packages/moor_generator)
|
and the latest version of `moor_generator` is [](https://pub.dartlang.org/packages/moor_generator)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
dependencies:
|
dependencies:
|
||||||
moor: # use the latest version
|
moor: # use the latest version
|
||||||
moor_ffi: # use the latest version
|
sqlite3_flutter_libs: # Also use the latest version.
|
||||||
path_provider:
|
path_provider:
|
||||||
path:
|
path:
|
||||||
|
|
||||||
|
@ -32,7 +31,7 @@ dev_dependencies:
|
||||||
If you're wondering why so many packages are necessary, here's a quick overview over what each package does:
|
If you're wondering why so many packages are necessary, here's a quick overview over what each package does:
|
||||||
|
|
||||||
- `moor`: This is the core package defining most apis
|
- `moor`: This is the core package defining most apis
|
||||||
- `moor_ffi`: Contains code that will run the actual queries
|
- `sqlite3_flutter_libs`: Ships the latest `sqlite3` version with your Android or iOS app.
|
||||||
- `path_provider` and `path`: Used to find a suitable location to store the database. Maintained by the Flutter and Dart team
|
- `path_provider` and `path`: Used to find a suitable location to store the database. Maintained by the Flutter and Dart team
|
||||||
- `moor_generator`: Generates query code based on your tables
|
- `moor_generator`: Generates query code based on your tables
|
||||||
- `build_runner`: Common tool for code-generation, maintained by the Dart team
|
- `build_runner`: Common tool for code-generation, maintained by the Dart team
|
||||||
|
@ -89,7 +88,7 @@ database and data classes for your entities. To use it, change the `MyDatabase`
|
||||||
follows:
|
follows:
|
||||||
```dart
|
```dart
|
||||||
// These imports are only needed to open the database
|
// These imports are only needed to open the database
|
||||||
import 'package:moor_ffi/moor_ffi.dart';
|
import 'package:moor/ffi.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:moor/moor.dart';
|
import 'package:moor/moor.dart';
|
||||||
|
|
|
@ -9,14 +9,13 @@ declaring both tables and queries in Dart. This version will focus on how to use
|
||||||
|
|
||||||
## Adding the dependency
|
## Adding the dependency
|
||||||
First, lets add moor to your project's `pubspec.yaml`.
|
First, lets add moor to your project's `pubspec.yaml`.
|
||||||
At the moment, the current version of `moor` is [](https://pub.dartlang.org/packages/moor),
|
At the moment, the current version of `moor` is [](https://pub.dartlang.org/packages/moor)
|
||||||
`moor_ffi` is at [](https://pub.dartlang.org/packages/moor_ffi)
|
|
||||||
and the latest version of `moor_generator` is [](https://pub.dartlang.org/packages/moor_generator)
|
and the latest version of `moor_generator` is [](https://pub.dartlang.org/packages/moor_generator)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
dependencies:
|
dependencies:
|
||||||
moor: # use the latest version
|
moor: # use the latest version
|
||||||
moor_ffi: # use the latest version
|
sqlite3_flutter_libs: # Also use the latest version.
|
||||||
path_provider:
|
path_provider:
|
||||||
path:
|
path:
|
||||||
|
|
||||||
|
@ -28,7 +27,7 @@ dev_dependencies:
|
||||||
If you're wondering why so many packages are necessary, here's a quick overview over what each package does:
|
If you're wondering why so many packages are necessary, here's a quick overview over what each package does:
|
||||||
|
|
||||||
- `moor`: This is the core package defining most apis
|
- `moor`: This is the core package defining most apis
|
||||||
- `moor_ffi`: Contains code that will run the actual queries
|
- `sqlite3_flutter_libs`: Ships the latest `sqlite3` version with your Android or iOS app.
|
||||||
- `path_provider` and `path`: Used to find a suitable location to store the database. Maintained by the Flutter and Dart team
|
- `path_provider` and `path`: Used to find a suitable location to store the database. Maintained by the Flutter and Dart team
|
||||||
- `moor_generator`: Generates Dart apis for the sql queries and tables you wrote
|
- `moor_generator`: Generates Dart apis for the sql queries and tables you wrote
|
||||||
- `build_runner`: Common tool for code-generation, maintained by the Dart team
|
- `build_runner`: Common tool for code-generation, maintained by the Dart team
|
||||||
|
@ -95,7 +94,7 @@ import 'dart:io';
|
||||||
|
|
||||||
import 'package:moor/moor.dart';
|
import 'package:moor/moor.dart';
|
||||||
// These imports are only needed to open the database
|
// These imports are only needed to open the database
|
||||||
import 'package:moor_ffi/moor_ffi.dart';
|
import 'package:moor/ffi.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
---
|
---
|
||||||
title: moor_ffi (Desktop support)
|
title: moor ffi (Desktop support)
|
||||||
description: Run moor on both mobile and desktop
|
description: Run moor on both mobile and desktop
|
||||||
---
|
---
|
||||||
|
|
||||||
## Supported versions
|
## Supported versions
|
||||||
|
|
||||||
At the moment, `moor_ffi` supports iOS, macOS and Android out of the box. Most Linux
|
The `moor/ffi.dart` library uses the `sqlite3` package to send queries.
|
||||||
|
At the moment, that package supports iOS, macOS and Android out of the box. Most Linux
|
||||||
Distros have sqlite available as a shared library, those are supported as well.
|
Distros have sqlite available as a shared library, those are supported as well.
|
||||||
|
|
||||||
If you're shipping apps for Windows and Linux, it is recommended that you bundle a
|
If you're shipping apps for Windows and Linux, it is recommended that you bundle a
|
||||||
|
@ -15,14 +16,14 @@ support your setup by running this code before opening the database:
|
||||||
```dart
|
```dart
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:moor_ffi/database.dart';
|
import 'package:sqlite3/sqlite3.dart';
|
||||||
import 'package:moor_ffi/open_helper.dart';
|
import 'package:sqlite3/open.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
open.overrideFor(OperatingSystem.linux, _openOnLinux);
|
open.overrideFor(OperatingSystem.linux, _openOnLinux);
|
||||||
|
|
||||||
final db = Database.memory();
|
final db = sqlite3.openInMemory();
|
||||||
db.close();
|
db.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
DynamicLibrary _openOnLinux() {
|
DynamicLibrary _openOnLinux() {
|
||||||
|
@ -34,24 +35,24 @@ DynamicLibrary _openOnLinux() {
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Migrating from moor_flutter to moor_ffi
|
## Migrating from moor_flutter to moor ffi
|
||||||
|
|
||||||
First, adapt your `pubspec.yaml`: You can remove the `moor_flutter` dependency and instead
|
First, adapt your `pubspec.yaml`: You can remove the `moor_flutter` dependency and instead
|
||||||
add both the `moor` and `moor_ffi` dependencies:
|
add both the `moor` and `sqlite3_flutter_libs` dependencies:
|
||||||
```yaml
|
```yaml
|
||||||
dependencies:
|
dependencies:
|
||||||
moor: ^2.0.0
|
moor: ^3.0.0
|
||||||
moor_ffi: ^0.2.0
|
sqlite3_flutter_libs:
|
||||||
sqflite: ^1.1.7 # Still used to obtain the database location
|
sqflite: ^1.1.7 # Still used to obtain the database location
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
moor_generator: ^2.0.0
|
moor_generator: ^3.0.0
|
||||||
```
|
```
|
||||||
|
|
||||||
Adapt your imports:
|
Adapt your imports:
|
||||||
|
|
||||||
- In the file where you created a `FlutterQueryExecutor`, replace the `moor_flutter` import
|
- In the file where you created a `FlutterQueryExecutor`, replace the `moor_flutter` import
|
||||||
with `package:moor_ffi/moor_ffi.dart`.
|
with `package:moor/ffi.dart`.
|
||||||
- In all other files where you might have import `moor_flutter`, just import `package:moor/moor.dart`.
|
- In all other files where you might have imported `moor_flutter`, just import `package:moor/moor.dart`.
|
||||||
|
|
||||||
Replace the executor. This code:
|
Replace the executor. This code:
|
||||||
```dart
|
```dart
|
||||||
|
@ -75,7 +76,7 @@ Please be aware that `FlutterQueryExecutor.inDatabaseFolder` might yield a diffe
|
||||||
`path_provider` on Android. This can cause data loss if you've already shipped a version using
|
`path_provider` on Android. This can cause data loss if you've already shipped a version using
|
||||||
`moor_flutter`. In that case, using `getDatabasePath` from sqflite is the suggested solution.
|
`moor_flutter`. In that case, using `getDatabasePath` from sqflite is the suggested solution.
|
||||||
|
|
||||||
## Using moor_ffi with an existing database
|
## Using moor ffi with an existing database
|
||||||
|
|
||||||
If your existing sqlite database is stored as a file, you can just use `VmDatabase(thatFile)` - no further
|
If your existing sqlite database is stored as a file, you can just use `VmDatabase(thatFile)` - no further
|
||||||
changes are required.
|
changes are required.
|
||||||
|
|
|
@ -1,3 +1,15 @@
|
||||||
|
## 0.8.0
|
||||||
|
|
||||||
|
This package is now deprecated.
|
||||||
|
|
||||||
|
Moor users should use the new `package:moor/ffi.dart` library.
|
||||||
|
To migrate,
|
||||||
|
- replace imports of `moor_ffi` with `package:moor/ffi.dart`
|
||||||
|
- when using Flutter, add a dependency on `sqlite3_flutter_libs`
|
||||||
|
|
||||||
|
Users of this package that don't use moor should use the new [sqlite3](https://pub.dev/packages/sqlite3)
|
||||||
|
package.
|
||||||
|
|
||||||
## 0.7.0
|
## 0.7.0
|
||||||
|
|
||||||
- Throw an error when using an unsupported datatype as argument
|
- Throw an error when using an unsupported datatype as argument
|
||||||
|
|
Loading…
Reference in New Issue