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

Save data and included related data together with one call #1586

Closed
alFReD-NSH opened this issue Aug 12, 2015 · 6 comments
Closed

Save data and included related data together with one call #1586

alFReD-NSH opened this issue Aug 12, 2015 · 6 comments
Assignees

Comments

@alFReD-NSH
Copy link
Contributor

Basically what I'm looking is that, given I have the following models:

{
  "name": "Person",
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "category": {
      "type": "string",
      "required": true
    },
  "validations": [],
  "relations": {
    "belongings": {
      "type": "hasMany",
      "model": "PersonBelonging",
      "foreignKey": "personId"
    }
  },
  "acls": [],
  "methods": []
}

{
  "name": "PersonBelonging",
  "properties": {
    "label": {
      "type": "string",
      "required": true
    },
    "type": "string"
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

I should be able to save(create or update) the following data:

Person.create({ name: 'Farid', category: 'student', belongings: [{ label: 'laptop', type: 'lenovo' }] });

Which will also create the personBelongings entry. This makes my life much easier as, when I query with include: 'belongings', I can just modify and save in one command. But it looks like I have to remove the old belongings(on update) and add the new ones one by one.

We have more than a few of these cases in our project. I can write some code to abstract this pattern and reduce it to one, but I believe this code belongs in loopback code base and not my project codebase.

Note that this has been asked before on mailing list before:
https://groups.google.com/forum/#!searchin/loopbackjs/save$20related/loopbackjs/eECzHEa7MlI/-7Maqm1p4UUJ
https://groups.google.com/forum/#!searchin/loopbackjs/save$20related/loopbackjs/sKNauY9jv00/uI8dD77YGygJ
https://groups.google.com/forum/#!searchin/loopbackjs/save$20related/loopbackjs/yqpTRkJMzpI/55z31Rd-gB8J

and also on stackoverflow

@raymondfeng
Copy link
Member

I'll be looking into these use cases as part of composite model design.

@jannyHou jannyHou self-assigned this Dec 6, 2015
@jannyHou jannyHou added feature and removed triaging labels Dec 8, 2015
@jannyHou jannyHou added the #tob label Jan 12, 2016
@vanthome
Copy link

vanthome commented Apr 28, 2016

@alFReD-NSH I have a similar problem but for me this is not only if I update but also when I create the object initially. So in your example, the belongings property would just be missing. Can you confirm this?

Also I would like to know how I can set the embedded property at all then?

@alFReD-NSH
Copy link
Contributor Author

@vanthome Currently Person.create({ name: 'Farid', category: 'student', belongings: [{ label: 'laptop', type: 'lenovo' }] }); belongings wouldn't be saved. It was set in relations:

  "relations": {
    "belongings": {
      "type": "hasMany",
      "model": "PersonBelonging",
      "foreignKey": "personId"
    }
  },

@jannyHou jannyHou removed the #tob label Nov 18, 2016
@stale stale bot added the stale label Aug 23, 2017
@stale
Copy link

stale bot commented Aug 23, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Sep 6, 2017
@stale
Copy link

stale bot commented Sep 6, 2017

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@tellex
Copy link

tellex commented Feb 28, 2018

I added this functionality through a mixing method, checkout the code:
https://github.com/tellex/createNested/
soon i will create a npm package.

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

5 participants