coresmart / persistencejs

persistence.js is an asynchronous Javascript database mapper library. You can use it in the browser, as well on the server (and you can share data models between them).
http://persistencejs.org
1.73k stars 240 forks source link

How does sync handle deletes? #44

Open chetan51 opened 13 years ago

chetan51 commented 13 years ago

For example, if you delete an object on the server's database and try to sync, what would happen?

zefhemel commented 13 years ago

Currently: it doesn't. This has to be added at some point. I started work on this, but haven't finished it yet.

umbrae commented 13 years ago

I worked around this issue by adding an "is_active" flag to my content and syncing that, and only displaying records that are active. I find that's a pretty good DB best practice anyway, so it works well for me.

NickCharsley commented 12 years ago

I have to agree with umbrae, adding an "is_active" flag really works for me, although I would go with a "_isDeleted" like the "_lastChange" field.

Yes I know that's now two 'extra' fields that every synced table has to have, but it makes it obvious very quickly that every interface has to honour this as a way of deleting records.

It may be a bit of bloat, but in theory the 'local' deleted record could be removed once it had been synced up to the central repository. Although if it was kept locally then you would reduce even further the risk of re-allocating keys and ending up with unwanted relationships.

MarkMYoung commented 12 years ago

Not sure when it was added, but there is already a flag for deleting entities, it's called '_removed'. The following JSON would result in a delete.

{
    "now":1329222505551,
    "updates":[
        {
            "id":"c8340484-c1b7-4bf1-9a8f-a42af6f20f2d",
            "_removed":true
        }
    ]
}

I currently use an 'IsActive' field too because the server would have to have a "tombstone" table to know what IDs to flag as deleted for a period of time.

var Tombstone = persistence.define( 'Tombstone',
{
    entity:"TEXT",
    objectId:"TEXT",
    deleteDate:"DATE"
});