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

@badai147/dbsqlfmt

v0.1.4

Published

SQL formatter CLI tool

Readme

dbsqlfmt

Version Node TypeScript License

dbsqlfmt 是一个基于 sql-formatter 的 SQL 格式化 CLI 工具,支持 MySQL、PostgreSQL 两种方言,自动识别方言,支持配置文件、关键字/数据类型/函数名/标识符大小写控制等特性。

目录

特性

  • 格式化单个 SQL 文件(不限制文件后缀)
  • 支持 MySQL 和 PostgreSQL 两种方言
  • 自动识别 SQL 方言,无需手动指定
  • 可自定义缩进大小
  • 全面的关键字/数据类型/函数名/标识符大小写控制(大写、小写、保持原样)
  • Dry-run 预览模式,不改写文件
  • 通过 .dbsqlfmtrc 配置文件持久化选项

安装

npm i @badai147/dbsqlfmt

本地开发用 npm link

npm link

使用

dbsqlfmt format <file> [options]

示例

基础格式化:

dbsqlfmt format query.sql

指定方言:

dbsqlfmt format query.sql --language postgresql

关键字小写 + 数据类型保持原样:

dbsqlfmt format query.sql --keyword-case lower --data-type-case preserve

仅预览结果,不写回文件:

dbsqlfmt format query.sql --dry-run

DBeaver 外部工具配置:

dbsqlfmt format "${file}" --dry-run

选项

| 选项 | 描述 | 默认值 | |------|------|--------| | -l, --language <lang> | SQL 方言(mysql / postgresql,不指定则自动识别) | 自动检测 | | -i, --indent <size> | 缩进空格数 | 2 | | -u, --uppercase | --keyword-case upper 的快捷方式 | 关闭 | | --keyword-case <style> | 关键字大小写:upper / lower / preserve | upper | | --data-type-case <style> | 数据类型大小写:upper / lower / preserve | upper | | --function-case <style> | 函数名大小写:upper / lower / preserve | upper | | --identifier-case <style> | 标识符大小写:upper / lower / preserve(实验性) | upper | | --dry-run | 仅输出到终端,不改写文件 | 关闭 | | -V, --version | 查看版本号 | | | -h, --help | 查看帮助信息 | |

配置文件

在项目根目录创建 .dbsqlfmtrc 文件,使用 JSON 格式:

{
  "language": "mysql",
  "indent": "4",
  "keywordCase": "upper",
  "functionCase": "lower"
}

CLI 命令行选项会覆盖配置文件中的同名设置。

支持的方言

mysql postgresql

不传 --language 的情况下,工具会自动识别方言,无需手动指定。识别依据包括反引号、:: 类型转换、AUTO_INCREMENTILIKE 等方言特征。如果仍需要覆盖检测结果,显式传入 --language 即可。

开发

前置要求

  • Node.js >= 18
  • npm

本地开发

# 克隆仓库
git clone https://github.com/badai147/dbsqlfmt.git
cd dbsqlfmt

# 安装依赖
npm install

# 编译 TypeScript
npm run build

# 运行
node dist/cli.js format path/to/file.sql

测试

# 运行全量测试
npm test

# 监听模式(开发测试用)
npm run test:watch

测试文件位于 src/__tests__/,使用 vitest 框架。详见 src/__tests__/README.md

项目结构

dbsqlfmt/
├── src/
│   ├── __tests__/
│   │   ├── README.md       # 测试说明
│   │   ├── config.test.ts    # 配置加载测试(8 用例)
│   │   ├── detect.test.ts    # 方言检测单元测试(60 用例)
│   │   ├── formatter.test.ts # 格式化集成测试(16 用例)
│   │   ├── index.test.ts     # 主流程测试(5 用例)
│   │   └── utils.test.ts     # 工具函数测试(6 用例)
│   ├── cli.ts        # Commander 参数解析与命令注册
│   ├── index.ts      # 主流程编排
│   ├── formatter.ts  # sql-formatter 封装
│   ├── detect.ts     # 方言自动检测(加权评分)
│   ├── config.ts     # 配置文件加载与合并
│   └── utils.ts      # 文件读写工具函数
├── package.json
├── package-lock.json
├── tsconfig.json
└── README.md

更新日志

详见 CHANGELOG.md

许可证

MIT