Tip: Read this first to understand how LoopBack works.
<figcaption>Model inheritance</figcaption></figure>
</div>
Models
Models are at the heart of LoopBack, and represent back-end data sources such as databases or other back end services (REST, SOAP, and so on). LoopBack models are JavaScript objects with both Node and REST APIs.
A key powerful feature of LoopBack is that when you define a model it automatically comes with a predefined REST API with a full set of create, read, update, and delete operations.
The Basic model object has methods for adding hooks and for validating data. Other model objects all “inherit from” it. Models have an inheritance hierarchy, as shown at right: When you attach a model to a persistent data source it becomes a connected model with create, retrieve, update, and delete operations. LoopBack’s built-in models inherit from it.
Built-in models
Every LoopBack application has a set of predefined built-in models such as User, Role, and Application, so you don’t have to create these common models from scratch.
Custom models
You can define your own custom models specific to your application. You can make your custom models extend built-in models to build on the predefined functionality of User, Application, and other built-in models.
You can create LoopBack models in various ways, depending on what kind of data source the model is based on. You can create models:
- With the LoopBack model generator.
- From an existing relational database using model discovery. Then you can keep your model synchronized with the database using LoopBack’s schema / model synchronization API.
- By instance introspection for free-form data in NoSQL databases or REST APIs.
All three of these methods create a Model definition JSON file that defines your model in LoopBack,
by convention in a LoopBack project’s common/models
directory; for example, common/models/account.json
.
You can also create and customize models programmatically using the LoopBack API, or by manually editing the model definition JSON file. In most cases, you shouldn’t need to use those techniques to create models, but you generally will use them to modify and customize models.
Note:
The Model definition JSON file includes an idInjection
property that indicates
whether LoopBack automatically adds a unique id
property to a model. For a model connected to a database, the id property corresponds to the primary key.
See ID properties for more information.
Model relations
You can express relationships between models, such as BelongsTo, HasMany, and HasAndBelongsToMany.
Model create, retrieve, update, and delete operations
When you connect a model to a persistent data source such as a database, it becomes a connected model with a full set of create, read, update, and delete operations from the PersistedModel class:
Operation | REST | LoopBack model method (Node API)* |
Corresponding SQL Operation |
---|---|---|---|
Create |
PUT /modelName
POST /modelName |
create()* |
INSERT |
Read (Retrieve) | GET /modelName?filter=... | find()* |
SELECT |
Update (Modify) |
POST /modelName PUT /modelName |
updateAll()* |
UPDATE |
Delete (Destroy) | DELETE /modelName/modelID | destroyById()* |
DELETE |
(*) Methods listed are just prominent examples; other methods may provide similar functionality; for example: findById()
, findOne()
, and findOrCreate()
.
See PersistedModel API documentation for more information.
Application logic
You can add custom application logic in several ways; you can:
- Add application logic to models through remote methods (custom REST endpoints), remote hooks that are triggered by remote methods, and operation hooks that are triggered by model create, retrieve, update, and delete methods.
- Add boot scripts that run when the application starts.
- Define custom middleware, similar to Express middleware.
You can add code to validate data before saving it to the model and back-end data store.
Middleware phases
Middleware refers to functions executed when HTTP requests are made to REST endpoints. Since LoopBack is based on Express, LoopBack middleware is the same as Express middleware. However, LoopBack adds the concept of phases, to clearly define the order in which middleware is called. Using phases helps to avoid ordering issues that can occur with standard Express middleware.
See Defining middleware for more information.