| 39 | } |
| 40 | |
| 41 | export class EventRepository implements IEventRepository { |
| 42 | public constructor( |
| 43 | private readonly masterDbClient: DatabaseClient, |
| 44 | private readonly readReplicaDbClient: DatabaseClient, |
| 45 | ) {} |
| 46 | |
| 47 | public findByFilters(filters: SubscriptionFilter[]): IQueryResult<DBEvent[]> { |
| 48 | logger('querying for %o', filters) |
| 49 | if (!Array.isArray(filters) || !filters.length) { |
| 50 | throw new Error('Filters cannot be empty') |
| 51 | } |
| 52 | const queries = filters.map((currentFilter) => { |
| 53 | const builder = this.readReplicaDbClient<DBEvent>('events') |
| 54 | |
| 55 | const isTagQuery = this.applyFilterConditions(builder, currentFilter) |
| 56 | |
| 57 | if (typeof currentFilter.limit === 'number') { |
| 58 | builder.limit(currentFilter.limit).orderBy('event_created_at', 'DESC').orderBy('event_id', 'asc') |
| 59 | } else { |
| 60 | builder.limit(DEFAULT_FILTER_LIMIT).orderBy('event_created_at', 'asc').orderBy('event_id', 'asc') |
| 61 | } |
| 62 | |
| 63 | if (isTagQuery) { |
| 64 | builder.select('events.*') |
| 65 | } |
| 66 | |
| 67 | return builder |
| 68 | }) |
| 69 | |
| 70 | const [query, ...subqueries] = queries |
| 71 | if (subqueries.length) { |
| 72 | query.union(subqueries, true) |
| 73 | } |
| 74 | |
| 75 | return query |
| 76 | } |
| 77 | |
| 78 | public async countByFilters(filters: SubscriptionFilter[]): Promise<number> { |
| 79 | logger('counting events for %o', filters) |
| 80 | |
| 81 | if (!Array.isArray(filters) || !filters.length) { |
| 82 | throw new Error('Filters cannot be empty') |
| 83 | } |
| 84 | |
| 85 | const now = Math.floor(Date.now() / 1000) |
| 86 | |
| 87 | const queries = filters.map((currentFilter) => { |
| 88 | const builder = this.readReplicaDbClient<DBEvent>('events').select('events.event_id') |
| 89 | |
| 90 | const isTagQuery = this.applyFilterConditions(builder, currentFilter) |
| 91 | |
| 92 | if (typeof currentFilter.limit === 'number') { |
| 93 | builder.limit(currentFilter.limit).orderBy('event_created_at', 'DESC').orderBy('event_id', 'asc') |
| 94 | } |
| 95 | |
| 96 | if (isTagQuery) { |
| 97 | builder.select('events.event_id') |
| 98 | } |
nothing calls this directly
no outgoing calls
no test coverage detected