mirror of
https://github.com/directus/directus.git
synced 2026-01-31 00:48:16 -05:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ module.exports = function registerHook({ exceptions }) {
|
||||
| `error` | | No |
|
||||
| `auth` | `login`, `logout`<sup>[1]</sup> and `refresh`<sup>[1]</sup> | Optional |
|
||||
| `oauth.:provider`<sup>[2]</sup> | `login` and `redirect` | Optional |
|
||||
| `items` | `create`, `update` and `delete` | Optional |
|
||||
| `items` | `read`<sup>[3]</sup>, `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,
|
||||
|
||||
Reference in New Issue
Block a user