drift/moor_ffi
Simon Binder 4f6798c0ab
Test moor_ffi through Flutter in CI
2020-07-29 11:17:57 +02:00
..
android Add empty Android plugin class to moor_ffi 2020-07-03 12:10:37 +02:00
example Enable more recent lints and enforce them 2019-12-04 21:45:09 +01:00
ios
lib Begin migration to sqlite3 package 2020-07-08 19:04:57 +02:00
test Support flags in regexp (#644) 2020-06-22 22:20:43 +02:00
.gitignore Collect coverage for moor_ffi (#400) 2020-02-19 13:00:03 +01:00
CHANGELOG.md Prepare 3.3.0 release 2020-07-28 22:30:04 +02:00
LICENSE
README.md Test moor_ffi through Flutter in CI 2020-07-29 11:17:57 +02:00
analysis_options.yaml ci: Don't analyze moor_flutter, but analyze moor_ffi 2020-01-04 17:13:23 +01:00
pubspec.yaml Prepare 3.3.0 release 2020-07-28 22:30:04 +02:00

README.md

This package has been deprecated! Consider migrating to new sqlite packages depending on your setup:

  • You use moor to build Flutter apps: Remove moor_ffi from your dependencies and add sqlite3_flutter_libs instead. Also, replace the following imports:
    • change package:moor_ffi/moor_ffi.dart to package:moor/ffi.dart
    • change package:moor_ffi/open_helper.dart to package:sqlite3/open.dart
  • You use moor, but without Flutter: Just drop the moor_ffi dependency and use the new package:moor/ffi.dart library. You need to make sure that a dynamic sqlite3 library is available at runtime.
  • You don't use moor, but you do use Flutter: Depend on sqlite3 and sqlite3_flutter_libs. The sqlite3 package contains the new Dart apis to use sqlite3.
  • You don't use the main moor package or Flutter: Just depend on the sqlite3 package and make sure that a dynamic sqlite3 library is available at runtime

moor_ffi

Dart bindings to sqlite by using dart:ffi. This library contains utils to make integration with moor easier, but it can also be used as a standalone package. It also doesn't depend on Flutter, so it can be used on Dart VM applications as well.

Supported platforms

You can make this library work on any platform that lets you obtain a DynamicLibrary in which sqlite's symbols are available (see below).

Out of the box, this library supports all platforms where sqlite3 is installed:

  • iOS: Yes
  • macOS: Yes
  • Linux: Available on most distros
  • Windows: Additional setup is required
  • Android: Yes when used with Flutter, this library includes the necessary native libraries on Android

On other platforms

Using this library on platforms that are not supported out of the box is fairly straightforward. For instance, if you release your own sqlite3.so next to your application, you could use

import 'dart:ffi';
import 'dart:io';
import 'package:moor_ffi/database.dart';
import 'package:moor_ffi/open_helper.dart';

void main() {
  open.overrideFor(OperatingSystem.linux, _openOnLinux);

  final db = Database.memory();
  db.close();
}

DynamicLibrary _openOnLinux() {
  final script = File(Platform.script.toFilePath());
  final libraryNextToScript = File('${script.path}/sqlite3.so');
  return DynamicLibrary.open(libraryNextToScript.path);
}

Just be sure to first override the behavior and then open the database. Further, if you want to use the isolate api, you can only use a static method or a top-level function to open the library. For Windows, a similar setup with a sqlite3.dll library should work.

Supported datatypes

This library supports null, int, double, String and Uint8List to bind args. Returned columns from select statements will have the same types.

Using without moor

import 'package:moor_ffi/database.dart';

void main() {
  final database = Database.memory();
  // run some database operations. See the example for details
  database.close();
}

Be sure to always call Database.close to avoid memory leaks!

Using with moor

If you're migrating an existing project using moor_flutter, see the documentation.

Add both moor and moor_ffi to your pubspec:

dependencies:
  moor: ^2.0.0
  moor_ffi: ^0.2.0
dev_dependencies:
  moor_generator: ^2.0.0

You can then use a VmDatabase as an executor:

@UseMoor(...)
class MyDatabase extends _$MyDatabase {

  MyDatabase(): super(VmDatabase(File('app.db')));
}

If you need to find an appropriate directory for the database file, you can use the LazyDatabase wrapper from moor. It can be used to create the inner VmDatabase asynchronously:

import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

// use this instead of VmDatabase(...)
LazyDatabase(() async {
  final dbFolder = await getApplicationDocumentsDirectory();
  final file = File(p.join(dbFolder.path, 'app.db'));
  return VmDatabase(file);
});