Socket
Socket
Sign inDemoInstall

nestjs-mikro-orm

Package Overview
Dependencies
381
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nestjs-mikro-orm

NestJS module for MikroORM


Version published
Weekly downloads
44
increased by57.14%
Maintainers
1
Install size
22.2 kB
Created
Weekly downloads
 

Readme

Source

Nest Logo
MikroORM


NPM version Open issues Downloads

Description

The MikroORM module for Nest.

Installation

$ npm i --save mikro-orm nestjs-mikro-orm

Quick Start

Once the installation process is completed, we can import the MikroOrmModule into the root ApplicationModule.

@Module({
  imports: [
    MikroOrmModule.forRoot({
      entitiesDirs: ['../dist/entities'],
      entitiesDirsTs: ['../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.

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({ entities: [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<IEntityType<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

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Keywords

FAQs

Last updated on 02 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc