Add support for read hooks on items (#6341)

* Add emitter on item read

* Add performance warning to docs

* Make result instead of query the payload
This commit is contained in:
Thijs-Jan
2021-06-17 16:56:52 +02:00
committed by rijkvanzanten
parent 4133f08a24
commit d7835e036a
2 changed files with 28 additions and 2 deletions

View File

@@ -303,6 +303,17 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
throw new ForbiddenException();
}
emitAsyncSafe(`${this.eventScope}.read`, {
event: `${this.eventScope}.read`,
accountability: this.accountability,
collection: this.collection,
item: key,
action: 'read',
payload: results,
schema: this.schema,
database: getDatabase(),
});
return results[0];
}
@@ -329,6 +340,18 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
};
const results = await this.readByQuery(queryWithKeys, opts);
emitAsyncSafe(`${this.eventScope}.read`, {
event: `${this.eventScope}.read`,
accountability: this.accountability,
collection: this.collection,
item: keys,
action: 'read',
payload: results,
schema: this.schema,
database: getDatabase(),
});
return results;
}