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

v1.1.2

Published

Simple wrapper for mysql DML.

Downloads

3

Readme

MySQL simple dml wrapper

Install

$ npm i -S mysql-dml-wrapper

Usage

Connections

const MysqlDmlWrapper = require('mysql-dml-wrapper');

// * setup explicit
MysqlDmlWrapper.setupConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'base'
});

// * if wish open connection internally, only grant the following environment variables in 'process.env'

DB_HOST
DB_USER
DB_PASSWORD
DB_DATABASE

// ======== connection control in app client

// open connection
MysqlDmlWrapper.openConnection();

// do database operations
MysqlDmlWrapper.insert(...parameters);
MysqlDmlWrapper.update(...parameters);

// remember to close the connection
MysqlDmlWrapper.closeConnection();

// ========= using implicit connection

// the wrapper opens the connection, does the insert operation and closes the connection
// ** valid for all dml operations
MysqlDmlWrapper.insert(...parameters);

Select


// with promise

MysqlDmlWrapper
    .select('products', 'id, name', 'WHERE cost > 3')
    .then((result) => {
        const products = result;
    }).catch(e => console.log(e.message));

// using es7 async/await

async function selectProducts() {
    try {
        await MysqlDmlWrapper.select('products', 'id, name', 'WHERE cost > 3 ORDER BY id DESC');
    } catch (e) {
        console.log(e);
    }
}

Insert

// with promise
let product = {
    name: 'Product Example',
    price: 15.9,
    cost: 5.9
};

MysqlDmlWrapper
    .insert('products', product)
    .then((result) => {
        const products = result;
    }).catch(e => console.log(e.message));

// using es7 async/await

async function saveProduct(product) {
    try {
        await MysqlDmlWrapper.insert('products', product);
    } catch (e) {
        console.log(e);
    }
}

Update

// with promise
let changedData = {
    price: 19.9,
    cost: 4
};

// providing ID
MysqlDmlWrapper
    .update('products', changedData, 1) // product id
    .then((result) => {
        const products = result;
    }).catch(e => console.log(e.message));

// providing where object
MysqlDmlWrapper
    .update('products', { price: 7.95 }, { cost: 2.3 }) // SET price = 7.95 WHERE cost = 2.3
    .then((result) => {
        const products = result;
    }).catch(e => console.log(e.message));

// using es7 async/await

async function updateProduct(product) {
    try {
        // providing id
        await MysqlDmlWrapper.update('products', product, product.id);

        // or where object
        await MysqlDmlWrapper.update('products', product, { someCondition: 'someValue' });
    } catch (e) {
        console.log(e);
    }
}

Delete

// with promise

// providing ID
MysqlDmlWrapper
    .delete('products', product.id)
    .then((result) => {
        const products = result;
    }).catch(e => console.log(e.message));

// providing where object
MysqlDmlWrapper
    .delete('products', { price: 7.95 })
    .then((result) => {
        const products = result;
    }).catch(e => console.log(e.message));

// using es7 async/await

async function deleteProduct(product) {
    try {
        // providing id
        await MysqlDmlWrapper.delete('products', product.id);

        // or where object
        await MysqlDmlWrapper.delete('products', { someCondition: 'someValue' });
    } catch (e) {
        console.log(e);
    }
}

API

  • setupConnection:

    • connectionData = { host : process.env.DB_HOST, user : process.env.DB_USER, password : process.env.DB_PASSWORD, database : process.env.DB_DATABASE } // Object with connection data
  • openConnection: // Opens the database connection

  • closeConnection: // Closes the database connection

  • select:

    • table // Table
    • select // Fields to select, default '*'
    • complement // Any query complement, as where, joins, group by, etc
  • insert

    • table // Table
    • payload // Object to insert in database
  • update

    • table // Table
    • set // Object with fields to update
    • where // Object to do clause where or object id
  • delete

    • table // Table
    • where // Object to do clause where or object id