Register event on startup

This commit is contained in:
Alexandre Bodin 2019-12-17 20:59:57 +01:00
parent 1166d80332
commit a57354677d
5 changed files with 50 additions and 15 deletions

View File

@ -8,6 +8,7 @@ const { createQuery } = require('./queries');
class DatabaseManager {
constructor(strapi) {
this.strapi = strapi;
this.eventHub = strapi.eventHub;
this.initialized = false;
@ -87,7 +88,12 @@ class DatabaseManager {
.get(model.orm)
.queries({ model, modelKey: normalizedName, strapi });
const query = createQuery({ connectorQuery, model });
const query = createQuery({
connectorQuery,
model,
eventHub: this.eventHub,
});
this.queries.set(model.uid, query);
return query;
}

View File

@ -1,11 +1,12 @@
'use strict';
module.exports = function createQuery({ connectorQuery, model }) {
return new Query(connectorQuery, model);
module.exports = function createQuery(opts) {
return new Query(opts);
};
class Query {
constructor(connectorQuery, model) {
constructor({ model, connectorQuery, eventHub }) {
this.eventHub = eventHub;
this.connectorQuery = connectorQuery;
this.model = model;
}
@ -52,15 +53,21 @@ class Query {
}
async create(...args) {
return this.connectorQuery.create(...args);
const entry = await this.connectorQuery.create(...args);
this.eventHub.emit('entry.create', entry);
return entry;
}
async update(...args) {
return this.connectorQuery.update(...args);
const entry = await this.connectorQuery.update(...args);
this.eventHub.emit('entry.update', entry);
return entry;
}
async delete(...args) {
return this.connectorQuery.delete(...args);
const entry = await this.connectorQuery.delete(...args);
this.eventHub.emit('entry.delete', entry);
return entry;
}
async count(...args) {

View File

@ -86,6 +86,8 @@ module.exports = {
if (file.tmpPath) {
fs.unlinkSync(file.tmpPath);
}
strapi.eventHub.emit('media.create', res);
return res;
};
@ -126,6 +128,12 @@ module.exports = {
await actions.delete(file);
}
const media = strapi.query('file', 'upload').findOne({
id: file.id,
});
strapi.eventHub.emit('media.delete', media);
return strapi.query('file', 'upload').delete({ id: file.id });
},

View File

@ -90,7 +90,10 @@ class Strapi extends EventEmitter {
// internal services.
this.fs = createStrapiFs(this);
this.eventHub = createEventHub();
this.webhookRunner = createWebhookRunner({ eventHub: this.eventHub });
this.webhookRunner = createWebhookRunner({
eventHub: this.eventHub,
logger: this.log,
});
this.initConfig(opts);
this.requireProjectBootstrap();
@ -377,11 +380,18 @@ class Strapi extends EventEmitter {
this.webhookStore = createWebhookStore({ db: this.db });
await this.startWebhooks();
// Initialize hooks and middlewares.
await initializeMiddlewares.call(this);
await initializeHooks.call(this);
}
async startWebhooks() {
const webhooks = await this.webhookStore.findWebhooks();
this.webhookRunner.register(webhooks);
}
reload() {
const state = {
shouldReload: 0,

View File

@ -6,8 +6,9 @@
const fetch = require('node-fetch');
class WebhookRunner {
constructor({ eventHub }) {
constructor({ eventHub, logger }) {
this.eventHub = eventHub;
this.logger = logger;
}
run(webhook, event, info = {}) {
@ -25,20 +26,23 @@ class WebhookRunner {
'X-Strapi-Event': event,
'Content-Type': 'application/json',
},
timeout: 10000,
})
.then(res => {
console.log(res.status);
this.logger.info(res.status);
})
.catch(err => {
console.log('Error', err);
this.logger.error('Error', err);
});
}
register(webhook) {
const { events } = webhook;
register(webhooks) {
webhooks.forEach(webhook => {
const { events } = webhook;
events.forEach(event => {
this.eventHub.on(event, this.run.bind(this, webhook, event));
events.forEach(event => {
this.eventHub.on(event, info => this.run(webhook, event, info));
});
});
}
}