npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodesql

v0.2.4

Published

Adaptor for node-mysql and node-sqlite3

Downloads

24

Readme

#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 mysql
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret',
  database : 'databaseName',
  //only set the multipleStatements setting if you plan on using the transaction
  //method (and be careful) as it potentially exposes you to sql injection.
  multipleStatements: true
});

//or for sqlite3
var sqlite3 = require('sqlite3').verbose();
var connection = new sqlite3.Database(':memory:');

//wrap the database connection in a nodeSql adaptor.
var nodeSql = require('nodesql');
var db = nodeSql.createMySqlStrategy(connection);
//or
var db = nodeSql.createSqliteStrategy(connection);

###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.query('SELECT * FROM Table', function (err, rows) {
    if(!err) {
        console.log(rows);
    }
    else {
        console.log("An Error Occured");
        console.log(err);
    }
});

INSERT The second parameter will be the id of the row that was just inserted.

db.query('INSERT INTO Table (col) VALUES (?)', ['columnValue'], function (err, insertId) {
    if(!err) {
        console.log(insertId)
    }
})

UPDATE

db.query('UPDATE ...', ..., function (err) {});

DELETE

db.query('DELETE ...', ..., function (err) {});

###db.one(statement, *values, *callback) returns the first result of a select statement.

db.one('SELECT * FROM Table WHERE id = ?', [5], function (err, row) {
    //row is the first element of what would normally be
    //an array of rows.
});

###db.select(table, *whereEqualsObject, *callback)

//equivalent to db.query('SELECT * FROM Table WHERE id = ?', [5], function (err, rows) {});
db.select('Table', { id: 5 }, function (err, rows) {});

###db.selectOne(table, whereEqualsObject, *callback) returns the first result of select

db.selectOne('Table', { id: 5 }, function (err, row) {
    //row is the first element of what would normally be
    //an array of rows.
})

###db.insert(table, row, *callback)

//equivalent to db.query('INSERT INTO Table (col) VALUES (?)', ['foo'], function (err, insertId) {});
db.insert('Table', { col: 'foo' }, function (err, insertId) {});

###db.update(table, rowEdits, whereEqualsObject, *callback)

//equivalent to db.query('UPDATE Table SET col = ? WHERE id = ?', ['edit', 5], function (err) {});
db.update('Table', { col: 'edit' }, { id: 5 }, function (err) {});

###db.delete(table, whereEqualsObject, *callback)

//equivalent to db.query('DELETE FROM Table WHERE id = ?', [5], function (err) {});
db.delete('Table', { id: 5 }, function (err) {});

###db.transaction(statements, *callback) perform multiple sql statements that will be wrapped in a transaction.

db.transaction(['INSERT INTO ...', 'INSERT INTO ...']);

##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.select('Table', {id: 5}).then(
    function (rows) {},
    function (err) {}
);