|
1 | 1 | # loopback4-sequelize |
2 | 2 |
|
3 | | -[-@2x.png)](http://loopback.io/) |
| 3 | +[-@2x.png>)](http://loopback.io/) |
4 | 4 |
|
5 | | -## Installation |
| 5 | +This is a loopback 4 extension that provides Sequelize powered repository methods which is more performant than juggler (the default ORM in Loopback) for relational databases. |
6 | 6 |
|
7 | | -Install LB4SequelizeComponent using `npm`; |
| 7 | +## Installation |
8 | 8 |
|
9 | 9 | ```sh |
10 | | -$ [npm install | yarn add] loopback4-sequelize |
| 10 | +npm install loopback4-sequelize |
11 | 11 | ``` |
12 | 12 |
|
13 | | -## Basic Use |
| 13 | +## Usage |
| 14 | + |
| 15 | +The extension can be used in new as well as existing projects. By just changing the parent classes in target Data Source and Repositories. |
| 16 | + |
| 17 | +### Step 1: Configure DataSource |
14 | 18 |
|
15 | | -Configure and load LB4SequelizeComponent in the application constructor |
16 | | -as shown below. |
| 19 | +Change the parent class from `juggler.DataSource` to `SequelizeDataSource` like below. |
17 | 20 |
|
18 | 21 | ```ts |
19 | | -import {LB4SequelizeComponent, LB4SequelizeComponentOptions, DEFAULT_LOOPBACK4_SEQUELIZE_OPTIONS} from 'loopback4-sequelize'; |
20 | 22 | // ... |
21 | | -export class MyApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) { |
22 | | - constructor(options: ApplicationConfig = {}) { |
23 | | - const opts: LB4SequelizeComponentOptions = DEFAULT_LOOPBACK4_SEQUELIZE_OPTIONS; |
24 | | - this.configure(LB4SequelizeComponentBindings.COMPONENT).to(opts); |
25 | | - // Put the configuration options here |
26 | | - }); |
27 | | - this.component(LB4SequelizeComponent); |
28 | | - // ... |
29 | | - } |
| 23 | +import {SequelizeDataSource} from 'loopback4-sequelize'; |
| 24 | + |
| 25 | +// ... |
| 26 | +export class PgDataSource |
| 27 | + extends SequelizeDataSource |
| 28 | + implements LifeCycleObserver { |
| 29 | + // ... |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### Step 2: Configure Repository |
| 34 | + |
| 35 | +Change the parent class from `DefaultCrudRepository` to `SequelizeRepository` like below. |
| 36 | + |
| 37 | +```ts |
| 38 | +// ... |
| 39 | +import {SequelizeRepository} from 'loopback4-sequelize'; |
| 40 | + |
| 41 | +export class YourRepository extends SequelizeRepository< |
| 42 | + YourModel, |
| 43 | + typeof YourModel.prototype.id, |
| 44 | + YourModelRelations |
| 45 | +> { |
30 | 46 | // ... |
31 | 47 | } |
32 | 48 | ``` |
0 commit comments