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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@goldenapple/mysql-tool

v1.2.3

Published

nodejs使用的mysql相關工具包

Readme

nodejs mysql 工具

讓交易更方便使用

設定連線

let NinDb = require('@goldenapple/mysql-tool').DB;

function getDB() {
    return new NinDb(getPoolOption(), getConnectionOption());
}

function getConnectionOption() {
    return {
        host: 'test.example.ap-northeast-1.rds.amazonaws.com',
        user: 'admin',
        password: 'ppaassswwoorrdd',
        database: 'defaultDb',
        port: '3306',
        charset: 'utf8mb4',
        timezone: '+8:00',
    };
}
function getPoolOption() {
    return {
        connectionLimit: 1,
        host: 'test.example.ap-northeast-1.rds.amazonaws.com',
        user: 'admin',
        password: 'ppaassswwoorrdd',
        database: 'defaultDb',
        port: '3306',
        charset: 'utf8mb4',
        timezone: '+8:00',
    };
}

使用方法

/**
 * 一次性Query
 * */
async function test1(id) {
    let ninDb = getDB();
    let result = await ninDb.oneQuery(
        `SELECT * FROM Table
        WHERE id=?`,
        [id]
    );
    return result;
}

/**
 * 簡易批次(同一個連線),可透過參數設定是否使用交易、Pool,交易必須有FOR UPDATE來鎖表才有作用
 * */
async function test2(userId, orderId) {
    let ninDb = getDB();

    let results = await ninDb.batchQuery(
        [
            {
                commandText: `SELECT * FROM User
                WHERE id=? FOR UPDATE;`,
                value: [userId],
                resultName: 'User',
            },
            {
                commandText: `SELECT * FROM Order
                WHERE id=?;`,
                value: [orderId],
                resultName: 'Order',
            },
            {
                commandText: `UPDATE User SET name=?
                WHERE id=?;`,
                value: ['newUserName', userId],
                resultName: 'User',
            },
        ],
        false,
        true
    );
    return { User: results['User'], Order: results['Order'] };
}

/**
 * 自訂交易,較為彈性,但必須在發生錯誤時呼叫rollback。交易必須有FOR UPDATE來鎖表才有作用。
 * */
async function test3(userId, orderId, orderCreateTime) {
    let ninDb = getDB();

    let conn = ninDb.createConn();
    // 使用交易
    await ninDb.beginTrans(conn);
    try {
        let userTable = await ninDb.queryTrans(
            conn,
            `SELECT * FROM User
                WHERE id=? FOR UPDATE;`,
            [maxCount, workId]
        );
        if (userTable.length == 0) {
            throw new Error('找不到使用者');
        }
        let orderTable = await ninDb.queryTrans(
            conn,
            `SELECT * FROM Order
            WHERE id=? AND createTime>?`,
            [orderId, orderCreateTime]
        );

        if (userTable[0]['orderId'] != orderTable[0]['id']) {
            throw new Error('訂單編號不一致');
        }

        await ninDb.commitTrans(conn);
        return { ok: 1 };
    } catch (e) {
        console.log('catch rollback');
        await new Promise((resolve, reject) => {
            conn.rollback(() => {
                conn.destroy();
                reject(e);
            });
        });
    }
}