Create a webhook model to perist them outside of the core_store

This commit is contained in:
Alexandre Bodin 2019-12-23 16:38:42 +01:00
parent 9ad5b820d7
commit 36d662fc79
7 changed files with 100 additions and 68 deletions

View File

@ -16,7 +16,6 @@ module.exports = {
url, url,
headers, headers,
events, events,
isEnabled: true,
}); });
strapi.webhookRunner.add(webhook); strapi.webhookRunner.add(webhook);
@ -28,6 +27,10 @@ module.exports = {
const { id } = ctx.params; const { id } = ctx.params;
const webhook = await strapi.webhookStore.findWebhook(id); const webhook = await strapi.webhookStore.findWebhook(id);
if (!webhook) {
return ctx.notFound('webhook.notFound');
}
ctx.send({ data: webhook }); ctx.send({ data: webhook });
}, },

View File

@ -17,7 +17,7 @@ module.exports = {
// Create loaders for each relational field (exclude core models). // Create loaders for each relational field (exclude core models).
Object.keys(strapi.models) Object.keys(strapi.models)
.filter(model => model !== 'core_store') .filter(model => model.internal !== true)
.forEach(modelKey => { .forEach(modelKey => {
const model = strapi.models[modelKey]; const model = strapi.models[modelKey];
this.createLoader(model.uid); this.createLoader(model.uid);

View File

@ -135,7 +135,7 @@ const schemaBuilder = {
// build defaults schemas if shadowCRUD is enabled // build defaults schemas if shadowCRUD is enabled
if (strapi.plugins.graphql.config.shadowCRUD !== false) { if (strapi.plugins.graphql.config.shadowCRUD !== false) {
const modelCruds = Resolvers.buildShadowCRUD( const modelCruds = Resolvers.buildShadowCRUD(
_.omit(strapi.models, ['core_store']) _.omitBy(strapi.models, model => model.internal === true)
); );
shadowCRUD = Object.keys(strapi.plugins).reduce((acc, plugin) => { shadowCRUD = Object.keys(strapi.plugins).reduce((acc, plugin) => {

View File

@ -31,7 +31,10 @@ const getPrefixedDeps = require('./utils/get-prefixed-dependencies');
const createEventHub = require('./services/event-hub'); const createEventHub = require('./services/event-hub');
const createWebhookRunner = require('./services/webhook-runner'); const createWebhookRunner = require('./services/webhook-runner');
const createWebhookStore = require('./services/webhook-store'); const {
webhookModel,
createWebhookStore,
} = require('./services/webhook-store');
const { createCoreStore, coreStoreModel } = require('./services/core-store'); const { createCoreStore, coreStoreModel } = require('./services/core-store');
const { createDatabaseManager } = require('strapi-database'); const { createDatabaseManager } = require('strapi-database');
@ -366,6 +369,7 @@ class Strapi extends EventEmitter {
// Init core store // Init core store
this.models['core_store'] = coreStoreModel; this.models['core_store'] = coreStoreModel;
this.models['strapi_webhooks'] = webhookModel;
this.db = createDatabaseManager(this); this.db = createDatabaseManager(this);
await this.db.initialize(); await this.db.initialize();

View File

@ -1,6 +1,8 @@
'use strict'; 'use strict';
const coreStoreModel = { const coreStoreModel = {
uid: 'strapi::core-store',
internal: true,
connection: 'default', connection: 'default',
info: { info: {
name: 'core_store', name: 'core_store',

View File

@ -107,8 +107,8 @@ class WebhookRunner {
update(webhook) { update(webhook) {
debug(`Refreshing webhook '${webhook.id}'`); debug(`Refreshing webhook '${webhook.id}'`);
this.unregister(webhook); this.remove(webhook);
this.register(webhook); this.add(webhook);
} }
remove(webhook) { remove(webhook) {

View File

@ -3,81 +3,104 @@
*/ */
'use strict'; 'use strict';
const uuid = require('uuid'); const webhookModel = {
uid: 'strapi::webhooks',
internal: true,
connection: 'default',
globalId: 'StrapiWebhooks',
collectionName: 'strapi_webhooks',
info: {
name: 'Strapi webhooks',
description: '',
},
attributes: {
name: {
type: 'string',
},
url: {
type: 'text',
},
headers: {
type: 'json',
},
events: {
type: 'json',
},
enabled: {
type: 'boolean',
},
},
};
module.exports = ({ db }) => ({ const formatWebhookInfo = webhook => {
return {
id: webhook.id,
name: webhook.name,
url: webhook.url,
headers: webhook.headers,
events: webhook.events,
isEnabled: webhook.enabled,
};
};
const createWebhookStore = ({ db }) => {
const webhookQueries = db.query('strapi_webhooks');
return {
async findWebhooks() { async findWebhooks() {
const row = await db.query('core_store').findOne({ const results = await webhookQueries.find();
key: 'webhooks',
});
return row ? JSON.parse(row.value) : []; return results.map(formatWebhookInfo);
}, },
async findWebhook(id) { async findWebhook(id) {
const webhooks = await this.findWebhooks(); const result = await webhookQueries.findOne({ id });
return webhooks.find(hook => hook.id === id); return result ? formatWebhookInfo(result) : null;
}, },
async createWebhook(data) { createWebhook(data) {
const webhooks = await this.findWebhooks(); const { name, url, headers, events } = data;
const webhook = { const webhook = {
id: uuid(), name,
...data, url,
headers,
events,
enabled: true,
}; };
const res = await db.query('core_store').findOne({ return webhookQueries.create(webhook).then(formatWebhookInfo);
key: 'webhooks',
});
if (!res) {
await db
.query('core_store')
.create({ key: 'webhooks', value: JSON.stringify([webhook]) });
} else {
await db
.query('core_store')
.update(
{ key: 'webhooks' },
{ value: JSON.stringify(webhooks.concat(webhook)) }
);
}
return webhook;
}, },
async updateWebhook(id, data) { async updateWebhook(id, data) {
const oldWebhook = await this.findWebhook(id); const oldWebhook = await this.findWebhook(id);
const webhooks = await this.findWebhooks();
const updatedWebhook = { if (!oldWebhook) {
...oldWebhook, throw new Error('webhook.notFound');
...data,
};
const updatedWebhooks = webhooks.map(webhook => {
if (webhook.id === id) {
return updatedWebhook;
} }
return webhook; const { name, url, headers, events, isEnabled } = data;
});
await db const updatedWebhook = {
.query('core_store') name,
.update({ key: 'webhooks' }, { value: JSON.stringify(updatedWebhooks) }); url,
headers,
events,
enabled: isEnabled,
};
return updatedWebhook; return webhookQueries
.update({ id }, updatedWebhook)
.then(formatWebhookInfo);
}, },
async deleteWebhook(id) { deleteWebhook(id) {
const webhooks = await this.findWebhooks(); return webhookQueries.delete({ id });
const updatedWebhooks = webhooks.filter(webhook => webhook.id !== id);
await db
.query('core_store')
.update({ key: 'webhooks' }, { value: JSON.stringify(updatedWebhooks) });
}, },
}); };
};
module.exports = {
webhookModel,
createWebhookStore,
};