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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ying-mysqldb

v0.1.2

Published

## Features

Readme

Node JS MySQL utilities with squel.js

Features

  • Automatic Transaction management
  • Automatic Connection management
  • utilities API to simplify DB Accessing

Synopsis

MYSQL dbConfig.json

{
	"master":{
		"host": "localhost",
		"user": "user1",
		"password": "1122sz",
		"database": "user"
	},
	"slave":{
		"host": "localhost",
		"user": "user1",
		"password": "1122sz",
		"database": "user"
	}
}

###get 1 record or null

'use strict';
var debug = require('debug')('AppServer:DBService');
var sql = require('squel').useFlavour('mysql');
var db = require('ying-mysqldb');

exports.getById = function(conn, model, id) {
	debug('getById->model:%s,id:%s', model, id);
	var ql = sql.select();
	ql.from(model);

	ql.where('id=?', id);
	return db.get(conn, ql);
};

exports.getUser = function(conn, id) {
	debug('getUser->ID:%s', id);
	return exports.getById(conn, 'user', id);
};

exports.getEmailAuth = function(conn, email) {
	debug('getEmailAuth->email:%s', email);
	var ql = sql.select();
	ql.from('email_auth');

	ql.where('email=?', email);
	return db.get(conn, ql);
};

get multi records or empty array


exports.listProfile = function(conn) {
	debug('listProfile');
	var ql = sql.select();
	ql.from('profile');
	ql.where('deleteTime=0');
	ql.order('createTime', true);

	return db.list(conn, ql);
};

Insert an object


exports.insertUser = function(conn, user) {
	debug('insertUser:user:%o', user);
	if (!user.id) {
		return Promise.reject(Errors.fieldNotFound('id is not allowed null', user));
	}

	if (!user.nickName) {
		return Promise.reject(
			Errors.fieldNotFound('nickName is not allowed null', user));
	}

	if (!user.accessToken) {
		return Promise.reject(
			Errors.fieldNotFound('accessToken is not allowed null', user));
	}

	var ql = sql.insert();
	ql.into('user');

	ql.set('id', user.id);
	ql.set('nickName', user.nickName);
	ql.set('createTime', utils.now());

	if (user.lat && user.lng) {
		ql.set('lat', user.lat);
		ql.set('lng', user.lng);
	}
	if (user.ip) {
		ql.set('ip', user.ip);
	}
	if (user.pictureID) {
		ql.set('pictureID', user.pictureID);
	}
	if (user.timezone) {
		ql.set('timezone', user.timezone);
	}
	if (user.country) {
		ql.set('country', user.country);
	}

	return db.update(conn, ql);
};

Update an object

exports.updateUser = function(conn, user) {
	debug('updateUser:user:%o', user);
	if (!user.id) {
		return Promise.reject(Errors('id is not allowed null', user));
	}

	let flag = false;
	var ql = sql.update();
	ql.table('user');

	if (user.nickName) {
		flag = true;
		ql.set('nickName', user.nickName);
	}

	if (user.role) {
		flag = true;
		ql.set('role', user.role);
	}


	if (user.pictureID) {
		flag = true;
		ql.set('pictureID', user.pictureID);
	}

	if (user.hasOwnProperty('lockTime')) {
		flag = true;
		ql.set('lockTime', user.lockTime);
	}

	if (user.hasOwnProperty('lat')) {
		flag = true;
		ql.set('lat', user.lat);
	}

	if (user.hasOwnProperty('lng')) {
		flag = true;
		ql.set('lng', user.lng);
	}

	if (user.ip) {
		flag = true;
		ql.set('ip', user.ip);
	}

	if (!flag) {
		return Promise.reject(Errors.fieldNotFound('field not allowed null', user));
	}

	ql.where('id=?', user.id);

	return db.update(conn, ql);
};

Connection Management (only select, no changing in data)


var mysql = require('mysql');

var dbconfig = require('./dbconfig');

var db = require('ying-mysqldb');

var dbserv = require('../DBService');

var pool = mysql.createPool({
	host: dbconfig.slave.host,
	user: dbconfig.slave.user,
	password: dbconfig.slave.password,
	database: dbconfig.slave.database
});


var signIn = function(usr) {
	debug('signIn->user:%o', usr);
	return db.sql(pool, conn => {
		return dbserv.getEmailAuth(conn, usr.email).then(auth => {
			if (!auth) {
        throw Errors.notFound('email is not found.', {user: usr});
      }
			if (usr.pwd !== auth.pwd) {
        throw Errors.mismatched('password is mismatched.', {auth:maskAuth(auth)});
      }
      return dbserv.getUser(conn, auth.userID).then(user => {
        if (user.lockTime > 0) {
          throw Errors.isLocked('user is locked.', {user: maskUser(user)});
        }
        return user;
      });
		});
	});
};

Automatic Transaction management (multi insert/update/delete)


var mysql = require('mysql');

var dbconfig = require('../dbconfig');

var db = require('ying-mysqldb');

var dbserv = require('../DBService');

var pool = mysql.createPool({
	host: dbconfig.master.host,
	user: dbconfig.master.user,
	password: dbconfig.master.password,
	database: dbconfig.master.database
});

var saveUserInfo = function(sec_userID, userInfo) {
	debug('start saveUserInfo. userInfo:%o', userInfo);

	return db.tx(pool, conn => {
		return dbserv.getUserInfo(conn, userInfo.userID).then(usrInfo => {
			if (!usrInfo) {
				return dbserv.insertUserInfo(conn, userInfo);
			} else {
				return dbserv.updateUserInfo(conn, userInfo);
			}
		});
	});
};