Mysql service query interface for simple build sql query through mysql pool connections.
Warning! This repository renamed to mysql-query-js, please use a new version of the module.
Example:
var query = require('mysql-query');
var qs = "show databases";
query(qs, ['param1','param2'], 'master', function(err, rows, fields) {
if (rows.length) {
console.log(rows);
process.exit(0);
} else {
new Error('There is an Error ');
}
});
In your project directory, install and verify using npm:
my-project$ npm init
Edit the default package.json file:
(example fragment of package.json file):
"dependencies": {
"mysql-query": "git://github.com/ria-com/node-mysql-query.git#master"
}
or
"dependencies": {
"mysql-query": "https://github.com/ria-com/node-mysql-query/tarball/master"
}
Install the package:
my-project$ npm install mysql-query
Edit the mysql configuration file:
my-project$ vi config/default.js
module.exports = {
database: {
master: {
host: "localhost",
user: "root",
password: "",
port: "3306",
database: "test",
connectionLimit: 3
}
}
See config about using 'config' module
In mysql-query module directory:
(don't forget to edit 'config/default.js')
mysql-query-dir$ npm install
mysql-query-dir$ npm test
This module reconnects to database 5 times by default. But this limit may be exceeded. Therefore you can specify special section in config file databasePool. In this section restartTimeout must be specified. For example:
module.exports = {
databasePool: {
restartTimeout: 10000
},
database: {
master: {
host: "localhost",
user: "root",
password: "",
port: "3306",
database: "test",
connectionLimit: 3
}
}
}
It will tells node.js that it must reconnects to MySQL after 10 seconds
You can access to connections pool object via this module. For example:
var query = require("mysql-query");
query.pool.getConnection(function(connection){
/* your code here */
})