/simple-mysql-wrapper

A simple wrapper for the node.js mysql module.

Primary LanguageJavaScriptMIT LicenseMIT

simple-mysql-wrapper

npm npm

Table of Contents

Introduction

This is a node.js wrapper module for the mysql node module, it's written in JavaScript.

But why this module?
This module helps me to create / handle very easy more than one database connection with core features implementation.

Install

The module is currently only available on github, so use the following command to install:

$ npm install https://github.com/MoonLiightz/simple-mysql-wrapper/archive/v0.1.1.tar.gz --save

Usage

const mysql_helper = require('simple-mysql-wrapper');

let anyDatabase = new mysql_helper({
    host: '127.0.0.1',
    port: 3306,
    user: 'db_user',
    password: 'db_password',
    database: 'db_database',
    connectTimeout: 10000  // milliseconds
});

anyDatabase.connect((err) => {
    if(err) console.log(err.message);
    else {
        // Connection to database was successfully, run any query now
        anyDatabase.runQuery('SELECT * FROM users', (err, rows) => {
            if(err) console.log(err.message);
            else console.log(rows);
            
            // Don't forget to disconnect from database when you are ready
            anyDatabase.disconnect((err) => {
                if(err) console.log(err.message);
                else console.log('Now we are disconnected')
            })
        });
    }
});

Methods

There are only three methods.

anyDatabase.connect(callback)

Start trying to connect to the database.

callback
function(err)

Callback of mysql connect

anyDatabase.runQuery(sql, callback)

Execute an mysql query

sql
String

SQL string of the query

callback
function(err, rows)

Callback of mysql query

anyDatabase.disconnect(callback)

Close the database connection.

callback
function(err)

Callback of mysql disconnect

Examples