-
Notifications
You must be signed in to change notification settings - Fork 76
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
Attachments transform #135
Attachments transform #135
Conversation
Unit test courtesy of Matt Marcum @mattmarcum
@stevebest I think that the transforms should return |
I've added code to return console.log(store.createRecord('pca').get('files')) // => null
store.createRecord('pca').save().then(pca => console.log(pca.get('files'))) // => [] It looks like the transform isn't called when the model hasn't been saved, which makes sense, but does provide a challenge. I suppose we can just say that you need to save the model before adding files to it, but it doesn't seem right to me. I'll submit a PR to your fork now. |
@aexmachina You can use photos: DS.attr('attachment', { defaultValue: [] }); I guess the best is to mention it in the readme. |
Yeah, it probably makes sense to deserialize "no attachments" into an Meanwhile, when creating new instances of models with attachments, you can be explicit about them: const post = store.createRecord('post', {
title: 'How to attach files',
attachments: [], // It's a DS.attr('attachment')
}); Or you could be smarter than me and use |
@stevebest another thought is that there's no need for the Also I think the README should include the // myapp/models/photo-album.js
export default DS.Model.extend({
photos: DS.attr('attachment', {
defaultValue: () => []
});
}); |
This is working as expected now, however I've noticed that the When I use
@nolanlawson I can't really say, but it feels to me that this might be a problem with Relational Pouch or PouchDB itself? How could I investigate further? I've confirmed that this does work if I use PouchDB directly: new PouchDB('http://localhost:5984/files_development').put({
_id: 'test',
_attachments: {
'test.png': {
content_type: 'text/png',
data: document.querySelector('input[type=file]').files[0]
}
}
}) |
I've been looking into this and I can't see how this works at all. Relational Pouch checks for an |
@aexmachina Hm my hunch is that the test is failing for CouchDB because it only inserts a stub. CouchDB expects you to insert a stub only if you've already inserted the full attachment at some point. PouchDB may be more lenient on this. My recommendation would be to fix the test so that it truly inserts a real attachment, then afterwards update that same document but include the stub. Also protip: use non-text data (e.g. a GIF, PNG, etc.) so that the md5 sum is the same between Pouch and Couch; will make the test easier to write. |
@nolanlawson I'm not using the tests, I'm just trying to get my feature completed (it's taken longer than expected, so just focused on getting it to work right now). It works when the model uses a local PouchDB, and doesn't work when it's a CouchDB. If you can point me in the right direction then I can probably work it out, but I can't currently see where the data produced by this transform is handled as an attachment at all. I'd be really appreciative if we could screenshare for a few minutes and try to get it worked out. |
@aexmachina Uhm, guys, I lied to you. You MUST name the field of your model const Foo = DS.Model.extend({
attachments: DS.attr('attachment'),
}); Also, I should probably rename the transform to be plural, too, because this is ridiculous. Sorry about me being an idiot. :) The thing is, originally I thought about having several const Foo = DS.Model.extend({
image: DS.attr('attachment'),
song: DS.attr('attachment'),
maybeSomeOtherFile: DS.attr('attachment'),
}); but it doesn't quite work because |
@stevebest I'm working on this as well and nearly have a solution for that problem. |
I've submitted a PR to @stevebest's fork that contains |
@stevebest, closing this PR in favour of #136. |
Includes support for preserving
digest
andlength
ofstub: true
attachments.This is like #97, only a bit better. :)