BlairAllegroTech / js-data-jsonapi

JsonApi Adapter for js-data
MIT License
15 stars 5 forks source link

Relations without data #28

Closed rgant closed 8 years ago

rgant commented 8 years ago

Created this pull request at @BlairAllegroTech's request. However I don't think he should import c065780

I just didn't quickly see how to not submit that part in the pull request.

rgant commented 8 years ago

This pull is in relation to #25

BlairAllegroTech commented 8 years ago

Just reviewing this im interested in the belongsTo relationship you have created with a foreignKey. Is this actually valid for a belongsTo ?

var Image = ds.defineResource({
            name: 'image',
            relations: {
                belongsTo: {
                    article: {
                        localField: 'article',
                        foreignKey: 'imageid'
                    }
                }
            }
        });
rgant commented 8 years ago

It's not valid, my bad. According to the documentation:

belongsTo uses "localField" and "localKey"

I don't think I am understanding what belongsTo means to js-data. I was thinking that the Article table would have SQL-like foreign key field to the Images table of the Image ID that it should display. An Image "belongs to" an Article in that case. But apparently that is not how js-data thinks of things. I think you are right that I just don't need belongsTo in my relations. I don't think it's giving me anything I would want.

BlairAllegroTech commented 8 years ago

What you are describing is a hasOne relationship with a localKey on the parent.

With a localKey you can:

Think of a belongsTo as simply allowing you to traverse a hasOne in the opposite direction. In js-data this means the child has a property for accessing the related parent object.

And as mentioned earlier we can use this information to ensure that a parent is create before the child (this has not been done in js-data) though normally we would only be able to access child data via a parent, e.g. HATEOAS).