observe<T> method
Observe database changes for a specific table and event type with type safety.
@param path - Table name followed by event type (e.g., "users.insert", "posts.update") @returns A type-safe DatabaseObserver for setting up event handlers and subscriptions
Example Insert Events
final subscription = await db.observe<User>('users.insert')
.onInsert((event) {
for (final user in event.rows) {
print('New user: ${user.name}');
}
})
.filter(DatabaseGtFilter('age', 18))
.subscribe();
Example Update Events
final subscription = await db.observe<User>('users.update')
.onUpdate((event) {
for (final user in event.rows) {
print('Updated user: ${user.name}');
}
})
.subscribe();
Example Delete Events
final subscription = await db.observe('users.delete')
.onDelete((event) {
print('Deleted user IDs: ${event.rowIds}');
})
.subscribe();
Implementation
DatabaseObserver<T> observe<T>(String path) {
final observePath = DatabaseObservePath(path);
return DatabaseObserver<T>._(
this,
observePath.table,
observePath.eventType,
);
}