cbl_dart 2.1.3 copy "cbl_dart: ^2.1.3" to clipboard
cbl_dart: ^2.1.3 copied to clipboard

outdated

Couchbase Lite for pure Dart apps: An embedded, NoSQL JSON Document Style database, supporting Blobs, Encryption, N1QL Queries, Live Queries, Full-Text Search and Data Sync.

example/lib/main.dart

// ignore_for_file: avoid_print

import 'dart:io';

import 'package:cbl/cbl.dart';
import 'package:cbl_dart/cbl_dart.dart';

late final Database db;

Future<void> main(List<String> args) async {
  if (args.contains('--help') ||
      args.contains('-h') ||
      (args.isNotEmpty && args.length != 1)) {
    print(
      '''
Usage: dart lib/main.dart [message]

The provided message will be stored in the database.

If no message is provided, all stored messages will be listed.
''',
    );
    exit(1);
  }

  await CouchbaseLiteDart.init(edition: Edition.community);

  db = await Database.openAsync('messages');

  final message = args.isEmpty ? null : args[0];

  if (message != null) {
    await storeMessage(message);
  } else {
    await listMessages();
  }

  await db.close();
}

Future<void> storeMessage(String message) async {
  final doc = MutableDocument({
    'type': 'message',
    'createdAt': DateTime.now(),
    'body': message,
  });

  await db.saveDocument(doc);

  print('Message ${doc.id} stored: ${doc.toJson()}');
}

Future<void> listMessages() async {
  final query = const QueryBuilder()
      .select(
        SelectResult.expression(Meta.id),
        SelectResult.property('createdAt'),
        SelectResult.property('body'),
      )
      .from(DataSource.database(db))
      .where(Expression.property('type').equalTo(
        Expression.string('message'),
      ))
      .orderBy(Ordering.property('createdAt').descending());

  final resultSet = await query.execute();
  var messages = 0;

  await for (final result in resultSet.asStream()) {
    messages++;
    print(result.toJson());
  }

  print('$messages messages found');
}
8
likes
0
pub points
85%
popularity

Publisher

verified publishercbl-dart.dev

Couchbase Lite for pure Dart apps: An embedded, NoSQL JSON Document Style database, supporting Blobs, Encryption, N1QL Queries, Live Queries, Full-Text Search and Data Sync.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

cbl, cbl_libcblite_api, cbl_libcblitedart_api, crypto, path

More

Packages that depend on cbl_dart