diff --git a/src/lib/Federator.ts b/src/lib/Federator.ts index 9eeb9f7..e7d8615 100644 --- a/src/lib/Federator.ts +++ b/src/lib/Federator.ts @@ -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; @@ -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; @@ -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 }); } } } diff --git a/src/models/log.model.ts b/src/models/log.model.ts new file mode 100644 index 0000000..7375fe6 --- /dev/null +++ b/src/models/log.model.ts @@ -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) => { + await AppDataSource.getRepository(Log).insert(log); +} + +const updateLog = async (id: number, data: Partial) => { + return await AppDataSource.getRepository(Log).update({id}, {...data}); +} + +export { getLog, insertLog, updateLog }