@shys/crud 1.0.1-beta.1 → 1.0.1-beta.2
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 +68 -0
- package/package.json +1 -1
- package/templates/entity.njk +3 -3
package/README.md
CHANGED
|
@@ -22,6 +22,51 @@ npx @shys/crud
|
|
|
22
22
|
npx @shys/crud --module home --table sys_user --src ./src
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
### 配置到 package.json
|
|
26
|
+
|
|
27
|
+
在项目的 `package.json` 中添加 `scripts` 命令,方便团队成员快速使用:
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"scripts": {
|
|
32
|
+
"crud": "shys-crud",
|
|
33
|
+
"crud:entity": "shys-crud --only-entity",
|
|
34
|
+
"crud:dry": "shys-crud --dry-run",
|
|
35
|
+
"crud:user": "shys-crud --module system --table sys_user --force",
|
|
36
|
+
"crud:order": "shys-crud --module order --table order_info --force"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@shys/crud": "^1.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
配置后使用方式:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# 交互式运行(推荐,可按需填写参数)
|
|
48
|
+
npm run crud
|
|
49
|
+
|
|
50
|
+
# 只生成 entity
|
|
51
|
+
npm run crud:entity
|
|
52
|
+
|
|
53
|
+
# 模拟运行预览(不实际写文件)
|
|
54
|
+
npm run crud:dry
|
|
55
|
+
|
|
56
|
+
# 生成指定表的完整 CRUD(已预定义 module 和 table)
|
|
57
|
+
npm run crud:user
|
|
58
|
+
npm run crud:order
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
如果使用 pnpm:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pnpm crud
|
|
65
|
+
pnpm crud:user
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> **提示**:推荐将常用的表配置成独立脚本(如上面的 `crud:user`、`crud:order`),配合 `--force` 可直接覆盖重新生成,配合 `crud.config.ts` 配置文件使用体验更佳。
|
|
69
|
+
|
|
25
70
|
生成的目录结构:
|
|
26
71
|
|
|
27
72
|
```
|
|
@@ -108,6 +153,29 @@ export default {
|
|
|
108
153
|
};
|
|
109
154
|
```
|
|
110
155
|
|
|
156
|
+
### 配置字段说明
|
|
157
|
+
|
|
158
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
159
|
+
|------|------|--------|------|
|
|
160
|
+
| `baseSrcPath` | `string` | `./src` | Midway 项目的 src 根目录绝对或相对路径。生成的模块文件将放在此目录下的 `modules/` 中。 |
|
|
161
|
+
| `outputPath` | `string` | 与 `baseSrcPath` 相同 | 生成文件的输出路径。如需将代码生成到其他目录(如临时目录),可单独指定此项。 |
|
|
162
|
+
| `moduleName` | `string` | 交互式询问 | 业务模块名,用于组织目录结构,如 `home`、`order`、`goods`。 |
|
|
163
|
+
| `tableName` | `string` | 交互式询问 | 数据库表名,如 `sys_user`、`city`、`order_info`。用于生成类名和 DESCRIBE 表结构。 |
|
|
164
|
+
| `force` | `boolean` | `false` | 是否强制覆盖已存在的文件。为 `false` 时遇到同名文件会跳过并提示。 |
|
|
165
|
+
| `dryRun` | `boolean` | `false` | 模拟运行模式。只打印将要生成的文件列表,不实际写入磁盘。适合预览效果。 |
|
|
166
|
+
| `compile` | `boolean` | `false` | 生成完成后是否自动执行 `mwtsc --cleanOutDir` 进行 TypeScript 编译。 |
|
|
167
|
+
| `onlyEntity` | `boolean` | `false` | 仅生成 entity 实体文件,不生成 DTO、VO、Service、Controller 等其他文件。适合只需要实体类的场景。 |
|
|
168
|
+
| `noDb` | `boolean` | `false` | 不连接数据库读取表结构。启用后使用内置模板字段生成(字段为示例占位,需手动修改)。 |
|
|
169
|
+
| `templatesPath` | `string` | 无 | 自定义模板目录路径。在此目录下放置与默认模板同名的 `.njk` 文件即可覆盖对应模板。未覆盖的模板继续使用默认模板。 |
|
|
170
|
+
| `db.host` | `string` | `127.0.0.1` | 数据库主机地址。 |
|
|
171
|
+
| `db.port` | `number` | `3306` | 数据库端口。 |
|
|
172
|
+
| `db.user` | `string` | `root` | 数据库用户名。 |
|
|
173
|
+
| `db.password` | `string` | 空 | 数据库密码。 |
|
|
174
|
+
| `db.database` | `string` | 交互式询问 | 数据库名。指定后会尝试连接数据库并 DESCRIBE 表结构,使用真实字段信息生成代码。 |
|
|
175
|
+
| `db.type` | `string` | `mysql` | 数据库类型,目前仅支持 MySQL。 |
|
|
176
|
+
|
|
177
|
+
> **优先级说明**:命令行参数 > 配置文件 > 默认值。例如命令行传了 `--force`,即使配置文件中 `force: false`,最终也会以 `true` 为准。
|
|
178
|
+
|
|
111
179
|
## 自定义模板
|
|
112
180
|
|
|
113
181
|
本工具使用 [nunjucks](https://mozilla.github.io/nunjucks/) 作为模板引擎。所有模板文件均可自定义。
|
package/package.json
CHANGED
package/templates/entity.njk
CHANGED
|
@@ -6,7 +6,7 @@ import { Entity, Column{% if hasPk %}, PrimaryGeneratedColumn{% endif %}{% if id
|
|
|
6
6
|
import { Expose{% if hasDateField %}, Transform{% endif %} } from 'class-transformer';
|
|
7
7
|
import { ApiProperty, ApiPropertyOptional } from '@midwayjs/swagger';
|
|
8
8
|
import { Rule } from '@midwayjs/validation';
|
|
9
|
-
import
|
|
9
|
+
import * as Joi from 'joi';
|
|
10
10
|
import { BaseEntity } from '../../../framework/entity/base.entity';
|
|
11
11
|
|
|
12
12
|
@Entity({{ tableName | json }}, {
|
|
@@ -24,7 +24,7 @@ export class {{ Pascal }} extends BaseEntity {
|
|
|
24
24
|
description: {{ (pkInfo.comment or '主键ID') | json }},
|
|
25
25
|
})
|
|
26
26
|
@Expose()
|
|
27
|
-
@Rule(
|
|
27
|
+
@Rule(Joi.number().integer().required())
|
|
28
28
|
{{ pkInfo.camel }}: number;
|
|
29
29
|
|
|
30
30
|
{% endif -%}
|
|
@@ -46,7 +46,7 @@ export class {{ Pascal }} extends BaseEntity {
|
|
|
46
46
|
{% if f.defaultVal != null %} example: {{ f.defaultVal | json }},
|
|
47
47
|
{% endif %} })
|
|
48
48
|
@Expose()
|
|
49
|
-
@Rule({{ f.
|
|
49
|
+
@Rule({{ f.joiRule }})
|
|
50
50
|
{% if f.isDate %} @Transform(({ value }) => {
|
|
51
51
|
if (value == null) return null;
|
|
52
52
|
const d = value instanceof Date ? value : new Date(value);
|