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

node-mysql-helper-fork

v0.0.1

Published

A lightweight Promise-based wrapper and helper for felixge's node-mysql. Forked from Greg Bate (https://github.com/gdbate)

Downloads

5

Readme

node-mysql-helper

A lightweight Promise-based wrapper and helper for felixge's node-Mysql.

##Features:

  • Very slim library, only 2 dependancies (Q and felixge's node-mysql).
  • Convenience functions for record selecting, inserting, updating and insert (on duplicate) updating.
  • Connection pooling.
  • Everything based on Promises.

##Initialize

Format you connection options based on felixge's options.


var Mysql = require('node-mysql-helper');

var mysqlOptions = {
	host: 'localhost',
	user: 'myuser',
	password: 'chicken',
	database: 'mydb',
	socketPath: false,
	connectionLimit: 5
};

//For 5 pooled connections
Mysql.connect(mysqlOptions);

Selecting a record


//find user id 35
Mysql.record('user', 35)
	.then(function(record){
		console.log(record);
	})
	.catch(function(err){
		console.log('Error fetching record, mysql error:', err.message);
	});

//or select with an object
Mysql.record('user', {id: 35})
	.then(function(record){
		console.log(record);
	})
	.catch(function(err){
		console.log('Error fetching record, mysql error:', err.message);
	});

Inserting a record


var insert = {
	email: '[email protected]',
	firstName: 'Greg',
	lastName: 'Bate',
	createdAt: '2015-10-07 21-22:31'
};

Mysql.insert('user', insert)
	.then(function(info){
		console.log('New User Entered!', info);
	})
	.catch(function(err){
		console.log('Error creating new user, mysql error:', err.message);
	});

//info is an object with affectedRows and insertId

There is also a boolean 3rd argument, true if you want "INSERT IGNORE"

Updating a record


var where = {
	firstName: 'Greg',
	lastName: 'Bate'
};

var update = {
	lastSeen: '2015-10-07 21-54:58'
};

Mysql.update('user', where, update)
	.then(function(info){
		console.log('User Updated!', info);
	})
	.catch(function(err){
		console.log('Error updating record, mysql error:', err.message);
	});

//info is an object with affectedRows, changedRows

Insert (on duplicate) update a record

Used in case entering data conflicts with a unique index


var insert = {
	email: '[email protected]',
	firstName: 'Greg',
	lastName: 'Bate',
	lastSeen: '2015-10-07 21-49:58'
};

var update = {
	lastSeen: '2015-10-07 21-49:58'
};

Mysql.insertUpdate('user', insert, update)
	.then(function(info){
		console.log('New User Entered (or updated)!', info);
	})
	.catch(function(err){
		console.log('Error creating new user, mysql error:', err.message);
	});
//info is an object with affectedRows, changedRows and insertId (where applicable)

Custom Queries

Don't forget to release the pooled connection so another process can use it.


//query has sql structure
//values will be placed in the query when escaped, and are optional
Mysql.query(query, values)
	.then(function(results){
		console.log('my query results', results);
	})
	.catch(function(err){
		reject(err);
	});

The query values are used in the same way felixge's module expects it. They are also optional.

Deleting a record


//or select with an object
Mysql.delete('user', {id: 35})
	.then(function(record){
		console.log(record);
	})
	.catch(function(err){
		console.log('Error deleting record, mysql error:', err.message);
	});

Utilities


//Get last query (after escaping values)
var last = Mysql.getLastQuery();

//Escape a database,table or column name
var table = Mysql.escapeId(tableName);

//Escape a string
var noSqlInject = Mysql.escapeId(req.body.firstName);

To Do

  • Support ES6 Promises
  • Inline documenation
  • Some Spelcheking
  • Limited convenience functions like YMD and DATETIME Functions
  • Compatibility with RDB Select Module
  • Make sure values are optional on custom query
  • More Testing (heh)