Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side deletion doesn't update store #11

Closed
alexmipego opened this issue Oct 7, 2014 · 20 comments
Closed

Server side deletion doesn't update store #11

alexmipego opened this issue Oct 7, 2014 · 20 comments

Comments

@alexmipego
Copy link

Hi,

I'm rather new to Ember & Pouch but I've setup a small test project where I list a set of items. Everything like adding and updating seems to work but for single problem, if I delete a document (server-side) the changes aren't reflected until I refresh the page.

I've the afterModel function as per the docs implemented and updating a document server side is reflected on the page. I'm unsure about the issue but a potential problem is that every document on my couchdb has the id in the form "post_2_*" where post is my model name and 2 is (for some reason) always the same for every document. The * part does show some sort of unique identifier.

@nolanlawson
Copy link
Member

Which version of Pouch are you using, which version of CouchDB?

@alexmipego
Copy link
Author

I just installed ember-pouch using bower yesterday.

ember 1.5.1
ember-data 1.0.0-beta.9
ember-pouch 0.1.6
pouchdb 3.0.6
couchdb 1.6.1

@nolanlawson
Copy link
Member

Can you confirm whether this is an Ember Data issue or a PouchDB issue? I.e. could you take a look at the documents in the local PouchDB and see if the deletion really wasn't replicated, or if it's just not being updated in the Ember Data store?

@alexmipego
Copy link
Author

Please forgive any mishap from my part, I'm very new at this. I've tried to check this with something like this code:

temp1.currentModel.store.adapterFor('post').db.allDocs('post', function() {console.log(arguments[1].total_rows)})

and the number of records is correct. Checking the local db (chrome devtools > resources) does seem to mark all the documents as deleted. For some reason the local db is keeping every document revision even from long deleted documents, is there a way to tell PouchDB to cleanup?

As well as that previous snippet I showed you, I was calling the refresh method on my route. It does refresh (and I know the route's model method is called) but the deleted documents still show up as well as any model added in the meantime. I'm still new and didn't get the chance to learn but there is a chance that a filtered/query would also not be updated correctly.

@alexmipego
Copy link
Author

After a bit of debugging it seems that the update() with eventually call the store.pushMany() which only pushes data into the store and never removes items (afaik).

@nolanlawson
Copy link
Member

Aha, so this is an ember-pouch issue. Interesting; I'll see what I can do about fixing the update() function.

@nolanlawson
Copy link
Member

Or if you want to tackle this yourself, I happen to be super busy right now, and Ember Data is really new technology, so your guess is probably as good as mine. :)

@alexmipego
Copy link
Author

I've been trying to figure it out but I must be missing something. In ember-pouch and ember-relational where is the code that directly listens to pouchdb events?

I believe what needs to be done is to find the records that were deleted (update must be doing this) and call the transitionTo('deleted.saved') on the record. My investigation tells me that the record itself still responds with false to get('isDeleted') and that's the issue.

However, I can't find the places that directly glue pouchdb and the ember integrations. I need to 1) find out to translate a couchdb id into a ember record instance and 2) where the delete should be glued.

@nolanlawson
Copy link
Member

That code is implemented by the user him/herself; it's what I recommend in the README where you listen for live changes:

new PouchDB('mydb').changes({
      since: 'now', 
      live: true
    }).on('change', function () {
      recordArray.update();
    });

I forget the exact syntax, but deleted changes are definitely propagated to that changes listener, so you should be able to do something like

new PouchDB('mydb').changes({
      since: 'now', 
      live: true
    }).on('change', function (change) {
      recordArray.update();
      if (change.deleted) {
        // do something else
      }
    });

but like I said, I forget the exact syntax.

@nolanlawson
Copy link
Member

Also the folks in the #emberjs channel on Freenode are pretty responsive; we are also in #pouchdb if you want to ask us there

@alexmipego
Copy link
Author

For now I've settled with the following on the change handler. Somehow I've the feeling that when I have multiple routes and stores this might not work as expected, but at least it works now.

https://gist.github.com/alexmipego/9fc936d9544ac2ae5168

@nolanlawson
Copy link
Member

Yeah, this is not ideal. The relational-pouch should probably offer a changes API that does the string manipulation for you.

@nolanlawson
Copy link
Member

Also we need something better than this recordArray.update() hack, but I don't know Ember Data well enough to figure it out right now.

@vectart
Copy link

vectart commented Nov 6, 2014

@nolanlawson I suppose, PouchdbAdapter must listen for updates, if db sync was set up.
I could try to improve it.

@nolanlawson
Copy link
Member

@vectart Yeah, originally I wanted to be agnostic and just hook into PouchDB, regardless of whether it was syncing or not, but it seems like 99% of people want to have a syncing PouchDB, which is understandable. So having an alternative API that handles all that stuff for you would be good.

@nolanlawson
Copy link
Member

Also a lot of people are reporting better results with the nightly than with 3.0.6. We'll release the next version soon. http://pouchtest.com/nightly

@vectart
Copy link

vectart commented Nov 7, 2014

@nolanlawson Sweet, then I'll postpone investigation until the next version is released.

@broerse
Copy link
Collaborator

broerse commented Nov 16, 2014

@vectart I would like the live changes patch to move from my applications to Ember-Pouch. I hope you can find the time.

@nolanlawson
Copy link
Member

OK so we now have a fix for this, which is detailed in the README. It's kind of ugly and I'm not 100% satisfied yet, but at least it fixes the immediate problem.

@nolanlawson
Copy link
Member

Also the relevant fix is this commit: 6f49b13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants