Skip to content

Commit

Permalink
feat: Added log model
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoares85 committed Jan 17, 2024
1 parent bc3c937 commit da51917
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/lib/Federator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IFederation } from '../contracts/IFederation';
import { LogWrapper } from './logWrapper';
import {AppDataSource} from "../services/AppDataSource";
import {Log} from "../entities/Log";
import {getLog, insertLog, updateLog} from "../models/log.model";

export default abstract class Federator {
public logger: LogWrapper;
Expand Down Expand Up @@ -77,8 +78,7 @@ export default abstract class Federator {
let fromBlock: number = null;
const originalFromBlock = this.config.mainchain.fromBlock || 0;
try {
const log = await AppDataSource.getRepository(Log)
.findOne({ where: { mainChain: mainChainId, sideChain: sideChainId }});
const log = await getLog(mainChainId, sideChainId);
fromBlock = log.block;
} catch (err) {
fromBlock = originalFromBlock;
Expand Down Expand Up @@ -178,13 +178,12 @@ export default abstract class Federator {

async _saveProgress(mainChain: number, sideChain: number, value: number) {
if (value) {
const log = await AppDataSource.getRepository(Log)
.findOne({ where: { mainChain, sideChain }});
const log = await getLog(mainChain, sideChain);

if (log) {
await AppDataSource.getRepository(Log).update({id: log.id}, { block: value });
await updateLog(log.id, { block: value });
} else {
await AppDataSource.getRepository(Log).insert({ mainChain, sideChain, block: value })
await insertLog({ mainChain, sideChain, block: value });
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/models/log.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {AppDataSource} from "../services/AppDataSource";
import {Log} from "../entities/Log";

const getLog = async (mainChainId: number, sideChainId: number) => {
return await AppDataSource.getRepository(Log)
.findOne({ where: { mainChain: mainChainId, sideChain: sideChainId }});
}

const insertLog = async(log: Partial<Log>) => {
await AppDataSource.getRepository(Log).insert(log);
}

const updateLog = async (id: number, data: Partial<Log>) => {
return await AppDataSource.getRepository(Log).update({id}, {...data});
}

export { getLog, insertLog, updateLog }

0 comments on commit da51917

Please sign in to comment.