Coder Social home page Coder Social logo

Comments (8)

simolus3 avatar simolus3 commented on May 29, 2024

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.

greenking19 avatar greenking19 commented on May 29, 2024

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.

greenking19 avatar greenking19 commented on May 29, 2024

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.

greenking19 avatar greenking19 commented on May 29, 2024

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.

greenking19 avatar greenking19 commented on May 29, 2024

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 👇🏻

  1. Listen for data with is_sync=false and upload it to the server in a timely manner

from drift.

greenking19 avatar greenking19 commented on May 29, 2024

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.

simolus3 avatar simolus3 commented on May 29, 2024

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.

greenking19 avatar greenking19 commented on May 29, 2024

@simolus3 Thank you!
I have a clear idea now

from drift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.