@zenorm/generate 1.8.1 → 1.9.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/CHANGELOG.md +6 -0
- package/dist/generate.js +26 -19
- package/dist/types.d.ts +12 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/generate.js
CHANGED
|
@@ -13,6 +13,8 @@ async function generate(tables, cfg) {
|
|
|
13
13
|
outputDir: './src/model',
|
|
14
14
|
tablesFilename: '_tables',
|
|
15
15
|
repositoriesFilename: '_repositories',
|
|
16
|
+
staticMethods: true,
|
|
17
|
+
instanceMethods: true,
|
|
16
18
|
}, cfg);
|
|
17
19
|
const outputDir = (0, utils_1.cwdPath)(config.outputDir);
|
|
18
20
|
await (0, utils_1.checkFileDir)(outputDir);
|
|
@@ -111,26 +113,31 @@ async function generate(tables, cfg) {
|
|
|
111
113
|
if (config.bindQuery) {
|
|
112
114
|
repositories.push(` /** Query 绑定的 ${className}Repository */`);
|
|
113
115
|
repositories.push(` static repository = ${className}.query(_query);`);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
116
|
+
// 静态方法
|
|
117
|
+
if (config.staticMethods) {
|
|
118
|
+
repositories.push(...[
|
|
119
|
+
'of',
|
|
120
|
+
'find',
|
|
121
|
+
'findByPk',
|
|
122
|
+
'getByPk',
|
|
123
|
+
'count',
|
|
124
|
+
'exists',
|
|
125
|
+
'create',
|
|
126
|
+
'createAndGet',
|
|
127
|
+
'save',
|
|
128
|
+
'update',
|
|
129
|
+
'delete',
|
|
130
|
+
].map(i => ` static ${i}: typeof ${className}.repository.${i} = ${className}.repository.${i}.bind(${className}.repository);`));
|
|
131
|
+
}
|
|
127
132
|
// 实例方法
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
133
|
+
if (config.instanceMethods) {
|
|
134
|
+
repositories.push(` /** 保存当前实例数据 */`);
|
|
135
|
+
repositories.push(` save() { return ${className}.repository.save(this); }`);
|
|
136
|
+
repositories.push(` /** 更新当前实例数据 */`);
|
|
137
|
+
repositories.push(` update(data: UpdateRow<${className}>) { return ${className}.repository.update(this, data); }`);
|
|
138
|
+
repositories.push(` /** 删除当前实例数据 */`);
|
|
139
|
+
repositories.push(` delete() { return ${className}.repository.delete(this); }`);
|
|
140
|
+
}
|
|
134
141
|
}
|
|
135
142
|
repositories.push('}');
|
|
136
143
|
});
|
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,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenorm/generate",
|
|
3
3
|
"description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.9.1",
|
|
5
5
|
"exports": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"homepage": "https://zenorm.node.ltd",
|