If you're like me, then you are probably building web applications using Node.JS and MySQL (and maybe Redis, too). If so, you're probably going to need transactions, and you've probably already noticed that the current version of node-mysql doesn't support transactions yet. :(
But that's OK because I have a solution for you. Check out node-mysql-queues on github. This project provides pretty good support for MySQL transactions with a fairly simple API. There are a couple of things to remember, though. For one, Node.JS is very "callback-centric," so when executing a series of queries, you would normally chain the queries together with a series of callbacks. node-mysql sort of changes this model, by allowing you to place queries on a queue to be executed in order. If you only care about doing something when all of your queries are done, you can simply put your callback in the final query. node-mysql-queues allows you to do the same sort of thing.
Also note that node-mysql-queues solves the problem of queries getting intermixed during a transaction. For example, if you have a web server running and multiple users are accessing the web site, you might have many different database calls running "simultaneously". Well... Node is actually single-threaded; nevertheless, it is still possible for a query from client #2 to be queued while another query for client #1 is being executed. Suppose that client #1 performs an action that starts a transaction. In the middle of the transaction, client #2 could run some queries (possibly a ROLLBACK or COMMIT) that could interfere with client #1's transaction. Long story short, you don't have to worry about this using node-mysql-queues. :) Enjoy!
But that's OK because I have a solution for you. Check out node-mysql-queues on github. This project provides pretty good support for MySQL transactions with a fairly simple API. There are a couple of things to remember, though. For one, Node.JS is very "callback-centric," so when executing a series of queries, you would normally chain the queries together with a series of callbacks. node-mysql sort of changes this model, by allowing you to place queries on a queue to be executed in order. If you only care about doing something when all of your queries are done, you can simply put your callback in the final query. node-mysql-queues allows you to do the same sort of thing.
Also note that node-mysql-queues solves the problem of queries getting intermixed during a transaction. For example, if you have a web server running and multiple users are accessing the web site, you might have many different database calls running "simultaneously". Well... Node is actually single-threaded; nevertheless, it is still possible for a query from client #2 to be queued while another query for client #1 is being executed. Suppose that client #1 performs an action that starts a transaction. In the middle of the transaction, client #2 could run some queries (possibly a ROLLBACK or COMMIT) that could interfere with client #1's transaction. Long story short, you don't have to worry about this using node-mysql-queues. :) Enjoy!
Comments