Skip to content

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

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

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