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

@gas0324/mysql

v1.1.9

Published

mysql生成器

Downloads

3

Readme

mysql

适用于Node.js的MySQL生成器,项目基于mysql2,支持链式调用,请注意:本项目自动处理下划线与小驼峰命名方式的转换

目录

安装

npm install --save @gas0324/mysql mysql2

使用

import { init, mysql, Mysql } from '@gas0324/mysql';
init({
  prefix: 'as_',  // 表前缀
  host: 'localhost',
  user: 'root',
  database: 'test',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
  // 其他创建mysql2连接池的参数
});
// 方式一
mysql.table('{pre}menu').filed('id, name, parent_id').select();
// sql: select id, name, parent_id from as_menu

// 方式二
new Mysql('{pre}menu').filed('id, name, parent_id').select();
// sql: select id, name, parent_id from as_menu

// 以上查询返回结果 
[
  {
    id: 1,
    title: '菜单标题',
    parentId: null
  },{
    id: 2,
    title: '子菜单',
    parentId: 1
  }
  // ...
]

// PS: 两种方式结果完全相同, 注意field中的'parent_id' 与查询结果中的'parentId'会自动转换 

方法

使用下面的方法后,可直接链式调用本项目的其它方法

| 方法名 | 说明 | 参数 | | :-: | :-: | :-: | | table | 表名 | (table: string) | | field | 查询字段 | (field?: string) | | join | 联合查询 | (table: string, way?: 'inner/left/right) | | data | 插入/修改数据 | (data: Object) | | where | 条件 | (where: WhereOptions) | | order | 排序 | (order: string) | | limit | 行数 | (limit: number) | | pager | 分页 | (pageIndex: number, pageSize?: number): 页码、每页条数 |

使用下面的方法后,不可以继续使用链式方式调用本项目的其它方法

| 方法名 | 说明 | 参数 | | :-: | :-: | :-: | | init | 初始化数据库链接 | 参考: 使用 | | find | 数据库查询-单条 | - | | select | 数据库查询-列表 | - | | count | 数据库查询-行数 | - | | insert | 数据库执行-插入 | - | | update | 数据库执行-更新 | - | | delete | 数据库执行-删除 | - | | save | 数据库执行-插入或者更新 | - | | makeSql | 生成sql语句 | (type?: string): type可为:'select/insert/update/delete/save/count' | | getPool | 获取连接池 | - | | getConnection | 获取链接 | - |

where参数

// string
mysql.table('menu').where('parent_id = 1 or parent_id = 2').select();
// sql: select * from menu where parent_id = 1 or parent_id = 2;

// object: 基础模式
mysql.table('menu').where({parentId: 1, status: true}).select();
// sql: select * from menu where parent_id = 1 and status = true;

// object: 正常模式
mysql.table('menu').where({name: 'parent_id', value: [1,2], operator: 'in', _mode: 'full'}).select();
// sql: select * from menu where parent_id in (1,2);
 
// 数组:多个条件and拼接
mysql.table('menu').where([
  {name: 'parent_id', value: [1,2], operator: 'in', _mode: 'full'},
  {name: 'name', value: '标题', operator: 'like', _mode: 'full'}
  ]).select();
// sql: select * from menu where parent_id in (1,2) and name like '%标题%';

// 混合
mysql.table('menu').where([
  '(parent_id = 1 or parent_id = 2)',
  {status: true},
  {name: 'name', value: '标题', operator: 'like', _mode: 'full'}
  ]).select();
// sql: select * from menu where (parent_id = 1 or status = true) and status = ture and name like '%标题%';

日志

npm install --save log4js
import * as log4js from "log4js";

log4js.configure({
  appenders: {
    mysql: { type: "file", filename: "log/mysql.log" },
    other: { type: "file", filename: "log/other.log" }
  },
  categories: {
    mysql: { appenders: ["mysql"], level: "trace" },
    default: { appenders: ['other'], level: "trace" }
  },
});

其他

如果你需要更复杂的sql语句,使用本项目无法生成,也可以直接使用mysql2 注意:使用以下方式执行sql语句,无法通过本项目记录日志,请自行记录

// 使用连接池
import { mysql } from '@gas0324/mysql';
asd
const pool = mysql.getPool();
pool.query("SELECT field FROM atable", function(err, rows, fields) {
   // ...
});

// 或者使用链接
const conn = await mysql.getConnection();
conn.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // 结果集
    console.log(fields); // 额外的元数据(如果有的话)
  }
);

反馈

您拥有任何建议或想法,可以通过issues进行反馈

赞助

参考资料