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

Add Support for store.push() and store.pushPayload() #165

Open
Vatosteve opened this issue Dec 28, 2016 · 8 comments
Open

Add Support for store.push() and store.pushPayload() #165

Vatosteve opened this issue Dec 28, 2016 · 8 comments

Comments

@Vatosteve
Copy link

It would be a great addition to support these two methods for preloading the pouchdb instance with raw data.

https://guides.emberjs.com/v2.10.0/models/pushing-records-into-the-store/

@broerse
Copy link
Collaborator

broerse commented Dec 28, 2016

We now have the unloadedDocumentChanged hook. See: https://github.com/broerse/ember-cli-blog/blob/master/app/adapters/application.js#L51

In what way do you want to add support?

@Vatosteve
Copy link
Author

When I use store.push() to load in data from an external endpoint, its added to the ember data cache, but isn't saved within pouchDB. The rev field isn't populated as well.

@Vatosteve
Copy link
Author

I see that the unloadedDocumentChanged function is used with the couchdb sync to populate the ember-data cache. My scenario is a little different where I'm pulling data from an external source on the client and using store.push() to insert the data into the data store. Using store.push() in this method doesn't add the data into pouchDB.

@broerse
Copy link
Collaborator

broerse commented Dec 29, 2016

Adding data can be done with db.bulkDocs and the unloadedDocumentChanged hook should fire. Never tested by me ;-)

@broerse
Copy link
Collaborator

broerse commented Dec 29, 2016

If we support store.pushPayload() the way you use it now what is the expected ember data behavior for updating exiting records? Where can I read about this? Or do you generate new IDs every time you use store.push() like a real push?

Edit: I read This method can be used both to push in brand new records, as well as to update existing records. So it is not a real push and for existing records we should we should know their _rev

@kara-todd
Copy link

@broerse Would you be able to provide some clarity around how to use bulkDocs in this case?

In my particular situation, I'm importing a bunch of data once from some json files on the user's local file system. After that point, I'm reading the information from pouch (or at least that is my intention) :)

There are a lot of deeply embedded records in the sources files that I'm parsing out with DS.EmbeddedRecordsMixin... the source files also contain the object Ids. If I just push the raw data in with bulkDocs should ember-pouch then be able to read the pouch database and "figure out" the relationships if my serializers are correctly configured?

@broerse
Copy link
Collaborator

broerse commented Oct 10, 2017

@kara-todd Not sure static files with _rev values can be used with bulkDocs. It will work the first time but the next time the _rev values can be updated by your application. You then need to use batch_fetch to get the _rev values. So perhaps it is easier to use store.push() that only updates the ember-data cache and foreach save them back to pouch so the _rev values are handled by ember-pouch.

@kara-todd
Copy link

kara-todd commented Oct 10, 2017

@broerse Sorry, that wasn't clear on my part. I have some raw JSON data that is output by another application (a game in this case.)

It makes files that contain objects like the following:

{
 "id": 1,
 "name": "A Tale of the deep",
 "location": "City A",
 "events": [
    {
       "id":  2,
       "name": "Hear a fisherman's tale",
       ....
    },
    {
       "id":  3,
       "name": "No Thanks. I'm good",
       ....
    },
]
}

As part of my "import events" function I go through and read through the file data as a stream and get the individual objects. However, at present. I'm not transforming them in any way. I'm just importing them "as-is"

So:

    const store = get(this, 'store');
    const adapter = get(this, 'adapter');
    const db = adapter.get('db');

    fs.createReadStream(filepath)
      .pipe(JSONStream.parse('*'))
      .on('data', (data) => {
        store.pushPayload(modelName, store.normalize(modelName, data));
        db.bulkDocs([data]);
      })

This isn't quite working as I expected... It seems like the nested objects aren't getting picked up. I should have around 10k records and I'm getting around 2k (which about matches the "top" level objects.)

I was previously using db.rel.save(modelName, data); but now I'm getting Rel cannot be called on undefined.

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

3 participants