From 6d5b44e3ee2078a627016f8739d1436064ba202a Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:52:20 -0300 Subject: [PATCH] fix: tx kysely typing and add more verbose log --- .../data-flow/src/data-loader/dataLoader.ts | 10 +++++++++- .../kysely/application.repository.ts | 7 ++++--- .../kysely/applicationPayout.repository.ts | 11 ++++++++--- .../kysely/donation.repository.ts | 8 ++++---- .../repositories/kysely/project.repository.ts | 15 ++++++++------- .../repositories/kysely/round.repository.ts | 19 ++++++++++--------- .../repositories/kysely/transactionManager.ts | 6 +++--- .../repository/src/types/transaction.types.ts | 5 ++--- 8 files changed, 48 insertions(+), 33 deletions(-) diff --git a/packages/data-flow/src/data-loader/dataLoader.ts b/packages/data-flow/src/data-loader/dataLoader.ts index d87b0cc..91557a2 100644 --- a/packages/data-flow/src/data-loader/dataLoader.ts +++ b/packages/data-flow/src/data-loader/dataLoader.ts @@ -63,7 +63,9 @@ export class DataLoader implements IDataLoader { } await this.transactionManager.runInTransaction(async (tx) => { - this.logger.debug("Starting transaction..."); + this.logger.debug(`Starting transaction on ${changesets.length} changesets...`, { + className: DataLoader.name, + }); for (const changeset of changesets) { try { //TODO: inside each handler, we should add zod validation that the args match the expected type @@ -71,11 +73,17 @@ export class DataLoader implements IDataLoader { } catch (error) { this.logger.debug( `Error applying changeset ${changeset.type}. Rolling back transaction with ${changesets.length} changesets`, + { + className: DataLoader.name, + }, ); throw error; } } }); + this.logger.debug(`Successfully applied ${changesets.length} changesets`, { + className: DataLoader.name, + }); } } diff --git a/packages/repository/src/repositories/kysely/application.repository.ts b/packages/repository/src/repositories/kysely/application.repository.ts index 4d1e202..563c93d 100644 --- a/packages/repository/src/repositories/kysely/application.repository.ts +++ b/packages/repository/src/repositories/kysely/application.repository.ts @@ -7,11 +7,12 @@ import { ApplicationNotFound, Database, IApplicationRepository, + KyselyTransaction, NewApplication, PartialApplication, } from "../../internal.js"; -export class KyselyApplicationRepository implements IApplicationRepository> { +export class KyselyApplicationRepository implements IApplicationRepository { constructor( private readonly db: Kysely, private readonly schemaName: string, @@ -96,7 +97,7 @@ export class KyselyApplicationRepository implements IApplicationRepository): Promise { + async insertApplication(application: NewApplication, tx?: KyselyTransaction): Promise { const _application = this.formatApplication(application); const queryBuilder = (tx || this.db).withSchema(this.schemaName); @@ -107,7 +108,7 @@ export class KyselyApplicationRepository implements IApplicationRepository, + tx?: KyselyTransaction, ): Promise { const _application = this.formatApplication(application); const queryBuilder = (tx || this.db).withSchema(this.schemaName); diff --git a/packages/repository/src/repositories/kysely/applicationPayout.repository.ts b/packages/repository/src/repositories/kysely/applicationPayout.repository.ts index a384e8a..2a5e177 100644 --- a/packages/repository/src/repositories/kysely/applicationPayout.repository.ts +++ b/packages/repository/src/repositories/kysely/applicationPayout.repository.ts @@ -1,9 +1,14 @@ import { Kysely } from "kysely"; -import { Database, IApplicationPayoutRepository, NewApplicationPayout } from "../../internal.js"; +import { + Database, + IApplicationPayoutRepository, + KyselyTransaction, + NewApplicationPayout, +} from "../../internal.js"; export class KyselyApplicationPayoutRepository - implements IApplicationPayoutRepository> + implements IApplicationPayoutRepository { constructor( private readonly db: Kysely, @@ -13,7 +18,7 @@ export class KyselyApplicationPayoutRepository /** @inheritdoc */ async insertApplicationPayout( applicationPayout: NewApplicationPayout, - tx?: Kysely, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("applicationsPayouts").values(applicationPayout).execute(); diff --git a/packages/repository/src/repositories/kysely/donation.repository.ts b/packages/repository/src/repositories/kysely/donation.repository.ts index bf4a001..ba47ef8 100644 --- a/packages/repository/src/repositories/kysely/donation.repository.ts +++ b/packages/repository/src/repositories/kysely/donation.repository.ts @@ -1,15 +1,15 @@ import { Kysely } from "kysely"; -import { Database, IDonationRepository, NewDonation } from "../../internal.js"; +import { Database, IDonationRepository, KyselyTransaction, NewDonation } from "../../internal.js"; -export class KyselyDonationRepository implements IDonationRepository> { +export class KyselyDonationRepository implements IDonationRepository { constructor( private readonly db: Kysely, private readonly schemaName: string, ) {} /** @inheritdoc */ - async insertDonation(donation: NewDonation, tx?: Kysely): Promise { + async insertDonation(donation: NewDonation, tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder @@ -22,7 +22,7 @@ export class KyselyDonationRepository implements IDonationRepository): Promise { + async insertManyDonations(donations: NewDonation[], tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder diff --git a/packages/repository/src/repositories/kysely/project.repository.ts b/packages/repository/src/repositories/kysely/project.repository.ts index 58cfb46..0d4b069 100644 --- a/packages/repository/src/repositories/kysely/project.repository.ts +++ b/packages/repository/src/repositories/kysely/project.repository.ts @@ -5,6 +5,7 @@ import { Address, ChainId } from "@grants-stack-indexer/shared"; import { IProjectRepository } from "../../interfaces/projectRepository.interface.js"; import { Database, + KyselyTransaction, NewPendingProjectRole, NewProject, NewProjectRole, @@ -15,7 +16,7 @@ import { ProjectRoleNames, } from "../../internal.js"; -export class KyselyProjectRepository implements IProjectRepository> { +export class KyselyProjectRepository implements IProjectRepository { constructor( private readonly db: Kysely, private readonly schemaName: string, @@ -73,7 +74,7 @@ export class KyselyProjectRepository implements IProjectRepository): Promise { + async insertProject(project: NewProject, tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("projects").values(project).execute(); } @@ -82,7 +83,7 @@ export class KyselyProjectRepository implements IProjectRepository, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder @@ -96,7 +97,7 @@ export class KyselyProjectRepository implements IProjectRepository): Promise { + async insertProjectRole(projectRole: NewProjectRole, tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("projectRoles").values(projectRole).execute(); } @@ -107,7 +108,7 @@ export class KyselyProjectRepository implements IProjectRepository, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); const query = queryBuilder @@ -151,14 +152,14 @@ export class KyselyProjectRepository implements IProjectRepository, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("pendingProjectRoles").values(pendingProjectRole).execute(); } /* @inheritdoc */ - async deleteManyPendingProjectRoles(ids: number[], tx?: Kysely): Promise { + async deleteManyPendingProjectRoles(ids: number[], tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.deleteFrom("pendingProjectRoles").where("id", "in", ids).execute(); } diff --git a/packages/repository/src/repositories/kysely/round.repository.ts b/packages/repository/src/repositories/kysely/round.repository.ts index 356a840..a48727d 100644 --- a/packages/repository/src/repositories/kysely/round.repository.ts +++ b/packages/repository/src/repositories/kysely/round.repository.ts @@ -5,6 +5,7 @@ import { Address, ChainId, stringify } from "@grants-stack-indexer/shared"; import { Database, IRoundRepository, + KyselyTransaction, NewPendingRoundRole, NewRound, NewRoundRole, @@ -17,7 +18,7 @@ import { RoundRoleNames, } from "../../internal.js"; -export class KyselyRoundRepository implements IRoundRepository> { +export class KyselyRoundRepository implements IRoundRepository { constructor( private readonly db: Kysely, private readonly schemaName: string, @@ -114,7 +115,7 @@ export class KyselyRoundRepository implements IRoundRepository> } /* @inheritdoc */ - async insertRound(round: NewRound, tx?: Kysely): Promise { + async insertRound(round: NewRound, tx?: KyselyTransaction): Promise { const _round = this.formatRound(round); const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("rounds").values(_round).execute(); @@ -124,7 +125,7 @@ export class KyselyRoundRepository implements IRoundRepository> async updateRound( where: { id: string; chainId: ChainId } | { chainId: ChainId; strategyAddress: Address }, round: PartialRound, - tx?: Kysely, + tx?: KyselyTransaction, ): Promise { const _round = this.formatRound(round); const queryBuilder = (tx || this.db).withSchema(this.schemaName); @@ -145,7 +146,7 @@ export class KyselyRoundRepository implements IRoundRepository> where: { chainId: ChainId; roundId: string }, amount: bigint, amountInUsd: string, - tx?: Kysely, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder @@ -163,7 +164,7 @@ export class KyselyRoundRepository implements IRoundRepository> async incrementRoundTotalDistributed( where: { chainId: ChainId; roundId: string }, amount: bigint, - tx?: Kysely, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder @@ -184,7 +185,7 @@ export class KyselyRoundRepository implements IRoundRepository> } /* @inheritdoc */ - async insertRoundRole(roundRole: NewRoundRole, tx?: Kysely): Promise { + async insertRoundRole(roundRole: NewRoundRole, tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("roundRoles").values(roundRole).execute(); } @@ -195,7 +196,7 @@ export class KyselyRoundRepository implements IRoundRepository> roundId: string, role: RoundRoleNames, address: Address, - tx?: Kysely, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder @@ -226,14 +227,14 @@ export class KyselyRoundRepository implements IRoundRepository> /* @inheritdoc */ async insertPendingRoundRole( pendingRoundRole: NewPendingRoundRole, - tx?: Kysely, + tx?: KyselyTransaction, ): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.insertInto("pendingRoundRoles").values(pendingRoundRole).execute(); } /* @inheritdoc */ - async deleteManyPendingRoundRoles(ids: number[], tx?: Kysely): Promise { + async deleteManyPendingRoundRoles(ids: number[], tx?: KyselyTransaction): Promise { const queryBuilder = (tx || this.db).withSchema(this.schemaName); await queryBuilder.deleteFrom("pendingRoundRoles").where("id", "in", ids).execute(); } diff --git a/packages/repository/src/repositories/kysely/transactionManager.ts b/packages/repository/src/repositories/kysely/transactionManager.ts index bb4f619..6c4c810 100644 --- a/packages/repository/src/repositories/kysely/transactionManager.ts +++ b/packages/repository/src/repositories/kysely/transactionManager.ts @@ -1,12 +1,12 @@ import { Kysely } from "kysely"; -import { Database, ITransactionManager } from "../../internal.js"; +import { Database, ITransactionManager, KyselyTransaction } from "../../internal.js"; -export class KyselyTransactionManager implements ITransactionManager> { +export class KyselyTransactionManager implements ITransactionManager { constructor(private readonly db: Kysely) {} /** @inheritdoc */ - async runInTransaction(fn: (tx: Kysely) => Promise): Promise { + async runInTransaction(fn: (tx: KyselyTransaction) => Promise): Promise { return this.db.transaction().execute(fn); } } diff --git a/packages/repository/src/types/transaction.types.ts b/packages/repository/src/types/transaction.types.ts index 89d2347..fddf619 100644 --- a/packages/repository/src/types/transaction.types.ts +++ b/packages/repository/src/types/transaction.types.ts @@ -1,8 +1,7 @@ -// packages/repository/src/types/transaction.types.ts -import { Kysely } from "kysely"; +import { Transaction } from "kysely"; import { Database } from "../internal.js"; -export type KyselyTransaction = Kysely; +export type KyselyTransaction = Transaction; export type TransactionConnection = KyselyTransaction;