diff --git a/core/package.json b/core/package.json index 2a037b4..629af0e 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/core", - "version": "7.1.19", + "version": "7.1.20", "description": "", "main": "dist/index.js", "typings": "index.d.ts", diff --git a/core/src/config/config.default.ts b/core/src/config/config.default.ts index 8f23754..b8f7858 100644 --- a/core/src/config/config.default.ts +++ b/core/src/config/config.default.ts @@ -9,6 +9,8 @@ export default { initDB: false, // 是否自动导入模块菜单 initMenu: true, + // 判断是否初始化的方式 + initJudge: "file", // crud配置 crud: { // 软删除 diff --git a/core/src/interface.ts b/core/src/interface.ts index 416d79b..64ab288 100644 --- a/core/src/interface.ts +++ b/core/src/interface.ts @@ -28,6 +28,8 @@ export interface CoolConfig { eps?: boolean; /** 是否自动导入模块菜单 */ initMenu?: boolean; + /** 判断是否初始化的方式 */ + initJudge: "file" | "db"; // 实体配置 // entity?: { // primaryType: "uuid" | "increment" | "rowid" | "identity"; diff --git a/core/src/module/import.ts b/core/src/module/import.ts index 1283ee6..1295aae 100644 --- a/core/src/module/import.ts +++ b/core/src/module/import.ts @@ -1,14 +1,13 @@ -import { ILogger, IMidwayApplication } from "@midwayjs/core"; +import { ILogger, IMidwayApplication, Scope, ScopeEnum } from "@midwayjs/core"; import { App, Config, Inject, Logger, Provide } from "@midwayjs/decorator"; -import * as fs from "fs"; -import { CoolModuleConfig } from "./config"; -import * as path from "path"; import { InjectDataSource, TypeORMDataSourceManager } from "@midwayjs/typeorm"; -import { DataSource } from "typeorm"; -import { CoolEventManager } from "../event"; -import { CoolModuleMenu } from "./menu"; +import * as fs from "fs"; import * as _ from "lodash"; -import { Scope, ScopeEnum } from "@midwayjs/core"; +import * as path from "path"; +import { DataSource, Equal } from "typeorm"; +import { CoolEventManager } from "../event"; +import { CoolModuleConfig } from "./config"; +import { CoolModuleMenu } from "./menu"; /** * 模块sql @@ -43,24 +42,33 @@ export class CoolModuleImport { @Inject() coolModuleMenu: CoolModuleMenu; + initJudge: "file" | "db"; + + /** + * 初始化 + */ async init() { + this.initJudge = this.coolConfig.initJudge; + if (!this.initJudge) { + this.initJudge = "file"; + } // 是否需要导入 if (this.coolConfig.initDB) { const modules = this.coolModuleConfig.modules; - const importLockPath = path.join( - `${this.app.getBaseDir()}`, - "..", - "lock", - "db" - ); - if (!fs.existsSync(importLockPath)) { - fs.mkdirSync(importLockPath, { recursive: true }); - } + const metadatas = await this.getDbMetadatas(); setTimeout(async () => { for (const module of modules) { - const lockPath = path.join(importLockPath, module + ".db.lock"); - if (!fs.existsSync(lockPath)) { - await this.initDataBase(module, lockPath); + if (this.initJudge == "file") { + const { exist, lockPath } = this.checkFileExist(module); + if (!exist) { + await this.initDataBase(module, metadatas, lockPath); + } + } + if (this.initJudge == "db") { + const exist = await this.checkDbExist(module, metadatas); + if (!exist) { + await this.initDataBase(module, metadatas); + } } } this.coolEventManager.emit("onDBInit", {}); @@ -69,12 +77,60 @@ export class CoolModuleImport { } } + /** + * 获取数据库元数据 + */ + async getDbMetadatas() { + // 获得所有的实体 + const entityMetadatas = this.defaultDataSource.entityMetadatas; + const metadatas = _.mapValues( + _.keyBy(entityMetadatas, "tableName"), + "target" + ); + return metadatas; + } + + /** + * 检查数据是否存在 + * @param module + * @param metadatas + */ + async checkDbExist(module: string, metadatas) { + const cKey = `init_db_${module}`; + const repository = this.defaultDataSource.getRepository( + metadatas["base_sys_conf"] + ); + const data = await repository.findOneBy({ cKey: Equal(cKey) }); + return !!data; + } + + /** + * 检查文件是否存在 + * @param module + */ + checkFileExist(module: string) { + const importLockPath = path.join( + `${this.app.getBaseDir()}`, + "..", + "lock", + "db" + ); + if (!fs.existsSync(importLockPath)) { + fs.mkdirSync(importLockPath, { recursive: true }); + } + const lockPath = path.join(importLockPath, module + ".db.lock"); + return { + exist: fs.existsSync(lockPath), + lockPath, + }; + } + /** * 导入数据库 * @param module * @param lockPath 锁定导入 */ - async initDataBase(module: string, lockPath: string) { + async initDataBase(module: string, metadatas, lockPath?: string) { // 计算耗时 const startTime = new Date().getTime(); // 模块路径 @@ -83,12 +139,6 @@ export class CoolModuleImport { const dataPath = `${modulePath}/db.json`; // 判断文件是否存在 if (fs.existsSync(dataPath)) { - // 获得所有的实体 - const entityMetadatas = this.defaultDataSource.entityMetadatas; - const metadatas = _.mapValues( - _.keyBy(entityMetadatas, "tableName"), - "target" - ); // 读取数据 const data = JSON.parse(fs.readFileSync(dataPath).toString() || "{}"); // 导入数据 @@ -107,7 +157,12 @@ export class CoolModuleImport { } } const endTime = new Date().getTime(); - fs.writeFileSync(lockPath, `time consuming:${endTime - startTime}ms`); + await this.lockImportData( + module, + metadatas, + lockPath, + endTime - startTime + ); this.coreLogger.info( "\x1B[36m [cool:core] midwayjs cool core init " + module + @@ -116,6 +171,42 @@ export class CoolModuleImport { } } + /** + * 锁定导入 + * @param module + * @param metadatas + * @param lockPath + * @param time + */ + async lockImportData( + module: string, + metadatas, + lockPath: string, + time: number + ) { + if (this.initJudge == "file") { + fs.writeFileSync(lockPath, `time consuming:${time}ms`); + } + if (this.initJudge == "db") { + const repository = this.defaultDataSource.getRepository( + metadatas["base_sys_conf"] + ); + if (this.ormConfig.default.type == "postgres") { + await repository.save( + repository.create({ + cKey: `init_db_${module}`, + cValue: `time consuming:${time}ms`, + }) + ); + } else { + await repository.insert({ + cKey: `init_db_${module}`, + cValue: `time consuming:${time}ms`, + }); + } + } + } + /** * 导入数据 * @param metadatas diff --git a/core/src/module/menu.ts b/core/src/module/menu.ts index 4903454..2370a54 100644 --- a/core/src/module/menu.ts +++ b/core/src/module/menu.ts @@ -9,11 +9,14 @@ import { Scope, ScopeEnum, } from "@midwayjs/core"; +import { InjectDataSource, TypeORMDataSourceManager } from "@midwayjs/typeorm"; import * as fs from "fs"; -import { CoolModuleConfig } from "./config"; +import * as _ from "lodash"; import * as path from "path"; -import { CoolConfig } from "../interface"; +import { DataSource, Equal } from "typeorm"; import { CoolEventManager } from "../event"; +import { CoolConfig } from "../interface"; +import { CoolModuleConfig } from "./config"; /** * 菜单 @@ -36,29 +39,43 @@ export class CoolModuleMenu { @Inject() coolEventManager: CoolEventManager; + initJudge: "file" | "db"; + + @Config("typeorm.dataSource") + ormConfig; + + @InjectDataSource("default") + defaultDataSource: DataSource; + + @Inject() + typeORMDataSourceManager: TypeORMDataSourceManager; + datas = {}; async init() { + this.initJudge = this.coolConfig.initJudge; + if (!this.initJudge) { + this.initJudge = "file"; + } // 是否需要导入 if (this.coolConfig.initMenu) { const modules = this.coolModuleConfig.modules; - const importLockPath = path.join( - `${this.app.getBaseDir()}`, - "..", - "lock", - "menu" - ); - if (!fs.existsSync(importLockPath)) { - fs.mkdirSync(importLockPath, { recursive: true }); - } + const metadatas = await this.getDbMetadatas(); for (const module of modules) { - const lockPath = path.join(importLockPath, module + ".menu.lock"); - if (!fs.existsSync(lockPath)) { - await this.importMenu(module, lockPath); + if (this.initJudge == "file") { + const { exist, lockPath } = this.checkFileExist(module); + if (!exist) { + await this.importMenu(module, metadatas, lockPath); + } + } + if (this.initJudge == "db") { + const exist = await this.checkDbExist(module, metadatas); + if (!exist) { + await this.importMenu(module, metadatas); + } } } this.coolEventManager.emit("onMenuImport", this.datas); - // this.coolEventManager.emit("onMenuInit", {}); } } @@ -67,7 +84,7 @@ export class CoolModuleMenu { * @param module * @param lockPath */ - async importMenu(module: string, lockPath: string) { + async importMenu(module: string, metadatas, lockPath?: string) { // 模块路径 const modulePath = `${this.app.getBaseDir()}/modules/${module}`; // json 路径 @@ -78,7 +95,7 @@ export class CoolModuleMenu { try { // this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString())); this.datas[module] = JSON.parse(data.toString()); - fs.writeFileSync(lockPath, data); + await this.lockImportData(module, metadatas, lockPath); } catch (error) { this.coreLogger.error(error); this.coreLogger.error( @@ -87,4 +104,83 @@ export class CoolModuleMenu { } } } + + /** + * 获取数据库元数据 + */ + async getDbMetadatas() { + // 获得所有的实体 + const entityMetadatas = this.defaultDataSource.entityMetadatas; + const metadatas = _.mapValues( + _.keyBy(entityMetadatas, "tableName"), + "target" + ); + return metadatas; + } + + /** + * 检查数据是否存在 + * @param module + * @param metadatas + */ + async checkDbExist(module: string, metadatas) { + const cKey = `init_menu_${module}`; + const repository = this.defaultDataSource.getRepository( + metadatas["base_sys_conf"] + ); + const data = await repository.findOneBy({ cKey: Equal(cKey) }); + return !!data; + } + + /** + * 检查文件是否存在 + * @param module + */ + checkFileExist(module: string) { + const importLockPath = path.join( + `${this.app.getBaseDir()}`, + "..", + "lock", + "menu" + ); + if (!fs.existsSync(importLockPath)) { + fs.mkdirSync(importLockPath, { recursive: true }); + } + const lockPath = path.join(importLockPath, module + ".menu.lock"); + return { + exist: fs.existsSync(lockPath), + lockPath, + }; + } + + /** + * 锁定导入 + * @param module + * @param metadatas + * @param lockPath + * @param time + */ + async lockImportData(module: string, metadatas, lockPath: string) { + if (this.initJudge == "file") { + fs.writeFileSync(lockPath, `success`); + } + if (this.initJudge == "db") { + const repository = this.defaultDataSource.getRepository( + metadatas["base_sys_conf"] + ); + if (this.ormConfig.default.type == "postgres") { + await repository.save( + repository.create({ + cKey: `init_menu_${module}`, + cValue: `success`, + }) + ); + } else { + await repository.insert({ + cKey: `init_menu_${module}`, + cValue: `success`, + }); + } + } + } } diff --git a/core/src/package.json b/core/src/package.json index 46a4cf5..7aef125 100644 --- a/core/src/package.json +++ b/core/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/core", - "version": "7.1.19", + "version": "7.1.20", "description": "", "main": "index.js", "typings": "index.d.ts",