alokraj68/eslint-plugin-typeorm-enterprise
GitHub: alokraj68/eslint-plugin-typeorm-enterprise
面向 TypeORM 应用的企业级 ESLint/oxlint 静态规则插件,在 lint 阶段拦截原生 SQL、强制事务与多租户隔离等后端数据库治理规范。
Stars: 0 | Forks: 0
# 🛡️ eslint-plugin-typeorm-enterprise
**在原生 SQL 进入生产环境之前将其拦截。**
一个生产就绪的 ESLint 插件,可阻止 TypeORM 应用程序中的原生 SQL 执行,并强制执行企业级后端治理 —— 引导团队使用 query builder、repository、migrations 以及安全的数据库抽象。
[](https://github.com/alokraj68/eslint-plugin-typeorm-enterprise/actions/workflows/ci.yml)
[](https://github.com/alokraj68/eslint-plugin-typeorm-enterprise/actions/workflows/codeql.yml)
[](https://github.com/alokraj68/eslint-plugin-typeorm-enterprise/actions/workflows/publish.yml)
[](https://codecov.io/gh/alokraj68/eslint-plugin-typeorm-enterprise)
[](https://www.npmjs.com/package/eslint-plugin-typeorm-enterprise)
[](https://www.npmjs.com/package/eslint-plugin-typeorm-enterprise)
[](https://packagephobia.com/result?p=eslint-plugin-typeorm-enterprise)
[](https://www.npmjs.com/package/eslint-plugin-typeorm-enterprise)
[](https://github.com/alokraj68/eslint-plugin-typeorm-enterprise/blob/main/LICENSE)
[](https://eslint.org)
[](https://nodejs.org)
适用于任何 TypeScript 版本 —— 或者完全不使用(内置编译好的 JS + 打包的 `.d.ts`):
[](https://www.typescriptlang.org)
[](https://www.typescriptlang.org)
[](https://www.typescriptlang.org)
[](https://developer.mozilla.org/docs/Web/JavaScript)
## ✨ 为什么使用这个插件?
散布在 TypeORM 代码库中的原生和动态构建的 SQL 是一种治理和安全责任:它绕过了 query builder,引发了注入风险,并使大型团队中的数据访问模式碎片化。这个插件在 lint 阶段就能捕获这些模式 —— **在代码审查之前、在合并之前、在生产之前** —— 同时保持足够的保守性,以避免在普通的请求/路由代码中出现误报。
| | |
|---|---|
| 🚫 **拦截原生 SQL** | 静态(`no-raw-query`)和动态/注入(`require-parameterized-query`、`no-interpolated-where`)SQL |
| 🧨 **保护你的数据** | `no-synchronize-true`(可自动修复)和 `no-unsafe-query-builder-delete` 阻止 schema 擦除和全表修改 |
| 🧱 **强制执行抽象** | `no-entity-manager-query`、`require-transaction`、`prefer-transaction-for-multiple-writes` 将访问保持在安全层中 |
| 🏢 **多租户感知** | 带有可配置 `tenantKeys` 的 `require-tenant-scope` 可捕获跨租户查询 |
| 🚀 **性能提示** | `prefer-exists-over-count` 将存在性检查引导脱离行数统计 |
| 🎚️ **配置层级** | `recommended`、`warn`、`strict`、`performance`、`multiTenant` 可共享配置 |
| 📦 **同时支持 ESM + CJS** | 单一 TypeScript 源码被编译为 `.mjs`、`.cjs` 和 `.d.ts` |
## 📚 目录
- [安装](#-installation)
- [快速开始](#-quick-start)
- [配置](#-configs)
- [规则](#-rules)
- [工作原理](#-how-it-works)
- [路线图](#-roadmap)
- [贡献](#-contributing)
- [许可证](#-license)
## ✅ 环境要求
- **Node** `>=18`
- **ESLint** `^9 || ^10`(flat config),或通过其 JS 插件 API 运行的 **oxlint**
- **TypeScript** 是可选的 —— 该包内置了编译好的 JS 和打包的类型,
因此它适用于任何 TypeScript 版本或完全不使用 TypeScript。
## 📦 安装
```
npm install --save-dev eslint eslint-plugin-typeorm-enterprise
```
## 🚀 快速开始
### Flat config — `eslint.config.js` (ESLint 9+)
扩展一个内置的配置:
```
const typeormEnterprise = require('eslint-plugin-typeorm-enterprise');
module.exports = [typeormEnterprise.configs.recommended];
```
或者手动配置规则:
```
const typeormEnterprise = require('eslint-plugin-typeorm-enterprise');
module.exports = [
{
plugins: { 'typeorm-enterprise': typeormEnterprise },
rules: {
'typeorm-enterprise/no-raw-query': 'error',
'typeorm-enterprise/require-parameterized-query': 'error',
'typeorm-enterprise/no-synchronize-true': 'error',
'typeorm-enterprise/no-entity-manager-query': 'error',
},
},
];
```
### 旧版 `.eslintrc`
```
module.exports = {
plugins: ['typeorm-enterprise'],
rules: {
'typeorm-enterprise/no-raw-query': 'error',
'typeorm-enterprise/require-parameterized-query': 'error',
},
};
```
### oxlint (`.oxlintrc.json`)
这些规则仅基于 AST,因此它们可以在 [oxlint](https://oxc.rs) 的 JS 插件 API(兼容 ESLint v9,目前为 alpha 版本)下运行,无需适配器:
```
{
"jsPlugins": ["eslint-plugin-typeorm-enterprise"],
"rules": {
"typeorm-enterprise/no-raw-query": "error",
"typeorm-enterprise/require-parameterized-query": "error",
"typeorm-enterprise/no-unsafe-query-builder-delete": "error"
}
}
```
该插件的 `meta.name` 为 `typeorm-enterprise`,因此规则名称在 ESLint 和 oxlint 中完全相同。可共享配置是 ESLint 的特性 —— 在 oxlint 下,请像上面那样单独启用规则。
### 框架指南
可直接复制粘贴的入门示例位于 [`examples/`](./examples):
- **NestJS** — [`nestjs.eslint.config.mjs`](./examples/nestjs.eslint.config.mjs) (`strict` + `performance`)
- **Express / Node** — [`express.eslint.config.js`](./examples/express.eslint.config.js)
- **Multi-tenant** — [`multitenant.eslint.config.mjs`](./examples/multitenant.eslint.config.mjs) (自定义 `tenantKeys`)
- **oxlint** — [`examples/.oxlintrc.json`](./examples/.oxlintrc.json)
## 🎚️ 配置
| 配置 | 严重级别 | 内容 |
|---|---|---|
| `recommended` | `error` | 广泛安全的规则:原生 SQL、注入、schema、EntityManager、不安全的删除操作 |
| `warn` | `warn` | 与 `recommended` 相同的规则,但作为警告 |
| `strict` | `error` | `recommended` + `require-transaction` + `prefer-transaction-for-multiple-writes` |
| `performance` | `warn` | 性能调优提示(`prefer-exists-over-count`) |
| `multiTenant` | `error` | `recommended` + `require-tenant-scope` |
```
const typeormEnterprise = require('eslint-plugin-typeorm-enterprise');
module.exports = [
typeormEnterprise.configs.strict, // maximum enforcement
typeormEnterprise.configs.performance, // + perf warnings
// typeormEnterprise.configs.multiTenant, // for multi-tenant apps
];
```
## 📏 规则
| 规则 | 描述 | 🔧 | 配置 |
|---|---|:--:|:--:|
| [`no-raw-query`](./docs/rules/no-raw-query.md) | 禁止通过 TypeORM 查询辅助工具和原生 SQL 方法执行原生 SQL。 | | ✅ recommended |
| [`require-parameterized-query`](./docs/rules/require-parameterized-query.md) | 要求使用参数化查询,而不是插值或拼接的原生 SQL。 | | ✅ recommended |
| [`no-synchronize-true`](./docs/rules/no-synchronize-true.md) | 禁止在 TypeORM 数据源配置中启用 `synchronize: true`。 | 🔧 | ✅ recommended |
| [`no-entity-manager-query`](./docs/rules/no-entity-manager-query.md) | 禁止直接在 TypeORM EntityManager 上执行原生查询。 | | ✅ recommended |
| [`require-transaction`](./docs/rules/require-transaction.md) | 要求数据修改操作必须在 transaction 回调中运行。 | | ⚠️ strict |
| [`no-unsafe-query-builder-delete`](./docs/rules/no-unsafe-query-builder-delete.md) | 禁止在没有 where 子句的情况下执行 QueryBuilder 的 delete/update 链。 | | ✅ recommended |
| [`no-interpolated-where`](./docs/rules/no-interpolated-where.md) | 禁止在 QueryBuilder 的 where 子句中使用插值或拼接的字符串。 | | ✅ recommended |
| [`prefer-transaction-for-multiple-writes`](./docs/rules/prefer-transaction-for-multiple-writes.md) | 建议将多个写操作合并为一个单一的 transaction。 | | ⚠️ strict |
| [`require-tenant-scope`](./docs/rules/require-tenant-scope.md) | 要求在 TypeORM 读写操作中进行租户范围内的访问(多租户)。 | | 🏢 multiTenant |
| [`prefer-exists-over-count`](./docs/rules/prefer-exists-over-count.md) | 当只关心是否存在时,建议优先进行存在性检查而不是计算行数。 | | 🚀 performance |
🔧 = 支持自动修复。完整的选项参考可在 [`docs/rules/`](./docs/rules) 中找到。
运行 `npm run doc` 可根据规则元数据重新生成此表。
## 🧠 工作原理
该插件使用 **TypeScript** (`src/`) 编写,并通过
[tsup](https://tsup.egoist.dev) 编译为同时支持 ESM + CommonJS 的包以及类型声明(`dist/`)。每条规则都基于 AST,不需要项目中的类型信息,因此它可以在任何 ESLint 9 配置中运行,且无需额外的 parser 配置。
每条规则都会解析调用的 callee(方法 + 对象名),应用配置的允许/限制列表和 `ignorePatterns` glob,然后才检查相关参数。这种设计是刻意保守的:它只强制执行它能够证明的模式,对于其他任何情况保持沉默。
## 🗺️ 路线图
- [x] 覆盖 SQL 安全、schema、transaction、多租户和性能的 10 条规则
- [x] `recommended` / `warn` / `strict` / `performance` / `multiTenant` 配置
- [x] 使用 tsup 构建的 TypeScript 源码(同时支持 ESM + CJS)
- [x] CI 矩阵 (Node 18/20/22 · TypeScript 5.5–7) + 覆盖率
- [x] 自动生成的规则表(在 CI 中检查偏差)
- [x] 可同时在 ESLint 9+ 和 oxlint(JS 插件 API)下运行
- [x] 通过 Trusted Publishing (OIDC) 发布到 npm 并附带 provenance
- [ ] 针向 Repository / QueryBuilder API 的自动修复建议
- [ ] 通过 `@typescript-eslint` 服务进行类型感知检测
- [ ] 文档站点 / playground
## 📄 许可证
[MIT](https://github.com/alokraj68/eslint-plugin-typeorm-enterprise/blob/main/LICENSE) © alokraj68标签:MITM代理, 自动化攻击