moor_ffi 0.4.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

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);
});

0.4.0 #

  • Use precompiled libraries for faster build times

0.3.2 #

  • Fix a bug where empty blobs would read as null instead of an empty list

0.3.1 #

  • Implement overrideForAll and overrideFor - thanks, @negator

0.3.0 #

  • Better setup for compiling sqlite3 on Android
    • Compilation options to increase runtime performance, enable fts5 and json1
    • We no longer download sqlite sources on the first run, they now ship with the plugin

0.2.0 #

  • Remove the background flag from the moor apis provided by this package. Use the moor isolate api instead.
  • Remove builtin support for background execution from the low-level Database api
  • Support Dart 2.6, drop support for older versions

0.0.1 #

  • Initial release. Contains standalone bindings and a moor implementation.

example/main.dart

import 'package:moor_ffi/database.dart';

const _createTable = r''' 
CREATE TABLE frameworks (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name VARCHAR NOT NULL
);
''';

void main() {
  final db = Database.memory();
  db.execute(_createTable);

  final insertStmt = db.prepare('INSERT INTO frameworks(name) VALUES (?)');
  insertStmt.execute(['Flutter']);
  insertStmt.execute(['AngularDart']);
  insertStmt.close();

  final selectStmt = db.prepare('SELECT * FROM frameworks ORDER BY name');
  final result = selectStmt.select();
  for (final row in result) {
    print('${row['id']}: ${row['name']}');
  }

  selectStmt.close();
  db.close();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  moor_ffi: ^0.4.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:moor_ffi/moor_ffi.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
94
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Mar 26, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
collection ^1.0.0 1.14.12
ffi ^0.1.3 0.1.3
meta ^1.0.2 1.1.8
moor >=1.7.0 <3.0.0 2.4.2
Transitive dependencies
charcode 1.1.3
convert 2.1.1
pedantic 1.9.0
synchronized 2.2.0
typed_data 1.1.6
Dev dependencies
path ^1.6.0
test ^1.6.0