Skip to content

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

Closed

Description

@faridnsh

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

Activity

  1. raymondfeng commented on Aug 12, 2015

    @raymondfeng
    Member

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

  2. self-assigned this
    on Dec 6, 2015
  3. vanthome commented on Apr 28, 2016

    @vanthome

    @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?

  4. faridnsh commented on Jul 5, 2016

    @faridnsh
    ContributorAuthor

    @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"
        }
      },
    
  5. stale commented on Aug 23, 2017

    @stale

    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.

  6. stale commented on Sep 6, 2017

    @stale

    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.

  7. tellex commented on Feb 28, 2018

    @tellex

    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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions