Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support MikroORM v6 #153

Merged
merged 14 commits into from
May 19, 2024
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module.exports = {
},
plugins: [
'@typescript-eslint',
'import',
],
rules: {
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-duplicate-imports': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down Expand Up @@ -64,6 +64,7 @@ module.exports = {
named: 'never',
asyncArrow: 'always',
}],
'import/no-duplicates': ['error', { 'prefer-inline': true }],
'arrow-parens': ['error', 'as-needed'],
'no-var': 'error',
'prefer-const': 'error',
Expand Down Expand Up @@ -124,10 +125,11 @@ module.exports = {
'comma-dangle': ['error', 'always-multiline'],
'dot-notation': 'error',
'eol-last': 'error',
'eqeqeq': ['error', 'always', {"null": "ignore"}],
'eqeqeq': ['error', 'always', { "null": "ignore" }],
'no-console': 'error',
'no-duplicate-imports': 'off',
'no-multiple-empty-lines': 'error',
'no-multi-spaces': 'error',
'no-throw-literal': 'error',
'no-trailing-spaces': 'error',
'no-undef-init': 'error',
Expand Down
45 changes: 23 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mikro-orm/nestjs",
"version": "5.2.3",
"version": "6.0.0",
"license": "MIT",
"author": {
"name": "Martin Adamek",
Expand Down Expand Up @@ -40,30 +40,31 @@
"lint": "eslint src/**/*.ts"
},
"peerDependencies": {
"@mikro-orm/core": "^5.0.0 || ^6.0.0-dev.0",
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0"
"@mikro-orm/core": "^6.0.0",
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0"
},
"devDependencies": {
"@mikro-orm/core": "^5.7.12",
"@mikro-orm/sqlite": "^5.7.12",
"@nestjs/common": "^9.4.3",
"@nestjs/core": "^9.4.3",
"@nestjs/platform-express": "^9.4.3",
"@nestjs/testing": "^9.4.3",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "~5.61.0",
"@typescript-eslint/parser": "~5.61.0",
"conventional-changelog": "^5.0.0",
"conventional-changelog-cli": "^4.0.0",
"eslint": "^8.43.0",
"jest": "^29.5.0",
"@mikro-orm/core": "^6.2.7",
"@mikro-orm/sqlite": "^6.2.7",
"@nestjs/common": "^10.3.8",
"@nestjs/core": "^10.3.8",
"@nestjs/platform-express": "^10.3.8",
"@nestjs/testing": "^10.3.8",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "~7.9.0",
"@typescript-eslint/parser": "~7.9.0",
"conventional-changelog": "^6.0.0",
"conventional-changelog-cli": "^5.0.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"jest": "^29.7.0",
"rxjs": "^7.8.1",
"supertest": "^7.0.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "5.4.5"
},
"commitlint": {
Expand Down Expand Up @@ -102,7 +103,7 @@
]
},
"engines": {
"node": ">= 14.0.0"
"node": ">= 18.12.0"
},
"packageManager": "[email protected]"
}
15 changes: 6 additions & 9 deletions src/mikro-orm-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Dictionary } from '@mikro-orm/core';
import { Configuration, ConfigurationLoader, EntityManager, MikroORM } from '@mikro-orm/core';
import type { DynamicModule, MiddlewareConsumer, OnApplicationShutdown, Type } from '@nestjs/common';
import { Global, Inject, Module, RequestMethod } from '@nestjs/common';
import { Configuration, ConfigurationLoader, EntityManager, MikroORM, type Dictionary } from '@mikro-orm/core';
import { Global, Inject, Module, RequestMethod, type DynamicModule, type MiddlewareConsumer, type OnApplicationShutdown, type Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';

import { CONTEXT_NAMES, getEntityManagerToken, getMikroORMToken, MIKRO_ORM_MODULE_OPTIONS } from './mikro-orm.common';
import { createAsyncProviders, createEntityManagerProvider, createMikroOrmProvider } from './mikro-orm.providers';
import type { MikroOrmModuleAsyncOptions, MikroOrmModuleSyncOptions } from './typings';
import { MikroOrmModuleOptions } from './typings';
import { MikroOrmMiddleware } from './mikro-orm.middleware';
import { forRoutesPath } from './middleware.helper';
import { CONTEXT_NAMES, getEntityManagerToken, getMikroORMToken, MIKRO_ORM_MODULE_OPTIONS } from './mikro-orm.common';
import { MikroOrmEntitiesStorage } from './mikro-orm.entities.storage';
import { MikroOrmMiddleware } from './mikro-orm.middleware';
import { createAsyncProviders, createEntityManagerProvider, createMikroOrmProvider } from './mikro-orm.providers';
import { MikroOrmModuleOptions, type MikroOrmModuleAsyncOptions, type MikroOrmModuleSyncOptions } from './typings';

async function tryRequire(name: string): Promise<Dictionary | undefined> {
try {
Expand Down
7 changes: 3 additions & 4 deletions src/mikro-orm-middleware.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { MiddlewareConsumer } from '@nestjs/common';
import { Global, Inject, Module, RequestMethod } from '@nestjs/common';
import { Global, Inject, Module, RequestMethod, type MiddlewareConsumer } from '@nestjs/common';

import type { MikroORM } from '@mikro-orm/core';
import { forRoutesPath } from './middleware.helper';
import { CONTEXT_NAMES, getMikroORMToken, MIKRO_ORM_MODULE_OPTIONS } from './mikro-orm.common';
import { MultipleMikroOrmMiddleware } from './multiple-mikro-orm.middleware';
import { MikroOrmMiddlewareModuleOptions } from './typings';
import type { MikroORM } from '@mikro-orm/core';
import { forRoutesPath } from './middleware.helper';

@Global()
@Module({})
Expand Down
3 changes: 1 addition & 2 deletions src/mikro-orm.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MikroORM, RequestContext } from '@mikro-orm/core';
import type { NestMiddleware } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Injectable, type NestMiddleware } from '@nestjs/common';

@Injectable()
export class MikroOrmMiddleware implements NestMiddleware {
Expand Down
18 changes: 8 additions & 10 deletions src/mikro-orm.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import type { AnyEntity } from '@mikro-orm/core';
import { Utils } from '@mikro-orm/core';
import type { DynamicModule } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { createMikroOrmRepositoryProviders } from './mikro-orm.providers';
import { Utils, type AnyEntity } from '@mikro-orm/core';
import { Module, type DynamicModule } from '@nestjs/common';
import { MikroOrmCoreModule } from './mikro-orm-core.module';
import { MikroOrmMiddlewareModule } from './mikro-orm-middleware.module';
import { MikroOrmEntitiesStorage } from './mikro-orm.entities.storage';
import { createMikroOrmRepositoryProviders } from './mikro-orm.providers';
import type {
MikroOrmModuleAsyncOptions,
MikroOrmModuleSyncOptions,
EntityName,
MikroOrmMiddlewareModuleOptions,
MikroOrmModuleAsyncOptions,
MikroOrmModuleFeatureOptions,
EntityName,
MikroOrmModuleSyncOptions,
} from './typings';
import { MikroOrmMiddlewareModule } from './mikro-orm-middleware.module';
import { MikroOrmEntitiesStorage } from './mikro-orm.entities.storage';

@Module({})
export class MikroOrmModule {
Expand Down
16 changes: 7 additions & 9 deletions src/mikro-orm.providers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { getEntityManagerToken, getMikroORMToken, getRepositoryToken, logger, MIKRO_ORM_MODULE_OPTIONS } from './mikro-orm.common';
import type { AnyEntity } from '@mikro-orm/core';
import { ConfigurationLoader, EntityManager, MetadataStorage, MikroORM } from '@mikro-orm/core';
import { ConfigurationLoader, EntityManager, MetadataStorage, MikroORM, type AnyEntity, type EntityClass, type EntityClassGroup, type EntitySchema } from '@mikro-orm/core';
import { MIKRO_ORM_MODULE_OPTIONS, getEntityManagerToken, getMikroORMToken, getRepositoryToken, logger } from './mikro-orm.common';

import type { MikroOrmModuleAsyncOptions, MikroOrmModuleOptions, MikroOrmOptionsFactory, EntityName } from './typings';
import type { InjectionToken, Provider, Type } from '@nestjs/common';
import { Scope } from '@nestjs/common';
import { Scope, type InjectionToken, type Provider, type Type } from '@nestjs/common';
import { MikroOrmEntitiesStorage } from './mikro-orm.entities.storage';
import type { EntityName, MikroOrmModuleAsyncOptions, MikroOrmModuleOptions, MikroOrmOptionsFactory } from './typings';

export function createMikroOrmProvider(
contextName?: string,
Expand All @@ -17,8 +15,8 @@ export function createMikroOrmProvider(
options = { ...options };

if (options?.autoLoadEntities) {
options.entities = [...(options.entities || []), ...MikroOrmEntitiesStorage.getEntities(contextName)];
options.entitiesTs = [...(options.entitiesTs || []), ...MikroOrmEntitiesStorage.getEntities(contextName)];
options.entities = [...(options.entities || []), ...MikroOrmEntitiesStorage.getEntities(contextName)] as (string | EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema)[];
options.entitiesTs = [...(options.entitiesTs || []), ...MikroOrmEntitiesStorage.getEntities(contextName)] as (string | EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema)[];
delete options.autoLoadEntities;
}

Expand Down Expand Up @@ -105,7 +103,7 @@ export function createMikroOrmRepositoryProviders(entities: EntityName<AnyEntity

(entities || []).forEach(entity => {
const meta = metadata.find(meta => meta.class === entity);
const repository = (meta?.repository ?? meta?.customRepository) as unknown as (() => InjectionToken) | undefined;
const repository = meta?.repository as unknown as (() => InjectionToken) | undefined;

if (repository) {
providers.push({
Expand Down
6 changes: 2 additions & 4 deletions src/multiple-mikro-orm.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { MikroORM } from '@mikro-orm/core';
import { RequestContext } from '@mikro-orm/core';
import type { NestMiddleware } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { RequestContext, type MikroORM } from '@mikro-orm/core';
import { Inject, Injectable, type NestMiddleware } from '@nestjs/common';

@Injectable()
export class MultipleMikroOrmMiddleware implements NestMiddleware {
Expand Down
9 changes: 5 additions & 4 deletions tests/mikro-orm.middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Options } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
import type { INestApplication } from '@nestjs/common';
import {
Controller,
Expand All @@ -8,14 +11,12 @@ import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import { InjectMikroORM, MikroOrmModule } from '../src';
import type { Options } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/core';
import { Foo } from './entities/foo.entity';
import { Bar } from './entities/bar.entity';
import { Foo } from './entities/foo.entity';

const testOptions: Options = {
dbName: ':memory:',
type: 'sqlite',
driver: SqliteDriver,
baseDir: __dirname,
entities: ['entities'],
};
Expand Down
9 changes: 5 additions & 4 deletions tests/mikro-orm.module.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { EntityRepository, Options } from '@mikro-orm/core';
import { EntityManager, MikroORM } from '@mikro-orm/core';
import type { Options, EntityRepository } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
import { Inject, Logger, Module, Scope } from '@nestjs/common';
import { ContextIdFactory } from '@nestjs/core';
import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import type { MikroOrmOptionsFactory } from '../src';
import { CONTEXT_NAMES, getEntityManagerToken, getMikroORMToken, getRepositoryToken, MikroOrmModule } from '../src';
import { Foo } from './entities/foo.entity';
import { CONTEXT_NAMES, MikroOrmModule, getEntityManagerToken, getMikroORMToken, getRepositoryToken } from '../src';
import { Bar } from './entities/bar.entity';
import { Foo } from './entities/foo.entity';

const testOptions: Options = {
dbName: ':memory:',
type: 'sqlite',
driver: SqliteDriver,
baseDir: __dirname,
entities: ['entities'],
};
Expand Down
Loading