sembast 1.17.0-dev.1 copy "sembast: ^1.17.0-dev.1" to clipboard
sembast: ^1.17.0-dev.1 copied to clipboard

outdated

NoSQL persistent embedded file system document-based database for Dart VM and Flutter with encryption support.

sembast.dart #

sembast db stands for Simple Embedded Application Store database

Build Status

General #

Yet another NoSQL persistent store database solution for single process io applications. The whole document based database resides in a single file and is loaded in memory when opened. Changes are appended right away to the file and the file is automatically compacted when needed.

Works on Dart VM and Flutter (no plugin needed, 100% Dart). Inspired from IndexedDB, DataStore, WebSql, NeDB, Lawndart...

Supports encryption using user-defined codec.

Usage #

Opening a database #

A database is a single file represented by a path in the file system

// File path to a file in the current directory
String dbPath = 'sample.db';
DatabaseFactory dbFactory = databaseFactoryIo;

// We use the database factory to open the database
Database db = await dbFactory.openDatabase(dbPath);

The db object is ready for use.

More information here.

Simple put/get records #

For quick usage, data can be written and read quickly using the put/get/delete api on the database object

// Easy to put/get simple values or map
// A key can be anything (int, String) as long is it can
// be properly JSON encoded/decoded
await db.put('Simple application', 'title');
await db.put(10, 'version');
await db.put({'offline': true}, 'settings');

// read values
String title = await db.get('title') as String; 
int version = await db.get('version') as int;
Map settings = await db.get('settings') as Map;
  
// ...and delete
await db.delete('version');

Record fields can be reference using a dot (.) unless escaped

For example

var value = record['path.sub'];
// means value at {'path': {'sub': value}}
value = record[FieldKey.escape('path.sub')];
// means value at {'path.sub': value}

Follow the links with more informatin on how to write or read data

Auto increment #

If no key is provided, the object is inserted with an auto-increment value

// Auto incrementation is built-in
int key1 = await db.put('value1') as int;
int key2 = await db.put('value2') as int;
// key1 = 1, key2 = 2...

Transaction #

Actions can be group in transaction for consistency and optimization (single write on the file system). If an error is thrown, the transaction is cancelled and the changes reverted.

To prevent deadlock, never use an existing Database or Store object.

await db.transaction((txn) async {
  await txn.put('value1');
  await txn.put('value2');
});

More info on transaction here

Using the new Store API #

The new API takes advantage of strong mode to make database access less error prone.

// Use the main store for storing key values as String
var store = StoreRef<String, String>.main();

// Writing the data
await store.record('username').put(db, 'my_username');
await store.record('url').put(db, 'my_url');

// Reading the data
var url = await store.record('url').get(db);
var username = await store.record('username').get(db);

More info on the new API here

Store #

The store has some similarities with IndexedDB store and DataStore entities. The database always has a main store for easy access (like in the example aboves or typically to save singletons) and allows for an infinite number of stores where a developer would store entity specific data (such as list of record of the same 'type')

 // Use the animals store using Map records with int keys
var store = intMapStoreFactory.store('animals');

// Store some objects
await db.transaction((txn) async {
  await store.add(txn, {'name': 'fish'});
  await store.add(txn, {'name': 'cat'});
  
  // You can specify a key
  await store.record(10).put({'name': 'dog'});
});
 

Simple find mechanism #

Filtering and sorting can be done on any field

More information here.

 // Use the animals store using Map records with int keys
var store = intMapStoreFactory.store('animals');

// Store some objects
await db.transaction((txn) async {
  await store.add(txn, {'name': 'fish'});
  await store.add(txn, {'name': 'cat'});
  await store.add(txn, {'name': 'dog'});
});

// Look for any animal "greater than" (alphabetically) 'cat'
// ordered by name
var finder = Finder(
    filter: Filter.greaterThan('name', 'cat'),
    sortOrders: [SortOrder('name')]);
var records = await store.find(db, finder: finder);

expect(records.length, 2);
expect(records[0]['name'], 'dog');
expect(records[1]['name'], 'fish');

Codec and encryption #

Sembast supports using a user-defined codec to encode/decode data when read/written to disk. It provides a way to support encryption. Encryption itself is not part of sembast but an example of a simple encryption codec is provided in the test folder.

// Initialize the encryption codec with a user password
var codec = getEncryptSembastCodec(password: '[your_user_password]');

// Open the database with the codec
Database db = await factory.openDatabase(dbPath, codec: codec);

// ...your database is ready to use

More information here.

idb_shim #

The project idb_shim provides a shim allowing accessing it using the IndexedDB api. The benefit is to be able to write the logic/synchronization part of the database layer and test its algorithms using Dart VM and not Dartium

// Idb factory based on sembast
var idbFactory = IdbSembastFactory(databaseFactoryIo);

String store = "my_store";

// Here the indexed db API can be used
void _initializeDatabase(VersionChangeEvent e) {
  Database db = e.database;
  // create a store
  ObjectStore objectStore = db.createObjectStore(store);
}
Database db = await idbFactory.open(dbPath, version: 1, onUpgradeNeeded: _initializeDatabase);

Transaction transaction = db.transaction(store, IDB_MODE_READ_WRITE);
ObjectStore objectStore = transaction.objectStore(store);

// put and read on object
await objectStore.put("value", "test");
expect(await objectStore.getObject("test"), "value");

await transaction.completed;

Information #

Storage format #

Data is stored in a text file where each line is (json format) either:

  • meta information of the database (first line)
  • record data

Each data written is appended lazily to the file for best performance. Compact might happen at any moment to prevent record duplication. The whole compact information is done in a new file followed by a rename to make it atomic.

More information here.

Supported types #

Supported types depends on JSON supported types. More information here

Keys

Supported key types are:

  • int (default with autoincrement when no key are passed)
  • String (String keys can also be generated à la firestore)
  • double

Values

Supported value types are:

  • String.
  • num (int and double)
  • Map (Map<String, dynamic>)
  • List (List<dynamic>)
  • bool
  • null

Build status #

Travis: Build Status

945
likes
0
pub points
98%
popularity

Publisher

verified publishertekartik.com

NoSQL persistent embedded file system document-based database for Dart VM and Flutter with encryption support.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

logging, meta, path, synchronized

More

Packages that depend on sembast