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

jaypha-mysql-ext

v1.0.3

Published

Some convenience functions to extend the mysql2 module

Downloads

5

Readme

Jaypha MySQL Ext

Written by Jason den Dulk

A set of convenience functions to extend the functionality of your mysql connection. These functions reflect very common database related tasks, and can help reduce code overhead.

This module comes in two versions.

The first uses monkey patching (by adding functions to the mysql2 object, no functions are overriden).

The second wraps the mysql2 object in another object contining the provided functions.

Requirements

Node (v7.7.3), mysql2

Makes use of promises, so use mysql2/promise.

Install with NPM

npm install --save jaypha-mysql-ext

Usage

For monkeypatch version

const mysql2 = require('mysql2/promise');  
const mysqlExt = require('jaypha-mysql-ext');  

async function setup(config) {  
  let db = await mysql2.createConnection(config);  
  mysqlExt(db);  
  return db;  
}

For non-monkeypatch version

const mysql2 = require('mysql2/promise');  
const mysqlExt = require('jaypha-mysql-ext/nomonkey');  

async function setup(config) {  
  let db = await mysql2.createConnection(config);  
  return mysqlExt(db);  
}

API

connection

In the non-monkeypatch version, the mysql2 object can be accessed through the connection property.

queryValue(sql, values)

Calls query with sql and values and returns a single value. The first value of the first row.

let n = db.queryValue("select name from sometable where id=?",[someId]);  
console.log("name is "+n);

queryRow(sql, values)

Calls query with sql and values and returns the first row.

let row = db.queryRow("select * from sometable where id=?",[someId]);  
console.log("row is "+row);

queryData(sql, values)

Calls query with sql and values and returns the whole data set. Basically the same as query except does not include field info.

let data = db.queryData("select * from sometable");  
console.log("data is "+data);

queryColumn(sql, values)

Calls query with sql and values and returns a single column. If the sql selects one field, then queryColumn returns an array containing the values. If the sql selects two or more, then an object is returned with the contents of the first column as the keys and the contents of the second columns as the values.

let column = db.queryColumns("select name from sometable");
console.log("array of names is "+column);

let obj = db.queryColumns("select id,name from sometable");
console.log("object mapping ids to names is "+obj);

If a key is repeated, it will overwrite any existing value.

insert(tableName, columns, values)

A shortcut for insert statements. There are three cases

  • If columns is an object (that is, not an array), then it is inserted to the database using key/value pairs. values is ignored.
  • columns is an array and values is an array. Inserts a single row using columns and values.
  • columns is an array and values is a two dimensional array. Inserts multiple rows. Each element of the values array is a row.

Returns the value of InsertId.

// insert into sometable set id=1, name='john'  
let  id = db.insert("sometable", { id : 1, name : "john" });  
console.log("new row ID is "+id);

// insert into sometable (id,name) values (1,'john')"  
db.insert("sometable", [ "id", "name"], [1, "john"]);

// insert into sometable (id,name) values (1,'john'), (2,'jane')  
db.insert("sometable", [ "id", "name"], [[1, "john"], [2, "jane"]]);

update(tableName, values, wheres)

A shortcut for update statements.

// update into sometable set name='john' where id=1  
db.update("sometable", { name : "john" }, { id : 1 });

get(tableName, id)

Selects a row where column 'id' is of value id. A column called 'id' must exist.

let row = db.get("sometable", id);  
console.log("row is "+row);

set(tableName, values, id)

Updates a table row where column 'id' is of value id. If no id is provided, performs an insert instead. A column called 'id' must exist.

Returns the row's id.

db.set("sometable", { name : "john" }, 1);  
let id = db.set("sometable", { name : "jane" });

delete(tableName, id)

Deletes row id from the table. A column called 'id' must exist.

db.delete("sometable", 1);

License

Copyright (C) 2017 Jaypha.
Distributed under the Boost Software License, Version 1.0.
See http://www.boost.org/LICENSE_1_0.txt