0.0.2 • Published 5 years ago

nodejs-mysql-driver v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Nodejs-MySQL-Driver

How to use

Nodejs-MySQL-Driver is a wrapper around mysql. You can use it as follows:

const mysql = require('nodejs-mysql-driver');

mysql.query("SELECT * FROM ? WHERE test = ?", ["testtable", 1], (err, res) => {
	if(err) throw err;
	console.log(res.data);
	console.log(res.fields);
})

To define a mysql server you have to add an environment variable DATABASE_URL.

DATABASE_URL=mysql://user:password@host:port/database

Callbacks

  • err
  • res

If the query wasn't interrupted by an error, err will be null. res is an object with two default indexes (data & fields). data will return the query results and fields the associated field information.

Query with multiple SQL statements

To query e.g. an SQL-File with multiple statements like:

CREATE DATABASE Test;
USE TEST;
CREATE TABLE TestTable (
	PK_ID int NOT NULL PRIMARY KEY AUTO_INCREMENT,
	Text varchar(255) NOT NULL
);

You should use mysql.queryMultiple(...).

mysql.queryMultiple("CREATE DATABASE Test; USE TEST; ...", null, (err, res) => {
	if(err) throw err;
	console.log(res.data);
	console.log(res.fields);
}) ;

Security

mysql has a built in SQL-Injection prevention which is active if you use the array in the second part of variable decleration in the function. If you do not want/need this just use null instead of the array.