@zenorm/generate 1.10.0 → 2.0.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 +11 -10
- package/dist/generate.d.ts +1 -1
- package/dist/generate.js +27 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -17
- package/dist/types.js +1 -2
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +10 -18
- package/package.json +9 -8
- package/CHANGELOG.md +0 -52
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const generate_1 = require("../generate");
|
|
6
|
-
function getConfig(filename) {
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { generate } from '../generate.js';
|
|
4
|
+
async function getConfig(filename) {
|
|
7
5
|
const configFile = path.join(process.cwd(), filename);
|
|
8
|
-
const config =
|
|
6
|
+
const config = (await import(configFile)).default;
|
|
9
7
|
return Object.assign({
|
|
10
8
|
backend: '@zenorm/generate-mysql',
|
|
11
9
|
}, config);
|
|
12
10
|
}
|
|
13
11
|
async function main(configFilename) {
|
|
14
12
|
const config = await getConfig(configFilename);
|
|
15
|
-
const call =
|
|
16
|
-
await
|
|
13
|
+
const call = (await import(config.backend)).default;
|
|
14
|
+
await generate(call()(config), config);
|
|
17
15
|
}
|
|
18
16
|
if (!process.argv[2]) {
|
|
19
|
-
console.log('zenorm-generate config.
|
|
17
|
+
console.log('zenorm-generate config.js');
|
|
20
18
|
process.exit(1);
|
|
21
19
|
}
|
|
22
20
|
else {
|
|
23
|
-
main(process.argv[2]).then(() =>
|
|
21
|
+
await main(process.argv[2]).then(() => {
|
|
22
|
+
console.log('Done.');
|
|
23
|
+
process.exit();
|
|
24
|
+
}, e => {
|
|
24
25
|
console.error(e);
|
|
25
26
|
process.exit(1);
|
|
26
27
|
});
|
package/dist/generate.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { GenerateConfig, TabelDescribe } from './types';
|
|
1
|
+
import { GenerateConfig, TabelDescribe } from './types.js';
|
|
2
2
|
export declare function generate(tables: AsyncGenerator<TabelDescribe>, cfg?: GenerateConfig): Promise<void>;
|
package/dist/generate.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const pascal_case_1 = require("pascal-case");
|
|
7
|
-
const snake_case_1 = require("snake-case");
|
|
8
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pascalCase } from 'pascal-case';
|
|
4
|
+
import { snakeCase } from 'snake-case';
|
|
5
|
+
import { checkFileDir, currentDatetime, cwdPath, notExistsPut } from './utils.js';
|
|
9
6
|
const zenormName = process.env.ZENORM_NAME || 'zenorm';
|
|
10
|
-
async function generate(tables, cfg) {
|
|
7
|
+
export async function generate(tables, cfg) {
|
|
11
8
|
console.log('generate models...');
|
|
12
9
|
const config = Object.assign({
|
|
13
10
|
outputDir: './src/model',
|
|
@@ -16,13 +13,13 @@ async function generate(tables, cfg) {
|
|
|
16
13
|
staticMethods: true,
|
|
17
14
|
instanceMethods: true,
|
|
18
15
|
}, cfg);
|
|
19
|
-
const outputDir =
|
|
20
|
-
await
|
|
16
|
+
const outputDir = cwdPath(config.outputDir);
|
|
17
|
+
await checkFileDir(outputDir);
|
|
21
18
|
console.log('database:', config.database);
|
|
22
19
|
const remark = [
|
|
23
20
|
'// zenorm 自动生成文件',
|
|
24
21
|
'// 请不要修改此文件,因为此文件在每次重新生成数据库结构时会被覆盖',
|
|
25
|
-
`// create at: ${
|
|
22
|
+
`// create at: ${currentDatetime()}`,
|
|
26
23
|
`// create by: ${process.env.USER || process.env.USERNAME || '-'}@${process.env.COMPUTERNAME || '-'}`,
|
|
27
24
|
`// database: ${config.database}`,
|
|
28
25
|
];
|
|
@@ -41,8 +38,8 @@ async function generate(tables, cfg) {
|
|
|
41
38
|
// console.log('table:', tableName, 'ignore');
|
|
42
39
|
continue;
|
|
43
40
|
}
|
|
44
|
-
const className =
|
|
45
|
-
const name =
|
|
41
|
+
const className = pascalCase(tableName);
|
|
42
|
+
const name = snakeCase(tableName);
|
|
46
43
|
const outputFilename = path.join(outputDir, name + '.ts');
|
|
47
44
|
console.log('table:', tableName);
|
|
48
45
|
let pk;
|
|
@@ -77,10 +74,10 @@ async function generate(tables, cfg) {
|
|
|
77
74
|
structs.push('}');
|
|
78
75
|
structs.push('');
|
|
79
76
|
// model class
|
|
80
|
-
await
|
|
77
|
+
await notExistsPut(outputFilename, () => {
|
|
81
78
|
return [
|
|
82
79
|
`import { model } from '${zenormName}';`,
|
|
83
|
-
`import { ${className}Table } from './${config.tablesFilename}';`,
|
|
80
|
+
`import { ${className}Table } from './${config.tablesFilename}.js';`,
|
|
84
81
|
'',
|
|
85
82
|
`@model({`,
|
|
86
83
|
pk ? ` pk: '${pk}',` : null,
|
|
@@ -96,12 +93,16 @@ async function generate(tables, cfg) {
|
|
|
96
93
|
}
|
|
97
94
|
const tablesFilename = path.join(outputDir, config.tablesFilename + '.ts');
|
|
98
95
|
console.log(`write tables file: ${tablesFilename}`);
|
|
99
|
-
await
|
|
96
|
+
await fs.writeFile(tablesFilename, structs.filter(i => i !== null).join('\n'));
|
|
97
|
+
const imports = ['createRepositoryQuery'];
|
|
98
|
+
if (config.generateRepositories || config.bindQuery) {
|
|
99
|
+
imports.push('QueryParam');
|
|
100
|
+
}
|
|
100
101
|
const repositories = [
|
|
101
102
|
...remark,
|
|
102
|
-
`import { ${(
|
|
103
|
-
`import * as _tables from '
|
|
104
|
-
...models.map(({ name, className }) => `import _${className} from './${name}';`),
|
|
103
|
+
`import { ${imports.join(', ')} } from '${zenormName}';`,
|
|
104
|
+
`import * as _tables from './${config.tablesFilename}.js';`,
|
|
105
|
+
...models.map(({ name, className }) => `import _${className} from './${name}.js';`),
|
|
105
106
|
];
|
|
106
107
|
// 绑定静态 Query
|
|
107
108
|
if (config.bindQuery) {
|
|
@@ -135,7 +136,7 @@ async function generate(tables, cfg) {
|
|
|
135
136
|
repositories.push(` /** 保存当前实例数据 */`);
|
|
136
137
|
repositories.push(` save() { return ${className}.repository.save(this); }`);
|
|
137
138
|
repositories.push(` /** 更新当前实例数据 */`);
|
|
138
|
-
repositories.push(` update(data:
|
|
139
|
+
repositories.push(` update(data: Partial<${className}>) { return ${className}.repository.update(this, data); }`);
|
|
139
140
|
repositories.push(` /** 删除当前实例数据 */`);
|
|
140
141
|
repositories.push(` delete() { return ${className}.repository.delete(this); }`);
|
|
141
142
|
}
|
|
@@ -160,23 +161,22 @@ async function generate(tables, cfg) {
|
|
|
160
161
|
}
|
|
161
162
|
const repositoriesFilename = path.join(outputDir, config.repositoriesFilename + '.ts');
|
|
162
163
|
console.log(`write repositories file: ${repositoriesFilename}`);
|
|
163
|
-
await
|
|
164
|
+
await fs.writeFile(repositoriesFilename, repositories.join('\n'));
|
|
164
165
|
// 生成 global.ts
|
|
165
166
|
if (config.globalFilename) {
|
|
166
167
|
const globalFilename = path.join(outputDir, config.globalFilename + '.ts');
|
|
167
|
-
await
|
|
168
|
+
await notExistsPut(globalFilename, () => {
|
|
168
169
|
console.log(`write file: ${globalFilename}`);
|
|
169
170
|
return 'export default class Global {}';
|
|
170
171
|
});
|
|
171
172
|
}
|
|
172
173
|
// 生成 index.ts
|
|
173
174
|
const indexFilename = path.join(outputDir, 'index.ts');
|
|
174
|
-
await
|
|
175
|
+
await notExistsPut(indexFilename, () => {
|
|
175
176
|
console.log(`write file: ${indexFilename}`);
|
|
176
177
|
return [
|
|
177
|
-
`export * from './${config.tablesFilename}';`,
|
|
178
|
-
`export * from './${config.repositoriesFilename}';`,
|
|
178
|
+
`export * from './${config.tablesFilename}.js';`,
|
|
179
|
+
`export * from './${config.repositoriesFilename}.js';`,
|
|
179
180
|
].join('\n');
|
|
180
181
|
});
|
|
181
182
|
}
|
|
182
|
-
exports.generate = generate;
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './types';
|
|
1
|
+
export * from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types"), exports);
|
|
1
|
+
export * from './types.js';
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare function cwdPath(p: string): string;
|
|
2
|
-
export declare function checkFileDir(dir: string): Promise<
|
|
2
|
+
export declare function checkFileDir(dir: string): Promise<string | undefined>;
|
|
3
3
|
export declare function fileExists(f: string): Promise<boolean>;
|
|
4
4
|
/**
|
|
5
5
|
* 文件不存在则创建并写入内容
|
package/dist/utils.js
CHANGED
|
@@ -1,37 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const fs_1 = require("fs");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
function cwdPath(p) {
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
export function cwdPath(p) {
|
|
7
4
|
if (p.startsWith('./')) {
|
|
8
5
|
return path.join(process.cwd(), p.slice(2));
|
|
9
6
|
}
|
|
10
7
|
return p;
|
|
11
8
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return fs_1.promises.mkdir(dir, { recursive: true });
|
|
9
|
+
export function checkFileDir(dir) {
|
|
10
|
+
return fs.mkdir(dir, { recursive: true });
|
|
15
11
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return fs_1.promises.access(f).then(() => true, () => false);
|
|
12
|
+
export function fileExists(f) {
|
|
13
|
+
return fs.access(f).then(() => true, () => false);
|
|
19
14
|
}
|
|
20
|
-
exports.fileExists = fileExists;
|
|
21
15
|
/**
|
|
22
16
|
* 文件不存在则创建并写入内容
|
|
23
17
|
* @param filename
|
|
24
18
|
* @param getContent
|
|
25
19
|
*/
|
|
26
|
-
async function notExistsPut(filename, getContent) {
|
|
20
|
+
export async function notExistsPut(filename, getContent) {
|
|
27
21
|
if (!await fileExists(filename)) {
|
|
28
|
-
await
|
|
22
|
+
await fs.writeFile(filename, await getContent());
|
|
29
23
|
return true;
|
|
30
24
|
}
|
|
31
25
|
return false;
|
|
32
26
|
}
|
|
33
|
-
|
|
34
|
-
function currentDatetime() {
|
|
27
|
+
export function currentDatetime() {
|
|
35
28
|
return new Date().toLocaleString();
|
|
36
29
|
}
|
|
37
|
-
exports.currentDatetime = currentDatetime;
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenorm/generate",
|
|
3
|
+
"type": "module",
|
|
3
4
|
"description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
|
|
4
|
-
"version": "
|
|
5
|
+
"version": "2.0.0",
|
|
5
6
|
"exports": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
7
8
|
"homepage": "https://zenorm.node.ltd",
|
|
@@ -15,9 +16,9 @@
|
|
|
15
16
|
],
|
|
16
17
|
"scripts": {
|
|
17
18
|
"build": "rimraf dist && tsc",
|
|
18
|
-
"
|
|
19
|
-
"gen": "cd test && ts-node ../src/bin/zenorm-generate.ts config.
|
|
20
|
-
"t1": "cd test && cross-env DEBUG=* ts-node t1"
|
|
19
|
+
"prepack": "npm run build",
|
|
20
|
+
"gen": "cd test && node --loader ts-node/esm ../src/bin/zenorm-generate.ts config.js",
|
|
21
|
+
"t1": "cd test && cross-env DEBUG=* node --loader ts-node/esm t1.ts"
|
|
21
22
|
},
|
|
22
23
|
"keywords": [
|
|
23
24
|
"mysql",
|
|
@@ -28,14 +29,14 @@
|
|
|
28
29
|
"database"
|
|
29
30
|
],
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"@types/node": "^
|
|
32
|
-
"@zenorm/generate-mysql": "^
|
|
32
|
+
"@types/node": "^16",
|
|
33
|
+
"@zenorm/generate-mysql": "^2.0.0",
|
|
33
34
|
"cross-env": "^7.0.3",
|
|
34
35
|
"mysql-easy-query": "^3.13.0",
|
|
35
36
|
"rimraf": "^4.4.1",
|
|
36
37
|
"ts-node": "^10.9.1",
|
|
37
|
-
"typescript": "^5.
|
|
38
|
-
"zenorm": "^
|
|
38
|
+
"typescript": "^5.6.3",
|
|
39
|
+
"zenorm": "^4.0.0"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
42
|
"pascal-case": "^3.1.2",
|
package/CHANGELOG.md
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [1.10.0] - 2023-8-29
|
|
4
|
-
- getOrCreate
|
|
5
|
-
|
|
6
|
-
## [1.9.1] - 2023-8-9
|
|
7
|
-
- 默认选项: staticMethods=true
|
|
8
|
-
|
|
9
|
-
## [1.9.0] - 2023-8-9
|
|
10
|
-
- 增加配置选项: staticMethods instanceMethods
|
|
11
|
-
|
|
12
|
-
## [1.8.1] - 2023-5-19
|
|
13
|
-
- fix: 使用 global 继承时构造函数缺少 super 调用
|
|
14
|
-
|
|
15
|
-
## [1.8.0] - 2023-5-18
|
|
16
|
-
- 撤销 [1.6.0] 的修改,动态 as 功能无法满足需求
|
|
17
|
-
|
|
18
|
-
## [1.7.0] - 2023-5-18
|
|
19
|
-
- 构造器增加 pkval 参数
|
|
20
|
-
|
|
21
|
-
## [1.6.0] - 2023-5-18
|
|
22
|
-
- 使用构造器提前初始化对象的属性,用于优化 v8 对象缓存机制
|
|
23
|
-
|
|
24
|
-
## [1.5.0] - 2023-4-19
|
|
25
|
-
- 更新: update 方法使用 UpdateRow<M>
|
|
26
|
-
|
|
27
|
-
## [1.4.0] - 2023-3-29
|
|
28
|
-
- createRepositoryQuery 添加表描述
|
|
29
|
-
- 如果没有主键 pkType = never
|
|
30
|
-
|
|
31
|
-
## [1.3.1] - 2023-3-27
|
|
32
|
-
- fix: windows 换行符问题
|
|
33
|
-
|
|
34
|
-
## [1.3.0] - 2023-3-25
|
|
35
|
-
- update: bindQuery 增加 save, update, delete
|
|
36
|
-
|
|
37
|
-
## [1.2.0] - 2023-3-24
|
|
38
|
-
- update: bindQuery 改为函数形式
|
|
39
|
-
|
|
40
|
-
## [1.1.1] - 2023-3-24
|
|
41
|
-
- fix: 生成格式少个空格和空行
|
|
42
|
-
- update: bindQuery 增加 count 和 exists
|
|
43
|
-
- 补充注释说明
|
|
44
|
-
|
|
45
|
-
## [1.1.0] - 2023-3-24
|
|
46
|
-
- 新增: 绑定静态 Query
|
|
47
|
-
|
|
48
|
-
## [1.0.0] - 2023-3-23
|
|
49
|
-
- 完善
|
|
50
|
-
|
|
51
|
-
## [0.0.1] - 2023-3-23
|
|
52
|
-
- 从 zenorm@3.25.0 分离为独立项目
|