@shys/crud 1.0.1-beta.0 → 1.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +274 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
# @shys/crud
|
|
2
|
+
|
|
3
|
+
MidwayJS v4 模块化 CRUD CLI 代码生成器,支持 TypeORM / Sequelize / MikroORM,支持配置文件和自定义模板。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# 全局安装
|
|
9
|
+
npm install -g @shys/crud
|
|
10
|
+
|
|
11
|
+
# 或项目内安装
|
|
12
|
+
npm install -D @shys/crud
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 快速开始
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# 交互式运行
|
|
19
|
+
npx @shys/crud
|
|
20
|
+
|
|
21
|
+
# 完整参数
|
|
22
|
+
npx @shys/crud --module home --table sys_user --src ./src
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
生成的目录结构:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
src/modules/home/
|
|
29
|
+
├── entity/
|
|
30
|
+
│ └── sysUser.entity.ts
|
|
31
|
+
├── dto/
|
|
32
|
+
│ └── sysUser/
|
|
33
|
+
│ ├── create.dto.ts
|
|
34
|
+
│ ├── update.dto.ts
|
|
35
|
+
│ ├── replace.dto.ts
|
|
36
|
+
│ ├── query.dto.ts
|
|
37
|
+
│ └── index.ts
|
|
38
|
+
├── vo/
|
|
39
|
+
│ └── sysUser/
|
|
40
|
+
│ ├── sysUser.vo.ts
|
|
41
|
+
│ ├── sysUserList.vo.ts
|
|
42
|
+
│ └── index.ts
|
|
43
|
+
├── service/
|
|
44
|
+
│ └── sysUser.service.ts
|
|
45
|
+
└── controller/
|
|
46
|
+
└── sysUser.controller.ts
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## CLI 参数
|
|
50
|
+
|
|
51
|
+
| 参数 | 简写 | 说明 |
|
|
52
|
+
|------|------|------|
|
|
53
|
+
| `--module` | `-m` | 模块名(如 home/order/goods) |
|
|
54
|
+
| `--table` | `-t` | 数据库表名(如 sys_dept/city/order_info) |
|
|
55
|
+
| `--src` | `-s` | Midway src 根路径,默认 `./src` |
|
|
56
|
+
| `--output` | `-o` | 生成文件输出路径,默认与 `--src` 相同 |
|
|
57
|
+
| `--templates-path` | - | 自定义模板目录路径 |
|
|
58
|
+
| `--force` | `-f` | 强制覆盖已存在的文件 |
|
|
59
|
+
| `--dry-run` | `-n` | 模拟运行,不实际写入文件 |
|
|
60
|
+
| `--compile` | `-c` | 生成后自动执行 `mwtsc --cleanOutDir` |
|
|
61
|
+
| `--only-entity` | - | 仅生成 entity 文件,不生成 DTO/VO/Service/Controller |
|
|
62
|
+
| `--no-db` | - | 不连接数据库,使用模板字段模式 |
|
|
63
|
+
| `--db-host` | - | 数据库 host |
|
|
64
|
+
| `--db-port` | - | 数据库端口,默认 3306 |
|
|
65
|
+
| `--db-user` | - | 数据库用户名 |
|
|
66
|
+
| `--db-pass` | - | 数据库密码 |
|
|
67
|
+
| `--db-name` | - | 数据库名 |
|
|
68
|
+
| `--help` | `-h` | 显示帮助信息 |
|
|
69
|
+
|
|
70
|
+
## 配置文件
|
|
71
|
+
|
|
72
|
+
在项目根目录或 `--src` 指向的目录下放置配置文件,支持以下格式(按优先级从高到低):
|
|
73
|
+
|
|
74
|
+
- `crud.config.ts`
|
|
75
|
+
- `crud.config.js`
|
|
76
|
+
- `crud.config.mjs`
|
|
77
|
+
- `crud.config.cjs`
|
|
78
|
+
- `.crudrc`
|
|
79
|
+
- `.crudrc.json`
|
|
80
|
+
- `.crudrc.yaml`
|
|
81
|
+
- `.crudrc.yml`
|
|
82
|
+
- `.crudrc.js`
|
|
83
|
+
- `.config/crudrc`
|
|
84
|
+
- `.config/crudrc.json`
|
|
85
|
+
- `package.json` 中的 `"crud"` 字段
|
|
86
|
+
|
|
87
|
+
### 配置示例(crud.config.ts)
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
export default {
|
|
91
|
+
baseSrcPath: './src',
|
|
92
|
+
outputPath: './src',
|
|
93
|
+
moduleName: 'home',
|
|
94
|
+
tableName: 'sys_user',
|
|
95
|
+
force: false,
|
|
96
|
+
dryRun: false,
|
|
97
|
+
compile: false,
|
|
98
|
+
onlyEntity: false,
|
|
99
|
+
noDb: false,
|
|
100
|
+
templatesPath: './my-templates',
|
|
101
|
+
db: {
|
|
102
|
+
host: '127.0.0.1',
|
|
103
|
+
port: 3306,
|
|
104
|
+
user: 'root',
|
|
105
|
+
password: '',
|
|
106
|
+
database: 'mydb',
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## 自定义模板
|
|
112
|
+
|
|
113
|
+
本工具使用 [nunjucks](https://mozilla.github.io/nunjucks/) 作为模板引擎。所有模板文件均可自定义。
|
|
114
|
+
|
|
115
|
+
### 使用方式
|
|
116
|
+
|
|
117
|
+
1. 将默认模板复制到项目目录(如 `./my-templates`)
|
|
118
|
+
2. 修改模板内容
|
|
119
|
+
3. 通过 `--templates-path ./my-templates` 参数或配置文件 `templatesPath` 指定自定义模板目录
|
|
120
|
+
|
|
121
|
+
未覆盖的模板会自动使用默认模板。
|
|
122
|
+
|
|
123
|
+
### 可用的模板文件
|
|
124
|
+
|
|
125
|
+
| 模板名 | 文件名 | 说明 |
|
|
126
|
+
|--------|--------|------|
|
|
127
|
+
| entity | `entity.njk` | 实体类 |
|
|
128
|
+
| dto-create | `dto-create.njk` | 创建 DTO |
|
|
129
|
+
| dto-update | `dto-update.njk` | 更新 DTO |
|
|
130
|
+
| dto-replace | `dto-replace.njk` | 替换 DTO |
|
|
131
|
+
| dto-query | `dto-query.njk` | 查询 DTO |
|
|
132
|
+
| dto-index | `dto-index.njk` | DTO 索引导出 |
|
|
133
|
+
| vo-single | `vo-single.njk` | 单个 VO |
|
|
134
|
+
| vo-list | `vo-list.njk` | 列表 VO |
|
|
135
|
+
| vo-index | `vo-index.njk` | VO 索引导出 |
|
|
136
|
+
| service | `service.njk` | Service 类 |
|
|
137
|
+
| controller | `controller.njk` | Controller 类 |
|
|
138
|
+
| crud-page-vo | `crud-page-vo.njk` | 分页 VO(全局共享) |
|
|
139
|
+
|
|
140
|
+
### 模板变量参考
|
|
141
|
+
|
|
142
|
+
#### entity.njk
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
orm: string // ORM 类型:typeorm/sequelize/mikro-orm
|
|
146
|
+
ormSuffix: string // 实体文件后缀:entity/model
|
|
147
|
+
fields: Array<{ // 字段列表(排除主键和基础字段)
|
|
148
|
+
name: string // 数据库字段名
|
|
149
|
+
camel: string // 驼峰命名
|
|
150
|
+
type: string // 数据库类型
|
|
151
|
+
jsType: string // JavaScript 类型
|
|
152
|
+
ormType: string // ORM 类型
|
|
153
|
+
len: number | null // 字段长度
|
|
154
|
+
nullable: boolean // 是否可空
|
|
155
|
+
comment: string // 字段注释
|
|
156
|
+
supportsLength: boolean
|
|
157
|
+
isDate: boolean
|
|
158
|
+
isSensitive: boolean
|
|
159
|
+
idx: boolean // 是否索引字段
|
|
160
|
+
}>
|
|
161
|
+
tableName: string // 表名
|
|
162
|
+
Pascal: string // PascalCase 命名
|
|
163
|
+
Camel: string // camelCase 命名
|
|
164
|
+
pkInfo: Object | null // 主键信息
|
|
165
|
+
hasPk: boolean // 是否有主键
|
|
166
|
+
hasDateField: boolean // 是否有日期字段
|
|
167
|
+
idxCount: number // 索引字段数量
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### dto-create.njk / dto-update.njk / dto-replace.njk
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
CreateName: string // Create DTO 类名
|
|
174
|
+
UpdateName: string // Update DTO 类名
|
|
175
|
+
ReplaceName: string // Replace DTO 类名
|
|
176
|
+
Pascal: string // PascalCase 命名
|
|
177
|
+
createFields: Array // 可创建字段
|
|
178
|
+
writable: Array // 可写字段
|
|
179
|
+
picks: string // Pick 类型的字段列表字符串
|
|
180
|
+
typeArgs: string // 类型联合字符串
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
#### service.njk
|
|
184
|
+
|
|
185
|
+
```
|
|
186
|
+
ServiceName: string // Service 类名
|
|
187
|
+
Pascal: string // PascalCase 命名
|
|
188
|
+
Camel: string // camelCase 命名
|
|
189
|
+
ormInfo: {
|
|
190
|
+
orm: string
|
|
191
|
+
ormSuffix: string
|
|
192
|
+
serviceParent: string // 父类名
|
|
193
|
+
importCrudOrm: string // 导入语句
|
|
194
|
+
injectEntity: string // 装饰器名
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### controller.njk
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
CtrlName: string // Controller 类名
|
|
202
|
+
ServiceName: string // Service 类名
|
|
203
|
+
Pascal: string
|
|
204
|
+
Camel: string
|
|
205
|
+
pluralKebab: string // 复数 kebab-case 路由名
|
|
206
|
+
tagName: string // Swagger tag 名
|
|
207
|
+
desc: string // 接口描述
|
|
208
|
+
sortable: string[] // 可排序字段
|
|
209
|
+
filterable: string[] // 可过滤字段
|
|
210
|
+
searchable: string[] // 可搜索字段
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### 自定义模板示例
|
|
214
|
+
|
|
215
|
+
在 `./my-templates/service.njk` 中:
|
|
216
|
+
|
|
217
|
+
```njk
|
|
218
|
+
import { Provide, Inject } from '@midwayjs/core';
|
|
219
|
+
import { {{ Pascal }} } from '../entity/{{ Camel }}.entity';
|
|
220
|
+
import { BaseService } from '../framework/base.service';
|
|
221
|
+
|
|
222
|
+
@Provide()
|
|
223
|
+
export class {{ ServiceName }} extends BaseService<{{ Pascal }}> {
|
|
224
|
+
|
|
225
|
+
entityClass = {{ Pascal }};
|
|
226
|
+
|
|
227
|
+
// 在这里添加你的自定义方法
|
|
228
|
+
async customMethod() {
|
|
229
|
+
// ...
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## 编程式 API
|
|
235
|
+
|
|
236
|
+
除了 CLI,也可以在代码中直接调用:
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
import { buildFiles, createRenderer, loadConfig } from '@shys/crud';
|
|
240
|
+
import { writeFileSync } from 'node:fs';
|
|
241
|
+
|
|
242
|
+
// 1. 使用 buildFiles
|
|
243
|
+
const result = buildFiles({
|
|
244
|
+
moduleName: 'home',
|
|
245
|
+
tableName: 'sys_user',
|
|
246
|
+
baseSrcPath: './src',
|
|
247
|
+
ormInfo: { /* ... */ },
|
|
248
|
+
fields: [ /* ... */ ],
|
|
249
|
+
onlyEntity: false,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
result.files.forEach((f) => {
|
|
253
|
+
writeFileSync(f.path, f.content);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// 2. 使用模板渲染器
|
|
257
|
+
const renderer = createRenderer({
|
|
258
|
+
customTemplatesPath: './my-templates',
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const code = renderer.render('entity', {
|
|
262
|
+
orm: 'typeorm',
|
|
263
|
+
fields: [ /* ... */ ],
|
|
264
|
+
Pascal: 'SysUser',
|
|
265
|
+
// ...
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// 3. 加载配置文件
|
|
269
|
+
const config = await loadConfig(process.cwd());
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shys/crud",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.1",
|
|
4
4
|
"description": "MidwayJS v4 模块化 CRUD CLI 生成器",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"dist",
|
|
17
|
-
"templates"
|
|
17
|
+
"templates",
|
|
18
|
+
"README.md"
|
|
18
19
|
],
|
|
19
20
|
"scripts": {
|
|
20
21
|
"build": "tsc",
|