Chinese Documentation : LoopBack types

Overview

Various LoopBack methods accept type descriptions, for example remote methods and dataSource.createModel(). The following is a list of supported types.

TypeDescription

Example

nullJSON nullnull
BooleanJSON Booleantrue
NumberJSON number

3.1415

StringJSON 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 ]
DateJavaScript Date object

new Date("December 17, 2003 03:24:00");

BufferNode.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.

 

Icon

 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}]}

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 streetcitystate, 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);
Icon

The user model has to reference the Address constructor or the model name - 'Address'.