UNPKG

undeexcepturi

Version:

TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.

56 lines (45 loc) 1.71 kB
--- title: Using MikroORM with NestJS framework sidebar_label: Usage with NestJS --- ## Installation Easiest way to integrate MikroORM to Nest is via [`nestjs-mikro-orm` module](https://github.com/dario1985/nestjs-mikro-orm). Simply install it next to Nest, MikroORM and underlying driver: ``` $ yarn add mikro-orm nestjs-mikro-orm mongodb # for mongo $ yarn add mikro-orm nestjs-mikro-orm mysql2 # for mysql $ yarn add mikro-orm nestjs-mikro-orm pg # for postgre $ yarn add mikro-orm nestjs-mikro-orm sqlite3 # for sqlite ``` or ``` $ npm i -s mikro-orm nestjs-mikro-orm mongodb # for mongo $ npm i -s mikro-orm nestjs-mikro-orm mysql2 # for mysql $ npm i -s mikro-orm nestjs-mikro-orm pg # for postgre $ npm i -s mikro-orm nestjs-mikro-orm sqlite3 # for sqlite ``` Then import the `MikroOrmModule` in your top level module (usually called `AppModule`) via `forRoot()`, which will register `MikroORM` and `EntityManager` services. It will also create the request context for you automatically. ```typescript @Module({ imports: [ MikroOrmModule.forRoot({ entitiesDirs: ['dist/entities'], entitiesDirsTs: ['src/entities'], dbName: 'my-db-name.sqlite3', type: 'sqlite', autoFlush: false, // read more here: https://mikro-orm.io/unit-of-work/ }), // ... your feature modules ], }) export class AppModule {} ``` Then use `forFeature()` to register entity repositories at feature module level: ```typescript @Module({ imports: [MikroOrmModule.forFeature({ entities: [Photo] })], providers: [PhotoService], controllers: [PhotoController], }) export class PhotoModule {} ``` > Don't forget to import the feature module in your top level module.