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: Change getRepositoryToken provider generation to use getEntityManagerToken instead of getConnectionToken #1820

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/typeorm.providers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Provider } from '@nestjs/common';
import { DataSource, DataSourceOptions, getMetadataArgsStorage } from 'typeorm';
import { getDataSourceToken, getRepositoryToken } from './common/typeorm.utils';
import { DataSource, DataSourceOptions, EntityManager, getMetadataArgsStorage } from 'typeorm';
import { getDataSourceToken, getEntityManagerToken, getRepositoryToken } from './common/typeorm.utils';
import { EntityClassOrSchema } from './interfaces/entity-class-or-schema.type';

export function createTypeOrmProviders(
Expand All @@ -9,16 +9,16 @@ export function createTypeOrmProviders(
): Provider[] {
return (entities || []).map((entity) => ({
provide: getRepositoryToken(entity, dataSource),
useFactory: (dataSource: DataSource) => {
const entityMetadata = dataSource.entityMetadatas.find((meta) => meta.target === entity)
useFactory: (entityManager: EntityManager) => {
const entityMetadata = entityManager.connection.entityMetadatas.find((meta) => meta.target === entity)
const isTreeEntity = typeof entityMetadata?.treeType !== 'undefined'
return isTreeEntity
? dataSource.getTreeRepository(entity)
: dataSource.options.type === 'mongodb'
? dataSource.getMongoRepository(entity)
: dataSource.getRepository(entity);
return isTreeEntity
? entityManager.getTreeRepository(entity)
: entityManager.connection.options.type === 'mongodb'
? entityManager.getMongoRepository(entity)
: entityManager.getRepository(entity);
},
inject: [getDataSourceToken(dataSource)],
inject: [getEntityManagerToken(dataSource)],
/**
* Extra property to workaround dynamic modules serialisation issue
* that occurs when "TypeOrm#forFeature()" method is called with the same number
Expand Down