Overview
Various LoopBack methods accept type descriptions, for example remote methods and dataSource.createModel(). The following is a list of supported types.
Type | Description | Example |
---|---|---|
null | JSON null | null |
Boolean | JSON Boolean | true |
Number | JSON number |
|
String | JSON string | "StrongLoop" |
Object | JSON object or any type See Object types below. | { "firstName": "John", "lastName": "Smith", "age": 25 } |
Array | JSON array See Array types below. | [ "one", 2, true ] |
Date | JavaScript Date object |
|
Buffer | Node.js Buffer object | new Buffer(42); |
GeoPoint | LoopBack GeoPoint object | new GeoPoint({lat: 10.32424, lng: 5.84978}); |
In general, a property will have `undefined`
value if no explicit or default value is provided.
Array types
LoopBack supports array types as follows:
{emails: [String]}
{"emails": ["String"]}
{emails: [{type: String, length: 64}]}
Object types
Use the Object type when you need to be able to accept values of different types, for example a string or an array.
A model often has properties that consist of other properties. For example, the user model can have an address
property that in turn has properties such as street
, city
, state
, and zipCode
. LoopBack allows inline declaration of such properties, for example:
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 an "anonymous" model.
If you intend to reuse the address model, 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);