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

mysql-suspend

v1.0.0

Published

ES6 wrapper for MySQL database connections using generator async flow controls.

Downloads

2

Readme

mysql-suspend

MySQL database wrapper that provides helpers to query the database.

  • Select, Insert, Update and Delete Query Builders
  • Merge Command allowing insert or update in one command
  • Suspend integration for generator-based async control-flow
  • Idle Connection Auto Closer
  • No transpiling required

requirements

  • ECMAScript 2015 (ES6)
  • Node.JS 6.2.2 or later (tested on 6.2.2)

install

npm install mysql-suspend

Copy the dbConfig.ex.json file into your project source folder, rename to dbConfig.json, and update with your database connection information.

asynchronous nature

All methods that include a callback (cb) have been designed to be used with the suspend library and may be placed behind a yield command. Be sure to leave the cb parameter null to use the suspend.resume functionality automatically.

IMPORTANT: You will also need to set the resume reference in the constructor or the setResume() method, before the suspend.resume functionality will be enabled.

If you do provide a callback, the 3rd parameter, "next" (ex: cb(err,result, next)) will be the suspend.resume function reference so you may resume execution to move past the next yield command.

basic example

	var suspend = require("suspend");
	var MysqlSuspend = require("mysql-suspend");

	//Attach your dbConfig to the mysql-suspend module
	MysqlSuspend.config = require('./dbConfig.json');

	var db = new MysqlSuspend(suspend.resume);

	suspend(function*() {
	   yield db.start();
	   
	   //... place query methods here ...
	   
	   yield db.end();
	})();

accessing rows

There are 3 ways to loop over the resulting rows of a query.

1: Standard callback.

	yield db.query("select email from friends where user_id=? ;",[userid],
		function(err,rows,cbResume) {
			for (let row of rows) {
				console.log(row.email);
			}
			
			//move past the yield
			cbResume();
	});

2: Rows Getter.

	yield db.query("select email from friends where user_id=? ;",[userid]);	
	
	for (let row of db.rows) {
		console.log(row.email);
	}

3: Async Looping (non-blocking)

	yield db.query("select email from friends where user_id=? ;",[userid]);
	
	yield db.asyncForEach(function(index,row,cbNext) {
		console.log(row.email);
		cbNext();
	});

checking for errors and rowcount

After every query you should check if it was an error.

	if (db.error()) {
		console.error("Database error: ",db.error());
		return;
	}

And you may also want to check how many rows were returned before looping.

	if (db.rowCount > 0) {
		//.. loop
		
	} else {
		console.log("No rows found.");
	}

auto closer

Enabling auto closer in the dbConfig.json file allows database connections that you leave open to be automatically close after a timeout interval provided in minutes.

By default this is not enabled, however you may want to keep this enabled and watch the console to see if the auto closer picks up on any open connections so you can address it and proprely close it when you are done.

merge utility

Conditionally insert or update a row. If the row exists, update the columns; otherwise, insert as a new row.

See Merge Helper below.

query helpers examples

The Helpers will help you build SQL statements and provide parameterized values which safeguard your queries from SQL injections. Any property that is labled as "value" will be converted to a parameter internally.

Select Helper
	//..
	yield db.selectHelper({
		 table:"users",
		 columns:["username","email"],
		 where: db.whereHelper({
			 "username -like":"t%"
		 }),
		 orderBy: db.orderByHelper({
			 "email":"ASC"
		 })
	});
		 
	for (let row of db.rows) {
		 console.log(row);
		 //Output example: {username:"Tester",email:"[email protected]"}
	}
	//..
Insert Helper
	//..
	yield db.insertHelper({
	   table:"users",
	   columns:{
		   "username":"tester",
		   "email":"[email protected]"
	   }
	});
	//..
Update Helper
	//..
	yield db.updateHelper({
	  table: "users",
	  columns: {
		  "email":"[email protected]"
	  },
	  where: db.whereHelper({
		  "username":"tester"
	  })
	});
	//..
Delete Helper
	//..
	yield db.deleteHelper({
	  table: "users",
	  where: db.whereHelper({
		  "username":"tester"
	  })
	});
	//..
Merge Helper
	//..
	yield db.mergeHelper({
	  table: "users",
	  columns: {
		  "username":"tester",
		  "email":"[email protected]"
	  },
	  where: db.whereHelper({
		  "username":"tester"
	  })
	});
	//..

Module Dependencies