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

Fetching actual attachment data from an "ember-pouch" model #164

Open
handegar opened this issue Dec 20, 2016 · 12 comments
Open

Fetching actual attachment data from an "ember-pouch" model #164

handegar opened this issue Dec 20, 2016 · 12 comments

Comments

@handegar
Copy link

I have difficulties fetching the actual attachment-data from my PouchDB/CouchDB when using an ember-pouch model with a DS.attr("attachments") property.

My model:

import Model from 'ember-pouch/model';
import DS from 'ember-data';

const { attr, hasMany, belongsTo } = DS;

export default Model.extend({
  visualId: DS.attr("number"),
  rowJSON: DS.attr("json"),
  markersJSON: DS.attr("json"),
  attachments: DS.attr("attachments", {
    defaultValue: function() {
      return [];
    }
  })
});

I can upload data without issues (confirmed by inspecting the CouchDB directly), but when I try to fetch back the data via my model, all I get is a stub, ie. the data-field is undefined and the stub-flag is true.

Is there something I have overlooked here?

According to the PouchDB API spec, one must explicitly specify that the actual attachments data shall be included in the response via the {attachments: true} option in the db.get(..) call (ref: https://pouchdb.com/guides/attachments.html). Is this relevant?

I am currently using Ember-Pouch version 4.2.2 and PouchDB.version says 6.1.0.

@handegar handegar changed the title Fetching actual attachment data from a "ember-pouch" model Fetching actual attachment data from an "ember-pouch" model Dec 20, 2016
@simonexmachina
Copy link
Collaborator

simonexmachina commented Dec 20, 2016

Thanks for pointing this out, this really should be supported. Something like the following should be added to addon/adapters/pouch.js:

  getAttachment(model, attr) {
    let type = model.constructor.modelName
    let id = model.get('id')
    let name = model.get(`${attr}.name`)
    return this.get('db').rel.getAttachment(this.getRecordTypeName(type), id, name)
  }

This would allow usage as follows:

const Model = PouchModel.extend({
  file: attr('attachment'),
  getFile() {
    let adapter = this.get('store').adapterFor(this.constructor.modelName)
    return adapter.getAttachment(this, 'file')
  }
})

Feel like doing a PR that adds this with a test?

@broerse
Copy link
Collaborator

broerse commented Dec 21, 2016

@stevebest do you agree with this?

@stevebest
Copy link
Collaborator

Looks fine to me.

My use case doesn't access the attachment data after it's been uploaded. I use a rather dirty hack to get their URLs for <img>s, and this useful addition probably won't break them.

@handegar
Copy link
Author

handegar commented Jan 8, 2017

Hi!
Thanks (and sorry for the late reply)!
I'll take a look and see if I'm be able to get this to work. This is my first project with both Ember and PouchDb/CouchDB, so things might move along abit slow :)

@simonexmachina
Copy link
Collaborator

@handegar I submitted the initial implementation for attachments, happy to help out.

@bastientanesie
Copy link

I'm currently working (issue #229) with attachments myself and ran into this issue. As anyone made progress on this subject?

I'm able to save attachments to my models in CouchDB (thank you @jlami!), but now I need to get the content back for display purposes.
I implemented @aexmachina's solution in my Adapter but I get an error (ember-cli 3.3). The call to this.getRecordTypeName() is the starting point (see the stack trace) 😞. Ideas anyone?

@jlami
Copy link
Collaborator

jlami commented Sep 5, 2018

I haven't tested this. But it looks like getRecordTypeName looks up the modelName itself.

I think you might need to change it to

getAttachment(model, attr) {
    let type = model.constructor
    let id = model.get('id')
    let name = model.get(`${attr}.name`)
    return this.get('db').rel.getAttachment(this.getRecordTypeName(type), id, name)
  }

@bastientanesie
Copy link

Good spot @jlami, it works. Thanks again! 👍

@jlami
Copy link
Collaborator

jlami commented Sep 7, 2018

Good to hear, if it works as expected we could make a PR out of this.

@mbernwieser
Copy link

Good spot @jlami, it works. Thanks again!

@bastientanesie I got stuck on the same problem but I can't solve it. Could you please give some more details how you solved it?

@bastientanesie
Copy link

Sure @mbernwieser, here's what I came up with in my model:

getAttachment(model, attr) {
  return this.get('db').rel.getAttachment(
    this.getRecordTypeName(model.constructor),
    model.get('id'),
    model.get(`${attr}.name`)
  );
}

The trick is to use model.constructor instead of model.constructor.modelName. The codebase has changed since late 2016, so I guess that's why @aexmachina's snippet doesn't work anymore.
Let me know if you need more info.

@mbernwieser
Copy link

Thanks @bastientanesie, I got it working 😄

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

7 participants