Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrumatei36 committed Nov 19, 2024
1 parent a619577 commit c0c0348
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 114 deletions.
112 changes: 0 additions & 112 deletions packages/indexer-database/src/utils/BaseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
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 BaseRepository {
constructor(
protected postgres: DataSource,
Expand Down Expand Up @@ -71,104 +59,4 @@ export class BaseRepository {

return savedData.generatedMaps as Entity[];
}

/**
* 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,
};
}
}
121 changes: 121 additions & 0 deletions packages/indexer-database/src/utils/BlockchainEventRepository.ts
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,
};
}
}
1 change: 1 addition & 0 deletions packages/indexer-database/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./BaseRepository";
export * from "./BlockchainEventRepository";
4 changes: 2 additions & 2 deletions packages/indexer/src/database/SpokePoolRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import * as across from "@across-protocol/sdk";
import { DataSource, entities, utils as dbUtils } from "@repo/indexer-database";
import * as utils from "../utils";

export class SpokePoolRepository extends dbUtils.BaseRepository {
export class SpokePoolRepository extends dbUtils.BlockchainEventRepository {
constructor(
postgres: DataSource,
logger: winston.Logger,
private chunkSize = 2000,
) {
super(postgres, logger, true);
super(postgres, logger);
}

public updateDepositEventWithIntegratorId(id: number, integratorId: string) {
Expand Down

0 comments on commit c0c0348

Please sign in to comment.