In laravel we have something called an observer, where you can observe a change to a model table for events such as CREATE,UPDATE,DELETE is there any way of achieving the same with moor.?
Moor keeps track of changes tables internally, this is needed for stream queries to work. We could expose a stream of changed tables, if that helps. But that wouldn't include what kind of update that was (insert, update, delete).
For a feature like #313, we'd have to add additional information, which could include what kind of change was made.
In the next moor version, you can use database.listenForTableUpdates() for this purpose. It returns a stream that will emit an item whenever a specified table changes. It supports the following modes:
tableUpdates(const TableUpdateQuery.any) (the default)tableUpdates(TableUpdateQuery.onTable(users))tableUpdates(TableUpdateQuery.onTable(users, kind: UpdateKind.delete))TableUpdateQuery.allOf(). The util factory TableUpdateQuery.onAllTables(List<Table>) can be used to listen on all updates across multiple tables.Is this version on https://pub.dev? what is the version number thank you so much for your good work, I appreciate
Implementing this required some breaking changes, so it will be made available with moor 3.0, which is not yet on pub.
I can release a beta at the end of this week if that helps.
It will help a lot thank you once again.
any update about new release?
You can use a beta version of moor 3.0 by adding this to your pubspec:
dependency_overrides:
moor:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: moor
moor_ffi:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: moor_ffi
moor_generator:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: moor_generator
sqlparser:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: sqlparser
I know it's a bit of a hassle, but maintaining dev releases on pub across multiple packages is too much work to be feasible.
In the next moor version, you can use
database.listenForTableUpdates()for this purpose. It returns a stream that will emit an item whenever a specified table changes. It supports the following modes:
- Listening for all updates:
tableUpdates(const TableUpdateQuery.any)(the default)- Listening on one table:
tableUpdates(TableUpdateQuery.onTable(users))- Listening on one table a specific kind of update:
tableUpdates(TableUpdateQuery.onTable(users, kind: UpdateKind.delete))- Multiple TableUpdateQueries can also be merged by using
TableUpdateQuery.allOf(). The util factoryTableUpdateQuery.onAllTables(List<Table>)can be used to listen on all updates across multiple tables.
A full example will be much appreciated!
I assume that this should be in Dao and call the DAO i.e in DAO I should have a method like this Stream<Null> getStockByVariantStream() {
return tableUpdates(const TableUpdateQuery.any());
}
Stream<Null> getStockByVariantStream()
I assume stockByVariant is some kind of table? In that case you could use TableUpdateQuery.onTable(stockByVariant).
A full example will be much appreciated!
Anything specific you want to achieve? I could give an example based on that.
Most helpful comment
In the next moor version, you can use
database.listenForTableUpdates()for this purpose. It returns a stream that will emit an item whenever a specified table changes. It supports the following modes:tableUpdates(const TableUpdateQuery.any)(the default)tableUpdates(TableUpdateQuery.onTable(users))tableUpdates(TableUpdateQuery.onTable(users, kind: UpdateKind.delete))TableUpdateQuery.allOf(). The util factoryTableUpdateQuery.onAllTables(List<Table>)can be used to listen on all updates across multiple tables.