How to remove records in store when they are not in server anymore

I have application which may lost connection with server for days and when it finally reconnect some record exist in store didn’t exist in server any more.

I try to fix it but can’t get any where.

store.findall didn’t seems to have option to delete record not returned by server.

I can’t find ways to chain store.unloadAll and store.findAll without have the blank state take effect on the screen and cause flickering

I also can’t find out how to get what actually returned by server without go completely manual (make my own ajax calls,which won’t scale obviously)

I am wondering what everyone else is using to sync record in store with server.

Thanks for any help

We use CouchDB as server and ember-pouch as ember-data adapter. This combination takes care of all your sync problems.

I looked at it sometimes ago and although i like the idea, i really can’t figure out how to handle permissions if i sync the database to the client.

Permissions can be done per DB so when we need secure we use a private database per user setup. It is no problem to have 2000 databases. We don’t use it but there is new tool for this called Spiegel. See also Myapp (and try disconnecting from the internet)

Seems find it, store.query will return what actually returned by server and i can manually unload the record now.

import DS from 'ember-data';
import _ from 'npm:lodash'

export default DS.Store.extend({
    reloadFromServer(modelName) {
        return this.query(modelName, {}).then((serverRecordArray) => {
            let storeRecordArray = this.peekAll(modelName)
            _.differenceBy(storeRecordArray.toArray(), serverRecordArray.toArray(), (i) => {
                return i.get('id');
            }).forEach((record) => {
                this.unloadRecord(record)
            })
            return this.peekAll(modelName)
        })
    }
});
1 Like

unloadRecord is effectively “move this record into a never-loaded state”. It is also a “mark” for a “mark and sweep” GC that will eliminate the record entirely if it has no retainers. Relationships are retainers.

With this knowledge, you might be able to discern how to “push a deletion” into the store:

  • update it’s relationships to being empty
  • unload it

You’ll want this to occur within a runloop because it requires a queue flush to update the relationships and associated RecordArrays that the record may be in.

Gist: Useful Ember Data helpers · GitHub

1 Like