Feature Request: Store Purge Records


#1

Would it be possible to have a feature on the store where we can remove all records from the store for a particular type, regardless of the state they are in?

This is similar to unloadAll but works on new and dirty records (which unloadAll does not at the moment).

My use case is that when a user signs out of my app, I want to remove all user related records from the store (but keep “public” records). unloadAll does not work in this use case because I want to get rid of the records regardless of the state that they are in (as the user is signing out, the records need to be removed without any questions asked!).

I’m currently achieving this by filtering out all new and dirty records and dealing with them before calling unloadAll on the store. This works but doesn’t seem entirely efficient. I believe my use case is fairly generic so will apply to lots of other applications out there.

I’d suggest a method such as purgeRecord and purgeAll, but I’m not fussy about the names!

I’ve never delved into the Ember-Data code so don’t know how to write something to do this right now. So posting as a feature request now… there’s a chance I might be able to delve into the code and look at this myself in the future, but it’s not going to happen in the next month or two because of what I’m working on at the moment.


#2

I’m totally :+1: on this one. My app needs to have offline/online capabilities, so I need 2 stores due to limitations in ED – which are outside the scope of this text.

I’m creating records on the fly in the online store to allow them to be pushed. Sometimes I need to purge records, but I can’t because they’re in a dirt state.


#3

This is probably something that’s fairly easy to implement yourself. I’ve never tried to unload a record before, but from reading the documentation, I came up with this:

App.Store = DS.Store.extend({
	purgeAll: function(type) {
		this.all(type).forEach(function(record) {
			record.rollback();
			this.unloadRecord(record);
		}, this);
	}
});

#4

With your code (rollback() followed by unloadRecord()), I get this:

Error: Attempted to handle event `unloadRecord` on <App.Cart:ember449:96h20> while in state root.deleted.saved. 

Rolling back works as expected, it sets isDirty to false, but unloadRecord() doesn’t :frowning:


#5

This is why I think there needs to be a specific feature in the API that allows you to purge the record regardless of state. The rollback then unloadRecord works for some scenarios, but not all.


#6

For info, I’m using:

records.forEach(function(record) {
   if(record.get('isNew')) {
	record.deleteRecord();
   } 
   else {
	record.rollback();
	store.unloadRecord(record);
   }
});

Which works in my application as it covers the states that can occur in my app for which unloadRecord/unloadAll do not work.


#7

Actually, this solution does not actually work all the time as I’m occasionally hitting an error when a record is in a root.empty state. Sure I can figure that one out too with a bit of modification, but this is why the Ember Data API needs a purge method because the reality is I need confidence that the store is just going to dump all records for my use case - data security is really important when signing a user out, so I really need to dump the data.

In my App for the moment I’m probably going to force a reload of the browser on sign out as a temporary solution to give me that confidence.


#8

Just going to breathe some life into this thread by supporting this request. How else can we purge records effectively between sessions that may not belong to the same user?

My current work around for a demo application is to refresh the page, forcing a reload of the entire application from the cache. I’m sure their are better methods, this is just a temporary fix until I have time to address it properly, but it shows it’ll iikely be a problem for other developers.

It’s not very ember::rails::rubylike in it’s current state. Granted if you reach this problem you’ve probably already sacrificed your left arm to the Ember gods in your commitment to the framework.


#9

Hi! I think one of the fairly recent updates to Ember Data has fixed this for me. I’m currently using 1.0.0-beta.8.2a68c63a and am unloading records when a user signs out. It seems to be working fine.