aquabase 0.7.0
aquabase: ^0.7.0 copied to clipboard
Flutter SDK for Aquabase — encrypted real-time database with offline sync, tags, and binary transport.
example/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:aquabase/aquabase.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// ── Init ──────────────────────────────────────────────
final app = await Aquabase.init(
projectId: 'demo',
apiKey: 'aq_pub_demo',
// url: 'http://localhost:3280', // optional
// encrypted: true, // optional: cifra TODAS las colecciones
// jwt: 'user-jwt-token', // optional: para rules "auth"/"self"
);
// ── Auth ──────────────────────────────────────────────
await app.auth.register('user@test.com', 'pass123');
final login = await app.auth.login('user@test.com', 'pass123');
await app.auth.refresh(login.token);
// OAuth: await app.auth.signInWithOAuth(OAuthProvider.google, idToken);
// ── Collections ───────────────────────────────────────
final users = app.collection('users'); // sin cifrar, synced
final students = app.collection('_students', local: true); // local, sin cifrar
final session = app.collection('_session', local: true, encrypted: true); // local + AES-256-GCM
// Store encrypted credentials for fast login
await session.doc('current').set({'jwt': login.token, 'email': 'user@test.com'});
// ── Document CRUD ─────────────────────────────────────
await users.doc('u1').set({'name': 'Ana', 'age': 25, 'createdAt': DateTime.now()});
final user = await users.doc('u1').get();
debugPrint('User: $user');
await users.doc('u1').delete();
// ── Document write (auto-index) ──────────────────────
final scores = app.collection('scores');
await scores.doc('s1').set({'studentId': 'u1', 'subject': 'math', 'score': 95, 'date': '2026-03-19'});
// ── Equality query ────────────────────────────────────
final mathScores = await scores
.where('studentId', eq: 'u1')
.where('subject', eq: 'math')
.get();
debugPrint('Math scores: ${mathScores.length} results');
// ── Range query ───────────────────────────────────────
// score entre 90 y 100 (inclusivo)
final highScores = await scores
.where('score', from: 90, to: 100)
.get();
debugPrint('High scores: ${highScores.length} results');
// fechas de marzo completo + filtro por studentId
final marchScores = await scores
.where('date', from: '2026-03-01', to: '2026-03-31')
.where('studentId', eq: 'u1')
.get();
debugPrint('March scores: ${marchScores.length} results');
// ── Watch doc ─────────────────────────────────────────
final sub1 = users.watch('u1').listen((data) {
debugPrint('User changed: $data');
});
// ── Watch query ───────────────────────────────────────
final sub2 = scores
.where('subject', eq: 'math')
.watch()
.listen((results) {
debugPrint('Math scores: ${results.length}');
});
// ── Bulk ops ──────────────────────────────────────────
await users.bulkSet({'u1': {'name': 'Ana'}, 'u2': {'name': 'Luis'}, 'u3': {'name': 'Carlos'}});
final bulk = await users.bulkGet(['u1', 'u2', 'u3']);
debugPrint('Bulk get: ${bulk.keys.length} docs');
await users.bulkDelete(['u2', 'u3']);
// ── Local collection ──────────────────────────────────
await students.doc('s1').set({'name': 'Ana', 'age': 12, 'grade': '6A'});
final student = await students.doc('s1').get();
debugPrint('Student: $student');
await students.bulkSet({'s1': {'name': 'Ana'}, 's2': {'name': 'Luis'}});
// ── Storage ───────────────────────────────────────────
await app.storage.createBucket(name: 'avatars', public: true);
await app.storage.upload('avatars', 'photo.jpg', File('photo.jpg').readAsBytesSync(), contentType: 'image/jpeg');
final files = await app.storage.listFiles('avatars');
debugPrint('Files: ${files.length}');
final photo = await app.storage.download('avatars', 'photo.jpg');
debugPrint('Downloaded: ${photo.length} bytes');
await app.storage.deleteFile('avatars', 'photo.jpg');
await app.storage.deleteBucket('avatars');
// ── Logs ──────────────────────────────────────────────
await app.logs.connect(); // also captures crashes
app.logs.info('db', 'User created');
app.logs.warn('auth', 'Failed login', source: '192.168.1.1');
app.logs.error('ws', 'Timeout');
final logs = await app.logs.query(LogQueryOptions(
since: DateTime.now().subtract(const Duration(hours: 1)).microsecondsSinceEpoch * 1000,
minLevel: LogLevel.warn,
limit: 50,
));
debugPrint('Logs: ${logs.length} entries');
// ── Offline status ────────────────────────────────────
debugPrint('Connected: ${app.isConnected}');
debugPrint('Pending ops: ${app.pendingOfflineOps}');
await app.flushOfflineQueue();
app.connectionState.listen((online) => debugPrint('Connection: $online'));
app.deniedOperations.listen((id) => debugPrint('Denied: $id'));
// ── Cleanup ───────────────────────────────────────────
sub1.cancel();
sub2.cancel();
app.logs.dispose();
app.dispose();
}