Loopback: How to write the nested models in the loopback models.json file

Created on 11 Jun 2014  Â·  2Comments  Â·  Source: strongloop/loopback

Well, basically I would like to have something similar to the following in the models.json file:

var UserModel = {
    firstName: String,
    lastName: String,
    address: {
        street: String,
        city: String,
        state: String,
        zipCode: String
    },
    ...
}

The value of the address is the definition of the address type, which can be also considered as an anonymous model.
If you intend to reuse the address model, we can define it independently and reference it in the user model. For example:

 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);

Most helpful comment

thank, but how can we do this from the json config for the model

    "properties": {
        "address": {
            "type": "Address",
            "required": true
        }
}

All 2 comments

Check this out:

https://github.com/strongloop/loopback-datasource-juggler/blob/master/examples/nesting-schema.js

Thanks,


Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

On Jun 11, 2014, at 7:30 AM, haohello [email protected] wrote:

Well, basically I would like to have something similar to the following in the models.json file:

var UserModel = {
firstName: String,
lastName: String,
address: {
street: String,
city: String,
state: String,
zipCode: String
},
...
}
The value of the address is the definition of the address type, which can be also considered as an anonymous model.
If you intend to reuse the address model, we can define it independently and reference it in the user model. For example:
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);

—
Reply to this email directly or view it on GitHub.

thank, but how can we do this from the json config for the model

    "properties": {
        "address": {
            "type": "Address",
            "required": true
        }
}
Was this page helpful?
0 / 5 - 0 ratings