mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-11 05:42:49 +00:00
修改判断数据是否初始化方式
This commit is contained in:
parent
b4210b3097
commit
783cab77df
@ -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",
|
||||
|
||||
@ -9,6 +9,8 @@ export default {
|
||||
initDB: false,
|
||||
// 是否自动导入模块菜单
|
||||
initMenu: true,
|
||||
// 判断是否初始化的方式
|
||||
initJudge: "file",
|
||||
// crud配置
|
||||
crud: {
|
||||
// 软删除
|
||||
|
||||
@ -28,6 +28,8 @@ export interface CoolConfig {
|
||||
eps?: boolean;
|
||||
/** 是否自动导入模块菜单 */
|
||||
initMenu?: boolean;
|
||||
/** 判断是否初始化的方式 */
|
||||
initJudge: "file" | "db";
|
||||
// 实体配置
|
||||
// entity?: {
|
||||
// primaryType: "uuid" | "increment" | "rowid" | "identity";
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cool-midway/core",
|
||||
"version": "7.1.19",
|
||||
"version": "7.1.20",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"typings": "index.d.ts",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user