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

@eunmo/mysql

v1.1.4

Published

Async wrapper for MySQL connection pool

Downloads

5

Readme

@eunmo/mysql

An async wrapper for MySQL connection pool. This package tries to hide away the boilerplates for the MySQL library to minimize setup.

Usage

const { dml, query, queryOne } = require('@eunmo/mysql');
awiat dml('CREATE TABLE some_table (C1 int)');
const added1 = await dml('INSERT INTO some_table VALUES (1);');
const added2 = await dml('INSERT INTO some_table VALUES (?);', 2);
const added3 = await dml('INSERT INTO some_table VALUES ?;', [[[3], [4]]]);
const rows = await query('SELECT * FROM some_table;');
const row1 = await queryOne('SELECT * FROM some_table WHERE C1 = ?;', 1);
const row1 = await queryOne('SELECT * FROM some_table WHERE C1 = ?;', [1]);

The return type of dml/query is identical to that of mysql package's connection.query. The return type of queryOne is a single row of mysql package's connection.query or null if no row can be fetched.

Database connection configuration

This package uses the config config package for setup. For starters, put the following in config/default.json.

{
  "dbConfig": {
    "host": "localhost",
    "user": "dummy_user",
    "password": "dummy_password",
    "database": "dummy"
  }
}

If you also want to change the connection pool size, you can also specify it. The default value is 10.

{
  "dbConfig": {
    "host": "localhost",
    "user": "dummy_user",
    "password": "dummy_password",
    "database": "dummy",
    "connectionLimit": 100
  }
}

Jest setup

To use a different config when running jest tests, the following three steps are required.

1. Write the new setup file.

Contents of NODE_CONFIG will be merged with the base config, so only the diff that needs to be changed can be designated. I put this code as server/setup-tests.js.

process.env.NODE_CONFIG = '{"dbConfig":{"database":"dummy_test"}}'

2. Create a jest config file.

This file will point to the setup file from step 1. I put this code as server/jest-config.json.

{
  "setupFiles": ["<rootDir>/setup-tests.js"]
}

3. Run jest with the config file from step 2.

jest server --config server/jest-config.json

Or as part of npm script, run npm run jest with the following in package.json.

{
  "scripts": {
    "jest": "jest server --config server/jest-config.json"
  }
}

Note that if the root directory for jest changes, the path in step 2 should also be updated.

Cleanup per test file

When running tests with jest, the connection pool gets created for every test file. So the connection pool needs to be manually closed. Example:

const { cleanup } = require('@eunmo/mysql');

afterAll(async () => {
  await cleanup();
});