loopback-typeorm
v1.0.1
Published
Easy way to use TypeORM datasource in Loopback application.
Downloads
6
Maintainers
Readme
loopback-typeorm
Installation
Install packages using npm
;
$ npm install loopback-typeorm reflect-metadata
Also you are required to install database drivers for your datasources. More info: https://typeorm.io/#installation
Basic Use
Describe your entities. More info: https://typeorm.io/#step-by-step-guide
Create observer in src/observers/my-typeorm.observer.ts
.
import {TypeOrmDataSource} from 'loopback-typeorm';
export class MyTypeOrmDataSource extends TypeOrmDataSource {}
If you have created your observer in different file like src/observers/**.observer.ts
, be sure to manually register the observer in the application instance(https://loopback.io/doc/en/lb4/Life-cycle.html#register-a-life-cycle-observer).
Bind TypeORM connection options of your application instance:
//application.ts file...
import {MyTypeOrmDataSource} from 'observers/my-typeorm.observer';
//...
const myDSOptions: DataSourceOptions = {
type: 'sqlite',
database: ':memory:',
dropSchema: false,
entities: [
//your entites...
],
synchronize: true,
logging: true,
};
this.bind(MyTypeOrmDataSource.OPTIONS_KEY).to(myDSOptions);
//app.bind(MyTypeOrmDataSource.OPTIONS_KEY).to(myDSOptions);
//...
Inject TypeORM datasource wherever you need using binding key MyTypeOrmDataSource.DATASOURCE_KEY
. Controller example:
//...
export class MyController {
constructor(
@inject(MyTypeOrmDataSource.DATASOURCE_KEY)
private myDatasource: DataSource,
) {}
//...
That's all. Now you are happy to play with TypeORM datasource ;)
Examples
You can find examples of using this package in the source folder /examples