The repository pattern is one of the more fundamental differences between LoopBack 3 and 4. In LoopBack 3, you would use the model class definitions themselves to perform CRUD operations. In LoopBack 4, the layer responsible for this has been separated from the definition of the model itself, into the repository layer.
Repository represents a specialized
Service interface that provides
strong-typed data access (for example, CRUD) operations of a domain model
against the underlying database or service.
For more information about Repositories, see Repositories.
Create your repository
From inside the project folder, run the
lb4 repository command to create a
repository for your to-do model using the
db datasource from the previous
db datasource shows up by its class name
DbDataSource from the
list of available datasources.
lb4 repository ? Please select the datasource DbDatasource ? Select the model(s) you want to generate a repository Todo ? Please select the repository base class DefaultCrudRepository (Juggler bridge) create src/repositories/todo.repository.ts update src/repositories/index.ts Repository TodoRepository was created in src/repositories/
src/repositories/index.ts file makes exporting artifacts central and also
easier to import.
The newly created
todo.repository.ts class has the necessary connections that
are needed to perform CRUD operations for our to-do model. It leverages the Todo
model definition and ‘db’ datasource configuration and retrieves the datasource
To view the completed file, see the
Now we can expose the
Todo API through the
Previous step: Add a datasource
Next step: Add a controller