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

yoyomysql

v2.0.22

Published

一个链式连接mysql数据库的扩展包

Readme

YoyoMysql 链式操作库 (采用mysql2)

前言

对于sql不熟悉的人不是很友好,而且复用并不是很方便,所以封装了这个链式方法库(可能有些地方写得不是很好)!但是我会尽力去完善它!

我是Yoyo感谢您的浏览!!!!

引入

import YoyoMysql from 'yoyomysql';

连接mysql数据库

YoyoMysql.createPool({
    	host: 'localhost',
        user: 'root',
        password: 'root',
        database: 'yoyo',
        waitForConnections: true,
        connectionLimit: 10,
        maxIdle: 10, // 最大空闲连接数,默认等于 `connectionLimit`
        idleTimeout: 60000, // 空闲连接超时,以毫秒为单位,默认值为 60000 ms
        queueLimit: 0,
        enableKeepAlive: true,
        keepAliveInitialDelay: 0,
});
//这里和mysql2一样的配置数据


// 如果你有mysql2的连接池对象可以直接设置
YoyoMysql.setPool(pool);

// 开启sql显示 默认 false
YoyoMysql.isShowSql = true;
/**
 * 设置打印sql语句的方法
 * @param sql sql语句
 * @param highlightSQL 高亮sql方法
 */
YoyoMysql.printMethod = (sql, highlightSQL) => {
    // sql 原生组成的 sql
    // highlightSQL 高亮sql方法
    console.log(highlightSQL(sql));//打印高亮sql
};

添加数据

const result =await YoyoMysql.table('user')//需要操作的完整表名
.field(['name','account','describe'])//设置字段
.insert(['我是名称',123456,'我是描述']);//添加一条数据
//你也可以添加多条(比如这样)
.insert([
    ['我是名称1',123456,'我是描述1'],
    ['我是名称2',123456,'我是描述2'],
]);
//或许你想这样做(这样直接指定字段)
const result =await YoyoMysql.table('user')//需要操作的完整表名
.insert({name:'我是名称呀',account:9877,describe:'说得是这样?'});//添加一条数据
//任然,你也可以添加多条(比如这样)
.insert([
    {name:'我是名称呀',account:9877,describe:'说得是这样?'},
    {name:'我是名称呀',account:9877,describe:'说得是这样?'}
]);

删除数据

const result =await YoyoMysql.table('user')//需要操作的完整表名
.where('id',1)//获取你可以添加一些条件  (id = 1)
.where('name','<>','yoyo')//获取你可以添加一些条件  (name <> 'yoyo')
.where([
    ['name','like','%yoyo%'],
    ['ps','in',[1,2,3,4]],
])//获取你可以添加一些条件  (name like %yoyo% AND ps in (1,2,3,4))
.limit(5)//获取还可以限制删除的条数
.delete();

更新数据(修改)

const result =await YoyoMysql.table('user')//需要操作的完整表名
.where('id',1)//获取你可以添加一些条件  (id = 1)
.update({
    rmb:99999,
    name:'我被修改了'
})
.update(['name',`'被修改了'`]);

// 注意这个是原始数据 字符串需要自行增加 单引号
.update([
         ['name', "'被修改了'"],
        ['rmb', 'rmb+1']
    ]);

查询数据

!!!!!!!!!!!!!查询返回值有修改哦!!!!!看我
result.data;//查询到的数据
result.field;//字段
result.count;//查询到的数据总数量

const result =await YoyoMysql.table('user')//需要操作的完整表名
.where('id',1)//获取你可以添加一些条件  (id = 1)
.find();//查询一条数据

whereOr() //或者

// 子查询
where('id',()=>{
return YoyoMysql.table('qd').whereOr('a2', '=', 222);
});
//或者 (子查询 IN)
.whereIn('id',()=>{
return YoyoMysql.table('qd').whereOr('a2', '=', 222).whereOr('a2', '=', 222);
});

//或者
.select();//查询多条
//你任然可以用
.limit(5)
//or
.limit(0,18)
//去限制它
//我们看看排序
.order('id','DESC')//设置降序
.order({
    id:'ASC',
    rmb:'DESC'
})

//你也可以单独设置字段显示默认是 *
.field(['id','name'])//只要两个字段
.field('name AS 名称')//或者用来取名也可以

.group('id')//分组查询
.group(['id','name'])//分组查询

.having('count(*) > 1')//分组条件(原始拼接)


//多表联查也是支持的
const result = await YoyoMysql.table('user u')//需要操作的完整表名
.join('user2 u2','u.id = u2.id')//你可以这样的当然它还有(leftJoin,rightJoin,fullJoin)他们分别代表(INNER JOIN,LEFT JOIN,RIGHT JOIN,FULL JOIN)

分页查询

 const result = await YoyoMysql.table('user').where('id', '>', 1).order('id','DESC').pages(1,2);
 console.log(result);

事务操作

async function test() {
    const result = await YoyoMysql.beginTransaction(async (conn) => {
        await conn.table('user').where('id', '=', 1).delete();
        await conn.table('user').where('id', '=', 1).update({ name: '张三' });
    });
    // result  是true就是成功提交了  是false就是回滚了

    // 还有另一种 try catch 的方式 也是异步的 你可以这样 捕获错误
    try {
        await YoyoMysql.beginTransaction(async (conn) => {
            await conn.table('user').where('id', '=', 1).delete();
            await conn.table('user').where('id', '=', 1).update({ name: '张三' });
        },true);
    } catch (error) {
        // 错误 回滚了
        console.log(error);
    }
    
}
// 启动事务(都是异步的你可以用 await 等待它或者 在 then 回调里面执行)

获取一条单独的连接

async function test() {
    const result = await YoyoMysql.getOneConnection(async (conn) => {
        await conn.table('user').where('id', '=', 1).delete();
        await conn.table('user').where('id', '=', 1).update({ name: '张三' });
        return '你的值';
    });
    // result  是 '你的值'
}
// 报错或者执行完成的时候 会自动是否这个连接

使用临时配置创建数据库连接并执行操作

async function queryFromTempDatabase() {
    const tempConfig = {
        host: '127.0.0.1',
        user: 'root',
        password: 'a123456',
        database: 'test',
        port: 3306
    };

    try {
        const result = await YoyoMysql.withTempConnection(tempConfig, async (conn) => {
            // 在这里使用临时连接执行查询 这个回调执行完成后就会自动销毁这个连接池
            const data = await conn.table('user')
                .select();
            
            return data;
        });
        
        console.log('查询结果:', result);
    } catch (error) {
        console.error('查询出错:', error);
    }
}

原始的查询和执行

//如果还是没有您需要的你可以自己编写sql使用以下的方法执行

await YoyoMysql.query(sql);//执行并返回
await YoyoMysql.query(sql,bind);//执行并返回  (它有一个可选参数绑定就是预处理方式)

await YoyoMysql.execute(sql);
await YoyoMysql.execute(sql,bind);//使用方式和上面雷同