mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
|
|
import { RpcBlockEntity } from './rpc-block.entity';
|
|
|
|
@Injectable()
|
|
export class RpcBlockService {
|
|
constructor(
|
|
@InjectRepository(RpcBlockEntity)
|
|
private rpcBlockRepository: Repository<RpcBlockEntity>
|
|
) {
|
|
|
|
}
|
|
public getBlock(blockHeight: number) {
|
|
return this.rpcBlockRepository.findOne({
|
|
where: { blockHeight }
|
|
});
|
|
}
|
|
|
|
public saveBlock(blockHeight: number, data: string) {
|
|
return this.rpcBlockRepository.save( {blockHeight, data });
|
|
}
|
|
|
|
public async deleteOldBlocks() {
|
|
const result = await this.rpcBlockRepository.createQueryBuilder('entity')
|
|
.select('MAX(entity.blockHeight)', 'maxNumber')
|
|
.getRawOne();
|
|
|
|
const newestBlock = result ? result.maxNumber : null;
|
|
|
|
await this.rpcBlockRepository.createQueryBuilder()
|
|
.delete()
|
|
.where('"blockHeight" < :newestBlock', { newestBlock })
|
|
.execute();
|
|
|
|
return;
|
|
}
|
|
} |