abstractdb 1.2.0 copy "abstractdb: ^1.2.0" to clipboard
abstractdb: ^1.2.0 copied to clipboard

A flexible document database abstraction layer for Dart and Flutter supporting pluggable persistence (Sembast, Supabase) and reactive queries.

example/abstractdb_example.dart

import 'package:abstractdb/abstractdb.dart';

void main() async {
  // 1. Initialize AbstractDb with our collection. By default, it operates in-memory.
  final db = AbstractDb(
    name: 'sample_db',
    collections: [
      Collection(
        name: 'users',
        indexes: [
          UniqueEqualityIndex(name: 'username_idx', fields: ['username']),
        ],
      ),
    ],
  );

  await db.ensureInitialized;
  print('Database "${db.name}" initialized successfully.');

  // 2. Retrieve the collection through the db database instance
  final usersCollection = db.collection('users')!;

  // 3. Save some documents using Document
  print('\nSaving users...');
  final doc1 = Document<String>({
    'username': 'alice',
    'role': 'Admin',
    'age': 30,
  });
  final doc2 = Document<String>({'username': 'bob', 'role': 'User', 'age': 25});
  final doc3 = Document<String>({
    'username': 'charlie',
    'role': 'User',
    'age': 35,
  });

  await usersCollection.save(doc1);
  await usersCollection.save(doc2);
  await usersCollection.save(doc3);
  print('Users saved.');

  // 4. Try saving a duplicate username to verify the UniqueEqualityIndex constraint works
  print(
    '\nAttempting to save duplicate username "alice" (should fail index uniqueness constraint)...',
  );
  final duplicateDoc = Document<String>({
    'username': 'alice',
    'role': 'User',
    'age': 28,
  });
  try {
    await usersCollection.save(duplicateDoc);
    print('Error: Duplicate document saved without throwing exception!');
  } catch (e) {
    print('Success: Caught index violation exception as expected: $e');
  }

  // 5. Query documents (indexed search for username 'bob')
  print('\nQuerying user with username "bob" (utilizes unique index)...');
  final queryBob = Query('username'.eq('bob'));
  final cursorBob = await usersCollection.find(queryBob);
  final resultsBob = await cursorBob.exec();
  print('Bob query results: $resultsBob');

  // 6. Query all Users (role: 'User'), sort them by age ascending, and limit to 1
  print(
    '\nQuerying users with role "User", sorted by age ascending, limited to 1...',
  );
  final queryUsers = Query('role'.eq('User'), sort: [('age', true)], limit: 1);
  final cursorUsers = await usersCollection.find(queryUsers);
  final resultsUsers = await cursorUsers.exec();
  print('Matched user (should be bob, age 25): $resultsUsers');

  // 7. Cleanup and dispose database
  await db.dispose();
  print('\nDatabase disposed.');
}
0
likes
160
points
42
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A flexible document database abstraction layer for Dart and Flutter supporting pluggable persistence (Sembast, Supabase) and reactive queries.

Repository (GitHub)

Topics

#database #nosql #storage #signals

License

MIT (license)

Dependencies

ensure_initialized, fuzzy, jdiff, json_path, json_schema, path, rfc_6901, sembast, sembast_web, signals, supabase, uuid, web

More

Packages that depend on abstractdb