Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Set the following keys in the object passed to the createModel() method to define the model:

NameRequiredDescription
fieldstrueAn object that represents the model’s schema defined as key-value pairs. The key is the name of the field and the value is the fields object. See the next table for details.
connectortrueConnector to which the model is bound (string). Each model can only have one connector. Connectors are responsible for reading and writing data from/to their data source.
documentedfalseDetermines whether to generate API documentation (true) or not (false). The default value is true.
metadatafalseUsed to provide connector specific configuration (e.g.for example, mapping the model to a specific database table for the MySQL connector or defining the join properties).
autogenfalseUsed to determine whether to generate API endpoints directly from the model. The default value is true. If the endpoint is auto-generated, you do not need to create an API endpoint definition.
actionsfalseAn array of data operations supported by the model. The valid values are: ‘create’ create, ‘read’ read, ‘update’ update, and ‘delete’ delete. By default, all are supported by the model.
pluralfalseA string used as the property name when your API endpoint returns an array. By default, the plural value is the plural of the model name. For example, if your model is named car, the default plural would be cars. Note: this value can be set on an API or a model.
singularfalseA string used as the property name when your API endpoint returns a single record. By default, the singular value is the name of the model. Note: this value can be set on an API or a model.
beforefalseOne or more blocks to be executed before the request. Blocks are referenced by their name property. If you want to execute multiple blocks, you should specify them as an array of block names. If multiple blocks are specified, they are executed in the order specified.
afterfalseOne or more blocks to be executed after the request. Blocks are referenced by their name property. If you want to execute multiple blocks, you should specify them as an array of block names. If multiple blocks are specified, they are executed in the order specified.

...

The fields property (mentioned above) supports a number of sub-properties as well. The table below outlines these properties.

NameRequiredDescription
typetrueThe field primitive type plus others (e.g., ‘string’, 'number', 'boolean', 'object', 'array', 'date'for example, string, number, boolean, object, array, date). Type can be any valid JavaScript primitive type. Type can be specified as a string (e.g., ‘string’for example, string) or by the type class (e.g.for example, String).
requiredfalseSpecifies whether the field is required. The default value is false.
validatorfalseA function or regular expression that validates the value of the field. The function is passed the data to validate and should return either null or undefined if the validation succeeds. Any other return value means the validation failed, and the return value will be used in the exception message. If a regular expression is used, it should evaluate to either true or false.
namefalseUsed if the model field name is different than the field name in the connector’s model or the underlying data source for the field name. For example, if my model field is first_name and the column in a MySQL database is fname, the value of the name property should be fname.
defaultfalseThe default value for the field.
descriptionfalseThe description of the field (used for API documentation).
readonlyfalseEither true or false. If true, the field will be read-only and any attempt to write the field value will fail.
maxlengthfalseThe max length of the field (specified as an integer)
getfalseA function used to set the value of a property that will be sent to the client. This property is useful if you want to define a custom field where the value is derived.
setfalseA function used to set the value of a property that will be sent to the connector.
customfalseThis property should be specified and set to true if you are defining a custom field. A custom field is one that does not exist in the underlying data source for the connector you specified.
modelfalseModel name of the field property. This is either the logical name of a custom model or a connector model name in the form connector/model_name (e.g.for example, appc.mysql/employee)

Model schema example

The example below creates the car model with the specified schema. The car models will be stored in Mobile Backend Services as CustomObjects. Since the autogen property was not set to false, API Builder automatically generates the pre-defined endpoints for the client to access the car models using the <SEVER_ADDRESS>/api/car endpoints.

...