crdt_lf_sqlite 0.1.0+2 copy "crdt_lf_sqlite: ^0.1.0+2" to clipboard
crdt_lf_sqlite: ^0.1.0+2 copied to clipboard

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

example/main.dart

// ignore_for_file: avoid_print just for example

import 'dart:io' as io;

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

Future<void> main() async {
  const changesToInsert = 3;
  final documentId = PeerId.parse('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 storage = CRDTSqlite.open(dbLocation);
  final changeStorage = storage.changeStorageForDocument(
    documentId.toString(),
  );

  final changesToSave = <Change>[];

  final document = CRDTDocument(peerId: documentId)
    ..importChanges(changeStorage.getChanges())
    ..localChanges.listen(changesToSave.add);
  final list = CRDTListHandler<String>(document, 'list');

  final length = list.value.length;

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

  print('${document.exportChanges().length} changes ');
  print('${list.value}');

  await Future<void>.delayed(const Duration(seconds: 1));

  print('${changesToSave.length} changes to save');

  changeStorage.saveChanges(changesToSave);

  print('changes saved');

  storage.close();

  print('database closed');
}
0
likes
150
points
94
downloads

Documentation

API reference

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

MIT (license)

Dependencies

crdt_lf, hlc_dart, sqlite3

More

Packages that depend on crdt_lf_sqlite