axor 1.0.0
axor: ^1.0.0 copied to clipboard
High-performance NoSQL database for Flutter with reactive queries, single-file storage, and zero-configuration setup.
example/example.dart
/// Example usage of Axor database
import 'package:axor/axor.dart';
// Example schema implementation
class UserSchema implements Schema<User> {
@override
String get name => 'users';
@override
Type get type => User;
@override
int get version => 1;
@override
String serialize(User object) {
return '{"id": ${object.id}, "name": "${object.name}", "age": ${object.age}}';
}
@override
User deserialize(String json) {
final map = <String, dynamic>{}; // Simplified JSON parsing
return User(id: 1, name: 'Example', age: 30);
}
}
// Example model
class User {
final Id id;
final String name;
final int age;
User({required this.id, required this.name, required this.age});
}
void main() async {
// Initialize Axor database
final axor = await Axor.open(
schemas: [UserSchema()],
directory: '/tmp/axor_example',
);
// Create a user
final user = User(id: Axor.autoIncrement, name: 'John Doe', age: 30);
// Store the user
final userId = await axor.collection<User>().put(user);
print('Stored user with ID: $userId');
// Retrieve the user
final retrievedUser = await axor.collection<User>().get(userId);
print('Retrieved user: ${retrievedUser?.name}');
// Query users
final users = await axor.collection<User>().filter().equalTo('name', 'John Doe').findAll();
print('Found ${users.length} users named John Doe');
// Watch for changes (simplified)
axor.collection<User>().filter().watch().listen((users) {
print('Users updated: ${users.length} total users');
});
// Get database stats
final stats = await axor.getStats();
print('Database stats: $stats');
// Close the database
await axor.close();
}