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

Inject relations for parent resource on create #132

Closed
Patrik-Lundqvist opened this issue Aug 22, 2014 · 13 comments
Closed

Inject relations for parent resource on create #132

Patrik-Lundqvist opened this issue Aug 22, 2014 · 13 comments
Assignees
Milestone

Comments

@Patrik-Lundqvist
Copy link

When creating a new item that belongs to a another resource, the parent resource doesn't seem to get updated with the relation.

Example:

DS.defineResource({
name: 'user',
relations: {
    belongsTo: {
        organization: {
            localKey: 'organizationId',
            localField: 'organization'
        }
    }
}
});

DS.defineResource({
name: 'organization',
relations: {
    hasMany: {
        user: {
            localField: 'users',
            foreignKey: 'organizationId'
        }
    }
}
});

Creating item

DS.create('users', { name: 'test', organizationId: 1}).then(function() {
    DS.get('organization', 1); // This will not contain a relation to the newly created user
});
@jmdobry
Copy link
Member

jmdobry commented Aug 22, 2014

Yes, I've thought about this. I'll need to augment the injection to search the store for existing relations to update the references.

@jmdobry jmdobry added this to the 1.0.0-beta.1 milestone Aug 22, 2014
@jmdobry jmdobry self-assigned this Aug 22, 2014
@Patrik-Lundqvist
Copy link
Author

This doesn't seem to be fixed, see plunker: http://plnkr.co/edit/KjTHJy?p=preview

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

Actually, your plunkr demonstrates a capability that hasn't been implemented yet. Issue #132 establishes belongsTo relations of the items being injected. In your plunkr there is DS.inject('user', {id:1, name: 'test', organizationId: 1}), and if you console.log it you'll see that indeed, the user.organization "belongsTo" link has been established.

I think the feature you're now requesting is "establish hasMany relations of the items being injected", which belongs in a new issue.

organization.users represents a "hasMany" relationship of the item being injected, so that's why organization.users is undefined.

@Patrik-Lundqvist
Copy link
Author

Ah, alright. I think its my fault for being unclear in my original description of this issue. I stated that "the parent resource doesn't seem to get updated with the relation." and with this I meant that the parent object of the created item should get updated with a relation to the inserted object. The code example may also be a bit unclear as it doesn't state what the desired result would be. The comment should read:

DS.create('user', { name: 'test', organizationId: 1}).then(function() {
    DS.get('organization', 1); // This should contain a relation to the newly created user
});

And as you stated, this should now be put in its own issue. Should I give it another try and rephrase the issue differently or do you have a good formulation for it?

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

Hah, I misinterpreted! I've just about got it fixed. Don't worry about it.
On Aug 25, 2014 8:28 AM, "Patrik Lundqvist" notifications@github.com
wrote:

Ah, alright. I think its my fault for being unclear in my original
description of this issue. I stated that "the parent resource doesn't seem
to get updated with the relation." and with this I meant that the parent
object of the created item should get updated with a relation to the
inserted object. The code example may also be a bit unclear as it doesn't
state what the desired result would be. The comment should read:

DS.create('user', { name: 'test', organizationId: 1}).then(function() {
DS.get('organization', 1); // This should contain a relation to the newly created user});

And as you stated, this should now be put in its own issue. Should I give
it another try and rephrase the issue differently or do you have a good
formulation for it?


Reply to this email directly or view it on GitHub
#132 (comment)
.

jmdobry added a commit that referenced this issue Aug 25, 2014
@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

I decided to forgo a new issue. Should be fixed now.

DS.inject has findBelongsTo and findHasMany options, which default to false. Set them to true to get the result you want.

@Patrik-Lundqvist
Copy link
Author

Alright it seems like I can't formulate the problem in an understandable way, sorry for that. What the current solution seems to do is update any hasMany relationship for the newly injected item right?
Which makes this code work (given the resource structure specified in the original issue):

// Current solution
// Note: Creating an _organization_
DS.create('organization', {id:1, name: 'orgname'}, {findHasMany: true}).then(
  function() {
    var org1 = DS.get('organization', 1);
    // org1 will now have relations to any related users
  }
);

The original issue tries to specify a problem where other objects should get a relation to the newly inserted object.

// Wanted solution
// Note: Creating a _user_
DS.create('user', {id:1,name: 'test',organizationId: 1}).then(function() {
    // Get the _organization_
    var org1 = DS.get('organization', 1); 
    // org1 should contain a relation to the newly created user like this:
    {
      id:1,
      name:"orgname",
      users: [ { id:1, name: "test", organizationId: 1 }]
    }
});

And by the way, doesn't these lines in loadRelations.js set the default values to true?

if (!('findBelongsTo' in options)) {
  options.findBelongsTo = true;
}
if (!('findHasMany' in options)) {
  options.findHasMany = true;
}

@Patrik-Lundqvist
Copy link
Author

Maybe the solution could be a synchronous method for re-loading local relations? Like this:

DS.create('users', { name: 'test', organizationId: 1}).then(function() {
    var org1 = DS.get('organization', 1);
    DS.reloadRelations('organization', org1, ['user']);
    console.log(org1.users); // This will now contain the new user    
});

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

This was removed in the last commit:

if (!('findBelongsTo' in options)) {
  options.findBelongsTo = true;
}
if (!('findHasMany' in options)) {
  options.findHasMany = true;
}

Okay, now I think I understand what you're getting at. Everything I've done so far has been to update the newly injected items with references to any of their relations that are already in the store. You want everything that is already in the store to get updated with appropriate references to newly injected items. Right?

@Patrik-Lundqvist
Copy link
Author

Correcto!

@jmdobry
Copy link
Member

jmdobry commented Aug 25, 2014

Okay, I'll come up with a solution.

@jmdobry
Copy link
Member

jmdobry commented Aug 26, 2014

@Patrik-Lundqvist Take a look at bc0d5fa

The following is what I implemented first:

DS.inject('organization', { id: 1 }); // { id: 1 }
DS.inject('user', { id: 1, organizationId: 1 }, { findBelongsTo: true });
DS.get('user', 1); // { id: 1, organizationId: 1, organization: { id: 1 } }

The following solves your original intention with #132:

DS.inject('organization', { id: 1 }); // { id: 1 }
DS.inject('user', { id: 1, organizationId: 1 }, { linkInverse: true }); // { id: 1, organizationId: 1 }
DS.get('organization', 1); // { id: 1, users: [{ id: 1, organizationId: 1 }] }

And they can be used together:

DS.inject('organization', { id: 1 }); // { id: 1 }
DS.inject('user', { id: 1, organizationId: 1 }, { linkInverse: true, findBelongsTo: true });
DS.get('organization', 1); // { id: 1, users: [{ id: 1, organizationId: 1, organization: {...} }] }
DS.get('user', 1); // { id: 1, organizationId: 1, organization: { id: 1, users: [...] } }

@Patrik-Lundqvist
Copy link
Author

Works great, well done!

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

No branches or pull requests

2 participants