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-magic

v0.3.0

Published

Promised based, high-level mysql library extension

Downloads

54

Readme

Build Status

mysql-magic

Promised based, high-level mysql library extension for Node.js >=7.6.

yarn add mysql-magic --save

Features

  • Supports multiple named connection pools (different database settings with global scope)
  • Safe async connection scopes - execute querys with build-in error handling
  • High-level API
  • Designed to run with the pure power of native Promise, await and async function

Usage

Application Bootstrap

const _mysqlMagic = require('mysql-magic');

// somewhere during your applications bootstrap...initialize a custom pool. It consumes any pool-options of [mysqljs](https://github.com/mysqljs/mysql)
_mysqlMagic.initPool('userdb', {
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'my_db'
});

Run Querys

// get the pool
const _db = require('mysql-magic').getPool('userdb');

// wait for connection becomes available
const con = await _db.getConnection();

// execute query
const [result, fields] = await con.query('SELECT * FROM `users` LIMIT 10;');

// release connection
await con.release();

Run Querys within scope

Within a connection scope, the connection will be automatically closed on finish or in case of an error. All kind of errors are proxied as exception.

const _db = require('mysql-magic');

// retrieve connection scope
const numUsers = await _db.getConnection('userdb', async function(){
    // run query
    const [rows, fields] = await this.query('SELECT COUNT(*) as `count` FROM `users`;');

    return rows.count;
});

console.log(numUsers);

API

  • initPool
  • getPool
  • getConnection()
  • Pool::getConnection
  • Connection::release
  • Connection::query
  • Connection::insert
  • Connection::fetchRow
  • Connection::fetchAll

mysql-magic::initPool

Description: Initializes a new named connection pool. Pass mysql connection options as second argument (passthrough)

Syntax: pool:Object = initPool(name:String, options:Object)

const _db = require('mysql-magic');

// initialize a new "named" pool
initPool('userdb', {
    // db credentials
    host: 'localhost',
    user: 'bob',
    password: 'secret',
    database: 'my_user_db'

    // special pool options
    connectionLimit: 50,
    acquireTimeout: 1000,
    waitForConnections: true,
    queueLimit: 200
});

// initialize default pool
initPool(null, {
    ...
})

mysql-magic::getPool

Description: Retrieves a named connection pool

Syntax: const pool:Object = getPool([name:String])

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();
...

mysql-magic::getConnection

Description: Retrieves a connection using async connection scope

Syntax: const result:Object = getConnection([name:String], scope:async-function)

Notice: A connection scope will automatically call con.release() in the end or on an error - exceptions are forwarded

Example 1 - Named Pool

// just include the module
const _db = require('mysql-magic');

// retrieve connection scope using userdb
const numUsers = await _db.getConnection('userdb', async function(){
    // run query
    const [rows, fields] = await this.query('SELECT COUNT(*) as num FROM users;');

    return rows[0].num;
});

console.log(numUsers);
...

Example 2 - Default Pool

// just include the module
const _db = require('mysql-magic');

// retrieve connection scope using default pool
const numUsers = await _db.getConnection(async function(){
    // run query
    const [rows, fields] = await this.query('SELECT COUNT(*) as num FROM users;');

    return rows[0].num;
});

console.log(numUsers);
...

mysql-magic::Pool::getConnection

Description: Requests a connection from given pool

Syntax: const connection:Object = Pool::getConnection([scope:async-function])

Notice: A connection scope will automatically call con.release() in the end or on an error - exceptions are forwarded

Example 1

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();

// do something
...

Example 2 - Scopes

const _db = require('mysql-magic');

// retrieve connection scope
const result = await _db.getConnection('userdb', async function(){
    // run query
    const [rows, fields] = await this.query('SELECT * FROM log LIMIT 20;');
    return rows.length;
});

console.log(result);

mysql-magic::Connection::release

Description: Releases the connection

Syntax: Connection::release()

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();

// do something
...

// release the connection
con.release();

mysql-magic::Connection::query

Description: Executes a mysql query

Syntax: const [result:Object, fields:Object] = Connection::query(sql:string, [values:Object])

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();

// do something
const [result] = await con.query('SELECT * FROM `users LIMIT 100`;');

console.log(result);

// release the connection
con.release();

mysql-magic::Connection::insert

Description: Executes a insert with given values wrapped into object

Syntax: const [insertID:int, result:Object] = Connection::insert(table:string, values:Object)

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();

// insert a new user
const [userid] = await con.insert('users', {
    name: 'John',
    email: '[email protected]',
    login: 'johndow1234'
});

console.log(result);

// release the connection
con.release();

mysql-magic::Connection::fetchRow

Description: Executes a query an returns a single result as object

Syntax: const [result:Object] = Connection::fetchRow(sql:string, [values:Object])

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();

// fetch user by its name
const user = await _db.fetchRow('SELECT * FROM `users` WHERE `name` LIKE ? LIMIT 1;', ['John']);

console.log(result);

// release the connection
con.release();

mysql-magic::Connection::fetchAll

Description: Executes a query an returns an array of objects (similar to query but returns an empty array in case of empty resultset)

Syntax: const [results:Array] = Connection::fetchAll(sql:string, [values:Object])

// get the userdb pool
const _db = require('mysql-magic').getPool('userdb');

// request a connection
const con = await _db.getConnection();

// fetch all users
const users = await _db.fetchAll('SELECT * FROM `users`;');

// show users
for (const user in users){
    console.log('User[', user.id, '] - ', user.name);
}

// release the connection
con.release

Any Questions ? Report a Bug ? Enhancements ?

Please open a new issue on GitHub

License

mysql-magic is OpenSource and licensed under the Terms of The MIT License