Comments (8)
See also: #136, perhaps #635 or this talk for ideas.
The current challenge is how to watch the data changes in the table and take corresponding actions.
I think triggers are well-suited for this. You don't need them for inserts (just set the is_sync
field to false by default), but you can use them for updates and deletions:
CREATE TRIGGER tbl_update AFTER UPDATE ON tbl WHEN old.is_sync BEGIN
UPDATE tbl SET is_sync = false WHERE tbl.id = old.id;
END;
CREATE TRIGGER tbl_delete INSTEAD OF DELETE ON tbl WHEN NOT old.is_delete BEGIN
UPDATE tbl SET is_sync = false, is_delete = true WHERE tbl.id = old.id;
END;
You can create these triggers in drift files, or manually by overriding allSchemaEntities
in the database to include Trigger
elements with the desired name and SQL.
Or are there any other solutions?
You could keep a separate table storing all pending changes and populate that with triggers. The benefit is that this avoids having to add a WHERE NOT is_deleted
clause to queries in the app itself.
from drift.
I think triggers are well-suited for this. You don't need them for inserts (just set the
is_sync
field to false by default), but you can use them for updates and deletions:CREATE TRIGGER tbl_update AFTER UPDATE ON tbl WHEN old.is_sync BEGIN UPDATE tbl SET is_sync = false WHERE tbl.id = old.id; END; CREATE TRIGGER tbl_delete INSTEAD OF DELETE ON tbl WHEN NOT old.is_delete BEGIN UPDATE tbl SET is_sync = false, is_delete = true WHERE tbl.id = old.id; END;You can create these triggers in drift files, or manually by overriding allSchemaEntities in the database to include Trigger > elements with the desired name and SQL.
Thank you, I have some ideas now.👨🏻💻
I think the trigger meets my basic needs.
Are "tbl_update" and "tal_delete" methods in the dart file?
Can Dart's methods be called in the drift file? If possible, that would be very convenient!
from drift.
Can I call the methods in Dart when the table data is modified?
I couldn't find it in the document.
For example, in the Todo table, when there is a data update, call Dart's method to upload it to the database
from drift.
May I ask how to use TRIGGER? I couldn't find any relevant examples.
Maybe I didn't look carefully enough.
Can you write a simple example?
Thank you! 🙋
from drift.
I can use it now, Drift files are really convenient! 🥳🥳
At the beginning, there was an error message when running, but later I uninstalled the app and debugged it again, and it was successful.
If during the development process, adding fields to the table later requires uninstallation and reinstallation?
Lastly, I have a question. I want to use where with watch, but it doesn't seem to support it?
Because I want to listen for data with is_sync=false
to be uploaded to the server
Similar to this 👇
void addWatcher() {
Stream<List<TodoItem>> stream = db
.select(
db.todoItems,
)
.where((tbl) => tbl.isSync.equals(false))
.watch();
subscriptionTodo = stream.listen((todos) {
uploadToServer(todos);
});
}
I am using a timer to loop now, but I feel that this method is a bit lacking.
There should be a better solution. Do you have any suggestions?
My current problem is 👇🏻
- Listen for data with
is_sync=false
and upload it to the server in a timely manner
from drift.
I've done it now, and it's okay to write it this way.👇
void addWatcher() {
Stream<List<TodoItem>> stream = (db
.select(
db.todoItems,
)
..where((tbl) => tbl.isSync.equals(false)))
.watch();
subscriptionTodo = stream.listen((todos) {
uploadToServer(todos);
});
}
Do you have any good advice for syncing data to the server? 🤔
from drift.
If during the development process, adding fields to the table later requires uninstallation and reinstallation?
You either need to write a migration or re-install the app, yes.
Do you have any good advice for syncing data to the server? 🤔
First, I think you may want to avoid running multiple synchronization tasks at the same time:
void addWatcher() {
Stream<List<TodoItem>> stream = (db
.select(
db.todoItems,
)
..where((tbl) => tbl.isSync.equals(false)))
.watch();
subscriptionTodo = stream.listen(null);
subscriptionTodo.onData((todos) async {
subscriptionTodo.pause();
try {
await uploadToServer(todos);
} finally {
subscriptionTodo.resume();
}
});
}
Apart from that I do't reaally know how to give specific advice since this very much depends on the architecture you have in your server and which protocols you use.
from drift.
@simolus3 Thank you!
I have a clear idea now
from drift.
Related Issues (20)
- Converting database keys in toJson from camelCase to snake_case HOT 1
- LazyDatabase is imposible to execute queries when reopen HOT 1
- flutter web unable to build HOT 1
- No `SqliteException` thrown if when fetching data from non existent column in test mode HOT 4
- Can't compile to wasm HOT 1
- [web] TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm' HOT 1
- SqliteException(1): while preparing a statement, unrecognized token: "@" HOT 3
- Database can no longer be mocked with new manager API HOT 10
- [Documentation] Insufficient documentation and examples on complex migration HOT 1
- Ability to mark foreign key constraint as deferrable HOT 1
- Schema verifier fails in setting DatabaseConfig.doubleQuotedStringLiterals HOT 4
- Flutter not resolving
- database is locked error with WAL journal mode HOT 4
- Option to run a few queries sequentially HOT 3
- Code generation creates database table with wrong type HOT 9
- Drift generator doesn't strip custom row class from SQL HOT 1
- Primary Key not required in companion insert HOT 2
- Issue with generated code: database.g.dart file HOT 29
- Select int64 return type HOT 4
- Start a select with the Dart API without specifiying a table HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drift.