@zenorm/generate 1.8.1 → 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 CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.9.0] - 2023-8-9
4
+ - 增加配置选项: staticMethods instanceMethods
5
+
3
6
  ## [1.8.1] - 2023-5-19
4
7
  - fix: 使用 global 继承时构造函数缺少 super 调用
5
8
 
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);
@@ -111,26 +112,31 @@ async function generate(tables, cfg) {
111
112
  if (config.bindQuery) {
112
113
  repositories.push(` /** Query 绑定的 ${className}Repository */`);
113
114
  repositories.push(` static repository = ${className}.query(_query);`);
114
- repositories.push(...[
115
- 'of',
116
- 'find',
117
- 'findByPk',
118
- 'getByPk',
119
- 'count',
120
- 'exists',
121
- 'create',
122
- 'createAndGet',
123
- 'save',
124
- 'update',
125
- 'delete',
126
- ].map(i => ` static ${i}: typeof ${className}.repository.${i} = ${className}.repository.${i}.bind(${className}.repository);`));
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
+ }
127
131
  // 实例方法
128
- repositories.push(` /** 保存当前实例数据 */`);
129
- repositories.push(` save() { return ${className}.repository.save(this); }`);
130
- repositories.push(` /** 更新当前实例数据 */`);
131
- repositories.push(` update(data: UpdateRow<${className}>) { return ${className}.repository.update(this, data); }`);
132
- repositories.push(` /** 删除当前实例数据 */`);
133
- repositories.push(` delete() { return ${className}.repository.delete(this); }`);
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
+ }
134
140
  }
135
141
  repositories.push('}');
136
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,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.8.1",
4
+ "version": "1.9.0",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "homepage": "https://zenorm.node.ltd",