@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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.9.1] - 2023-8-9
4
+ - 默认选项: staticMethods=true
5
+
6
+ ## [1.9.0] - 2023-8-9
7
+ - 增加配置选项: staticMethods instanceMethods
8
+
3
9
  ## [1.8.1] - 2023-5-19
4
10
  - fix: 使用 global 继承时构造函数缺少 super 调用
5
11
 
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
- 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);`));
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
- 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); }`);
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.8.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",