@zenorm/generate 2.0.1 → 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/bin/zenorm-generate.js +2 -1
- package/dist/generate.js +19 -10
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
3
4
|
import { generate } from '../generate.js';
|
|
4
5
|
async function getConfig(filename) {
|
|
5
|
-
const configFile = path.join(process.cwd(), filename);
|
|
6
|
+
const configFile = pathToFileURL(path.join(process.cwd(), filename)).href;
|
|
6
7
|
const config = (await import(configFile)).default;
|
|
7
8
|
return Object.assign({
|
|
8
9
|
backend: '@zenorm/generate-mysql',
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
74
|
+
tablesOut.push(` super();`);
|
|
71
75
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
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
|
|
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",
|