Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

clearing datastore #235

Closed
NickFranceschina opened this Issue May 2, 2012 · 33 comments

Comments

Projects
None yet

is there a way to clear out the entire datastore? say I have a user logged in, and they have built up some data.. then logout and login as someone else... I need all of that data to be erased. am I supposed to just set the original store == null and create a new one? or is there a way to clear the cache? if I try and blow it away and create a new one, does that screw with any of the existing observers that might be out there lingering? not familiar with the cleanup process....

Member

tchak commented May 7, 2012

I would go with

App.store.destroy()
App.store = DS.Store.create();
Owner

wagenet commented Jun 21, 2012

@tchak I wouldn't recommend this, since we actually pass the store reference around and those references would not be updated. We probably should have a method to do this.

Member

tchak commented Jun 21, 2012

Confirm. I written this comment long before router and outlet stuff appeared :)

Owner

wagenet commented Jun 21, 2012

@tchak Yup, just wanted to make it clear for anyone else running across this.

Contributor

KasperTidemann commented Sep 25, 2012

+1 on this one. Resetting the entire data store is a common use case for single-page apps that have user authorization. Clearing out the store is definitely something the app should do when logging out. Any news on this?

sakshij commented Oct 15, 2012

Any updates on this?
I do not want to clear the whole store but just clean the dirty records. I do that using rollback but I still get an error:
could not respond to event didChangeData in state rootState.loaded.updated.uncommitted. when I reload the store without committing it.

rundeks commented Feb 18, 2013

+1 This is definately needed. As KasperTidemann says any app that has user authorization will need to clear out the data store when users logout. Since the last comment on this is 4 months ago is there any plan to actually add this and if so will it be added anytime soon?

Contributor

KasperTidemann commented Feb 18, 2013

The way I solve this currently is to force-refresh the page on logout, as this of course clears out everything. But it's still sub-optimal.

Member

tchak commented Feb 18, 2013

@KasperTidemann same here and actually I think it is a good practice to reload a page on signout. You newer know what you may have left behind.

Contributor

KasperTidemann commented Feb 18, 2013

@tchak, I definitely agree with you about that part. However, reloading the page (practically by redirecting to a "You've logged out!" page or something similar) may not be applicable for all use cases. If it's a low-hanging fruit code-wise, providing an ability to reset everything seems like a good idea.

Contributor

CodeOfficer commented Feb 18, 2013

I'd love to be able to reset the store for testing purposes. The new reset approach for Application is lightning fast, perhaps that would be a fitting perk for the Store as well.

Contributor

heartsentwined commented Feb 25, 2013

Any updates on this issue?

I have written an authentication model for ember, but I cannot find any way around the issue yet. I can only recommend the browser-refresh method as proposed in the comment by @KasperTidemann in the docs at present.

As mentioned in my docs page, any "sign out" functionality, without a browser-refresh, is superficial at present. Perhaps, is there any way I can help? Though I must confess that I know not much about the inner workings of ember, yet @wagenet seemed to suggest that the data store is referenced throughout the code and needs contextual code understanding to update the code...

+1

This is what I do in my spec helper to teardown the models in the store:

  teardown: function () {
      for (key in App.store.clientIdToType) {
         var type = App.store.clientIdToType[key];
         this.clearCache(type);
      }
      App.store.clientIdToData = {};
      App.store.clientIdToPrematerializedData = {};
      App.store.clientIdToId = {};
      App.store.clientIdToType = {};
      App.store.recordReferences = {};
      App.store.relationshipChanges = {};
      App.store.recordArraysByClientId = {};
      App.store.recordCache = [];
      App.store.defaultTransaction.buckets = {
        clean:    Ember.OrderedSet.create(),
        created:  Ember.OrderedSet.create(),
        updated:  Ember.OrderedSet.create(),
        deleted:  Ember.OrderedSet.create(),
        inflight: Ember.OrderedSet.create()
    }
  },
  clearCache: function (type) {
      var map = App.store.typeMapFor(type);
      map.idToCid = {};
      map.clientIds = [];
      map.recordArrays = [];
      map.findAllCache = null;
  }

essentially 'removes elements' from the store .. by making them un-finable by the typical find query

Contributor

toranb commented Apr 23, 2013

+1

Member

darthdeus commented May 1, 2013

please :(

Owner

stefanpenner commented May 1, 2013

if we instantiate the store via the container, and make ember-data container aware then clearing the store would happen just happen during app#reset. Can anyone think of any downsides?

Contributor

heartsentwined commented May 1, 2013

@stefanpenner would it be better to have a ember-data method just to clear the datastore? Most of the time, I think, one would not want to reset the whole app. The only use case I can think of, where resetting the whole app would make sense, is during unit tests. But we already have app.destroy() for this case.

seems better to have a method on the store to reset itself like store.reset() .. since it might be a bit of an over couple to assume that ember users are all using ember data

Owner

stefanpenner commented May 2, 2013

I think this is a legit use case. Sorry my mind has been in reseting state for testing, I should have read this thread more closely.

Member

ghempton commented May 2, 2013

Shouldn't we use the container for this? If the use case is testing, then that seems like the ideal way to go. @danielspaniel, using the container wouldn't couple ember to ember-data.

We need to bite the bullet and make ember-data container aware (and use the container to look up types etc.)

Owner

stefanpenner commented May 2, 2013

@ghempton would u use the container destroy/rebuild when a user changes there identity in a system, and expects different data?

Contributor

heartsentwined commented May 2, 2013

@ghempton I think the original use case as mentioned in this thread is post-sign-out clearing the data store, which I would agree is a very common real world use case.

Member

ghempton commented May 2, 2013

Ah yes that seems like a very valid use case, however it also becomes very tricky. The store can't just clear its data, since the view layer will most likely be using it. Instead will have to iterate all models and unload their data.

ddewaele commented May 6, 2013

If this is not the place to discuss this I apologize, bit I was wondering if this approach (clearing / resetting the store) is also advised for the simple use-case where

  • user A performs an action on the UI resulting in the deletion of a record from the DB
  • user B goes to an overview page on the UI and doesn't want to see the deleted record anymore.

Currently the store never detects/removes deleted records from its internal collection. This means that if user B is working in the app while user A deletes the record, user B needs to do a full page refresh or he will keep on seeing the deleted record.

I can see this being an issue when implementing

  • an overview screen that people go to to see an up-to-date view on what is in the database.
  • a "refresh" like functionality where the user can click a refresh button to see an up-to-date view on the database.

It's not clear to me how this should be tackled "the right way" without too much plumbing. And it seems to be a very common use-case.

I can imagine certain high level entry points in my app could benefit from simply clearing / resetting the store.

taavo commented Jun 1, 2013

I'm pretty new to ember and ember-data, so my app is small and a poor test case for this. I'm currently experimenting with the following:

App.store.reopen({
  clear: function() {
    var typeMaps = this.get('typeMaps');

    for(var typeId in typeMaps) {
      if(typeMaps.hasOwnProperty(typeId)) {
        typeMaps[typeId].references.forEach(function(reference) {
          reference.record.unloadRecord();
        });
      }
    }
  }
});

datzzy commented Jul 14, 2013

much needed for secondary authentication purposes. i have a server auth and a second layer of client side auth that is not cleared on sign out without reloading the page messily.

@heartsentwined heartsentwined referenced this issue in getoutreach/epf Jul 14, 2013

Open

clearing datastore / session #13

Owner

wagenet commented Aug 9, 2013

In my opinion, a full page reload is the safest way to clear the data. That said, I understand that it's not necessarily the best user experience. @wycats, I seem to recall you having an opinion on this.

Owner

wycats commented Aug 10, 2013

App.reset() should work no?

Owner

wagenet commented Aug 10, 2013

@wycats That should work, though it will cause all routes to be exited and re-entered. Correct? I think this is not what the requester is hoping for.

Owner

wycats commented Aug 10, 2013

I would expect logging out of an app to bring you back to /? Once all models are destroyed, what hope do you have of keeping anything on the screen (you really do need to re-run the model hooks and re-enter if that's what you want).

Owner

wagenet commented Aug 13, 2013

Actually, I think @wycats is right on this one. If someone else has a case we're not thinking of, please start a discussion on the forums. Thanks!

@wagenet wagenet closed this Aug 13, 2013

@marcoow marcoow referenced this issue in simplabs/ember-simple-auth Mar 5, 2014

Closed

Lock store before clearing or persisting (Fix #97) #99

Contributor

runspired commented Jun 16, 2015

@wagenet This has come to be the trickiest part of working with Ember data on a mobile app. When you do want to completely wipe data / reset the app, App.reset() (ember-cli) only has a globals version available, while other suggestions (such as a page refresh) don't work consistently within a cordova wrapper. Now that there are initializers and instance-initializers, I'd think a method for tearing down the store entirely and creating a new instance should be easier to achieve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment