Merge pull request #424 from directus/hooks

Emit events for system collections
This commit is contained in:
Rijk van Zanten
2020-09-25 17:27:45 -04:00
committed by GitHub
3 changed files with 45 additions and 45 deletions

View File

@@ -3,6 +3,6 @@ import { EventEmitter2 } from 'eventemitter2';
const emitter = new EventEmitter2({ wildcard: true, verboseMemoryLeak: true, delimiter: '.' });
// No-op function to ensure we never end up with no data
emitter.on('item.*.*.before', (input) => input);
emitter.on('*.*.before', input => input);
export default emitter;

View File

@@ -28,11 +28,13 @@ export class ItemsService implements AbstractService {
collection: string;
knex: Knex;
accountability: Accountability | null;
eventScope: string;
constructor(collection: string, options?: AbstractServiceOptions) {
this.collection = collection;
this.knex = options?.knex || database;
this.accountability = options?.accountability || null;
this.eventScope = this.collection.startsWith('directus_') ? this.collection.substring(9) : 'item';
return this;
}
@@ -52,20 +54,20 @@ export class ItemsService implements AbstractService {
knex: trx,
});
if (this.collection.startsWith('directus_') === false) {
const customProcessed = await emitter.emitAsync(
`item.create.${this.collection}.before`,
payloads,
{
event: `item.create.${this.collection}.before`,
accountability: this.accountability,
collection: this.collection,
item: null,
action: 'create',
payload: payloads,
}
);
const customProcessed = await emitter.emitAsync(
`${this.eventScope}.create.before`,
payloads,
{
event: `${this.eventScope}.create.before`,
accountability: this.accountability,
collection: this.collection,
item: null,
action: 'create',
payload: payloads,
}
);
if (customProcessed) {
payloads = customProcessed[customProcessed.length - 1];
}
@@ -169,18 +171,16 @@ export class ItemsService implements AbstractService {
await cache.clear();
}
if (this.collection.startsWith('directus_') === false) {
emitter
.emitAsync(`item.create.${this.collection}`, {
event: `item.create.${this.collection}`,
accountability: this.accountability,
collection: this.collection,
item: primaryKeys,
action: 'create',
payload: payloads,
})
.catch((err) => logger.warn(err));
}
emitter
.emitAsync(`${this.eventScope}.create`, {
event: `${this.eventScope}.create`,
accountability: this.accountability,
collection: this.collection,
item: primaryKeys,
action: 'create',
payload: payloads,
})
.catch((err) => logger.warn(err));
return primaryKeys;
});
@@ -267,20 +267,20 @@ export class ItemsService implements AbstractService {
let payload = clone(data);
if (this.collection.startsWith('directus_') === false) {
const customProcessed = await emitter.emitAsync(
`item.update.${this.collection}.before`,
const customProcessed = await emitter.emitAsync(
`${this.eventScope}.update.before`,
payload,
{
event: `${this.eventScope}.update.before`,
accountability: this.accountability,
collection: this.collection,
item: null,
action: 'update',
payload,
{
event: `item.update.${this.collection}.before`,
accountability: this.accountability,
collection: this.collection,
item: null,
action: 'update',
payload,
}
);
}
);
if (customProcessed) {
payload = customProcessed[customProcessed.length - 1];
}
@@ -368,8 +368,8 @@ export class ItemsService implements AbstractService {
}
emitter
.emitAsync(`item.update.${this.collection}`, {
event: `item.update.${this.collection}`,
.emitAsync(`${this.eventScope}.update`, {
event: `${this.eventScope}.update`,
accountability: this.accountability,
collection: this.collection,
item: key,
@@ -423,8 +423,8 @@ export class ItemsService implements AbstractService {
await authorizationService.checkAccess('delete', this.collection, key);
}
await emitter.emitAsync(`item.delete.${this.collection}.before`, {
event: `item.update.${this.collection}`,
await emitter.emitAsync(`${this.eventScope}.delete.before`, {
event: `${this.eventScope}.delete.before`,
accountability: this.accountability,
collection: this.collection,
item: keys,
@@ -454,8 +454,8 @@ export class ItemsService implements AbstractService {
}
emitter
.emitAsync(`item.delete.${this.collection}`, {
event: `item.delete.${this.collection}`,
.emitAsync(`${this.eventScope}.delete`, {
event: `${this.eventScope}.delete`,
accountability: this.accountability,
collection: this.collection,
item: keys,

View File

@@ -40,7 +40,7 @@ export class MetaService {
const dbQuery = database(collection).count('*', { as: 'count' });
if (query.filter) {
applyFilter(dbQuery, query.filter);
applyFilter(dbQuery, query.filter, collection);
}
const records = await dbQuery;