database-js-mysql2

1.0.0 • Public • Published

database-js-mysql2

Build Status

MySQL2 wrapper for database-js

Database-js-mysql is a wrapper around the mysql package by Doug Wilson. It is intended to be used with the database-js package. However it can also be used in stand alone mode. The only reason to do that would be to use Promises.

Install

npm install database-js-mysql2

Usage

Stand Alone

var mysql = require('database-js-mysql2');

(async () => {
    let connection, rows;
    connection = mysql.open({
        Hostname: localhost,
        Port: 3306,
        Username: 'my_secret_username',
        Password: 'my_secret_password',
        Database: 'my_top_secret_database'
    });

    try {
        rows = await connection.query("SELECT * FROM tablea WHERE user_name = 'not_so_secret_user'");
        console.log(rows);
    } catch (error) {
        console.log(error);
    } finally {
        await connection.close();
    }
})();

With Database-js

var Database = require('database-js').Connection;

(async () => {
    let connection, statement, rows;
    connection = new Database('mysql2://my_secret_username:my_secret_password@localhost:3306/my_top_secret_database');

    try {
        statement = await connection.prepareStatement("SELECT * FROM tablea WHERE user_name = ?");
        rows = await statement.query('not_so_secret_user');
        console.log(rows);
    } catch (error) {
        console.log(error);
    } finally {
        await connection.close();
    }
})();

With SSL

If you need to support SSL for your MySQL connection (for AWS RDS, for example), you can add extra params:

var Database = require('database-js').Connection;

(async () => {
    let connection, statement, rows;
    connection = new Database('mysql2://my_secret_username:my_secret_password@localhost:3306/my_top_secret_database?ssl[ca]=%2Fpath%2Fto%2Fca.pem');

    try {
        statement = await connection.prepareStatement("SELECT * FROM tablea WHERE user_name = ?");
        rows = await statement.query('not_so_secret_user');
        console.log(rows);
    } catch (error) {
        console.log(error);
    } finally {
        await connection.close();
    }
})();

Note that ssl[ca], ssl[key] and ssl[cert] will be checked for file paths and loaded:

// The query parameters are loaded into an 'options' object
if (options.ssl.ca && fs.existsSync(options.ssl.ca)) {
    options.ssl.ca = fs.readFileSync(options.ssl.ca);
}

License

MIT

Package Sidebar

Install

npm i database-js-mysql2

Weekly Downloads

32

Version

1.0.0

License

MIT

Unpacked Size

9.47 kB

Total Files

5

Last publish

Collaborators

  • estebanserfe