Loopback: How to define properties for a model containing child objects

Created on 6 Apr 2016  路  8Comments  路  Source: strongloop/loopback

Hello,
While creating model, in discoverModelProperties, I am able to represent single json as mentioned below - var props= [{"name": "title", "type": "string", "length": 100, "required": true}] and passing this in the callback method. I can view this property names in json format appearing for the model in the loopback explorer against Model Schema.

But, for cases where I have a child object for example like below, how do I define the properties similar to what I have defined as above.

{
  "name1" : "", 
  "name2" : { 
          "title" : "" 
  },
  "name3" : "", 
  "name3Props" : { 
         "title" : "", 
        "col1" : 100 , 
        "col2" : 100, 
  },
  "moreInfo" : { 
    "key1": "value",
        "key2":"value"
  }
}

Most helpful comment

@gunjpan

I've read that guide doc and apply to my project like this.

[user.json]
.....
"properties" : {
  ....
  "name" : {
    "type" : {
       "family" : "string",
       "given" : "string"
     }
  },
  "regions" : {
     "type" : [
        "object"
      ]
   }
  ...
}

It's working.

But, there are 2 questions.

  1. In the case of reusing, if I want to reuse the Name objet type in multiple models, when should I define the Name object type?

At the above guide doc, author define the object type (named address) with this code

 var AddressModel = {
    street: String,
    city: String,
    state: String,
    zipCode: String
};

var Address = ds.define('Address', AddressModel);

var UserModel = {
        firstName: String,
        lastName: String,
        address: 'Address',  // or address: Address
        ...
}

var User = ds.define('User', UserModel);

But, it's JS code! I can't insert JS code to my json file.

  1. I want to define the Region type like this
var RegionModel = {
    administrative : string, 
    locality : string,
    sublocality : string,
    geopoint : GeoPoint
}

and modify the user.json code to this

"regions" : {
  type : [ "Region"]
}

Is this available?

It'd be nice to see a working example of the object type.

Thanks.

All 8 comments

@mountain1234585 : Hi, LoopBack allows properties of Object types. I recommend reading about LoobBack types: Object Types.

Hope that answers your question. If it's not the case, could you please provide more detail about your issue.

@mountain1234585 : Hi, is this good to close.

@mountain1234585: Closing this due to inactivity. If you're still facing this issue, please feel free to re-open it and mention my GH handle, I will be happy to assist. Thank you :)

@gunjpan

I've read that guide doc and apply to my project like this.

[user.json]
.....
"properties" : {
  ....
  "name" : {
    "type" : {
       "family" : "string",
       "given" : "string"
     }
  },
  "regions" : {
     "type" : [
        "object"
      ]
   }
  ...
}

It's working.

But, there are 2 questions.

  1. In the case of reusing, if I want to reuse the Name objet type in multiple models, when should I define the Name object type?

At the above guide doc, author define the object type (named address) with this code

 var AddressModel = {
    street: String,
    city: String,
    state: String,
    zipCode: String
};

var Address = ds.define('Address', AddressModel);

var UserModel = {
        firstName: String,
        lastName: String,
        address: 'Address',  // or address: Address
        ...
}

var User = ds.define('User', UserModel);

But, it's JS code! I can't insert JS code to my json file.

  1. I want to define the Region type like this
var RegionModel = {
    administrative : string, 
    locality : string,
    sublocality : string,
    geopoint : GeoPoint
}

and modify the user.json code to this

"regions" : {
  type : [ "Region"]
}

Is this available?

It'd be nice to see a working example of the object type.

Thanks.

This issue should be reopened to address @casamia918 question above.

Having the same issue here. Any news on that ?

@casamia918 Inorder to do that all you've got to do is build a persisted model for name and use the 'embedsOne' relation in the json file of all the models that require the name model. Hope this clarifies the question.

And there is clear doc about how to do it? I want to do the same but I didn't get a clear response answering how to do it..

Was this page helpful?
0 / 5 - 0 ratings