flutter_easy_db 1.1.0 copy "flutter_easy_db: ^1.1.0" to clipboard
flutter_easy_db: ^1.1.0 copied to clipboard

A secure, easy-to-use encrypted local database for Flutter combining the best of Isar and Drift with AES-256 encryption built in.

example/main.dart

import 'package:flutter_easy_db/flutter_easy_db.dart';

Future<void> main() async {
  await noSqlExample();
  await sqlExample();
}

/// ─── Isar-style NoSQL usage ───────────────────────────────────────────
Future<void> noSqlExample() async {
  print('=== EasyDbNoSql (Isar-style) ===');

  final db = await EasyDbNoSql.open(
    EasyDbConfig(
      dbName: 'my_app_nosql',
      encryptionKey: 'my-32-character-secret-key!!!!!',
    ),
  );

  final users = db.collection('users');

  // Put (insert/update)
  await users.put({'name': 'Alice', 'email': 'alice@example.com', 'age': 30});
  await users.put({'name': 'Bob', 'email': 'bob@example.com', 'age': 25});
  await users
      .put({'name': 'Charlie', 'email': 'charlie@example.com', 'age': 35});

  // Query with filters
  final adults = await users
      .query()
      .where('age', isGreaterThanOrEqual: 30)
      .sortBy('name')
      .find();

  print('Adults (age >= 30):');
  for (final doc in adults) {
    print('  ${doc.data['name']} - ${doc.data['age']}');
  }

  // Watch for reactive changes
  users.watch().listen((docs) {
    print('  [NoSQL] Collection now has ${docs.length} docs');
  });

  await db.close();
}

/// ─── Drift-style SQL usage ────────────────────────────────────────────
Future<void> sqlExample() async {
  print('\n=== EasyDbSql (Drift-style) ===');

  // Define schemas (like Drift table definitions)
  final usersSchema = EasyTableSchema(
    name: 'users',
    columns: [
      EasyColumn(
          name: 'id',
          type: ColumnType.integer,
          isPrimaryKey: true,
          autoIncrement: true),
      EasyColumn(name: 'name', type: ColumnType.text),
      EasyColumn(name: 'email', type: ColumnType.text, isUnique: true),
      EasyColumn(name: 'age', type: ColumnType.integer),
    ],
  );

  final postsSchema = EasyTableSchema(
    name: 'posts',
    columns: [
      EasyColumn(
          name: 'id',
          type: ColumnType.integer,
          isPrimaryKey: true,
          autoIncrement: true),
      EasyColumn(name: 'title', type: ColumnType.text),
      EasyColumn(name: 'body', type: ColumnType.text),
      EasyColumn(
        name: 'user_id',
        type: ColumnType.integer,
        foreignKey: ForeignKey(referenceTable: 'users', referenceColumn: 'id'),
      ),
    ],
  );

  // Open with schemas
  final db = await EasyDbSql.open(
    EasyDbConfig(
      dbName: 'my_app_sql',
      encryptionKey: 'my-32-character-secret-key!!!!!',
    ),
    tables: [usersSchema, postsSchema],
  );

  // Get table — encrypt the email column
  final users = db.table('users', encryptedColumns: ['email']);
  final posts = db.table('posts');

  // Insert (typed)
  final userId = await users
      .insert({'name': 'Alice', 'email': 'alice@example.com', 'age': 30});
  await users.insert({'name': 'Bob', 'email': 'bob@example.com', 'age': 25});

  await posts.insert(
      {'title': 'Hello World', 'body': 'First post!', 'user_id': userId});
  await posts.insert(
      {'title': 'Flutter Tips', 'body': 'Use EasyDb!', 'user_id': userId});

  // Select with WHERE + ORDER
  final results =
      await users.select().where('age > ?', [20]).orderBy('name ASC').run();

  print('Users (age > 20):');
  for (final row in results) {
    print('  ${row['name']} - ${row['email']} (age: ${row['age']})');
  }

  // JOIN query
  final joined = await users
      .select(columns: ['users.name', 'posts.title'])
      .join('posts', on: 'users.id = posts.user_id')
      .run();

  print('Users with posts (JOIN):');
  for (final row in joined) {
    print('  ${row['name']} wrote "${row['title']}"');
  }

  // Update
  await users.update({'age': 31}, where: 'name = ?', whereArgs: ['Alice']);

  // Delete
  await posts.delete(where: 'title = ?', whereArgs: ['Hello World']);

  // Count
  final count = await users.count();
  print('Total users: $count');

  await db.close();
}
0
likes
160
points
29
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A secure, easy-to-use encrypted local database for Flutter combining the best of Isar and Drift with AES-256 encryption built in.

Repository (GitHub)
View/report issues

Topics

#database #encryption #storage #sqlite #nosql

License

MIT (license)

Dependencies

crypto, encrypt, flutter, path, path_provider, sqflite, uuid

More

Packages that depend on flutter_easy_db