@zenorm/generate 1.8.0 → 1.9.0
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/CHANGELOG.md +46 -40
- package/README.md +24 -24
- package/dist/generate.js +28 -19
- package/dist/types.d.ts +12 -0
- package/package.json +44 -44
package/CHANGELOG.md
CHANGED
|
@@ -1,40 +1,46 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [1.
|
|
4
|
-
-
|
|
5
|
-
|
|
6
|
-
## [1.
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
## [1.
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
## [1.
|
|
13
|
-
-
|
|
14
|
-
|
|
15
|
-
## [1.
|
|
16
|
-
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
## [1.
|
|
26
|
-
-
|
|
27
|
-
|
|
28
|
-
## [1.
|
|
29
|
-
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
-
|
|
38
|
-
|
|
39
|
-
## [
|
|
40
|
-
-
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [1.9.0] - 2023-8-9
|
|
4
|
+
- 增加配置选项: staticMethods instanceMethods
|
|
5
|
+
|
|
6
|
+
## [1.8.1] - 2023-5-19
|
|
7
|
+
- fix: 使用 global 继承时构造函数缺少 super 调用
|
|
8
|
+
|
|
9
|
+
## [1.8.0] - 2023-5-18
|
|
10
|
+
- 撤销 [1.6.0] 的修改,动态 as 功能无法满足需求
|
|
11
|
+
|
|
12
|
+
## [1.7.0] - 2023-5-18
|
|
13
|
+
- 构造器增加 pkval 参数
|
|
14
|
+
|
|
15
|
+
## [1.6.0] - 2023-5-18
|
|
16
|
+
- 使用构造器提前初始化对象的属性,用于优化 v8 对象缓存机制
|
|
17
|
+
|
|
18
|
+
## [1.5.0] - 2023-4-19
|
|
19
|
+
- 更新: update 方法使用 UpdateRow<M>
|
|
20
|
+
|
|
21
|
+
## [1.4.0] - 2023-3-29
|
|
22
|
+
- createRepositoryQuery 添加表描述
|
|
23
|
+
- 如果没有主键 pkType = never
|
|
24
|
+
|
|
25
|
+
## [1.3.1] - 2023-3-27
|
|
26
|
+
- fix: windows 换行符问题
|
|
27
|
+
|
|
28
|
+
## [1.3.0] - 2023-3-25
|
|
29
|
+
- update: bindQuery 增加 save, update, delete
|
|
30
|
+
|
|
31
|
+
## [1.2.0] - 2023-3-24
|
|
32
|
+
- update: bindQuery 改为函数形式
|
|
33
|
+
|
|
34
|
+
## [1.1.1] - 2023-3-24
|
|
35
|
+
- fix: 生成格式少个空格和空行
|
|
36
|
+
- update: bindQuery 增加 count 和 exists
|
|
37
|
+
- 补充注释说明
|
|
38
|
+
|
|
39
|
+
## [1.1.0] - 2023-3-24
|
|
40
|
+
- 新增: 绑定静态 Query
|
|
41
|
+
|
|
42
|
+
## [1.0.0] - 2023-3-23
|
|
43
|
+
- 完善
|
|
44
|
+
|
|
45
|
+
## [0.0.1] - 2023-3-23
|
|
46
|
+
- 从 zenorm@3.25.0 分离为独立项目
|
package/README.md
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
# ZenORM Generate
|
|
2
|
-
|
|
3
|
-
[ZenORM](https://www.npmjs.com/package/zenorm) Table Structure Code Generation Tool
|
|
4
|
-
|
|
5
|
-
表结构代码生成工具
|
|
6
|
-
|
|
7
|
-
## Generate Config - 代码生成配置
|
|
8
|
-
|
|
9
|
-
| 配置项 | 类型 | 默认值 | 说明
|
|
10
|
-
| ----- | --- | ----- | ----
|
|
11
|
-
| host | `string` | 无 | 目标数据库地址
|
|
12
|
-
| port | `number` | 无 | 数据库端口
|
|
13
|
-
| user | `string` | 无 | 数据库用户名
|
|
14
|
-
| password | `string` | 无 | 数据库密码
|
|
15
|
-
| database | `string` | 无 | 数据库名
|
|
16
|
-
| outputDir | `string` | `'./src/model'` | 代码输出目录
|
|
17
|
-
| tablesFilename | `string` | `'_tables'` | 生成数据库表结构文件名
|
|
18
|
-
| repositoriesFilename | `string` | `'_repositories'` | 生成 repositories 文件名
|
|
19
|
-
| globalFilename | `string` | 无 | 全局文件名 - 如果设置所有表将继承于此 - 例如设置为:'_global' - 如果文件不存在则自动创建
|
|
20
|
-
| bindQuery | `boolean` | `false` | repositories 文件生成 bindQuery(query: QueryParam) 方法用于设置模型的 Query 源 - 并生成模型的静态 Repository 方法
|
|
21
|
-
| generateRepositories | `boolean` | `false` | 是否生成 Repositories 类 - 通常用于多租户模式做数据库前置绑定 `Query` 对象
|
|
22
|
-
| declareRepositoriesToModules | `string[]` | 无 | 是否需将 Repositories 实例定义到目标模块中 - 例如: `["@zenweb/core.Core.repositories"]`
|
|
23
|
-
| filter | `string` | 无 | 表过滤规则正则
|
|
24
|
-
| include | `string` | 无 | 表包含规则正则
|
|
1
|
+
# ZenORM Generate
|
|
2
|
+
|
|
3
|
+
[ZenORM](https://www.npmjs.com/package/zenorm) Table Structure Code Generation Tool
|
|
4
|
+
|
|
5
|
+
表结构代码生成工具
|
|
6
|
+
|
|
7
|
+
## Generate Config - 代码生成配置
|
|
8
|
+
|
|
9
|
+
| 配置项 | 类型 | 默认值 | 说明
|
|
10
|
+
| ----- | --- | ----- | ----
|
|
11
|
+
| host | `string` | 无 | 目标数据库地址
|
|
12
|
+
| port | `number` | 无 | 数据库端口
|
|
13
|
+
| user | `string` | 无 | 数据库用户名
|
|
14
|
+
| password | `string` | 无 | 数据库密码
|
|
15
|
+
| database | `string` | 无 | 数据库名
|
|
16
|
+
| outputDir | `string` | `'./src/model'` | 代码输出目录
|
|
17
|
+
| tablesFilename | `string` | `'_tables'` | 生成数据库表结构文件名
|
|
18
|
+
| repositoriesFilename | `string` | `'_repositories'` | 生成 repositories 文件名
|
|
19
|
+
| globalFilename | `string` | 无 | 全局文件名 - 如果设置所有表将继承于此 - 例如设置为:'_global' - 如果文件不存在则自动创建
|
|
20
|
+
| bindQuery | `boolean` | `false` | repositories 文件生成 bindQuery(query: QueryParam) 方法用于设置模型的 Query 源 - 并生成模型的静态 Repository 方法
|
|
21
|
+
| generateRepositories | `boolean` | `false` | 是否生成 Repositories 类 - 通常用于多租户模式做数据库前置绑定 `Query` 对象
|
|
22
|
+
| declareRepositoriesToModules | `string[]` | 无 | 是否需将 Repositories 实例定义到目标模块中 - 例如: `["@zenweb/core.Core.repositories"]`
|
|
23
|
+
| filter | `string` | 无 | 表过滤规则正则
|
|
24
|
+
| include | `string` | 无 | 表包含规则正则
|
package/dist/generate.js
CHANGED
|
@@ -13,6 +13,7 @@ async function generate(tables, cfg) {
|
|
|
13
13
|
outputDir: './src/model',
|
|
14
14
|
tablesFilename: '_tables',
|
|
15
15
|
repositoriesFilename: '_repositories',
|
|
16
|
+
instanceMethods: true,
|
|
16
17
|
}, cfg);
|
|
17
18
|
const outputDir = (0, utils_1.cwdPath)(config.outputDir);
|
|
18
19
|
await (0, utils_1.checkFileDir)(outputDir);
|
|
@@ -67,6 +68,9 @@ async function generate(tables, cfg) {
|
|
|
67
68
|
structs.push(...props);
|
|
68
69
|
structs.push('');
|
|
69
70
|
structs.push(` constructor(data?: object) {`);
|
|
71
|
+
if (config.globalFilename) {
|
|
72
|
+
structs.push(` super();`);
|
|
73
|
+
}
|
|
70
74
|
structs.push(` data && Object.assign(this, data);`);
|
|
71
75
|
structs.push(` }`);
|
|
72
76
|
structs.push('}');
|
|
@@ -108,26 +112,31 @@ async function generate(tables, cfg) {
|
|
|
108
112
|
if (config.bindQuery) {
|
|
109
113
|
repositories.push(` /** Query 绑定的 ${className}Repository */`);
|
|
110
114
|
repositories.push(` static repository = ${className}.query(_query);`);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
115
|
+
// 静态方法
|
|
116
|
+
if (config.staticMethods) {
|
|
117
|
+
repositories.push(...[
|
|
118
|
+
'of',
|
|
119
|
+
'find',
|
|
120
|
+
'findByPk',
|
|
121
|
+
'getByPk',
|
|
122
|
+
'count',
|
|
123
|
+
'exists',
|
|
124
|
+
'create',
|
|
125
|
+
'createAndGet',
|
|
126
|
+
'save',
|
|
127
|
+
'update',
|
|
128
|
+
'delete',
|
|
129
|
+
].map(i => ` static ${i}: typeof ${className}.repository.${i} = ${className}.repository.${i}.bind(${className}.repository);`));
|
|
130
|
+
}
|
|
124
131
|
// 实例方法
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
132
|
+
if (config.instanceMethods) {
|
|
133
|
+
repositories.push(` /** 保存当前实例数据 */`);
|
|
134
|
+
repositories.push(` save() { return ${className}.repository.save(this); }`);
|
|
135
|
+
repositories.push(` /** 更新当前实例数据 */`);
|
|
136
|
+
repositories.push(` update(data: UpdateRow<${className}>) { return ${className}.repository.update(this, data); }`);
|
|
137
|
+
repositories.push(` /** 删除当前实例数据 */`);
|
|
138
|
+
repositories.push(` delete() { return ${className}.repository.delete(this); }`);
|
|
139
|
+
}
|
|
131
140
|
}
|
|
132
141
|
repositories.push('}');
|
|
133
142
|
});
|
package/dist/types.d.ts
CHANGED
|
@@ -92,6 +92,18 @@ export interface GenerateConfig {
|
|
|
92
92
|
* @default false
|
|
93
93
|
*/
|
|
94
94
|
bindQuery?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* 是否在模型类中增加 Repository 静态方法
|
|
97
|
+
* - 必须启用 bindQuery
|
|
98
|
+
* @default true
|
|
99
|
+
*/
|
|
100
|
+
staticMethods?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 是否在实例中增加方法,例如 save update delete
|
|
103
|
+
* - 必须启用 bindQuery
|
|
104
|
+
* @default true
|
|
105
|
+
*/
|
|
106
|
+
instanceMethods?: boolean;
|
|
95
107
|
/**
|
|
96
108
|
* 是否生成 Repositories 类
|
|
97
109
|
* - 通常用于多租户模式做数据库前置绑定 `Query` 对象
|
package/package.json
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zenorm/generate",
|
|
3
|
-
"description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
|
|
4
|
-
"version": "1.
|
|
5
|
-
"exports": "./dist/index.js",
|
|
6
|
-
"types": "./dist/index.d.ts",
|
|
7
|
-
"homepage": "https://zenorm.node.ltd",
|
|
8
|
-
"author": "YeFei <316606233@qq.com>",
|
|
9
|
-
"license": "MIT",
|
|
10
|
-
"bin": {
|
|
11
|
-
"zenorm-generate": "./dist/bin/zenorm-generate.js"
|
|
12
|
-
},
|
|
13
|
-
"files": [
|
|
14
|
-
"dist"
|
|
15
|
-
],
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": "rimraf dist && tsc",
|
|
18
|
-
"prepublishOnly": "npm run build",
|
|
19
|
-
"gen": "cd test && ts-node ../src/bin/zenorm-generate.ts config.json",
|
|
20
|
-
"t1": "cd test && cross-env DEBUG=* ts-node t1"
|
|
21
|
-
},
|
|
22
|
-
"keywords": [
|
|
23
|
-
"mysql",
|
|
24
|
-
"query",
|
|
25
|
-
"model",
|
|
26
|
-
"types",
|
|
27
|
-
"generate",
|
|
28
|
-
"database"
|
|
29
|
-
],
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@types/node": "^12.20.41",
|
|
32
|
-
"@zenorm/generate-mysql": "^1.3.0",
|
|
33
|
-
"cross-env": "^7.0.3",
|
|
34
|
-
"mysql-easy-query": "^3.13.0",
|
|
35
|
-
"rimraf": "^4.4.1",
|
|
36
|
-
"ts-node": "^10.9.1",
|
|
37
|
-
"typescript": "^5.0.4",
|
|
38
|
-
"zenorm": "^3.31.0"
|
|
39
|
-
},
|
|
40
|
-
"dependencies": {
|
|
41
|
-
"pascal-case": "^3.1.2",
|
|
42
|
-
"snake-case": "^3.0.4"
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zenorm/generate",
|
|
3
|
+
"description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
|
|
4
|
+
"version": "1.9.0",
|
|
5
|
+
"exports": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"homepage": "https://zenorm.node.ltd",
|
|
8
|
+
"author": "YeFei <316606233@qq.com>",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"bin": {
|
|
11
|
+
"zenorm-generate": "./dist/bin/zenorm-generate.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "rimraf dist && tsc",
|
|
18
|
+
"prepublishOnly": "npm run build",
|
|
19
|
+
"gen": "cd test && ts-node ../src/bin/zenorm-generate.ts config.json",
|
|
20
|
+
"t1": "cd test && cross-env DEBUG=* ts-node t1"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"mysql",
|
|
24
|
+
"query",
|
|
25
|
+
"model",
|
|
26
|
+
"types",
|
|
27
|
+
"generate",
|
|
28
|
+
"database"
|
|
29
|
+
],
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^12.20.41",
|
|
32
|
+
"@zenorm/generate-mysql": "^1.3.0",
|
|
33
|
+
"cross-env": "^7.0.3",
|
|
34
|
+
"mysql-easy-query": "^3.13.0",
|
|
35
|
+
"rimraf": "^4.4.1",
|
|
36
|
+
"ts-node": "^10.9.1",
|
|
37
|
+
"typescript": "^5.0.4",
|
|
38
|
+
"zenorm": "^3.31.0"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"pascal-case": "^3.1.2",
|
|
42
|
+
"snake-case": "^3.0.4"
|
|
43
|
+
}
|
|
44
|
+
}
|