Skip to content

Commit

Permalink
add custom repository to handler insert with upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 29, 2024
1 parent b7b726f commit 294aa05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
8 changes: 7 additions & 1 deletion api/src/modules/config/app-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 }
Expand Down
8 changes: 7 additions & 1 deletion api/src/modules/data/data.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
29 changes: 29 additions & 0 deletions api/src/modules/data/ecosystem-data.repository.ts
Original file line number Diff line number Diff line change
@@ -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<EcosystemProject> {
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<any> {
return this.createQueryBuilder()
.insert()
.into(EcosystemProject)
.values(data)
.orUpdate(this.columns, this.columns, {
skipUpdateIfNoValuesChanged: true,
})
.execute();
}
}

0 comments on commit 294aa05

Please sign in to comment.