@nestjs-crud/mikro-orm
v2.2.6
Published
NestJS CRUD MikroORM adapter
Downloads
65
Maintainers
Readme
The API follows the same patterns as TypeORM. See the MikroORM service docs for full MikroORM adapter API.
Node 22+ required. This package depends on
@mikro-orm/core ^7.0.0(peer-dep, bumped in v2.0.0 from v6) which uses pure ESM. Run tests viayarn test:mikro-ormonly — barenpx jest packages/mikro-orm/test/...fails withSyntaxError: Cannot use 'import.meta' outside a module. See CONTRIBUTING.md for the full ESM caveat.
Install
npm i @nestjs-crud/mikro-orm @mikro-orm/core @mikro-orm/knexUsage
Assume you have a MikroORM entity:
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
@Entity()
export class Company {
@PrimaryKey()
id!: number;
@Property()
name!: string;
}Then create a service:
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { EntityRepository } from '@mikro-orm/core';
import { MikroOrmCrudService } from '@nestjs-crud/mikro-orm';
import { Company } from './company.entity';
@Injectable()
export class CompaniesService extends MikroOrmCrudService<Company> {
constructor(@InjectRepository(Company) public companiesRepo: EntityRepository<Company>) {
super(companiesRepo, Company);
}
}The constructor accepts EntityManager | EntityRepository<T> (v2.2.0+). super(companiesRepo, Company) unwraps via repo.getEntityManager() internally — resulting em is the same ALS-backed proxy MikroORM injects, so request-scope identity-map isolation is preserved. To pass EntityManager directly: constructor(em: EntityManager) { super(em, Company); } — same behavior.
Then provide your service in a controller:
import { Controller } from '@nestjs/common';
import { Crud, CrudController } from '@nestjs-crud/core';
import { Company } from './company.entity';
import { CompaniesService } from './companies.service';
@Crud({
model: {
type: Company,
},
})
@Controller('companies')
export class CompaniesController implements CrudController<Company> {
constructor(public service: CompaniesService) {}
}See also
- Wiki: ServiceMikroOrm — full MikroORM adapter API
- Wiki: Logging — optional
LoggerServiceconstructor parameter (v2.0.0) - Wiki: Caching —
@Crud({ query: { cache } })honored viaMikroOrmCacheStrategy(v2.2.0+) - v2 Migration guide — including the typed public method signatures migration
