sembast_sqflite

sembast DB for flutter on top of sqflite.

sembast_sqflite provides a sembast DatabaseFactory that stores the database in a SQLite file. The API to read and write data is the sembast API, unchanged.

See sembast for API usage, starting with Getting started.

Setup

flutter pub add sembast sembast_sqflite sqflite
# Desktop, Dart VM and tests
dart pub add sembast sembast_sqflite sqflite_common_ffi

Setup instructions for all platforms (Flutter/VM, iOS/Android/MacOS, Windows/Linux)

Quick usage

Example for Flutter (iOS/Android/MacOS):

import 'package:sembast/sembast.dart';
import 'package:sembast_sqflite/sembast_sqflite.dart';
import 'package:sqflite/sqflite.dart' as sqflite;

/// Sembast sqflite based database factory, created once.
///
/// Supports iOS/Android/MacOS for now.
final databaseFactorySqflite = getDatabaseFactorySqflite(
  sqflite.databaseFactory,
);

/// Opens the app database. Call it once at startup and keep the result.
Future<Database> openAppDatabase() {
  // A relative path is in the sqflite databases folder (getDatabasesPath()).
  return databaseFactorySqflite.openDatabase('my_app.db');
}

Future<void> demo(Database db) async {
  // Define the store, key is a string, value is a string
  var store = StoreRef<String, String>.main();
  // Define the record
  var record = store.record('my_key');

  // Write a record
  await record.put(db, 'my_value');

  // Read it
  print(await record.get(db));
}
  • sembast_sqflite.dart does not export sembast: also import package:sembast/sembast.dart.
  • Import sqflite with a prefix (as sqflite): both packages have a Database and a DatabaseFactory class.
  • To open the database in main() before runApp(), call WidgetsFlutterBinding.ensureInitialized() first.
  • A relative path is in getDatabasesPath(): the app databases folder on Android, the Documents folder on iOS and macOS (visible in the Files app on iOS when file sharing is enabled). An absolute path, for example in path_provider's getApplicationSupportDirectory(), is used as is. An existing app must keep its path: a new path opens a new, empty database.

Why

You might wonder why...sembast already has its own io format. However sembast io is not cross process safe and one might consider that it is not a well known robust database system.

Here sqflite is used as the based of a journal database that provides data to sembast, allowing a fast all-in-memory access and safe cross process database storage and transaction mechanism.

Usage

  • sembast_sqflite should be used from the main isolate only
  • While being cross-process safe, you might encounter locked access when using multiple transactions are in progress, which could happen while debugging.
  • Do not open (and close) the same database file with sqflite directly or with another sqflite based factory while sembast has it open: sqflite opens a single instance per path by default, so closing it there closes the connection sembast uses.
  • Opening sembast on an existing plain sqlite file replaces its entry and info tables.
  • Applications should not rely on internal storage format

Libraries

sembast_sqflite
sembast on top of sqflite: a sembast database stored in a SQLite file, cross process safe.