import { TimestampFilter , StoredEventBatchPointer , loadEventsByTimestampResult , } from '@resolve-js/eventstore-base' ; import { AdapterPool } from './types' ; const loadEventsByTimestamp = async ( { query , eventsTableName , escapeId , escape , shapeEvent } , { eventTypes , aggregateIds , startTime , finishTime , limit } ) : Promise < StoredEventBatchPointer > => { const injectString = ( value ) : string => `${ escape ( value ) }` ; const injectNumber = ( value ) : string => `${ + value }` ; const queryConditions = [ ] ; const events = [ ] ; if ( eventTypes != null ) { queryConditions . push ( `\`type\` IN (${ eventTypes . map ( injectString ) })` ) ; } if ( aggregateIds != null ) { queryConditions . push ( `\`aggregateId\` IN (${ aggregateIds . map ( injectString ) })` ) ; } if ( startTime != null ) { queryConditions . push ( `\`timestamp\` > ${ injectNumber ( startTime ) }` ) ; } if ( finishTime != null ) { queryConditions . push ( `\`timestamp\` < ${ injectNumber ( finishTime ) }` ) ; } const resultQueryCondition = queryConditions . length > 0 ? `WHERE ${ queryConditions . join ( ' AND ' ) }` : '' ; const eventsTableNameAsId = escapeId ( eventsTableName ) ; const [ rows ] = await query ( `SELECT * FROM ${ eventsTableNameAsId }
    ${ resultQueryCondition }
    ORDER BY \`timestamp\` ASC,
    \`threadCounter\` ASC,
    \`threadId\` ASC
    LIMIT 0, ${ + limit }` ) ; for ( const event of rows ) { events . push ( shapeEvent ( event ) ) ; } return loadEventsByTimestampResult ( events ) ; } ; export default loadEventsByTimestamp ;