I am building a product management platform using node.js and my data is saved in a MySQL database. At this point I needed to use MySQL's event scheduler to perform some actions at specific times and it all goes great when I test it in the MySQL console but when it comes to do it through node.js I am having the following error:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ CREATE EVENT IF NOT EXISTS test ON SCHEDULE AT DATE_ADD(NOW(), ' at line 1
I am using node-orm2 to connect to my database and I am doing the following
var query = 'DROP EVENT IF EXISTS test;' +
' DELIMITER $$' +
' CREATE EVENT IF NOT EXISTS test' +
' ON SCHEDULE AT DATE_ADD(NOW(), INTERVAL 10 SECOND) ON COMPLETION PRESERVE ENABLE' +
' DO BEGIN' +
' SELECT @data_id:=quiz_platforms_has_quiz_data.quiz_data_id,' +
' @date_end:=quiz_platforms_has_quiz_data.timestamp_end' +
' FROM quiz_platforms_has_quiz_data' +
' WHERE quiz_platforms_has_quiz_data.quiz_platforms_id=5' +
' AND quiz_platforms_has_quiz_data.timestamp_start >= CURDATE()' +
' ORDER BY timestamp_start LIMIT 1;' +
' UPDATE quiz_platforms SET quiz_platforms.current_quiz_id=@data_id' +
' WHERE quiz_platforms.id=5;' +
' IF @date_end>0 THEN' +
' ALTER EVENT test ON SCHEDULE AT @date_end;' +
' ALTER EVENT test ENABLE;' +
' ELSE' +
' DROP EVENT test;' +
' END IF;' +
' END $$' +
' DELIMITER ; ';
orm_db.driver.execQuery(query, function(err, result) {
if (err) {
console.log(NOW + " - [DB-Event-ERR] " + err);
}
});
The thing is: I already know neither this library nor node-mysql itself support this and I wanted to know if there is a way.
I also thought about using a cron manager and do this from the server instead of using the database's event schedule but I have no idea what would be the lightest solution.
[EDIT]
I need to use the event scheduler through node.js and I haven't successfully done it. I can't reveal much details because of company policy. Let's just say we have devices that run a certain task during a time interval. I have to update a column in a database table so that it represents which task is currently running in that device.
table devices (columns): id, name, description, current_task;
table tasks(columns): id, name, description;
So if I were to check the device current_task, I would know just by going to the device's table.
I have to be able to update that column's value as soon as the task changes.