mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
chore(deps): update typeorm
This commit is contained in:
parent
9900bc641d
commit
ef84800ce8
22 changed files with 232 additions and 242 deletions
4
src/database/entity-repository/README.md
Normal file
4
src/database/entity-repository/README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
This module restores the classes-based custom Repository functionality from
|
||||
typeorm v0.2.
|
||||
|
||||
Directly adapted from anchan828: https://gist.github.com/anchan828/9e569f076e7bc18daf21c652f7c3d012
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import { SetMetadata } from "@nestjs/common";
|
||||
|
||||
export const TYPEORM_ENTITY_REPOSITORY = "TYPEORM_ENTITY_REPOSITORY";
|
||||
|
||||
export function EntityRepository(entity: Function): ClassDecorator {
|
||||
return SetMetadata(TYPEORM_ENTITY_REPOSITORY, entity);
|
||||
}
|
||||
3
src/database/entity-repository/index.ts
Normal file
3
src/database/entity-repository/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { EntityRepository } from "./entity-repository.decorator";
|
||||
|
||||
export { EntityRepository };
|
||||
39
src/database/entity-repository/typeorm-repository.module.ts
Normal file
39
src/database/entity-repository/typeorm-repository.module.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { DynamicModule, Provider } from "@nestjs/common";
|
||||
import { getDataSourceToken } from "@nestjs/typeorm";
|
||||
import { DataSource } from "typeorm";
|
||||
import { TYPEORM_ENTITY_REPOSITORY } from "./entity-repository.decorator";
|
||||
|
||||
export class TypeOrmRepositoryModule {
|
||||
public static for<T extends new (...args: any[]) => any>(
|
||||
repositories: T[]
|
||||
): DynamicModule {
|
||||
const providers: Provider[] = [];
|
||||
|
||||
for (const repository of repositories) {
|
||||
const entity = Reflect.getMetadata(TYPEORM_ENTITY_REPOSITORY, repository);
|
||||
|
||||
if (!entity) {
|
||||
continue;
|
||||
}
|
||||
|
||||
providers.push({
|
||||
inject: [getDataSourceToken()],
|
||||
provide: repository,
|
||||
useFactory: (dataSource: DataSource): typeof repository => {
|
||||
const baseRepository = dataSource.getRepository<any>(entity);
|
||||
return new repository(
|
||||
baseRepository.target,
|
||||
baseRepository.manager,
|
||||
baseRepository.queryRunner
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
exports: providers,
|
||||
module: TypeOrmRepositoryModule,
|
||||
providers,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue