From 4f55f2ea68156dbc64a61a78c1b1da3f418dbeaf Mon Sep 17 00:00:00 2001 From: oXtxNt9U <120286271+oXtxNt9U@users.noreply.github.com> Date: Tue, 18 Jun 2024 17:13:02 +0900 Subject: [PATCH] update `typeorm` --- packages/core-database/package.json | 2 +- .../core-database/src/database-service.ts | 10 +-- .../src/repositories/block-repository.ts | 22 ++++--- .../repositories/transaction-repository.ts | 62 ++++++++++--------- .../src/repositories/block-repository.ts | 4 +- 5 files changed, 54 insertions(+), 46 deletions(-) diff --git a/packages/core-database/package.json b/packages/core-database/package.json index 0676044e33..31d088c301 100644 --- a/packages/core-database/package.json +++ b/packages/core-database/package.json @@ -29,7 +29,7 @@ "joi": "17.12.1", "pg": "8.11.3", "reflect-metadata": "0.2.1", - "typeorm": "0.2.25" + "typeorm": "0.3.17" }, "engines": { "node": ">=10.x" diff --git a/packages/core-database/src/database-service.ts b/packages/core-database/src/database-service.ts index 1a0b4597cb..3cb4e174db 100644 --- a/packages/core-database/src/database-service.ts +++ b/packages/core-database/src/database-service.ts @@ -70,9 +70,9 @@ export class DatabaseService { public async getBlock(id: string): Promise { // TODO: caching the last 1000 blocks, in combination with `saveBlock` could help to optimise - const block: Interfaces.IBlockData = (await this.blockRepository.findOne( - id, - )) as unknown as Interfaces.IBlockData; + const block: Interfaces.IBlockData = (await this.blockRepository.findOne({ + where: { id }, + })) as unknown as Interfaces.IBlockData; if (!block) { return undefined; @@ -81,7 +81,7 @@ export class DatabaseService { const transactions: Array<{ serialized: Buffer; id: string; - }> = await this.transactionRepository.find({ blockId: block.id }); + }> = await this.transactionRepository.find({ where: { blockId: block.id } }); block.transactions = transactions.map( ({ serialized, id }) => Transactions.TransactionFactory.fromBytesUnsafe(serialized, id).data, @@ -166,7 +166,7 @@ export class DatabaseService { } public async getTransaction(id: string) { - return this.transactionRepository.findOne(id); + return this.transactionRepository.findOne({ where: { id } }); } public async deleteBlocks(blocks: Interfaces.IBlockData[]): Promise { diff --git a/packages/core-database/src/repositories/block-repository.ts b/packages/core-database/src/repositories/block-repository.ts index dcb7b00412..33f8f44f50 100644 --- a/packages/core-database/src/repositories/block-repository.ts +++ b/packages/core-database/src/repositories/block-repository.ts @@ -8,9 +8,9 @@ import { AbstractRepository } from "./abstract-repository"; @EntityRepository(Block) export class BlockRepository extends AbstractRepository { public async findLatest(): Promise { - return (this.findOne({ + return this.findOne({ order: { height: "DESC" }, - }) as unknown) as Interfaces.IBlockData; // TODO: refactor + }) as unknown as Interfaces.IBlockData; // TODO: refactor } public async findRecent(limit: number): Promise<{ id: string }[]> { @@ -35,7 +35,7 @@ export class BlockRepository extends AbstractRepository { public async findByHeight(height: number): Promise { return this.findOne({ where: { height }, - }); + }) as Promise; } public async findByHeights(heights: number[]): Promise { @@ -96,13 +96,15 @@ export class BlockRepository extends AbstractRepository { totalAmount: string; count: number; }> { - return this.createQueryBuilder() - .select([]) - .addSelect("COALESCE(SUM(number_of_transactions), 0)", "numberOfTransactions") - .addSelect("COALESCE(SUM(total_fee), 0)", "totalFee") - .addSelect("COALESCE(SUM(total_amount), 0)", "totalAmount") - .addSelect("COUNT(DISTINCT(height))", "count") - .getRawOne(); + return ( + (await this.createQueryBuilder() + .select([]) + .addSelect("COALESCE(SUM(number_of_transactions), 0)", "numberOfTransactions") + .addSelect("COALESCE(SUM(total_fee), 0)", "totalFee") + .addSelect("COALESCE(SUM(total_amount), 0)", "totalAmount") + .addSelect("COUNT(DISTINCT(height))", "count") + .getRawOne()) ?? { numberOfTransactions: 0, totalFee: "0", totalAmount: "0", count: 0 } + ); } public async getBlockRewards(): Promise<{ generatorPublicKey: string; rewards: string }[]> { diff --git a/packages/core-database/src/repositories/transaction-repository.ts b/packages/core-database/src/repositories/transaction-repository.ts index ea661f965e..872ccc1635 100644 --- a/packages/core-database/src/repositories/transaction-repository.ts +++ b/packages/core-database/src/repositories/transaction-repository.ts @@ -50,16 +50,18 @@ export class TransactionRepository extends AbstractRepository { totalFee: string; totalAmount: string; }> { - return this.createQueryBuilder() - .select([]) - .addSelect("COUNT(DISTINCT(id))", "count") - .addSelect("COALESCE(SUM(fee), 0)", "totalFee") - .addSelect("COALESCE(SUM(amount), 0)", "totalAmount") - .getRawOne(); + return ( + (await this.createQueryBuilder() + .select([]) + .addSelect("COUNT(DISTINCT(id))", "count") + .addSelect("COALESCE(SUM(fee), 0)", "totalFee") + .addSelect("COALESCE(SUM(amount), 0)", "totalAmount") + .getRawOne()) ?? { count: 0, totalFee: "0", totalAmount: "0" } + ); } public async getFeeStatistics( - txTypes: Array<{ type: number, typeGroup: number }>, + txTypes: Array<{ type: number; typeGroup: number }>, days?: number, minFee?: number, ): Promise { @@ -86,37 +88,41 @@ export class TransactionRepository extends AbstractRepository { for (const feeStatsByType of txTypes) { // we don't use directly this.createQueryBuilder() because it forces to have FROM transactions // instead of just the FROM (...) subquery - const feeStatsForType: FeeStatistics = await this.manager.connection.createQueryBuilder() + const feeStatsForType: FeeStatistics | undefined = await this.manager.connection + .createQueryBuilder() .select(['subquery.type_group AS "typeGroup"', "subquery.type"]) .addSelect("COALESCE(AVG(subquery.fee), 0)::int8", "avg") .addSelect("COALESCE(MIN(subquery.fee), 0)::int8", "min") .addSelect("COALESCE(MAX(subquery.fee), 0)::int8", "max") .addSelect("COALESCE(SUM(subquery.fee), 0)::int8", "sum") .from( - qb => qb - .subQuery() - .select() - .from("transactions", "txs") - .where( - "txs.type = :type and txs.type_group = :typeGroup", - { type: feeStatsByType.type, typeGroup: feeStatsByType.typeGroup } - ) - .orderBy("txs.block_height", "DESC") - .addOrderBy("txs.sequence", "DESC") - .limit(20), - "subquery" + (qb) => + qb + .subQuery() + .select() + .from("transactions", "txs") + .where("txs.type = :type and txs.type_group = :typeGroup", { + type: feeStatsByType.type, + typeGroup: feeStatsByType.typeGroup, + }) + .orderBy("txs.block_height", "DESC") + .addOrderBy("txs.sequence", "DESC") + .limit(20), + "subquery", ) .groupBy("subquery.type_group") .addGroupBy("subquery.type") .getRawOne(); - feeStatistics.push(feeStatsForType ?? { - type: feeStatsByType.type, - typeGroup: feeStatsByType.typeGroup, - avg: 0, - min: 0, - max: 0, - sum: 0, - }); + feeStatistics.push( + feeStatsForType ?? { + type: feeStatsByType.type, + typeGroup: feeStatsByType.typeGroup, + avg: 0, + min: 0, + max: 0, + sum: 0, + }, + ); } return feeStatistics; diff --git a/packages/core-snapshots/src/repositories/block-repository.ts b/packages/core-snapshots/src/repositories/block-repository.ts index dd40a22706..052a86d067 100644 --- a/packages/core-snapshots/src/repositories/block-repository.ts +++ b/packages/core-snapshots/src/repositories/block-repository.ts @@ -77,7 +77,7 @@ export class BlockRepository extends AbstractRepository { public async findByHeight(height: number): Promise { return this.findOne({ - height: height, - }); + where: { height: height }, + }) as Promise; } }