-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom repository to handler insert with upsert
- Loading branch information
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |