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

namshi-node-mysql

v2.1.0

Published

Small wrapper for mysql2.

Downloads

64

Readme

node-mysql

This wrapper provides some enhancements for node-mysql2

Build Status

Installation

This module can be installed with either yarn or npm:

$ yarn add namshi-node-mysql
$ npm install namshi-node-mysql --save

Example Usage of query

query() uses prepared statements but does not support bulk operations.


let config = {
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db"
}

let db = require('namshi-node-mysql')(config);

db.query('UPDATE foo SET key = ?', ['value']).then(() => {
	return db.query('SELECT * FROM foo');
}).spread(rows => {
	console.log('Look at all the foo', rows);
});

// using multiple databases, you can add a "name" key to your config object. For example:
let config = {
	name: "second-db",
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db"
}

let db2 = require('namshi-node-mysql')(config);

db2.query('SELECT * FROM users').spread(users => {
	console.log('Hello users', users);
});

Enable DEBUG mode to log the query being executed and its parameters.

// You can enable debugging by passing the `debug` parameter as follow:
// by default it is set to false.

let config = {
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db",
	debug: true;
}

Example Usage of bulk

bulk() uses execute which supports prepared statements and we use prepared statements for bulk.


let config = {
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db"
}

var values = [
    ['demian', '[email protected]', 1],
    ['john', '[email protected]', 2],
    ['mark', '[email protected]', 3],
    ['pete', '[email protected]', 4]
];
let db = require('namshi-node-mysql')(config);

db.bulk('INSERT INTO foo (name, email, n) VALUES ?', values).then(() => {
	return db.query('SELECT * FROM foo');
}).spread(rows => {
	console.log('Look at all the foo', rows);
});

Example of prepareBulk

prepareBulk() can be used if you want to format a query for bulk operation with a connection reused for a transaction.


let config = {
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db"
}

var values = [
    ['demian', '[email protected]', 1],
    ['john', '[email protected]', 2],
    ['mark', '[email protected]', 3],
    ['pete', '[email protected]', 4]
];
let db = require('namshi-node-mysql')(config);
let connection;

db.startTransaction().then(conn => {
	connection = conn;
}).then(() => {
	let [query, params] = db.prepareBulk('INSERT INTO foo (name, email, n) VALUES ?', [values]);
	return connection.execute(query, params);
}).then(result => {
	return db.commit(connection);
}).then(result => {
	console.log('Rows committed');
}).catch(err => {
	db.rollback(connection).then(result => {
		console.log('Rollback executed due to ', err.message);
	});
})

Example usage of namedPlaceholders

let config = {
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db",
	namedPlaceholders: true
}

let db = require('namshi-node-mysql')(config);

db.query('SELECT * FROM users WHERE LIMIT = :limit', {limit: 10}).spread( users => {
	console.log('Hello users', users);
});

Example usage of startTransaction, commit and rollback

let config = {
	host: "localhost",
	user: "foo",
	password: "bar",
	database: "db"
}

let db = require('namshi-node-mysql')(config);

let connection;

db.startTransaction(30).then(conn => {
	connection = con;
}).catch(err) {
	//handle error
};

//default timeout here is set to 20
db.startTransaction().then(conn => {
	connection = con;
}).catch(err) {
	//handle error
};

db.commit(connection).catch(err => {
	//handle err
});

db.rollback(connection).catch(err => {
	//handle err
});

Credits

This library depends on node-mysql2. It is also considered a breaking-change upgrade of node-mysql2-promise.