This post's content
We analyzed dozens of MySQL driver packages in NPM (world's largest software registry) and came up with a list of the top three MySQL drivers for you to use in your next NodeJS application.
To measure popularity, we will look into a few parameters: Amount of downloads in the last month, amount of contributors and amount of application depending on this package (as this means that the package is used by them).
#1 - mysql
A node.js driver for mysql, written in JavaScript, does not require compiling, and is 100% free - MIT licensed.
Downloads last month: 753,053
Packages depending on this driver: 2,280
Amount of contributors: 4
Code usage example:
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'mydbserver', user : 'user', password : 'password', database : 'schemaname' }); connection.connect(); connection.query('SELECT MAX(order_num) AS result, function (error, results, fields) { if (error) throw error; console.log('The result is: ', results[0].result); }); connection.end();
#2 - mysql2
MySQL client which gives focus on performance.
Several important features: Prepared statements, non-utf8 encodings, support for the binary log protocol, compressions, secure connection (ssl) and more.
Downloads last month: 54,594
Packages depending on this driver: 138
Amount of contributors: 3
Code usage example:
var mysql = require('mysql2'); var connection = mysql.createConnection({host:'mydbserver', user: 'user', database: 'schemaname'}); connection.query('SELECT * FROM `employees` WHERE `name` = "Bob" AND `age` > 35', function (err, results, fields) { console.log(results); }); // with placeholder connection.query('SELECT * FROM `employees` WHERE `name` = "Bob" AND `age` > ?', [45], function (err, results) { console.log(results); });
#3 - sequelize
This one is actually not a driver, but a promise based ORM for MySQL and other databases (Postgres, MariaDB, SQLite and Microsoft SQL Server).
Downloads last month: 371,577
Packages depending on this driver: 1003
Amount of contributors: 4
Code usage example:
const Sequelize = require('sequelize'); const sequelize = new Sequelize('schemaname', 'username', 'password'); const Employee = sequelize.define('name', { username: Sequelize.STRING, birthday: Sequelize.DATE }); sequelize.sync() .then(() => Employee.create({ username: 'johndoh', birthday: new Date(1988, 12, 16) }));