moor_ffi 0.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new74

moor_ffi #

Experimental 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.

Warnings #

At the moment, dart:ffi is in preview and there will be breaking changes that this library has to adapt to. This library has been tested on Dart 2.5.0. If you're using a development Dart version (this can include any Flutter channels that are not stable), this library might not work.

Also, please don't use this library on production apps yet.

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 works with and without Flutter. If you're using Flutter, this library will bundle sqlite3 in your Android app. This requires the Android NDK to be installed (You can get the NDK in the SDK Manager of Android Studio). Note that the first flutter run is going to take a very long time as we need to compile sqlite.

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

You can also use an asynchronous API on a background isolate by using IsolateDb.openFile or IsolateDb.openMemory, respectively. Be aware that the asynchronous API is much slower, but it moves work out of the UI isolate.

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

Using with moor #

Add both moor and moor_ffi to your pubspec, the moor_flutter dependency can be dropped.

dependencies:
  moor: ^1.7.0
  moor_ffi: ^0.0.1
dev_dependencies:
  moor_generator: ^1.7.0

In the file where you created a FlutterQueryExecutor, replace the moor_flutter import with both package:moor/moor.dart and package:moor_ffi/moor_ffi.dart. In all other project files that use moor apis (e.g. a Value class for companions), just import package:moor/moor.dart.

Finally, replace usages of FlutterQueryExecutor with VmDatabase.

Note that, at the moment, there is no direct counterpart for FlutterQueryExecutor.inDatabasePath and that the async API using a background isolate is not available for moor yet.

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 (var 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.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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]
60
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Oct 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/src/impl/isolate/background.dart. (-0.50 points)

Analysis of lib/src/impl/isolate/background.dart reported 1 hint:

line 101 col 11: Close instances of dart.core.Sink.

Maintenance issues and suggestions

Support future stable Dart 2 SDKs in pubspec.yaml. (-20 points)

The SDK constraint in pubspec.yaml doesn't allow future stable Dart 2.x SDK releases.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0-dev <2.6.0
collection ^1.0.0 1.14.12
moor >=1.7.0 <2.1.0 2.0.1
Transitive dependencies
charcode 1.1.2
convert 2.1.1
meta 1.1.7
pedantic 1.8.0+1
synchronized 2.1.0+1
typed_data 1.1.6
Dev dependencies
path ^1.6.0
test ^1.6.0