block save => upsert

This commit is contained in:
Benjamin Wilson
2025-02-01 11:57:34 -05:00
parent 220a1d1558
commit 65232da901
2 changed files with 4 additions and 4 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm'; import { InsertResult, Repository } from 'typeorm';
import { RpcBlockEntity } from './rpc-block.entity'; import { RpcBlockEntity } from './rpc-block.entity';
@@ -18,8 +18,8 @@ export class RpcBlockService {
}); });
} }
public saveBlock(blockHeight: number, data: string) { public saveBlock(blockHeight: number, data: string): Promise<InsertResult> {
return this.rpcBlockRepository.save( {blockHeight, data }); return this.rpcBlockRepository.upsert({ blockHeight, data }, ['blockHeight']);
} }
public async deleteOldBlocks() { public async deleteOldBlocks() {
+1 -1
View File
@@ -134,7 +134,7 @@ export class BitcoinRpcService implements OnModuleInit {
try{ try{
console.log(`Saving block ${blockHeight}`); console.log(`Saving block ${blockHeight}`);
const result = await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(blockTemplate)); await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(blockTemplate));
console.log('block saved'); console.log('block saved');
}catch(e){ }catch(e){
console.error('Error saving block', e); console.error('Error saving block', e);