Package Exports
- @mikro-orm/nestjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mikro-orm/nestjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Based on dario1985/nestjs-mikro-orm.
Description
The MikroORM module for NestJS.
π Quick Start
First install the module via yarn
or npm
and do not forget to install the database driver as well:
$ yarn add @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mongodb # for mongo
$ yarn add @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mysql # for mysql/mariadb
$ yarn add @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mariadb # for mysql/mariadb
$ yarn add @mikro-orm/core @mikro-orm/nestjs @mikro-orm/postgresql # for postgresql
$ yarn add @mikro-orm/core @mikro-orm/nestjs @mikro-orm/sqlite # for sqlite
or
$ npm i -s @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mongodb # for mongo
$ npm i -s @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mysql # for mysql/mariadb
$ npm i -s @mikro-orm/core @mikro-orm/nestjs @mikro-orm/mariadb # for mysql/mariadb
$ npm i -s @mikro-orm/core @mikro-orm/nestjs @mikro-orm/postgresql # for postgresql
$ npm i -s @mikro-orm/core @mikro-orm/nestjs @mikro-orm/sqlite # for sqlite
Once the installation process is completed, we can import the MikroOrmModule
into the root AppModule
.
@Module({
imports: [
MikroOrmModule.forRoot({
entities: ['../dist/entities'],
entitiesTs: ['../src/entities'],
dbName: 'my-db-name.sqlite3',
type: 'sqlite',
baseDir: __dirname,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
The forRoot()
method accepts the same configuration object as init()
from the MikroORM package.
You can also omit the parameter to use the CLI config.
Afterward, the EntityManager
will be available to inject across entire project (without importing any module elsewhere).
To define which repositories shall be registered in the current scope you can use the forFeature()
method. For example, in this way:
// photo.module.ts
@Module({
imports: [MikroOrmModule.forFeature([Photo])],
providers: [PhotoService],
controllers: [PhotoController],
})
export class PhotoModule {}
and import it into the root AppModule
:
// app.module.ts
@Module({
imports: [MikroOrmModule.forRoot(...), PhotoModule],
})
export class AppModule {}
In this way we can inject the PhotoRepository
to the PhotoService
using the @InjectRepository()
decorator:
@Injectable()
export class PhotoService {
constructor(
@InjectRepository(Photo)
private readonly photoRepository: EntityRepository<Photo>
) {}
// ...
}
Testing
The nestjs-mikro-orm
package exposes getRepositoryToken()
function that returns prepared token based on a given entity to allow mocking the repository.
@Module({
providers: [
PhotoService,
{
provide: getRepositoryToken(Photo),
useValue: mockedRepository,
},
],
})
export class PhotoModule {}
π€ Contributing
Contributions, issues and feature requests are welcome. Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.
Authors
π€ Dario Mancuso
- Github: @dario1985
π€ Martin AdΓ‘mek
See also the list of contributors who participated in this project.
Show Your Support
Please βοΈ this repository if this project helped you!
π License
Copyright Β© 2018 Martin AdΓ‘mek.
This project is licensed under the MIT License - see the LICENSE file for details.