const {models} = require('vn-loopback/server/server'); module.exports = Self => { app.on('started', function() { let models = ['Zone', 'ZoneEvent', 'ZoneExclusion']; for (let modelName of models) { let Model = models[modelName]; Model.observe('after save', doCalc); Model.observe('after delete', doCalc); } async function doCalc(ctx) { try { await Self.rawSql(` CREATE EVENT zoneClosure_doRecalc ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 15 SECOND DO CALL zoneClosure_recalc; `); } catch (err) { if (err.code != 'ER_EVENT_ALREADY_EXISTS') throw err; } } }); };