Skip to content

Commit

Permalink
update typeorm
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Jun 18, 2024
1 parent 09eb95b commit 4f55f2e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/core-database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export class DatabaseService {

public async getBlock(id: string): Promise<Interfaces.IBlock | undefined> {
// 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;
Expand All @@ -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,
Expand Down Expand Up @@ -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<void> {
Expand Down
22 changes: 12 additions & 10 deletions packages/core-database/src/repositories/block-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { AbstractRepository } from "./abstract-repository";
@EntityRepository(Block)
export class BlockRepository extends AbstractRepository<Block> {
public async findLatest(): Promise<Interfaces.IBlockData | undefined> {
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 }[]> {
Expand All @@ -35,7 +35,7 @@ export class BlockRepository extends AbstractRepository<Block> {
public async findByHeight(height: number): Promise<Block | undefined> {
return this.findOne({
where: { height },
});
}) as Promise<Block | undefined>;
}

public async findByHeights(heights: number[]): Promise<Block[]> {
Expand Down Expand Up @@ -96,13 +96,15 @@ export class BlockRepository extends AbstractRepository<Block> {
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 }[]> {
Expand Down
62 changes: 34 additions & 28 deletions packages/core-database/src/repositories/transaction-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ export class TransactionRepository extends AbstractRepository<Transaction> {
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<FeeStatistics[]> {
Expand All @@ -86,37 +88,41 @@ export class TransactionRepository extends AbstractRepository<Transaction> {
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;
Expand Down
4 changes: 2 additions & 2 deletions packages/core-snapshots/src/repositories/block-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class BlockRepository extends AbstractRepository<Models.Block> {

public async findByHeight(height: number): Promise<Models.Block | undefined> {
return this.findOne({
height: height,
});
where: { height: height },
}) as Promise<Models.Block | undefined>;
}
}

0 comments on commit 4f55f2e

Please sign in to comment.