crdt_lf_sqlite 0.3.1 copy "crdt_lf_sqlite: ^0.3.1" to clipboard
crdt_lf_sqlite: ^0.3.1 copied to clipboard

sqlite3 storage adapter for CRDT LF library objects, providing persistence for Change and Snapshot objects.

example/main.dart

// The example prints its result, and it is not a library entry point.
// ignore_for_file: avoid_print

import 'dart:io' as io;

import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_sqlite/crdt_lf_sqlite.dart';

/// The kind of the list in this example. A generic handler names its kind: the
/// default would carry the type argument, which dart2js minifies away.
const kList = 'string-list';

Future<void> main() async {
  const itemsToAdd = 3;
  const documentId = '784ff372-6f0a-4fe9-8e63-19b72fd18c23';

  // Resolve the database next to this script so the example works regardless
  // of the current working directory.
  final dbLocation =
      '${io.File.fromUri(io.Platform.script).parent.path}/crdt_example.db';

  final backend = CRDTSqlite.open(dbLocation);

  // Reads the database into a document and follows it: everything written from
  // here on is stored without another line of code. The identity comes from
  // the backend too, so every run is the same author.
  final note = await backend.openDocument(documentId);
  final list =
      CRDTListHandler<String>(note.document, 'list', handlerType: kList);

  print('read back: ${list.value}');

  final length = list.value.length;
  for (var i = length; i < length + itemsToAdd; i++) {
    list.insert(i, 'Item $i');
  }

  // Writes what is still waiting. Without it the process could end before the
  // delayed write runs.
  await note.persistence.dispose();
  note.document.dispose();

  print('now: ${list.value}');
  print('documents in the database: ${backend.documentIds.length}');

  backend.close();
}
0
likes
0
points
465
downloads

Publisher

verified publishermattiapispisa.it

Weekly Downloads

sqlite3 storage adapter for CRDT LF library objects, providing persistence for Change and Snapshot objects.

Homepage
Repository (GitHub)
View/report issues

Topics

#crdt #local-first #sqlite #persistence #dart

License

unknown (license)

Dependencies

crdt_lf, crdt_lf_persistence, hlc_dart, sqlite3

More

Packages that depend on crdt_lf_sqlite