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

@waterada/co-mysql

v1.0.2

Published

Easy to use the node-mysql with the co.

Downloads

3

Readme

CoMySQL

Build Status Dependency Status MIT License

概要

SQL を下記のように書けるようにするもの。

yield coMySQL.getMasterConnection(function * (conn) {
    //追加
    {
        let res1 = yield conn.query('INSERT INTO users (user_name) values (?)', ['aaa']);
        let res2 = yield conn.query('INSERT INTO users (user_name) values (?)', ['bbb']);
        assert.equal(res1.insertId, 1);
        assert.equal(res2.insertId, 2);
    }
    //複数行取得
    {
        let users = yield conn.query('SELECT * FROM users');
        assert.equal(users.length, 2);
        assert.equal(users[0]['user_id'], 1);
        assert.equal(users[1]['user_id'], 2);
        assert.equal(users[0]['user_name'], 'aaa');
        assert.equal(users[1]['user_name'], 'bbb');
    }
    //複数行変更
    {
        let res = yield conn.query('UPDATE users SET user_name=?', ['aaa']);
        assert.equal(res.affectedRows, 2, '対象は全件');
        assert.equal(res.changedRows, 1, '実際の変更は1件');
        let users = yield conn.query('SELECT * FROM users ORDER BY user_id');
        assert.equal(users.length, 2);
        assert.equal(users[0]['user_name'], 'aaa');
        assert.equal(users[1]['user_name'], 'aaa');
    }
    //単一行変更
    {
        let res = yield conn.query('UPDATE users SET user_name=? WHERE user_id=?', ['ccc', 1]);
        assert.equal(res.affectedRows, 1);
        assert.equal(res.changedRows, 1);
        let users = yield conn.query('SELECT * FROM users ORDER BY user_id');
        assert.equal(users.length, 2);
        assert.equal(users[0]['user_name'], 'ccc');
        assert.equal(users[1]['user_name'], 'aaa');
    }
    //単一行取得
    {
        let user = yield conn.selectOne('SELECT * FROM users WHERE user_id=?', [1]);
        assert.equal(user['user_name'], 'ccc');
    }
});

//Slaveで繋ぐなら
yield coMySQL.getSlaveConnection(function * (conn) {
    //ここでSQL
});

//トランザクションを使うなら
yield coMySQL.beginTransaction(function * (conn) {
    //ここでSQL
    if (isError) { //何か問題があれば
        conn.rollback();
    }
    //何もなければ自動的に commit される
});
  • co を使ってコールバック地獄にすることなく、Promise よりもシンプルに SQL を書ける。
  • MASTER/SLAVE の Pooling とコネクションの取得・リリース処理はほぼ自動化され簡単に書ける(実質 getMasterConnection() で囲むだけ)。
  • co だと SQL エラーの stacktrace から SQL の呼び出し元を辿れないが、これは辿れる。

依存

  • node 6.x 以上
  • mysql
  • co

インストール

npm install --save @waterada/co-mysql

実装

//接続
const CoMySQL = require('@waterada/co-mysql');
const coMySQL = new CoMySQL({
  "COMMON": {
    "connectTimeout": 1000,
    "supportBigNumbers": true,
    "connectionLimit": 1,
    "removeNodeErrorCount": 3,
    "host": "mysql",
    "port": "3306"
  },
  "MASTER": {
    "user": "co_mysql_test",
    "password": "co_mysql_test",
    "database": "co_mysql_test"
  },
  "SLAVES": [
    {
      "user": "co_mysql_test",
      "password": "co_mysql_test",
      "database": "co_mysql_test"
    },
    {
      "user": "co_mysql_test",
      "password": "co_mysql_test",
      "database": "co_mysql_test"
    }
  ]
});

//コネクション取得
yield coMySQL.getMasterConnection(function * (conn) {
    //SQL実行
});

//終了
coMySQL.end();

詳細な使い方

テスト を参考にしてください。

テスト実行方法

ローカルで動かす場合:

npm test

docker で動かす場合:

docker-compose run --rm node npm test