Skip to content

Commit

Permalink
refactor(typeorm): Update getRepositoryToken provider generation
Browse files Browse the repository at this point in the history
This change will make Repository generation use getEntityManagerToken instead of getConnectionToken

closes #1819
  • Loading branch information
edeesis committed Nov 27, 2023
1 parent b4e09dc commit 026a183
Showing 1 changed file with 10 additions and 10 deletions.
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 enitityMetadata = dataSource.entityMetadatas.find((meta) => meta.target === entity)
useFactory: (entityManager: EntityManager) => {
const enitityMetadata = entityManager.connection.entityMetadatas.find((meta) => meta.target === entity)
const isTreeEntity = typeof enitityMetadata?.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

0 comments on commit 026a183

Please sign in to comment.