diff --git a/api/src/services/items.ts b/api/src/services/items.ts index 588945c1f0..4aa0258ee2 100644 --- a/api/src/services/items.ts +++ b/api/src/services/items.ts @@ -303,6 +303,17 @@ export class ItemsService 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 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; } diff --git a/docs/guides/api-hooks.md b/docs/guides/api-hooks.md index 6d9e226d2a..26f224a50b 100644 --- a/docs/guides/api-hooks.md +++ b/docs/guides/api-hooks.md @@ -85,7 +85,7 @@ module.exports = function registerHook({ exceptions }) { | `error` | | No | | `auth` | `login`, `logout`[1] and `refresh`[1] | Optional | | `oauth.:provider`[2] | `login` and `redirect` | Optional | -| `items` | `create`, `update` and `delete` | Optional | +| `items` | `read`[3], `create`, `update` and `delete` | Optional | | `activity` | `create`, `update` and `delete` | Optional | | `collections` | `create`, `update` and `delete` | Optional | | `fields` | `create`, `update` and `delete` | Optional | @@ -114,7 +114,7 @@ on day-of-month 1) or `cron(5 4 * * sun)` (at 04:05 on Sunday). See example belo module.exports = function registerHook() { return { 'cron(*/15 * * * *)': async function () { - await axios.post('http://example.com/webhook', { message: "Another 15 minutes passed..." }); + await axios.post('http://example.com/webhook', { message: 'Another 15 minutes passed...' }); }, }; }; @@ -136,6 +136,9 @@ module.exports = function registerHook() { ## 4. Develop your Custom Hook +> Hooks can impact performance when not carefully implemented. This is especially true for `before` hooks (as these are +> blocking) and hooks on `read` actions, as a single request can result in a large ammount of database reads. + ### Register Function The register function (eg: `module.exports = function registerHook()`) must return an object where the key is the event,