feat(db): allow async filter for ressource listing

This commit is contained in:
Julien Oculi 2024-06-18 10:09:29 +02:00
parent fa88ab2f68
commit 8214dcd625

View file

@ -105,7 +105,7 @@ export class Db {
async *#list<T extends Ressource>( async *#list<T extends Ressource>(
type: RessourceType<T>, type: RessourceType<T>,
Builder: RessourceBuilder<T>, Builder: RessourceBuilder<T>,
filter: (entry: T) => boolean, filter: (entry: T) => boolean | Promise<boolean>,
): AsyncGenerator<T, void, void> { ): AsyncGenerator<T, void, void> {
const list = this.#kv.list<RessourceJson<T>>({ const list = this.#kv.list<RessourceJson<T>>({
prefix: [this.prefix.ressource, type], prefix: [this.prefix.ressource, type],
@ -114,7 +114,7 @@ export class Db {
const value = entry.value const value = entry.value
//@ts-expect-error Type union of Ressource types for Builder //@ts-expect-error Type union of Ressource types for Builder
const ressource = Builder.fromJSON(value) as T const ressource = Builder.fromJSON(value) as T
if (filter(ressource)) { if (await filter(ressource)) {
yield ressource yield ressource
} }
} }