All files / src/Metadata DbSyncMetadataService.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 4/4
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 3943x         43x 43x       43x   7x 7x   7x           7x 5x       18x   18x           18x 8x      
import * as Queries from './queries';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
import { TxMetadataModel, TxMetadataService } from './types';
import { hexStringToBuffer } from '@cardano-sdk/util';
import { mapTxMetadataByHashes } from './util';
 
export type TxMetadataByHashes = Map<Cardano.TransactionId, Cardano.TxMetadata>;
 
export const createDbSyncMetadataService = (db: Pool, logger: Logger): TxMetadataService => ({
  async queryTxMetadataByHashes(hashes: Cardano.TransactionId[]): Promise<TxMetadataByHashes> {
    const byteHashes = hashes.map((hash) => hexStringToBuffer(hash));
    logger.debug('About to find metadata for txs:', hashes);
 
    const result = await db.query<TxMetadataModel>({
      name: 'tx_metadata',
      text: Queries.findTxMetadataByTxHashes,
      values: [byteHashes]
    });
 
    if (result.rows.length === 0) return new Map();
    return mapTxMetadataByHashes(result.rows);
  },
 
  async queryTxMetadataByRecordIds(ids: string[]): Promise<TxMetadataByHashes> {
    logger.debug('About to find metadata for transactions with ids:', ids);
 
    const result: QueryResult<TxMetadataModel> = await db.query({
      name: 'tx_metadata_by_tx_ids',
      text: Queries.findTxMetadataByTxIds,
      values: [ids]
    });
 
    if (result.rows.length === 0) return new Map();
    return mapTxMetadataByHashes(result.rows);
  }
});