Add getFilters function

This commit is contained in:
Alex Gleason 2023-08-09 00:22:20 -05:00
parent b408838ae0
commit 0bfd9d83c1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 18 additions and 4 deletions

View File

@ -34,7 +34,7 @@ function insertEvent(event: SignedEvent): Promise<void> {
});
}
async function getFilter<K extends number = number>(filter: Filter<K>): Promise<SignedEvent<K>[]> {
function getFilterQuery<K extends number = number>(filter: Filter<K>) {
let query = db.selectFrom('events').selectAll().orderBy('created_at', 'desc');
for (const key of Object.keys(filter)) {
@ -69,11 +69,25 @@ async function getFilter<K extends number = number>(filter: Filter<K>): Promise<
}
}
const events = await query.execute();
return query;
}
async function getFilters<K extends number>(filters: [Filter<K>]): Promise<SignedEvent<K>[]>;
async function getFilters(filters: Filter[]): Promise<SignedEvent[]>;
async function getFilters(filters: Filter[]) {
const queries = filters
.map(getFilterQuery)
.map((query) => query.execute());
const events = (await Promise.all(queries)).flat();
return events.map((event) => (
{ ...event, tags: JSON.parse(event.tags) } as SignedEvent<K>
{ ...event, tags: JSON.parse(event.tags) }
));
}
export { getFilter, insertEvent };
function getFilter<K extends number = number>(filter: Filter<K>): Promise<SignedEvent<K>[]> {
return getFilters<K>([filter]);
}
export { getFilter, getFilters, insertEvent };