From 294aa05cdc5d0b509e8ab48c08a10ba71dc958ae Mon Sep 17 00:00:00 2001 From: alexeh Date: Sun, 29 Sep 2024 10:03:13 +0200 Subject: [PATCH] add custom repository to handler insert with upsert --- api/src/modules/config/app-config.service.ts | 8 ++++- api/src/modules/data/data.module.ts | 8 ++++- .../modules/data/ecosystem-data.repository.ts | 29 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 api/src/modules/data/ecosystem-data.repository.ts diff --git a/api/src/modules/config/app-config.service.ts b/api/src/modules/config/app-config.service.ts index be4eb247..106f3b7f 100644 --- a/api/src/modules/config/app-config.service.ts +++ b/api/src/modules/config/app-config.service.ts @@ -4,6 +4,8 @@ import { COMMON_DATABASE_ENTITIES } from '@shared/entities/database.entities'; import { ApiEventsEntity } from '@api/modules/events/api-events/api-events.entity'; import { TOKEN_TYPE_ENUM } from '@shared/schemas/auth/token-type.schema'; import { JwtConfigHandler } from '@api/modules/config/auth-config.handler'; +import { EcoSystemDataRepository } from '@api/modules/data/ecosystem-data.repository'; +import { EcosystemProject } from '@api/modules/data/ecosystem-data.entity'; export type JWTConfig = { secret: string; @@ -30,7 +32,11 @@ export class ApiConfigService { username: this.configService.get('DB_USERNAME'), password: this.configService.get('DB_PASSWORD'), database: this.configService.get('DB_NAME'), - entities: [...COMMON_DATABASE_ENTITIES, ApiEventsEntity], + entities: [ + ...COMMON_DATABASE_ENTITIES, + ApiEventsEntity, + EcosystemProject, + ], synchronize: true, ssl: this.isProduction() ? { require: true, rejectUnauthorized: false } diff --git a/api/src/modules/data/data.module.ts b/api/src/modules/data/data.module.ts index 60af2fb3..6b9de266 100644 --- a/api/src/modules/data/data.module.ts +++ b/api/src/modules/data/data.module.ts @@ -1,4 +1,10 @@ import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { EcosystemProject } from '@api/modules/data/ecosystem-data.entity'; +import { EcoSystemDataRepository } from '@api/modules/data/ecosystem-data.repository'; -@Module({}) +@Module({ + imports: [TypeOrmModule.forFeature([EcosystemProject])], + providers: [EcoSystemDataRepository], +}) export class DataModule {} diff --git a/api/src/modules/data/ecosystem-data.repository.ts b/api/src/modules/data/ecosystem-data.repository.ts new file mode 100644 index 00000000..58543d3f --- /dev/null +++ b/api/src/modules/data/ecosystem-data.repository.ts @@ -0,0 +1,29 @@ +import { EcosystemProject } from '@api/modules/data/ecosystem-data.entity'; +import { DataSource, Repository } from 'typeorm'; +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class EcoSystemDataRepository extends Repository { + columns = this.metadata.columns + .filter((c) => !c.isPrimary) + .map((c) => c.propertyName); + + constructor(private datasource: DataSource) { + super(EcosystemProject, datasource.createEntityManager()); + } + + /** + * @description Insert data into the database and performs an upsert if the data already exists + * @todo: We are now using all columns to determine if the data already exists, define which columns are relevant here, as ID is generated by the database and cant be used + */ + async insertData(data: EcosystemProject[]): Promise { + return this.createQueryBuilder() + .insert() + .into(EcosystemProject) + .values(data) + .orUpdate(this.columns, this.columns, { + skipUpdateIfNoValuesChanged: true, + }) + .execute(); + } +}