Skip to content

Commit

Permalink
Fixed trigger finding/adding
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogs committed Jul 18, 2018
1 parent 061ce52 commit 8949adc
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 79 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rules:
strict: off
max-len: off
no-console: off
no-continue: off
globals:
process: on
describe: on
Expand Down
32 changes: 15 additions & 17 deletions lib/MySQLEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MySQLEvents extends EventEmitter {

this.zongJi = null;
this.started = false;
this.triggers = {};
this.expressions = {};
}

/**
Expand All @@ -44,7 +44,7 @@ class MySQLEvents extends EventEmitter {
*/
_handleEvent(event) {
event = eventHandler.normalizeEvent(event);
const triggers = eventHandler.findTriggers(event, this.triggers);
const triggers = eventHandler.findTriggers(event, this.expressions);

Promise.all(triggers.map(async (trigger) => {
try {
Expand Down Expand Up @@ -130,21 +130,19 @@ class MySQLEvents extends EventEmitter {
if (!name) throw new Error('Missing trigger name');
if (!expression) throw new Error('Missing trigger expression');

const trigger = this.triggers[expression] || {};
trigger.statements = trigger.statements || {};
trigger.statements[statement] = trigger.statements[statement] || [];
this.expressions[expression] = this.expressions[expression] || {};
this.expressions[expression].statements = this.expressions[expression].statements || {};
this.expressions[expression].statements[statement] = this.expressions[expression].statements[statement] || [];

const statmnt = trigger.statements[statement];
if (statmnt.find(st => st.name === name)) {
const triggers = this.expressions[expression].statements[statement];
if (triggers.find(st => st.name === name)) {
throw new Error(`There's already a trigger named "${name}" for expression "${expression}" with statement "${statement}"`);
}

statmnt.push({
triggers.push({
name,
callback,
});

this.triggers[expression] = trigger;
}

/**
Expand All @@ -158,17 +156,17 @@ class MySQLEvents extends EventEmitter {
expression,
statement = STATEMENTS.ALL,
}) {
const trigger = this.triggers[expression];
if (!trigger) return;
const exp = this.expressions[expression];
if (!exp) return;

const statmnt = trigger.statements[statement];
if (!statmnt) return;
const triggers = exp.statements[statement];
if (!triggers) return;

const named = statmnt.find(st => st.name === name);
const named = triggers.find(st => st.name === name);
if (!named) return;

const index = statmnt.indexOf(named);
statmnt.splice(index, 1);
const index = triggers.indexOf(named);
triggers.splice(index, 1);
}
}

Expand Down
7 changes: 3 additions & 4 deletions lib/eventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ const findTriggers = (event, triggers) => {
const trigger = triggers[expression];

const parts = parseExpression(expression);
if (parts.schema !== '*' && parts.schema !== event.schema) return [];
if (!(!parts.table || parts.table === '*') && parts.table !== event.table) return [];
if (!(!parts.column || parts.column === '*') && event.affectedColumns.indexOf(parts.column) === -1) return [];
if (parts.schema !== '*' && parts.schema !== event.schema) continue;
if (!(!parts.table || parts.table === '*') && parts.table !== event.table) continue;
if (!(!parts.column || parts.column === '*') && event.affectedColumns.indexOf(parts.column) === -1) continue;

if (trigger.statements[STATEMENTS.ALL]) statements.push(...trigger.statements[STATEMENTS.ALL]);
if (trigger.statements[event.type]) statements.push(...trigger.statements[event.type]);
Expand All @@ -57,7 +57,6 @@ const findTriggers = (event, triggers) => {
*/
const eventHandler = {
normalizeEvent,

findTriggers,
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rodrigogs/mysql-events",
"version": "0.3.0",
"version": "0.3.1",
"license": "BSD-3-Clause",
"description": "A node.js package that watches a MySQL database and runs callbacks on matched events like updates on tables and/or specific columns.",
"homepage": "https://github.com/rodrigogs/mysql-events",
Expand Down Expand Up @@ -31,7 +31,7 @@
"chai": "^4.1.2",
"codeclimate-test-reporter": "^0.5.0",
"dotenv-cli": "^1.4.0",
"eslint": "^5.0.0",
"eslint": "^5.1.0",
"eslint-config-airbnb-base": "^13.0.0",
"eslint-plugin-import": "^2.13.0",
"mocha": "^5.2.0",
Expand Down
Loading

0 comments on commit 8949adc

Please sign in to comment.