NodeSQL
Adaptor that wraps node-mysql and node-sqlite3 databases with a common interface. The intention is to allow fast unit testing of the database with sqlite's in memory database.
NOTE * indicates an optional parameter
Install
via npm
npm install nodesql
Setup
//using mysqlvar mysql = ;var connection = mysql; //or for sqlite3var sqlite3 = ;var connection = ':memory:'; //wrap the database connection in a nodeSql adaptor.var nodeSql = ;var db = nodeSql;//orvar db = nodeSql;
db.query(sqlStatement, *values, *callback)
SELECT If no error occurs err will be null, and the second parameter will contain an array of rows.
db;
INSERT The second parameter will be the id of the row that was just inserted.
db
UPDATE
db;
DELETE
db;
db.one(statement, *values, *callback)
returns the first result of a select statement.
db;
db.select(table, *whereEqualsObject, *callback)
//equivalent to db.query('SELECT * FROM Table WHERE id = ?', [5], function (err, rows) {});db;
db.selectOne(table, whereEqualsObject, *callback)
returns the first result of select
db
db.insert(table, row, *callback)
//equivalent to db.query('INSERT INTO Table (col) VALUES (?)', ['foo'], function (err, insertId) {});db;
db.update(table, rowEdits, whereEqualsObject, *callback)
//equivalent to db.query('UPDATE Table SET col = ? WHERE id = ?', ['edit', 5], function (err) {});db;
db.delete(table, whereEqualsObject, *callback)
//equivalent to db.query('DELETE FROM Table WHERE id = ?', [5], function (err) {});db;
db.transaction(statements, *callback)
perform multiple sql statements that will be wrapped in a transaction.
db;
db.escape(value)
escapes a value used in query to protect against sql injection. using a prepared statement is preferred. Warning: No Escaping is actually implemented for sqlite! (simply returns value)
Promises
All NodeSQL that take callbacks also return Q promises.
db;