@hlw-midway/core 1.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/README.md +4 -0
- package/dist/bin/check.d.ts +4 -0
- package/dist/bin/check.js +41 -0
- package/dist/bin/entity.d.ts +8 -0
- package/dist/bin/entity.js +50 -0
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +49 -0
- package/dist/bin/obfuscate.d.ts +1 -0
- package/dist/bin/obfuscate.js +69 -0
- package/dist/cache/store.d.ts +18 -0
- package/dist/cache/store.js +38 -0
- package/dist/config/config.default.d.ts +8 -0
- package/dist/config/config.default.js +22 -0
- package/dist/configuration.d.ts +17 -0
- package/dist/configuration.js +106 -0
- package/dist/constant/global.d.ts +59 -0
- package/dist/constant/global.js +85 -0
- package/dist/controller/base.d.ts +107 -0
- package/dist/controller/base.js +236 -0
- package/dist/decorator/cache.d.ts +2 -0
- package/dist/decorator/cache.js +10 -0
- package/dist/decorator/controller.d.ts +71 -0
- package/dist/decorator/controller.js +169 -0
- package/dist/decorator/event.d.ts +21 -0
- package/dist/decorator/event.js +39 -0
- package/dist/decorator/index.d.ts +22 -0
- package/dist/decorator/index.js +120 -0
- package/dist/decorator/tag.d.ts +22 -0
- package/dist/decorator/tag.js +40 -0
- package/dist/decorator/transaction.d.ts +8 -0
- package/dist/decorator/transaction.js +10 -0
- package/dist/entity/base.d.ts +10 -0
- package/dist/entity/base.js +41 -0
- package/dist/entity/mongo.d.ts +10 -0
- package/dist/entity/mongo.js +35 -0
- package/dist/entity/typeorm.d.ts +3 -0
- package/dist/entity/typeorm.js +7 -0
- package/dist/event/index.d.ts +53 -0
- package/dist/event/index.js +188 -0
- package/dist/exception/base.d.ts +8 -0
- package/dist/exception/base.js +15 -0
- package/dist/exception/comm.d.ts +7 -0
- package/dist/exception/comm.js +15 -0
- package/dist/exception/core.d.ts +7 -0
- package/dist/exception/core.js +15 -0
- package/dist/exception/filter.d.ts +11 -0
- package/dist/exception/filter.js +42 -0
- package/dist/exception/validate.d.ts +7 -0
- package/dist/exception/validate.js +15 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +60 -0
- package/dist/interface.d.ts +432 -0
- package/dist/interface.js +24 -0
- package/dist/module/config.d.ts +21 -0
- package/dist/module/config.js +109 -0
- package/dist/module/import.d.ts +64 -0
- package/dist/module/import.js +244 -0
- package/dist/module/menu.d.ts +54 -0
- package/dist/module/menu.js +178 -0
- package/dist/rest/eps.d.ts +51 -0
- package/dist/rest/eps.js +269 -0
- package/dist/service/base.d.ts +167 -0
- package/dist/service/base.js +281 -0
- package/dist/service/mysql.d.ts +143 -0
- package/dist/service/mysql.js +524 -0
- package/dist/service/postgres.d.ts +160 -0
- package/dist/service/postgres.js +639 -0
- package/dist/service/sqlite.d.ts +142 -0
- package/dist/service/sqlite.js +560 -0
- package/dist/tag/data.d.ts +25 -0
- package/dist/tag/data.js +85 -0
- package/dist/util/func.d.ts +8 -0
- package/dist/util/func.js +44 -0
- package/dist/util/location.d.ts +26 -0
- package/dist/util/location.js +113 -0
- package/index.d.ts +10 -0
- package/package.json +68 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HlwModuleConfig = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const fs = require("fs");
|
|
15
|
+
const core_2 = require("../exception/core");
|
|
16
|
+
const _ = require("lodash");
|
|
17
|
+
const location_1 = require("../util/location");
|
|
18
|
+
/**
|
|
19
|
+
* 模块配置
|
|
20
|
+
*/
|
|
21
|
+
let HlwModuleConfig = class HlwModuleConfig {
|
|
22
|
+
async init() {
|
|
23
|
+
const modules = [];
|
|
24
|
+
// 模块路径
|
|
25
|
+
const moduleBasePath = `${location_1.default.getRunPath()}/modules/`;
|
|
26
|
+
if (!fs.existsSync(moduleBasePath)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (!this.allConfig['module']) {
|
|
30
|
+
this.allConfig['module'] = {};
|
|
31
|
+
}
|
|
32
|
+
// 全局中间件
|
|
33
|
+
const globalMiddlewareArr = [];
|
|
34
|
+
for (const module of fs.readdirSync(moduleBasePath)) {
|
|
35
|
+
const modulePath = `${moduleBasePath}/${module}`;
|
|
36
|
+
const dirStats = fs.statSync(modulePath);
|
|
37
|
+
if (dirStats.isDirectory()) {
|
|
38
|
+
const configPath = fs.existsSync(`${modulePath}/config.ts`)
|
|
39
|
+
? `${modulePath}/config.ts`
|
|
40
|
+
: `${modulePath}/config.js`;
|
|
41
|
+
if (fs.existsSync(configPath)) {
|
|
42
|
+
const moduleConfig = require(configPath).default({
|
|
43
|
+
app: this.app,
|
|
44
|
+
env: this.app.getEnv(),
|
|
45
|
+
});
|
|
46
|
+
modules.push({
|
|
47
|
+
order: moduleConfig.order || 0,
|
|
48
|
+
module: module,
|
|
49
|
+
});
|
|
50
|
+
await this.moduleConfig(module, moduleConfig);
|
|
51
|
+
// 处理全局中间件
|
|
52
|
+
if (!_.isEmpty(moduleConfig.globalMiddlewares)) {
|
|
53
|
+
globalMiddlewareArr.push({
|
|
54
|
+
order: moduleConfig.order || 0,
|
|
55
|
+
data: moduleConfig.globalMiddlewares,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new core_2.HlwCoreException(`模块【${module}】缺少config.ts配置文件`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
this.modules = _.orderBy(modules, ['order'], ['desc']).map(e => {
|
|
65
|
+
return e.module;
|
|
66
|
+
});
|
|
67
|
+
await this.globalMiddlewareArr(globalMiddlewareArr);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 模块配置
|
|
71
|
+
* @param module 模块
|
|
72
|
+
* @param config 配置
|
|
73
|
+
*/
|
|
74
|
+
async moduleConfig(module, config) {
|
|
75
|
+
// 追加配置
|
|
76
|
+
this.allConfig['module'][module] = config;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 全局中间件
|
|
80
|
+
* @param middleware 中间件
|
|
81
|
+
*/
|
|
82
|
+
async globalMiddlewareArr(middlewares) {
|
|
83
|
+
middlewares = _.orderBy(middlewares, ['order'], ['desc']);
|
|
84
|
+
for (const middleware of middlewares) {
|
|
85
|
+
for (const item of middleware.data) {
|
|
86
|
+
this.app.getMiddleware().insertLast(item);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
exports.HlwModuleConfig = HlwModuleConfig;
|
|
92
|
+
__decorate([
|
|
93
|
+
(0, core_1.App)(),
|
|
94
|
+
__metadata("design:type", Object)
|
|
95
|
+
], HlwModuleConfig.prototype, "app", void 0);
|
|
96
|
+
__decorate([
|
|
97
|
+
(0, core_1.Config)(core_1.ALL),
|
|
98
|
+
__metadata("design:type", Object)
|
|
99
|
+
], HlwModuleConfig.prototype, "allConfig", void 0);
|
|
100
|
+
__decorate([
|
|
101
|
+
(0, core_1.Init)(),
|
|
102
|
+
__metadata("design:type", Function),
|
|
103
|
+
__metadata("design:paramtypes", []),
|
|
104
|
+
__metadata("design:returntype", Promise)
|
|
105
|
+
], HlwModuleConfig.prototype, "init", null);
|
|
106
|
+
exports.HlwModuleConfig = HlwModuleConfig = __decorate([
|
|
107
|
+
(0, core_1.Provide)(),
|
|
108
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
109
|
+
], HlwModuleConfig);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ILogger, IMidwayApplication } from '@midwayjs/core';
|
|
2
|
+
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
|
3
|
+
import { DataSource } from 'typeorm';
|
|
4
|
+
import { HlwEventManager } from '../event';
|
|
5
|
+
import { HlwModuleConfig } from './config';
|
|
6
|
+
import { HlwModuleMenu } from './menu';
|
|
7
|
+
/**
|
|
8
|
+
* 模块sql
|
|
9
|
+
*/
|
|
10
|
+
export declare class HlwModuleImport {
|
|
11
|
+
ormConfig: any;
|
|
12
|
+
defaultDataSource: DataSource;
|
|
13
|
+
typeORMDataSourceManager: TypeORMDataSourceManager;
|
|
14
|
+
hlwConfig: any;
|
|
15
|
+
coreLogger: ILogger;
|
|
16
|
+
hlwModuleConfig: HlwModuleConfig;
|
|
17
|
+
hlwEventManager: HlwEventManager;
|
|
18
|
+
app: IMidwayApplication;
|
|
19
|
+
hlwModuleMenu: HlwModuleMenu;
|
|
20
|
+
initJudge: 'file' | 'db';
|
|
21
|
+
/**
|
|
22
|
+
* 初始化
|
|
23
|
+
*/
|
|
24
|
+
init(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* 获取数据库元数据
|
|
27
|
+
*/
|
|
28
|
+
getDbMetadatas(): Promise<any>;
|
|
29
|
+
/**
|
|
30
|
+
* 检查数据是否存在
|
|
31
|
+
* @param module
|
|
32
|
+
* @param metadatas
|
|
33
|
+
*/
|
|
34
|
+
checkDbExist(module: string, metadatas: any): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* 检查文件是否存在
|
|
37
|
+
* @param module
|
|
38
|
+
*/
|
|
39
|
+
checkFileExist(module: string): {
|
|
40
|
+
exist: boolean;
|
|
41
|
+
lockPath: string;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* 导入数据库
|
|
45
|
+
* @param module
|
|
46
|
+
* @param lockPath 锁定导入
|
|
47
|
+
*/
|
|
48
|
+
initDataBase(module: string, metadatas: any, lockPath?: string): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* 锁定导入
|
|
51
|
+
* @param module
|
|
52
|
+
* @param metadatas
|
|
53
|
+
* @param lockPath
|
|
54
|
+
* @param time
|
|
55
|
+
*/
|
|
56
|
+
lockImportData(module: string, metadatas: any, lockPath: string, time: number): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* 导入数据
|
|
59
|
+
* @param metadatas
|
|
60
|
+
* @param datas
|
|
61
|
+
* @param tableName
|
|
62
|
+
*/
|
|
63
|
+
importData(metadatas: any[], item: any, tableName: string, parentItem?: any): Promise<void>;
|
|
64
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HlwModuleImport = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const core_2 = require("@midwayjs/core");
|
|
15
|
+
const typeorm_1 = require("@midwayjs/typeorm");
|
|
16
|
+
const fs = require("fs");
|
|
17
|
+
const _ = require("lodash");
|
|
18
|
+
const path = require("path");
|
|
19
|
+
const typeorm_2 = require("typeorm");
|
|
20
|
+
const event_1 = require("../event");
|
|
21
|
+
const config_1 = require("./config");
|
|
22
|
+
const menu_1 = require("./menu");
|
|
23
|
+
const location_1 = require("../util/location");
|
|
24
|
+
/**
|
|
25
|
+
* 模块sql
|
|
26
|
+
*/
|
|
27
|
+
let HlwModuleImport = class HlwModuleImport {
|
|
28
|
+
/**
|
|
29
|
+
* 初始化
|
|
30
|
+
*/
|
|
31
|
+
async init() {
|
|
32
|
+
this.initJudge = this.hlwConfig.initJudge;
|
|
33
|
+
if (!this.initJudge) {
|
|
34
|
+
this.initJudge = 'file';
|
|
35
|
+
}
|
|
36
|
+
// 是否需要导入
|
|
37
|
+
if (this.hlwConfig.initDB) {
|
|
38
|
+
const modules = this.hlwModuleConfig.modules;
|
|
39
|
+
if (!modules || modules.length === 0)
|
|
40
|
+
return;
|
|
41
|
+
const metadatas = await this.getDbMetadatas();
|
|
42
|
+
setTimeout(async () => {
|
|
43
|
+
for (const module of modules) {
|
|
44
|
+
if (this.initJudge == 'file') {
|
|
45
|
+
const { exist, lockPath } = this.checkFileExist(module);
|
|
46
|
+
if (!exist) {
|
|
47
|
+
await this.initDataBase(module, metadatas, lockPath);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (this.initJudge == 'db') {
|
|
51
|
+
const exist = await this.checkDbExist(module, metadatas);
|
|
52
|
+
if (!exist) {
|
|
53
|
+
await this.initDataBase(module, metadatas);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
this.hlwEventManager.emit('onDBInit', {});
|
|
58
|
+
this.hlwModuleMenu.init();
|
|
59
|
+
}, 2000);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 获取数据库元数据
|
|
64
|
+
*/
|
|
65
|
+
async getDbMetadatas() {
|
|
66
|
+
// 获得所有的实体
|
|
67
|
+
const entityMetadatas = this.defaultDataSource.entityMetadatas;
|
|
68
|
+
const metadatas = _.mapValues(_.keyBy(entityMetadatas, 'tableName'), 'target');
|
|
69
|
+
return metadatas;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 检查数据是否存在
|
|
73
|
+
* @param module
|
|
74
|
+
* @param metadatas
|
|
75
|
+
*/
|
|
76
|
+
async checkDbExist(module, metadatas) {
|
|
77
|
+
const cKey = `init_db_${module}`;
|
|
78
|
+
const repository = this.defaultDataSource.getRepository(metadatas['system_config']);
|
|
79
|
+
const data = await repository.findOneBy({ key: (0, typeorm_2.Equal)(cKey) });
|
|
80
|
+
return !!data;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 检查文件是否存在
|
|
84
|
+
* @param module
|
|
85
|
+
*/
|
|
86
|
+
checkFileExist(module) {
|
|
87
|
+
const importLockPath = path.join(`${location_1.default.getRunPath()}`, '..', 'lock', 'db');
|
|
88
|
+
if (!fs.existsSync(importLockPath)) {
|
|
89
|
+
fs.mkdirSync(importLockPath, { recursive: true });
|
|
90
|
+
}
|
|
91
|
+
const lockPath = path.join(importLockPath, module + '.db.lock');
|
|
92
|
+
return {
|
|
93
|
+
exist: fs.existsSync(lockPath),
|
|
94
|
+
lockPath,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 导入数据库
|
|
99
|
+
* @param module
|
|
100
|
+
* @param lockPath 锁定导入
|
|
101
|
+
*/
|
|
102
|
+
async initDataBase(module, metadatas, lockPath) {
|
|
103
|
+
// 计算耗时
|
|
104
|
+
const startTime = new Date().getTime();
|
|
105
|
+
// 模块路径
|
|
106
|
+
const modulePath = `${location_1.default.getRunPath()}/modules/${module}`;
|
|
107
|
+
// 数据路径
|
|
108
|
+
const dataPath = `${modulePath}/db.json`;
|
|
109
|
+
// 判断文件是否存在
|
|
110
|
+
if (fs.existsSync(dataPath)) {
|
|
111
|
+
// 读取数据
|
|
112
|
+
const data = JSON.parse(fs.readFileSync(dataPath).toString() || '{}');
|
|
113
|
+
// 导入数据
|
|
114
|
+
for (const key in data) {
|
|
115
|
+
try {
|
|
116
|
+
for (const item of data[key]) {
|
|
117
|
+
await this.importData(metadatas, item, key);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
this.coreLogger.error('\x1B[36m [hlw:core] midwayjs hlw core init ' +
|
|
122
|
+
module +
|
|
123
|
+
' database err \x1B[0m');
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const endTime = new Date().getTime();
|
|
128
|
+
await this.lockImportData(module, metadatas, lockPath, endTime - startTime);
|
|
129
|
+
this.coreLogger.info('\x1B[36m [hlw:core] midwayjs hlw core init ' +
|
|
130
|
+
module +
|
|
131
|
+
' database complete \x1B[0m');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 锁定导入
|
|
136
|
+
* @param module
|
|
137
|
+
* @param metadatas
|
|
138
|
+
* @param lockPath
|
|
139
|
+
* @param time
|
|
140
|
+
*/
|
|
141
|
+
async lockImportData(module, metadatas, lockPath, time) {
|
|
142
|
+
if (this.initJudge == 'file') {
|
|
143
|
+
fs.writeFileSync(lockPath, `time consuming:${time}ms`);
|
|
144
|
+
}
|
|
145
|
+
if (this.initJudge == 'db') {
|
|
146
|
+
const repository = this.defaultDataSource.getRepository(metadatas['system_config']);
|
|
147
|
+
if (this.ormConfig.default.type == 'postgres') {
|
|
148
|
+
await repository.save(repository.create({
|
|
149
|
+
key: `init_db_${module}`,
|
|
150
|
+
value: `time consuming:${time}ms`,
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
await repository.insert({
|
|
155
|
+
key: `init_db_${module}`,
|
|
156
|
+
value: `time consuming:${time}ms`,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 导入数据
|
|
163
|
+
* @param metadatas
|
|
164
|
+
* @param datas
|
|
165
|
+
* @param tableName
|
|
166
|
+
*/
|
|
167
|
+
async importData(metadatas, item, tableName, parentItem = null) {
|
|
168
|
+
const repository = this.defaultDataSource.getRepository(metadatas[tableName]);
|
|
169
|
+
// 处理当前项中的引用
|
|
170
|
+
if (parentItem) {
|
|
171
|
+
for (const key in item) {
|
|
172
|
+
if (typeof item[key] === 'string' && item[key].startsWith('@')) {
|
|
173
|
+
const parentKey = item[key].substring(1); // 移除"@"符号
|
|
174
|
+
if (Object.prototype.hasOwnProperty.call(parentItem, parentKey)) {
|
|
175
|
+
item[key] = parentItem[parentKey];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// 插入当前项到数据库
|
|
181
|
+
let insertedItem;
|
|
182
|
+
if (this.ormConfig.default.type == 'postgres') {
|
|
183
|
+
insertedItem = await repository.save(repository.create(item));
|
|
184
|
+
if (item.id) {
|
|
185
|
+
await repository.update(insertedItem.id, { id: item.id });
|
|
186
|
+
await this.defaultDataSource.query(`SELECT setval('${tableName}_id_seq', (SELECT MAX(id) FROM ${tableName}));`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
insertedItem = await repository.insert(item);
|
|
191
|
+
}
|
|
192
|
+
// 递归处理@childDatas
|
|
193
|
+
if (!_.isEmpty(item['@childDatas'])) {
|
|
194
|
+
const childDatas = item['@childDatas'];
|
|
195
|
+
delete item['@childDatas'];
|
|
196
|
+
for (const childKey in childDatas) {
|
|
197
|
+
for (const childItem of childDatas[childKey]) {
|
|
198
|
+
await this.importData(metadatas, childItem, childKey, item);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
exports.HlwModuleImport = HlwModuleImport;
|
|
205
|
+
__decorate([
|
|
206
|
+
(0, core_2.Config)('typeorm.dataSource'),
|
|
207
|
+
__metadata("design:type", Object)
|
|
208
|
+
], HlwModuleImport.prototype, "ormConfig", void 0);
|
|
209
|
+
__decorate([
|
|
210
|
+
(0, typeorm_1.InjectDataSource)('default'),
|
|
211
|
+
__metadata("design:type", typeorm_2.DataSource)
|
|
212
|
+
], HlwModuleImport.prototype, "defaultDataSource", void 0);
|
|
213
|
+
__decorate([
|
|
214
|
+
(0, core_2.Inject)(),
|
|
215
|
+
__metadata("design:type", typeorm_1.TypeORMDataSourceManager)
|
|
216
|
+
], HlwModuleImport.prototype, "typeORMDataSourceManager", void 0);
|
|
217
|
+
__decorate([
|
|
218
|
+
(0, core_2.Config)('hlw'),
|
|
219
|
+
__metadata("design:type", Object)
|
|
220
|
+
], HlwModuleImport.prototype, "hlwConfig", void 0);
|
|
221
|
+
__decorate([
|
|
222
|
+
(0, core_2.Logger)(),
|
|
223
|
+
__metadata("design:type", Object)
|
|
224
|
+
], HlwModuleImport.prototype, "coreLogger", void 0);
|
|
225
|
+
__decorate([
|
|
226
|
+
(0, core_2.Inject)(),
|
|
227
|
+
__metadata("design:type", config_1.HlwModuleConfig)
|
|
228
|
+
], HlwModuleImport.prototype, "hlwModuleConfig", void 0);
|
|
229
|
+
__decorate([
|
|
230
|
+
(0, core_2.Inject)(),
|
|
231
|
+
__metadata("design:type", event_1.HlwEventManager)
|
|
232
|
+
], HlwModuleImport.prototype, "hlwEventManager", void 0);
|
|
233
|
+
__decorate([
|
|
234
|
+
(0, core_2.App)(),
|
|
235
|
+
__metadata("design:type", Object)
|
|
236
|
+
], HlwModuleImport.prototype, "app", void 0);
|
|
237
|
+
__decorate([
|
|
238
|
+
(0, core_2.Inject)(),
|
|
239
|
+
__metadata("design:type", menu_1.HlwModuleMenu)
|
|
240
|
+
], HlwModuleImport.prototype, "hlwModuleMenu", void 0);
|
|
241
|
+
exports.HlwModuleImport = HlwModuleImport = __decorate([
|
|
242
|
+
(0, core_2.Provide)(),
|
|
243
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
244
|
+
], HlwModuleImport);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ILogger, IMidwayApplication } from '@midwayjs/core';
|
|
2
|
+
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
|
3
|
+
import { DataSource } from 'typeorm';
|
|
4
|
+
import { HlwEventManager } from '../event';
|
|
5
|
+
import { HlwConfig } from '../interface';
|
|
6
|
+
import { HlwModuleConfig } from './config';
|
|
7
|
+
/**
|
|
8
|
+
* 菜单
|
|
9
|
+
*/
|
|
10
|
+
export declare class HlwModuleMenu {
|
|
11
|
+
hlwModuleConfig: HlwModuleConfig;
|
|
12
|
+
hlwConfig: HlwConfig;
|
|
13
|
+
app: IMidwayApplication;
|
|
14
|
+
coreLogger: ILogger;
|
|
15
|
+
hlwEventManager: HlwEventManager;
|
|
16
|
+
initJudge: 'file' | 'db';
|
|
17
|
+
ormConfig: any;
|
|
18
|
+
defaultDataSource: DataSource;
|
|
19
|
+
typeORMDataSourceManager: TypeORMDataSourceManager;
|
|
20
|
+
datas: {};
|
|
21
|
+
init(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* 导入菜单
|
|
24
|
+
* @param module
|
|
25
|
+
* @param lockPath
|
|
26
|
+
*/
|
|
27
|
+
importMenu(module: string, metadatas: any, lockPath?: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* 获取数据库元数据
|
|
30
|
+
*/
|
|
31
|
+
getDbMetadatas(): Promise<any>;
|
|
32
|
+
/**
|
|
33
|
+
* 检查数据是否存在
|
|
34
|
+
* @param module
|
|
35
|
+
* @param metadatas
|
|
36
|
+
*/
|
|
37
|
+
checkDbExist(module: string, metadatas: any): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* 检查文件是否存在
|
|
40
|
+
* @param module
|
|
41
|
+
*/
|
|
42
|
+
checkFileExist(module: string): {
|
|
43
|
+
exist: boolean;
|
|
44
|
+
lockPath: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 锁定导入
|
|
48
|
+
* @param module
|
|
49
|
+
* @param metadatas
|
|
50
|
+
* @param lockPath
|
|
51
|
+
* @param time
|
|
52
|
+
*/
|
|
53
|
+
lockImportData(module: string, metadatas: any, lockPath: string): Promise<void>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HlwModuleMenu = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const typeorm_1 = require("@midwayjs/typeorm");
|
|
15
|
+
const fs = require("fs");
|
|
16
|
+
const _ = require("lodash");
|
|
17
|
+
const path = require("path");
|
|
18
|
+
const typeorm_2 = require("typeorm");
|
|
19
|
+
const event_1 = require("../event");
|
|
20
|
+
const config_1 = require("./config");
|
|
21
|
+
const location_1 = require("../util/location");
|
|
22
|
+
/**
|
|
23
|
+
* 菜单
|
|
24
|
+
*/
|
|
25
|
+
let HlwModuleMenu = class HlwModuleMenu {
|
|
26
|
+
constructor() {
|
|
27
|
+
this.datas = {};
|
|
28
|
+
}
|
|
29
|
+
async init() {
|
|
30
|
+
this.initJudge = this.hlwConfig.initJudge;
|
|
31
|
+
if (!this.initJudge) {
|
|
32
|
+
this.initJudge = 'file';
|
|
33
|
+
}
|
|
34
|
+
// 是否需要导入
|
|
35
|
+
if (this.hlwConfig.initMenu) {
|
|
36
|
+
const modules = this.hlwModuleConfig.modules;
|
|
37
|
+
const metadatas = await this.getDbMetadatas();
|
|
38
|
+
for (const module of modules) {
|
|
39
|
+
if (this.initJudge == 'file') {
|
|
40
|
+
const { exist, lockPath } = this.checkFileExist(module);
|
|
41
|
+
if (!exist) {
|
|
42
|
+
await this.importMenu(module, metadatas, lockPath);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (this.initJudge == 'db') {
|
|
46
|
+
const exist = await this.checkDbExist(module, metadatas);
|
|
47
|
+
if (!exist) {
|
|
48
|
+
await this.importMenu(module, metadatas);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this.hlwEventManager.emit('onMenuImport', this.datas);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 导入菜单
|
|
57
|
+
* @param module
|
|
58
|
+
* @param lockPath
|
|
59
|
+
*/
|
|
60
|
+
async importMenu(module, metadatas, lockPath) {
|
|
61
|
+
// 模块路径
|
|
62
|
+
const modulePath = `${location_1.default.getRunPath()}/modules/${module}`;
|
|
63
|
+
// json 路径
|
|
64
|
+
const menuPath = `${modulePath}/menu.json`;
|
|
65
|
+
// 导入
|
|
66
|
+
if (fs.existsSync(menuPath)) {
|
|
67
|
+
const data = fs.readFileSync(menuPath);
|
|
68
|
+
try {
|
|
69
|
+
// this.hlwEventManager.emit("onMenuImport", module, JSON.parse(data.toString()));
|
|
70
|
+
this.datas[module] = JSON.parse(data.toString());
|
|
71
|
+
await this.lockImportData(module, metadatas, lockPath);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
this.coreLogger.error(error);
|
|
75
|
+
this.coreLogger.error(`自动初始化模块[${module}]菜单失败,请检查对应的数据结构是否正确`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 获取数据库元数据
|
|
81
|
+
*/
|
|
82
|
+
async getDbMetadatas() {
|
|
83
|
+
// 获得所有的实体
|
|
84
|
+
const entityMetadatas = this.defaultDataSource.entityMetadatas;
|
|
85
|
+
const metadatas = _.mapValues(_.keyBy(entityMetadatas, 'tableName'), 'target');
|
|
86
|
+
return metadatas;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 检查数据是否存在
|
|
90
|
+
* @param module
|
|
91
|
+
* @param metadatas
|
|
92
|
+
*/
|
|
93
|
+
async checkDbExist(module, metadatas) {
|
|
94
|
+
const cKey = `init_menu_${module}`;
|
|
95
|
+
const repository = this.defaultDataSource.getRepository(metadatas['system_config']);
|
|
96
|
+
const data = await repository.findOneBy({ key: (0, typeorm_2.Equal)(cKey) });
|
|
97
|
+
return !!data;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 检查文件是否存在
|
|
101
|
+
* @param module
|
|
102
|
+
*/
|
|
103
|
+
checkFileExist(module) {
|
|
104
|
+
const importLockPath = path.join(`${location_1.default.getRunPath()}`, '..', 'lock', 'menu');
|
|
105
|
+
if (!fs.existsSync(importLockPath)) {
|
|
106
|
+
fs.mkdirSync(importLockPath, { recursive: true });
|
|
107
|
+
}
|
|
108
|
+
const lockPath = path.join(importLockPath, module + '.menu.lock');
|
|
109
|
+
return {
|
|
110
|
+
exist: fs.existsSync(lockPath),
|
|
111
|
+
lockPath,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 锁定导入
|
|
116
|
+
* @param module
|
|
117
|
+
* @param metadatas
|
|
118
|
+
* @param lockPath
|
|
119
|
+
* @param time
|
|
120
|
+
*/
|
|
121
|
+
async lockImportData(module, metadatas, lockPath) {
|
|
122
|
+
if (this.initJudge == 'file') {
|
|
123
|
+
fs.writeFileSync(lockPath, 'success');
|
|
124
|
+
}
|
|
125
|
+
if (this.initJudge == 'db') {
|
|
126
|
+
const repository = this.defaultDataSource.getRepository(metadatas['system_config']);
|
|
127
|
+
if (this.ormConfig.default.type == 'postgres') {
|
|
128
|
+
await repository.save(repository.create({
|
|
129
|
+
key: `init_menu_${module}`,
|
|
130
|
+
value: 'success',
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
await repository.insert({
|
|
135
|
+
key: `init_menu_${module}`,
|
|
136
|
+
value: 'success',
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
exports.HlwModuleMenu = HlwModuleMenu;
|
|
143
|
+
__decorate([
|
|
144
|
+
(0, core_1.Inject)(),
|
|
145
|
+
__metadata("design:type", config_1.HlwModuleConfig)
|
|
146
|
+
], HlwModuleMenu.prototype, "hlwModuleConfig", void 0);
|
|
147
|
+
__decorate([
|
|
148
|
+
(0, core_1.Config)('hlw'),
|
|
149
|
+
__metadata("design:type", Object)
|
|
150
|
+
], HlwModuleMenu.prototype, "hlwConfig", void 0);
|
|
151
|
+
__decorate([
|
|
152
|
+
(0, core_1.App)(),
|
|
153
|
+
__metadata("design:type", Object)
|
|
154
|
+
], HlwModuleMenu.prototype, "app", void 0);
|
|
155
|
+
__decorate([
|
|
156
|
+
(0, core_1.Logger)(),
|
|
157
|
+
__metadata("design:type", Object)
|
|
158
|
+
], HlwModuleMenu.prototype, "coreLogger", void 0);
|
|
159
|
+
__decorate([
|
|
160
|
+
(0, core_1.Inject)(),
|
|
161
|
+
__metadata("design:type", event_1.HlwEventManager)
|
|
162
|
+
], HlwModuleMenu.prototype, "hlwEventManager", void 0);
|
|
163
|
+
__decorate([
|
|
164
|
+
(0, core_1.Config)('typeorm.dataSource'),
|
|
165
|
+
__metadata("design:type", Object)
|
|
166
|
+
], HlwModuleMenu.prototype, "ormConfig", void 0);
|
|
167
|
+
__decorate([
|
|
168
|
+
(0, typeorm_1.InjectDataSource)('default'),
|
|
169
|
+
__metadata("design:type", typeorm_2.DataSource)
|
|
170
|
+
], HlwModuleMenu.prototype, "defaultDataSource", void 0);
|
|
171
|
+
__decorate([
|
|
172
|
+
(0, core_1.Inject)(),
|
|
173
|
+
__metadata("design:type", typeorm_1.TypeORMDataSourceManager)
|
|
174
|
+
], HlwModuleMenu.prototype, "typeORMDataSourceManager", void 0);
|
|
175
|
+
exports.HlwModuleMenu = HlwModuleMenu = __decorate([
|
|
176
|
+
(0, core_1.Provide)(),
|
|
177
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
178
|
+
], HlwModuleMenu);
|