@zenorm/generate 2.0.2 → 2.1.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/dist/generate.js CHANGED
@@ -28,6 +28,8 @@ export async function generate(tables, cfg) {
28
28
  config.globalFilename ? `import _Global from './${config.globalFilename}.js';` : null,
29
29
  '',
30
30
  ];
31
+ const typesOut = {};
32
+ const tablesOut = [];
31
33
  const models = [];
32
34
  const filterRegExp = config.filter ? new RegExp(config.filter) : null;
33
35
  const includeRegExp = config.include ? new RegExp(config.include) : null;
@@ -42,6 +44,8 @@ export async function generate(tables, cfg) {
42
44
  const name = snakeCase(tableName);
43
45
  const outputFilename = path.join(outputDir, name + '.ts');
44
46
  console.log('table:', tableName);
47
+ // 自定义类型
48
+ Object.assign(typesOut, t.types);
45
49
  let pk;
46
50
  let pkType = 'never';
47
51
  const props = [];
@@ -61,18 +65,18 @@ export async function generate(tables, cfg) {
61
65
  props.push(` ${c.name}${c.required ? '!' : '?'}: ${c.type};`);
62
66
  columns.push(c.name);
63
67
  }
64
- structs.push(`export class ${className}Table${config.globalFilename ? ' extends _Global' : ''} {`);
65
- structs.push(` static columns = ${JSON.stringify(columns)};`);
66
- structs.push(...props);
67
- structs.push('');
68
- structs.push(` constructor(data?: object) {`);
68
+ tablesOut.push(`export class ${className}Table${config.globalFilename ? ' extends _Global' : ''} {`);
69
+ tablesOut.push(` static columns = ${JSON.stringify(columns)};`);
70
+ tablesOut.push(...props);
71
+ tablesOut.push('');
72
+ tablesOut.push(` constructor(data?: object) {`);
69
73
  if (config.globalFilename) {
70
- structs.push(` super();`);
74
+ tablesOut.push(` super();`);
71
75
  }
72
- structs.push(` data && Object.assign(this, data);`);
73
- structs.push(` }`);
74
- structs.push('}');
75
- structs.push('');
76
+ tablesOut.push(` data && Object.assign(this, data);`);
77
+ tablesOut.push(` }`);
78
+ tablesOut.push('}');
79
+ tablesOut.push('');
76
80
  // model class
77
81
  await notExistsPut(outputFilename, () => {
78
82
  return [
@@ -91,6 +95,11 @@ export async function generate(tables, cfg) {
91
95
  });
92
96
  models.push({ name, className, pkType });
93
97
  }
98
+ // 排序输出自定义类型到文件头部
99
+ for (const tname of Object.keys(typesOut).sort()) {
100
+ structs.push(`type ${tname} = ${typesOut[tname]}`);
101
+ }
102
+ structs.push(...tablesOut);
94
103
  const tablesFilename = path.join(outputDir, config.tablesFilename + '.ts');
95
104
  console.log(`write tables file: ${tablesFilename}`);
96
105
  await fs.writeFile(tablesFilename, structs.filter(i => i !== null).join('\n'));
package/dist/types.d.ts CHANGED
@@ -35,6 +35,10 @@ export interface TabelDescribe {
35
35
  * 列描述信息
36
36
  */
37
37
  columns: ColumnDescribe[];
38
+ /**
39
+ * 类型定义
40
+ */
41
+ types?: Record<string, string>;
38
42
  }
39
43
  /**
40
44
  * 生成配置
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@zenorm/generate",
3
3
  "type": "module",
4
4
  "description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
5
- "version": "2.0.2",
5
+ "version": "2.1.0",
6
6
  "exports": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "homepage": "https://zenorm.node.ltd",