Is there a Node.JS Driver for MySQL that is commonly used other than node-mysql?
(It seems like there is not much activity with node.js database drivers. Is there a reason for this or is it just because Node.JS is so young?)
Is there a Node.JS Driver for MySQL that is commonly used other than node-mysql?
(It seems like there is not much activity with node.js database drivers. Is there a reason for this or is it just because Node.JS is so young?)
Here are some options:
You can also try out a newer effort known as Node.js DB that aims to provide a common framework for several database engines. It is built with C++ so performance is guaranteed.
Specifically you could use its db-mysql driver for Node.js MySQL support.
Just look at https://github.com/Sannis/node-mysql-libmysqlclient.
If you need an ORM for MySQL you might want to check out http://sequelizejs.com :)
For connecting to MySQL with node.js, I've had great success using node-odbc
It's also worked flawlessly for connecting to other databases such as IBM's DB2, and it's been surprisingly fast.
This page is particularly useful for configuring ODBC on linux.
After installing with yum install mysql-connector-odbc
, my /etc/odbc.ini file looks like this:
[MYSQL]
Description = MySQL ODBC Driver
Driver = /usr/lib64/libmyodbc3.so
I left out stuff such as server, user, database, port, password etc. so that I can set these from my connection string (I need to connect to multiple databases).
After saving /etc/odbc.ini, it's installed with this command: odbcinst -i -s -l -f /etc/odbc.ini
And here's a code sample for testing it out:
var odbc = require("odbc");
var db = new odbc.Database();
var conn = "dsn=mysql;server=localhost;user=root;database=mydb;port=3306;password=mypwd;command timeout=30000;";
db.open(conn, function(err) {
if(err) throw err;
var params = ['jiy@stackoverflow.com'];
var qry = "select * users where email = ?";
db.query(qry, params, function(err, rows, def) {
if(err) console.log(err);
console.log(rows);
});
});
Or if you wanted to use coffeescript:
odbc = require "odbc"
db = new odbc.Database()
conn = "dsn=mysql;server=localhost;user=root;database=mydb;port=3306;password=mypwd;command timeout=30000;"
db.open conn, (err) ->
throw err if err
qry = "select * from users where email = ?"
db.query sql, ["jiy@stackoverflow.com"], (err, rows, def) ->
if err? then console.log err else
console.log rows