@mikro-orm/nestjs v5.2.3
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.
4 months ago
7 months ago
8 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago