404answernotfound
404answernotfound

Reputation: 771

loopback model with object

Good evening,

I just started with loopback (literally), so I went for the commands explained in the documentation:

Started the loopback project with the command:

slc loopback

Generated a model with the loopback model generator with the command:

slc loopback:model

When the generator starts, it asks for model name, storing method, base class, plural, whether we want it to be a common model or a server model. After that it asks for the properties of the model.

I have a model that could be like this:

MODEL NAME
Property1 : String,
Property2 : String,
Property3 : Number,
Property4 : {
            obj.Property1 : String,
            obj.Property2 : String,
            obj.Property3 : String
            },
Property5 : String

I thought that by choosing "Object" as the type of property it would ask me for additional properties on that object, but it didn't. Now I have no idea how to create those additional properties that are inside the object of this model.

How would I go about creating the properties that are nested inside the Property4 Object? Am I missing something from the loopback:model generator?

Upvotes: 2

Views: 1953

Answers (1)

Farid Nouri Neshat
Farid Nouri Neshat

Reputation: 30430

Well slc loopback:model doesn't do that. You just have to specify yourself in the generated json file(possibly in common/models/ directory) in the properties object:

"properties": {
  ...
  "Property4": {
    "type": {
      "Property1" : "String",
      "Property2" : "String",
      "Property3" : "String"
    }
  },
  ...
}

If you want any of the properties to be required you can do it like this:

"properties": {
  ...
  "Property4": {
    "type": {
      "Property1" : {
        "type": "String"
        "required": true
      },
      "Property2" : "String",
      "Property3" : "String"
    }
  },
  ...
}

If the object doesn't have a "type" property, you can just do this:

"properties": {
  ...
  "Property4": {
    "Property1" : "String",
    "Property2" : "String",
    "Property3" : "String"
  },
  ...
}

You can also put this definition into a another model and reference that here:

"properties": {
  ...
  "Property4": "AnotherModel",
  ...
}

I suggest you read through the 'properties' section of this document and also 'object types' section of this document.

Upvotes: 4

Related Questions