import { IDatabase } from "pg-promise" ; import { LedgerHeader } from "../model" ; import { LedgerHeaderFactory } from "../model/factories" ; import { unique } from "../util/array" ; const sql = { selectLedger : "SELECT * FROM ledgerheaders WHERE ledgerseq = $1" , selectLedgersIn : "SELECT * FROM ledgerheaders WHERE ledgerseq IN ($1:csv) ORDER BY ledgerseq" , selectMaxLedger : "SELECT * FROM ledgerheaders ORDER BY ledgerseq DESC LIMIT 1" , selectMinLedger : "SELECT ledgerseq FROM ledgerheaders ORDER BY ledgerseq ASC LIMIT 1" , selectFirstAfter : "SELECT * FROM ledgerheaders WHERE ledgerseq > $1 LIMIT 1" , selectCountIn : "SELECT COUNT(*) FROM ledgerheaders WHERE ledgerseq >= $1 AND ledgerseq <= $2" } ; export default class LedgerHeadersRepo { private db : IDatabase < any > ; constructor ( db ) { this . db = db ; } public findBySeq ( seq ) { return this . db . oneOrNone ( sql . selectLedger , seq , res => ( res === null ? null : LedgerHeaderFactory . fromDb ( res ) ) ) ; } public findFirstAfterBySeq ( seq ) { return this . db . oneOrNone ( sql . selectFirstAfter , seq , res => ( res === null ? null : LedgerHeaderFactory . fromDb ( res ) ) ) ; } public countInRange ( min , max ) { return this . db . one ( sql . selectCountIn , [ min , max ] , c => + c . count ) ; } public async findAllBySeq ( seqs ) { if ( seqs . length === 0 ) { return new Array < LedgerHeader | null > ( ) ; } const res = await this . db . manyOrNone ( sql . selectLedgersIn , [ seqs . filter ( unique ) ] ) ; const ledgers = res . map ( v => LedgerHeaderFactory . fromDb ( v ) ) ; return seqs . map < LedgerHeader | null > ( seq => ledgers . find ( a => a . ledgerSeq === seq ) || null ) ; } public findMaxSeq ( ) { return this . db . oneOrNone ( sql . selectMaxLedger , null , res => res . ledgerseq ) ; } public findMinSeq ( ) { return this . db . oneOrNone ( sql . selectMinLedger , null , res => res . ledgerseq ) ; } public getLastLedgerHeader ( ) { return this . db . one ( sql . selectMaxLedger , null , res => LedgerHeaderFactory . fromDb ( res ) ) ; } }