Skip to content

Commit

Permalink
mv code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
fangzhipeng committed Aug 23, 2020
1 parent cf7bd2f commit 1018a7e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
33 changes: 33 additions & 0 deletions lib/typeorm.custom.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {EntityClassOrSchema} from "./interfaces/entity-class-or-schema.type";
import {AbstractRepository, getMetadataArgsStorage, Repository} from "typeorm";


export function getCustomRepositoryEntity(entities: EntityClassOrSchema[]): Array<EntityClassOrSchema> {
let customRepositoryEntities = new Array<EntityClassOrSchema>();
for (let entity of entities) {
if (
entity instanceof Function &&
(entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository)
) {
const entityRepositoryMetadataArgs = getMetadataArgsStorage().entityRepositories.find(
(repository) => {
return (
repository.target ===
(entity instanceof Function
? entity
: (entity as any).constructor)
);
},
);
if (entityRepositoryMetadataArgs) {
if (
entities.indexOf(<EntityClassOrSchema>entityRepositoryMetadataArgs.entity) === -1
) {
customRepositoryEntities.push(<EntityClassOrSchema>entityRepositoryMetadataArgs.entity);
}
}
}
}
return customRepositoryEntities;
}
54 changes: 10 additions & 44 deletions lib/typeorm.module.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { DynamicModule, Module } from '@nestjs/common';
import {
AbstractRepository,
Connection,
ConnectionOptions,
EntitySchema,
getMetadataArgsStorage,
Repository,
} from 'typeorm';
import { EntitiesMetadataStorage } from './entities-metadata.storage';
import { EntityClassOrSchema } from './interfaces/entity-class-or-schema.type';
import {
TypeOrmModuleAsyncOptions,
TypeOrmModuleOptions,
} from './interfaces/typeorm-options.interface';
import { TypeOrmCoreModule } from './typeorm-core.module';
import { DEFAULT_CONNECTION_NAME } from './typeorm.constants';
import { createTypeOrmProviders } from './typeorm.providers';
import {DynamicModule, Module} from '@nestjs/common';
import {Connection, ConnectionOptions,} from 'typeorm';
import {EntitiesMetadataStorage} from './entities-metadata.storage';
import {EntityClassOrSchema} from './interfaces/entity-class-or-schema.type';
import {TypeOrmModuleAsyncOptions, TypeOrmModuleOptions,} from './interfaces/typeorm-options.interface';
import {TypeOrmCoreModule} from './typeorm-core.module';
import {DEFAULT_CONNECTION_NAME} from './typeorm.constants';
import {createTypeOrmProviders} from './typeorm.providers';
import {getCustomRepositoryEntity} from "./typeorm.custom.repository";

@Module({})
export class TypeOrmModule {
Expand All @@ -34,32 +25,7 @@ export class TypeOrmModule {
| string = DEFAULT_CONNECTION_NAME,
): DynamicModule {
const providers = createTypeOrmProviders(entities, connection);
let customRepositoryEntities = [];
for (let entity of entities) {
if (
entity instanceof Function &&
(entity.prototype instanceof Repository ||
entity.prototype instanceof AbstractRepository)
) {
const entityRepositoryMetadataArgs = getMetadataArgsStorage().entityRepositories.find(
(repository) => {
return (
repository.target ===
(entity instanceof Function
? entity
: (entity as any).constructor)
);
},
);
if (entityRepositoryMetadataArgs) {
if (
entities.indexOf(<any>entityRepositoryMetadataArgs.entity) === -1
) {
customRepositoryEntities.push(entityRepositoryMetadataArgs.entity);
}
}
}
}
let customRepositoryEntities = getCustomRepositoryEntity(entities);
EntitiesMetadataStorage.addEntitiesByConnection(connection, [
...entities,
...customRepositoryEntities,
Expand Down

0 comments on commit 1018a7e

Please sign in to comment.