Overview
Various LoopBack methods accept type descriptions, for example remote methods and dataSource.createModel().
The following table summarizes LoopBack’s data types.
Type | Description | Example |
---|---|---|
any | Any type, including array, object, Date, or GeoPoint | Any of: true , 123 , "foo" , [ "one", 2, true ] |
array |
JSON array See Array types below. |
[ "one", 2, true ] |
Boolean | JSON Boolean | true |
buffer | Node.js Buffer object |
new Buffer(42); |
date | JavaScript Date object |
|
GeoPoint |
LoopBack GeoPoint object |
new GeoPoint({lat: 10.32424, lng: 5.84978}); |
null | JSON null | null |
number | JSON number |
|
Object |
JSON object or any type See Object types below. |
{ "firstName": "John", "lastName": "Smith", "age": 25 } |
String | JSON string | "StrongLoop" |
In general, a property will have undefined
value if no explicit or default value is provided.
Important:
The type name is case-insensitive; so for example you can use either “Number” or “number”.
Array types
LoopBack supports array types as follows:
{emails: [String]}
{"emails": ["string"]}
{"emails": [{"type": "string", "length": 64}]}
{"emails": "array"}
(a shorthand notation for{"emails": ["any"]}
)
Array of objects
Define an array of objects as follows (for example):
...
"Address": {
"type": [
"object"
],
"required": true
}
...
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 DataSource = require('loopback-datasource-juggler').DataSource;
var ds = new DataSource('memory');
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);
Important:
The user model has to reference the Address constructor or the model name - 'Address'
.