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

mysqlm

v2.0.6

Published

A.K.A "MySQL Mini" is A wrapper of the minimalist Nodejs module mysql that will give you improved query methods (promised and stream-like and transactions)

Downloads

36

Readme

mysqlm

A.K.A "MySQL Mini" is A minimalist Nodejs module that will give you improved mysql/mariadb query methods (promised and stream-like and easy to use transactions)

(It's basically a wrapper of mysql module)

Setup

Install in your project using npm

npm install mysqlm --save

Require in your file as

const mysqlm = require('mysqlm');

Methods

(All examples will use async await syntax)

connect

connect(config: Object) :Object

Let's you connect to your database

const conn = mysql.connect({
    host: 'localhost',
    user: 'admin',
    password: '12345',
    database: 'mydatabase'
})

query

query(query: String, input: Array<String>) :Promise<result>

Let's you query your database, returns a promise with result

let result = await conn.query('SELECT * FROM pets');

for (const row of result) {
    console.log(row);
}

queryOne

queryOne(query: String, input: Array<String>) :Promise<result>

Let's you query your database, returns a promise with first element of result

let row = await conn.queryOne('SELECT * FROM pets WHERE id = ?', 4);

console.log(row) // Row will be the pet with ID 4

queryStream

queryStream(query: String, input: Array<String>) :Object

Let's you query your database, returns a object with the a promise method wich will return the result row by row.

Usage Case: You need to get or insert more than 100k rows on one or more tables

let stream = conn.queryStream('SELECT * FROM people'); // No need for await here since queryStream doesn't return a promise

// read method does in fact returns a promise so await must be used
await stream.read( (row) => {
    console.log(row);
})

// OR

await conn
.queryStream('SELECT * FROM people')
.read( (row) => {
    console.log(row);
}); // Notice how there are no ; before this, because its a chain of functions

Try (aka Transactions)

try(callback: Function(Connection)) :Promise

A transaction executes all queries if there are no errors. A single error means the rollback of all queries. to be concise, all or nothing

You can also think of it as a Try/Catch block, as this tries to execute every query

Note: If you want to do a soon rollback, just throw an error (see example 3)

// 1. This will throw an error

// Before Transaction, Table player have 0 rows
await conn.transaction(async (t) => {
  await t.query('INSERT INTO player SET ?', [{name: 'Max', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max2', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max3', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max4', points: 400}]);
  await t.query('INSERT INTO ThIsTaBleNaMeShoUldntExistz SET ?', [{name: 'Max5', points: 400}]);
});
// After Transaction, Table player still have 0 rows
// Because transaction failed

// 2. This will return true

// Before Transaction, Table player have 0 rows
await conn.transaction(async (t) => {
  await t.query('INSERT INTO player SET ?', [{name: 'Max', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max2', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max3', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max4', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max5', points: 400}]);
});
// After Transaction, Table player will have 5 rows
// Because transaction succeded

// 3. This will do a sooner rollback

// Before Transaction, Table player have 0 rows
await conn.transaction(async (t) => {
  await t.query('INSERT INTO player SET ?', [{name: 'Max', points: 400}]);
  if( 2 + 2 != 5 ){
    throw 'Rollback'; // Sooner Rollback
  }
  await t.query('INSERT INTO player SET ?', [{name: 'Max2', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max3', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max4', points: 400}]);
  await t.query('INSERT INTO player SET ?', [{name: 'Max5', points: 400}]);
});
// After Transaction, Table player still have 0 rows
// Because transaction failed

getMysql - deprecated

getMysql() :mysql

Deprecated (Now this module extends all mysql properties and methods to itself)

Let's you get the mysql module, (same as require('mysql'))

Usage Case: When you need to do something this module doesnt have implemented yet

const mysqlm = require('mysqlm');
const mysql = mysqlm.getMysql();

mysql.Types; // All mysql types

// Is the same as

const mysqlm = require('mysqlm');

mysqlm.Types; // All mysql Types