|
| 1 | +import { FactoryProvider } from '@nestjs/common'; |
| 2 | +import { getDataSourceToken } from '@nestjs/typeorm'; |
| 3 | +import { |
| 4 | + EntityParam, |
| 5 | + CheckRelationName, |
| 6 | + FindOneRowEntity, |
| 7 | + RunInTransaction, |
| 8 | + PrepareParams, |
| 9 | + FIND_ONE_ROW_ENTITY, |
| 10 | + CHECK_RELATION_NAME, |
| 11 | + RUN_IN_TRANSACTION_FUNCTION, |
| 12 | + ORM_SERVICE, |
| 13 | + MODULE_OPTIONS_TOKEN, |
| 14 | + ENTITY_PARAM_MAP, |
| 15 | +} from '@klerick/json-api-nestjs'; |
| 16 | +import { getEntityName } from '@klerick/json-api-nestjs-shared'; |
| 17 | + |
| 18 | +import { kebabCase } from 'change-case-commonjs'; |
| 19 | +import { DataSource, EntityManager, EntityTarget, Repository } from 'typeorm'; |
| 20 | + |
| 21 | +import { |
| 22 | + CURRENT_DATA_SOURCE_TOKEN, |
| 23 | + CURRENT_ENTITY_MANAGER_TOKEN, |
| 24 | + CURRENT_ENTITY_REPOSITORY, |
| 25 | +} from '../constants'; |
| 26 | + |
| 27 | +import { TypeOrmService, TypeormUtilsService } from '../service'; |
| 28 | + |
| 29 | +import { TypeOrmParam } from '../type'; |
| 30 | + |
| 31 | +import { |
| 32 | + getProps, |
| 33 | + getRelation, |
| 34 | + getPropsType, |
| 35 | + getPropsNullable, |
| 36 | + getPrimaryColumnName, |
| 37 | + getPrimaryColumnType, |
| 38 | + getRelationProperty, |
| 39 | + getArrayType, |
| 40 | +} from '../orm-helper'; |
| 41 | +import { EntityClass } from '@mikro-orm/core'; |
| 42 | + |
| 43 | +export function CurrentDataSourceProvider( |
| 44 | + connectionName?: string |
| 45 | +): FactoryProvider<DataSource> { |
| 46 | + return { |
| 47 | + provide: CURRENT_DATA_SOURCE_TOKEN, |
| 48 | + useFactory: (dataSource: DataSource) => dataSource, |
| 49 | + inject: [getDataSourceToken(connectionName)], |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +export function CurrentEntityManager(): FactoryProvider<EntityManager> { |
| 54 | + return { |
| 55 | + provide: CURRENT_ENTITY_MANAGER_TOKEN, |
| 56 | + useFactory: (dataSource: DataSource) => dataSource.manager, |
| 57 | + inject: [CURRENT_DATA_SOURCE_TOKEN], |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +export function CurrentEntityRepository<E extends object>( |
| 62 | + entity: E |
| 63 | +): FactoryProvider<Repository<E>> { |
| 64 | + return { |
| 65 | + provide: CURRENT_ENTITY_REPOSITORY, |
| 66 | + useFactory: (entityManager: EntityManager) => |
| 67 | + entityManager.getRepository(entity as unknown as EntityTarget<E>), |
| 68 | + inject: [CURRENT_ENTITY_MANAGER_TOKEN], |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export function EntityPropsMap<E extends object>(entities: EntityClass<E>[]) { |
| 73 | + return { |
| 74 | + provide: ENTITY_PARAM_MAP, |
| 75 | + inject: [CURRENT_ENTITY_MANAGER_TOKEN], |
| 76 | + useFactory: (entityManager: EntityManager) => { |
| 77 | + const mapProperty = new Map<EntityClass<E>, EntityParam<E>>(); |
| 78 | + |
| 79 | + for (const item of entities) { |
| 80 | + const entityRepo = entityManager.getRepository<E>(item); |
| 81 | + |
| 82 | + const className = getEntityName(item); |
| 83 | + mapProperty.set(item, { |
| 84 | + props: getProps(entityRepo), |
| 85 | + propsType: getPropsType(entityRepo), |
| 86 | + propsNullable: getPropsNullable(entityRepo), |
| 87 | + primaryColumnName: getPrimaryColumnName(entityRepo), |
| 88 | + primaryColumnType: getPrimaryColumnType(entityRepo), |
| 89 | + propsArrayType: getArrayType(entityRepo), |
| 90 | + typeName: kebabCase(className), |
| 91 | + className: className, |
| 92 | + relations: getRelation(entityRepo), |
| 93 | + relationProperty: getRelationProperty(entityRepo), |
| 94 | + }); |
| 95 | + } |
| 96 | + return mapProperty; |
| 97 | + }, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +export function FindOneRowEntityFactory<E extends object>(): FactoryProvider< |
| 102 | + FindOneRowEntity<E> |
| 103 | +> { |
| 104 | + return { |
| 105 | + provide: FIND_ONE_ROW_ENTITY, |
| 106 | + inject: [CURRENT_ENTITY_REPOSITORY, TypeormUtilsService], |
| 107 | + useFactory: ( |
| 108 | + repository: Repository<E>, |
| 109 | + typeormUtilsService: TypeormUtilsService<E> |
| 110 | + ) => { |
| 111 | + return async (entity, value) => { |
| 112 | + const params = 'params'; |
| 113 | + return await repository |
| 114 | + .createQueryBuilder(typeormUtilsService.currentAlias) |
| 115 | + .where( |
| 116 | + `${typeormUtilsService.getAliasPath( |
| 117 | + typeormUtilsService.currentPrimaryColumn |
| 118 | + )} = :${params}` |
| 119 | + ) |
| 120 | + .setParameters({ |
| 121 | + [params]: value, |
| 122 | + }) |
| 123 | + .getOne(); |
| 124 | + }; |
| 125 | + }, |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +export function CheckRelationNameFactory< |
| 130 | + E extends object, |
| 131 | + IdKey extends string = 'id' |
| 132 | +>(): FactoryProvider<CheckRelationName<E>> { |
| 133 | + return { |
| 134 | + provide: CHECK_RELATION_NAME, |
| 135 | + inject: [TypeormUtilsService], |
| 136 | + useFactory(typeormUtilsService: TypeormUtilsService<E, IdKey>) { |
| 137 | + return (entity, value) => |
| 138 | + !!(typeormUtilsService.relationFields as any).find( |
| 139 | + (i: any) => i === value |
| 140 | + ); |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +export function RunInTransactionFactory(): FactoryProvider<RunInTransaction> { |
| 146 | + return { |
| 147 | + provide: RUN_IN_TRANSACTION_FUNCTION, |
| 148 | + inject: [MODULE_OPTIONS_TOKEN, CURRENT_DATA_SOURCE_TOKEN], |
| 149 | + useFactory(options: PrepareParams<TypeOrmParam>, dataSource: DataSource) { |
| 150 | + const { |
| 151 | + options: { runInTransaction }, |
| 152 | + } = options; |
| 153 | + |
| 154 | + if (runInTransaction && typeof runInTransaction === 'function') { |
| 155 | + return (callback) => |
| 156 | + runInTransaction('READ COMMITTED', () => callback()); |
| 157 | + } |
| 158 | + |
| 159 | + return async (callback) => { |
| 160 | + const queryRunner = dataSource.createQueryRunner(); |
| 161 | + await queryRunner.startTransaction('READ UNCOMMITTED'); |
| 162 | + let result: unknown; |
| 163 | + try { |
| 164 | + result = await callback(); |
| 165 | + await queryRunner.commitTransaction(); |
| 166 | + } catch (e) { |
| 167 | + await queryRunner.rollbackTransaction(); |
| 168 | + throw e; |
| 169 | + } finally { |
| 170 | + await queryRunner.release(); |
| 171 | + } |
| 172 | + return result; |
| 173 | + }; |
| 174 | + }, |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +export function OrmServiceFactory() { |
| 179 | + return { |
| 180 | + provide: ORM_SERVICE, |
| 181 | + useClass: TypeOrmService, |
| 182 | + }; |
| 183 | +} |
0 commit comments