mysql-oh-wait
TypeScript icon, indicating that this package has built-in type declarations

0.18.0 • Public • Published

Mysql Oh Wait! (Mysql await)

code coverage version downloads license

Uses the great node mysqljs/mysql package and wraps around it to facilitate getting results as a return from MysqlReq.query(). Instead of needing to use callbacks, this package uses Promises and the async / await syntax which is much easier. On top of that the advantage (if you need this feature of course) is that you don't need to worry about connections not being closed or open at the moment of querying.

Installation

To install this npm package do

npm i mysql-oh-wait

Usage

MysqlReq

Then from your javascript files import either MysqlReq or MysqlDump with

//var MysqlReq = require('mysql-oh-wait').MysqlReq;
import { MysqlReq } from 'mysql-oh-wait';

Then you can directly query your database:

//import 'dotenv/config'; // this will get connection settings from .env file

import { MysqlReq, MysqlDump } from 'mysql-oh-wait';

const res = await MysqlReq.query({sql: 'SELECT * FROM MyTable WHERE ?', values: {myCol: 'myValue'}});
console.log(res); // [ { myCol: 'myValue', ...otherColumns }, { myCol: 'myValue', ...otherColumns2 }, ...otherRows ]

This is assuming you have set the connection details in environment variables like:

process.env.DB_HOST = 'myhost'
process.env.DB_USER = 'myuser'
process.env.DB_PASSWORD = 'mypwd'
process.env.DB_NAME = 'mydbname'
process.env.MULTIPLE_STATEMENTS = 1

Or you can store these in a .env file. In which case the import 'dotenv/config'; statement will load them for you. (You need to npm i -P dotenv for this to work.

MysqlDump

If you want to create the database tables from an sql file you can use MysqlDump

//var MysqlDump = require('mysql-oh-wait').MysqlDump;
import { MysqlDump } from 'mysql-oh-wait';

Then if you have an MysqlDump file somewhere you can simply do:

import { MysqlDump } from 'mysql-oh-wait';
await MysqlDump.executeSqlFile(`${__dirname}/mysqldump.sql`);

This should have loaded all your tables in the database. Again, assuming you have database connection config in process.env.DB_... properties.

Package Sidebar

Install

npm i mysql-oh-wait

Weekly Downloads

15

Version

0.18.0

License

MIT

Unpacked Size

221 kB

Total Files

59

Last publish

Collaborators

  • guillermo_at