Models are at the heart of LoopBack: they encapsulate your application's data and how your application handles the data. LoopBack models automatically have both a Node API and a REST API; for a model backed by a data source, the APIs provide create, read, update, and delete (CRUD) capabilities. You can access the REST API during development with the API explorer. For more information, see Working with data.
The model editor enables you to quickly develop LoopBack models without having to write JSON or code.
Creating a new model
To create a new model either:
- Click + Add New Model in the navigation pane.
- Click + New under MODEL in the toolbox palette.
The model editor opens to edit the new model.
Editing a model
To edit an existing model, simply click on it in the Models folder of the navigation pane.
The model editor opens to edit the selected model.
You can edit several models at the same time, since each opens in a separate tab.
When you create a new model or edit an existing model as described above, you'll see the model editor:
The model editor has three main sections:
Model name in singular form, along with the Save and Migrate Model buttons (see Migrating a model below).
Model details: special properties that apply to the entire model.
Model properties: each model property has individual attributes.
Click + New Property to add a new property to the model.
Model details apply to the entire model:
- Plural form of the model - this is automatically set, but you can override it.
- Base model class - the class on which this model is based, typically either
PersistedModel(for models that will be connected to a persistent data source such as a databse). See LoopBack core concepts for an overivew of model inheritance.
- Datasource - Click in this field to attach a model to one of the data sources defined for the application. By default new models are attached to the "In-Memory" data source.
- Public - whether the model is exposed over REST API; see API explorer.
- Strict - determines whether or not the LoopBack enforces the schema defined by the model properties and ignores others added programmatically. If this is true, then only properties defined in the model are accepted. Use if you want to ensure the model accepts only predefined properties.
Each model has one or more properties, each with the following attributes:
- Name - See Valid names in LoopBack for rules.
- Type - Property data type: String, Number, and so on; see LoopBack types for more information.
- Is ID - whether the property is a unique identifier for the model. See ID properties for more information.
- Required - whether the property is required to have a value.
- Index - whether the property will be or is already indexed. See Indexes for more information.
- Comments - Information annotating the property.
Migrating a model
Click the Migrate Model button to create a database table for each model in your application, with a column for each model property. This is called LoopBack auto-migration.
The following data sources support auto-migration:
When your application is attached to one of the above data sources, the Migrate Model button will be enabled when you mouse over it. Click it to start the migration process.
For more information on auto-updating, see Creating a database schema from models.