db/events: perform multiple filters with one union query, greatly simplify logic

This commit is contained in:
Alex Gleason 2023-08-16 15:32:02 -05:00
parent 4602b85afe
commit 59b7a3eed8
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 3 additions and 4 deletions

View File

@ -106,11 +106,10 @@ function getFilterQuery(filter: DittoFilter) {
/** Get events for filters from the database. */ /** Get events for filters from the database. */
async function getFilters<K extends number>(filters: DittoFilter<K>[]): Promise<SignedEvent<K>[]> { async function getFilters<K extends number>(filters: DittoFilter<K>[]): Promise<SignedEvent<K>[]> {
const queries = filters const events = await filters
.map(getFilterQuery) .map(getFilterQuery)
.map((query) => query.execute()); .reduce((acc, curr) => acc.union(curr))
.execute();
const events = (await Promise.all(queries)).flat();
return events.map((event) => ( return events.map((event) => (
{ ...event, tags: JSON.parse(event.tags) } as SignedEvent<K> { ...event, tags: JSON.parse(event.tags) } as SignedEvent<K>