UNPKG

4.86 kBMarkdownView Raw
1# @loopback/repository
2
3This module provides a common set of interfaces for interacting with databases.
4
5## Overview
6
7This module provides data access facilities to various databases and services as
8well as the constructs for modeling and accessing those data.
9
10## Installation
11
12```sh
13npm install --save @loopback/repository
14```
15
16## Basic use
17
18At the moment, we only have implementations of `Repository` based on LoopBack
193.x `loopback-datasource-juggler` and connectors. The following steps illustrate
20how to define repositories and use them with controllers.
21
22### Defining a legacy datasource and a model
23
24The repository module provides APIs to define LoopBack 3.x data sources and
25models. For example,
26
27```ts
28// src/datasources/db.datasource.ts
29import {juggler} from '@loopback/repository';
30
31export const db: juggler.DataSource = new juggler.DataSource({
32 name: 'db',
33 connector: 'memory',
34});
35```
36
37```ts
38// src/models/note.model.ts
39import {model, Entity, property} from '@loopback/repository';
40
41@model()
42export class Note extends Entity {
43 @property({id: true})
44 id: string;
45 @property()
46 title: string;
47 @property()
48 content: string;
49}
50
51export interface NoteRelations {
52 // describe navigational properties here
53}
54
55export type NoteWithRelations = Note & NoteRelations;
56```
57
58**NOTE**: There is no declarative support for data source and model yet in
59LoopBack 4. These constructs need to be created programmatically as illustrated
60above.
61
62### Defining a repository
63
64A repository can be created by extending `DefaultCrudRepository` and using
65dependency injection to resolve the datasource.
66
67```ts
68// src/repositories/note.repository.ts
69import {DefaultCrudRepository, DataSourceType} from '@loopback/repository';
70import {Note, NoteRelations} from '../models';
71import {inject} from '@loopback/core';
72
73export class NoteRepository extends DefaultCrudRepository<
74 Note,
75 typeof Note.prototype.id,
76 NoteRelations
77> {
78 constructor(@inject('datasources.db') protected dataSource: DataSourceType) {
79 super(Note, dataSource);
80 }
81}
82```
83
84### Defining a controller
85
86Controllers serve as handlers for API requests. We declare controllers as
87classes with optional dependency injection by decorating constructor parameters
88or properties.
89
90```ts
91// src/controllers/note.controller.ts
92import {repository} from '@loopback/repository';
93import {NoteRepository} from '../repositories';
94import {Note} from '../models';
95import {post, requestBody, get, param} from '@loopback/rest';
96
97export class NoteController {
98 constructor(
99 // Use constructor dependency injection to set up the repository
100 @repository(NoteRepository) public noteRepo: NoteRepository,
101 ) {}
102
103 // Create a new note
104 @post('/note')
105 create(@requestBody() data: Note) {
106 return this.noteRepo.create(data);
107 }
108
109 // Find notes by title
110 @get('/note/{title}')
111 findByTitle(@param.path.string('title') title: string) {
112 return this.noteRepo.find({where: {title}});
113 }
114}
115```
116
117### Run the controller and repository together
118
119#### Using the Repository Mixin for Application
120
121A Repository Mixin is available for Application that provides convenience
122methods for binding and instantiating a repository class. Bound instances can be
123used anywhere in your application using Dependency Injection. The
124`.repository(RepositoryClass)` function can be used to bind a repository class
125to an Application. The mixin will also instantiate any repositories declared by
126a component in its constructor using the `repositories` key.
127
128Repositories will be bound to the key `repositories.RepositoryClass` where
129`RepositoryClass` is the name of the Repository class being bound.
130
131We'll use `BootMixin` on top of `RepositoryMixin` so that Repository bindings
132can be taken care of automatically at boot time before the application starts.
133
134```ts
135import {BootMixin} from '@loopback/boot';
136import {ApplicationConfig} from '@loopback/core';
137import {RepositoryMixin} from '@loopback/repository';
138import {RestApplication} from '@loopback/rest';
139import {db} from './datasources/db.datasource';
140
141export class RepoApplication extends BootMixin(
142 RepositoryMixin(RestApplication),
143) {
144 constructor(options?: ApplicationConfig) {
145 super(options);
146 this.projectRoot = __dirname;
147 this.dataSource(db);
148 }
149}
150```
151
152## Related resources
153
154- <https://martinfowler.com/eaaCatalog/repository.html>
155- <https://msdn.microsoft.com/en-us/library/ff649690.aspx>
156- <http://docs.spring.io/spring-data/data-commons/docs/2.0.0.M3/reference/html/#repositories>
157
158## Contributions
159
160- [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
161- [Join the team](https://github.com/loopbackio/loopback-next/issues/110)
162
163## Tests
164
165Run `npm test` from the root folder.
166
167## Contributors
168
169See
170[all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
171
172## License
173
174MIT