-
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.
- Loading branch information
1 parent
a619577
commit c0c0348
Showing
4 changed files
with
124 additions
and
114 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
121 changes: 121 additions & 0 deletions
121
packages/indexer-database/src/utils/BlockchainEventRepository.ts
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,121 @@ | ||
import { DataSource, EntityTarget, ObjectLiteral } from "typeorm"; | ||
import winston from "winston"; | ||
|
||
import { SaveQueryResultType, SaveQueryResult } from "../model"; | ||
|
||
export function filterSaveQueryResults<Entity extends ObjectLiteral>( | ||
results: SaveQueryResult<Entity>[], | ||
type: SaveQueryResultType, | ||
) { | ||
return results | ||
.filter((result) => result.result === type) | ||
.map((result) => result.data) | ||
.filter((data) => data !== undefined); | ||
} | ||
|
||
export class BlockchainEventRepository { | ||
constructor( | ||
protected postgres: DataSource, | ||
protected logger: winston.Logger, | ||
) {} | ||
|
||
/** | ||
* Saves the entities to the database. | ||
* @param entity - The entity to save. | ||
* @param data - The data to save. | ||
* @param uniqueKeys | ||
* The unique keys to check for. It is recommended these keys to be indexed columns, so that the query is faster. | ||
* @param comparisonKeys - The keys to compare for changes. | ||
*/ | ||
protected async saveAndHandleFinalisationBatch<Entity extends ObjectLiteral>( | ||
entity: EntityTarget<Entity>, | ||
data: Partial<Entity>[], | ||
uniqueKeys: (keyof Entity)[], | ||
comparisonKeys: (keyof Entity)[], | ||
): Promise<SaveQueryResult<Entity>[]> { | ||
return Promise.all( | ||
data.map((dataItem) => | ||
this.saveAndHandleFinalisation( | ||
entity, | ||
dataItem, | ||
uniqueKeys, | ||
comparisonKeys, | ||
), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Saves the entity to the database. | ||
* @param entity - The entity to save. | ||
* @param data - The data to save. | ||
* @param uniqueKeys | ||
* The unique keys to check for. It is recommended these keys to be indexed columns, so that the query is faster. | ||
* @param comparisonKeys - The keys to compare for changes. | ||
*/ | ||
protected async saveAndHandleFinalisation<Entity extends ObjectLiteral>( | ||
entity: EntityTarget<Entity>, | ||
data: Partial<Entity>, | ||
uniqueKeys: (keyof Entity)[], | ||
comparisonKeys: (keyof Entity)[], | ||
): Promise<SaveQueryResult<Entity>> { | ||
const where = uniqueKeys.reduce( | ||
(acc, key) => { | ||
acc[key] = data[key]; | ||
return acc; | ||
}, | ||
{} as Record<keyof Entity, any>, | ||
); | ||
const dbEntity = await this.postgres | ||
.getRepository(entity) | ||
.findOne({ where }); | ||
|
||
if (!dbEntity) { | ||
await this.postgres.getRepository(entity).insert(data); | ||
return { | ||
data: (await this.postgres | ||
.getRepository(entity) | ||
.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.Inserted, | ||
}; | ||
} | ||
|
||
// Check if the any of values of the comparison keys have changed | ||
const isChanged = comparisonKeys.some((key) => data[key] !== dbEntity[key]); | ||
// Check if the data moved in finalised state | ||
const isFinalisedChanged = data.finalised && !dbEntity.finalised; | ||
|
||
if (isChanged) { | ||
await this.postgres.getRepository(entity).update(where, data); | ||
if (isFinalisedChanged) { | ||
return { | ||
data: (await this.postgres | ||
.getRepository(entity) | ||
.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.UpdatedAndFinalised, | ||
}; | ||
} | ||
return { | ||
data: (await this.postgres | ||
.getRepository(entity) | ||
.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.Updated, | ||
}; | ||
} | ||
|
||
if (isFinalisedChanged) { | ||
await this.postgres.getRepository(entity).update(where, data); | ||
return { | ||
data: (await this.postgres | ||
.getRepository(entity) | ||
.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.Finalised, | ||
}; | ||
} | ||
|
||
return { | ||
data: undefined, | ||
result: SaveQueryResultType.Nothing, | ||
}; | ||
} | ||
} |
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 +1,2 @@ | ||
export * from "./BaseRepository"; | ||
export * from "./BlockchainEventRepository"; |
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