修改判断数据是否初始化方式

This commit is contained in:
COOL 2024-07-19 15:52:13 +08:00
parent b4210b3097
commit 783cab77df
6 changed files with 238 additions and 47 deletions

View File

@ -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",

View File

@ -9,6 +9,8 @@ export default {
initDB: false,
// 是否自动导入模块菜单
initMenu: true,
// 判断是否初始化的方式
initJudge: "file",
// crud配置
crud: {
// 软删除

View File

@ -28,6 +28,8 @@ export interface CoolConfig {
eps?: boolean;
/** 是否自动导入模块菜单 */
initMenu?: boolean;
/** 判断是否初始化的方式 */
initJudge: "file" | "db";
// 实体配置
// entity?: {
// primaryType: "uuid" | "increment" | "rowid" | "identity";

View File

@ -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

View File

@ -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`,
});
}
}
}
}

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "7.1.19",
"version": "7.1.20",
"description": "",
"main": "index.js",
"typings": "index.d.ts",