Git Product home page Git Product logo

Comments (7)

dfahlander avatar dfahlander commented on May 21, 2024

As the delete() method is not implemented yet, you could work around it by deleting the database and recreate it. The delete() method will be implemented by removing the sync-node representing the particual remote endpoint. If you feel lucky, you could give it a try to implement it. Might need to try review the code to find whether we would need to await or cancel existing sync operations before deleting the node. The Observable and Syncable addons are in alpha and may need some refactoring.

from dexie.js.

martindiphoorn avatar martindiphoorn commented on May 21, 2024

I'm aware that is alpha, but it can only get out of alpha by using it. So im using and trying to help you out if i can. But i need to learn the way you program :D

I have already been trying to implent it.
My idea was to disconnect the url (which was done outside this code). Call the Delete and wait for the promise (Didn't get the promise to work so left it out). Connect the url again.

This was the code i had:

db.syncable.delete = function (url, options) {
            db._syncNodes.where("url").equals(url).modify(function (node) {
                db._changes.where('node').equals(node.id).delete();
                db._uncommittedChanges.where('node').equals(node.id).delete();

                node.appliedRemoteRevision = 0;
                node.remoteBaseRevisions = {remote:0, local:0};
                db._syncNodes.put(node);
            });
        };

However it didn't worked fine. One problem was that the synchronisation keeps giving an wrong revision number.

Maybe you can help me out here? Thanks in advance.

from dexie.js.

dfahlander avatar dfahlander commented on May 21, 2024

I would love to have a look into it when I get a chance. Please remind me if I don't! Spontanously, without having looked into the source of the addon for a while, I could see one possible issue with your code snippet; promises aren't returned or waited for + I believe the node should not be recreated in the delete() method. Also, would be nice to encapsulate the method in a transaction to ensure atomicy as well as not missing out any failed operation. Without having tested it, I would suggest something like the following:

db.syncable.delete = function (url) {
    // Surround with a readwrite-transaction
    return db.transaction('rw', db._syncNodes, db._changes, db._uncommittedChanges, function () {
        // Find the node
        db._syncNodes.where("url").equals(url).first(function (node) {
            // If not found, resolve here (nothing deleted), else continue the promise chain and
            // delete the node and all that refers to it...
            if (!node) return; else return db._syncNodes.delete(node.id).then(function(){
                return db._changes.where('node').equals(node.id).delete();
            }).then(function(){
                return db._uncommittedChanges.where('node').equals(node.id).delete();
            });
        });
    });
};

If the use case is force a full resync, the caller could do:

db.syncable.disconnect(url).then(function(){
    return db.syncable.delete(url);
}).then(function(){
    return db.syncable.connect(protocol, url, options);
});

from dexie.js.

martindiphoorn avatar martindiphoorn commented on May 21, 2024

Ah thats a great starting point... Actually it worked almost out of the box 👍

This is the current version i have working:

        db.syncable.delete = function (url) {
            // Surround with a readwrite-transaction
            return db.transaction('rw', db._syncNodes, db._changes, db._uncommittedChanges, function () {
                // Find the node
                db._syncNodes.where("url").equals(url).first(function (node) {
                    // If not found, resolve here (nothing deleted), else continue the promise chain and
                    // delete the node and all that refers to it...
                    if (!node) {
                        return;
                    } else {
                        var nodeId = node.id;
                        return db._syncNodes.delete(nodeId).then(function () {
                            return db._changes.clear().then(function () {
                                return db._uncommittedChanges.where('node').equals(nodeId).delete();
                            });
                        });
                    }
                });
            });
        };

There is one small problem. The _changes tableStore does not have an node so i could not search on it. Trying to change it to source dit not work either. I also see that i have lots off sources with "null" values in de _changes.
However if I do an resync i don't care about the changes for now. So i used a clear for that part.

After that the sync is working in my project!

If you like the change, i'm willing to send a pull request if you like that.

Thanks

from dexie.js.

martindiphoorn avatar martindiphoorn commented on May 21, 2024

Send a pull request.

from dexie.js.

dfahlander avatar dfahlander commented on May 21, 2024

Thanks! I've merged the pull request in. Had to change one thing: Instead of clearing _changes, I only delete changes that were only still queued because of the deleted node. If there were other nodes alive, they may need those changes.

        db.syncable.delete = function (url) {
            // Notice: Caller should call db.syncable.disconnect(url) and wait for it to finish before calling db.syncable.delete(url)
            // Surround with a readwrite-transaction
            return db.transaction('rw', db._syncNodes, db._changes, db._uncommittedChanges, function() {
                // Find the node
                db._syncNodes.where("url").equals(url).first(function(node) {
                    // If not found, resolve here (nothing deleted), else continue the promise chain and
                    // delete the node and all that refers to it...
                    if (!node) {
                        return;
                    } else {
                        var nodeId = node.id;
                        // Delete the node
                        return db._syncNodes.delete(nodeId).then(function() {
                            // When this node is gone, let's clear the _changes table by
                            // from all revisions older than the oldest node.
                            // First check which is the currenly oldest node, now when we have deleted
                            // the given node:
                            return db._syncNodes.orderBy("myRevision").first();
                        }).then(function(oldestNode) {
                            // Delete all changes older than revision of oldest node:
                            return db._changes.where("rev").below(oldestNode.myRevision).delete();
                        }).then(function() {
                            // Also don't forget to delete all uncommittedChanges for the deleted node:
                            return db._uncommittedChanges.where('node').equals(nodeId).delete();
                        });
                    }
                });
            });
        };

from dexie.js.

martindiphoorn avatar martindiphoorn commented on May 21, 2024

Looking great, going to test this.

from dexie.js.

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.