-
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.
fix: replace bulk upserts with inserts/updates if needed (#103)
- Loading branch information
Showing
10 changed files
with
269 additions
and
80 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,3 +1,4 @@ | ||
export * from "./main"; | ||
export * as entities from "./entities"; | ||
export * as utils from "./utils"; | ||
export * from "./model"; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type DatabaseConfig = { | ||
host: string; | ||
port: string; | ||
user: string; | ||
password: string; | ||
dbName: string; | ||
}; | ||
|
||
/** | ||
* Enum to represent the result type of a query. | ||
* - If the entity is identical to the one in the database, return `Nothing`. | ||
* - If the unique keys are not present, return Inserted. | ||
* - If the finalised field was the only one that changed, return `Finalised`. | ||
* - If any of the entity fields were changed, return Updated. | ||
* - If both the finalised field and other fields were changed, return UpdatedAndFinalised. | ||
*/ | ||
export enum SaveQueryResultType { | ||
Nothing = "nothing", | ||
Inserted = "inserted", | ||
Finalised = "finalised", | ||
Updated = "updated", | ||
UpdatedAndFinalised = "updatedAndFinalised", | ||
} | ||
|
||
export type SaveQueryResult<T> = { | ||
data: T | undefined; | ||
result: SaveQueryResultType; | ||
}; |
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
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 }); | ||
const repository = this.postgres.getRepository(entity); | ||
|
||
if (!dbEntity) { | ||
await repository.insert(data); | ||
return { | ||
data: (await repository.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.Inserted, | ||
}; | ||
} | ||
|
||
// Check if any of the 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 repository.update(where, data); | ||
if (isFinalisedChanged) { | ||
return { | ||
data: (await repository.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.UpdatedAndFinalised, | ||
}; | ||
} | ||
return { | ||
data: (await repository.findOne({ where })) as Entity, | ||
result: SaveQueryResultType.Updated, | ||
}; | ||
} | ||
|
||
if (isFinalisedChanged) { | ||
await repository.update(where, data); | ||
return { | ||
data: (await repository.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
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
Oops, something went wrong.