mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-15 16:32:50 +00:00
修改判断数据是否初始化方式
This commit is contained in:
parent
b4210b3097
commit
783cab77df
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cool-midway/core",
|
"name": "@cool-midway/core",
|
||||||
"version": "7.1.19",
|
"version": "7.1.20",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "index.d.ts",
|
"typings": "index.d.ts",
|
||||||
|
|||||||
@ -9,6 +9,8 @@ export default {
|
|||||||
initDB: false,
|
initDB: false,
|
||||||
// 是否自动导入模块菜单
|
// 是否自动导入模块菜单
|
||||||
initMenu: true,
|
initMenu: true,
|
||||||
|
// 判断是否初始化的方式
|
||||||
|
initJudge: "file",
|
||||||
// crud配置
|
// crud配置
|
||||||
crud: {
|
crud: {
|
||||||
// 软删除
|
// 软删除
|
||||||
|
|||||||
@ -28,6 +28,8 @@ export interface CoolConfig {
|
|||||||
eps?: boolean;
|
eps?: boolean;
|
||||||
/** 是否自动导入模块菜单 */
|
/** 是否自动导入模块菜单 */
|
||||||
initMenu?: boolean;
|
initMenu?: boolean;
|
||||||
|
/** 判断是否初始化的方式 */
|
||||||
|
initJudge: "file" | "db";
|
||||||
// 实体配置
|
// 实体配置
|
||||||
// entity?: {
|
// entity?: {
|
||||||
// primaryType: "uuid" | "increment" | "rowid" | "identity";
|
// 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 { 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 { InjectDataSource, TypeORMDataSourceManager } from "@midwayjs/typeorm";
|
||||||
import { DataSource } from "typeorm";
|
import * as fs from "fs";
|
||||||
import { CoolEventManager } from "../event";
|
|
||||||
import { CoolModuleMenu } from "./menu";
|
|
||||||
import * as _ from "lodash";
|
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
|
* 模块sql
|
||||||
@ -43,24 +42,33 @@ export class CoolModuleImport {
|
|||||||
@Inject()
|
@Inject()
|
||||||
coolModuleMenu: CoolModuleMenu;
|
coolModuleMenu: CoolModuleMenu;
|
||||||
|
|
||||||
|
initJudge: "file" | "db";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化
|
||||||
|
*/
|
||||||
async init() {
|
async init() {
|
||||||
|
this.initJudge = this.coolConfig.initJudge;
|
||||||
|
if (!this.initJudge) {
|
||||||
|
this.initJudge = "file";
|
||||||
|
}
|
||||||
// 是否需要导入
|
// 是否需要导入
|
||||||
if (this.coolConfig.initDB) {
|
if (this.coolConfig.initDB) {
|
||||||
const modules = this.coolModuleConfig.modules;
|
const modules = this.coolModuleConfig.modules;
|
||||||
const importLockPath = path.join(
|
const metadatas = await this.getDbMetadatas();
|
||||||
`${this.app.getBaseDir()}`,
|
|
||||||
"..",
|
|
||||||
"lock",
|
|
||||||
"db"
|
|
||||||
);
|
|
||||||
if (!fs.existsSync(importLockPath)) {
|
|
||||||
fs.mkdirSync(importLockPath, { recursive: true });
|
|
||||||
}
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
for (const module of modules) {
|
for (const module of modules) {
|
||||||
const lockPath = path.join(importLockPath, module + ".db.lock");
|
if (this.initJudge == "file") {
|
||||||
if (!fs.existsSync(lockPath)) {
|
const { exist, lockPath } = this.checkFileExist(module);
|
||||||
await this.initDataBase(module, lockPath);
|
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", {});
|
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 module
|
||||||
* @param lockPath 锁定导入
|
* @param lockPath 锁定导入
|
||||||
*/
|
*/
|
||||||
async initDataBase(module: string, lockPath: string) {
|
async initDataBase(module: string, metadatas, lockPath?: string) {
|
||||||
// 计算耗时
|
// 计算耗时
|
||||||
const startTime = new Date().getTime();
|
const startTime = new Date().getTime();
|
||||||
// 模块路径
|
// 模块路径
|
||||||
@ -83,12 +139,6 @@ export class CoolModuleImport {
|
|||||||
const dataPath = `${modulePath}/db.json`;
|
const dataPath = `${modulePath}/db.json`;
|
||||||
// 判断文件是否存在
|
// 判断文件是否存在
|
||||||
if (fs.existsSync(dataPath)) {
|
if (fs.existsSync(dataPath)) {
|
||||||
// 获得所有的实体
|
|
||||||
const entityMetadatas = this.defaultDataSource.entityMetadatas;
|
|
||||||
const metadatas = _.mapValues(
|
|
||||||
_.keyBy(entityMetadatas, "tableName"),
|
|
||||||
"target"
|
|
||||||
);
|
|
||||||
// 读取数据
|
// 读取数据
|
||||||
const data = JSON.parse(fs.readFileSync(dataPath).toString() || "{}");
|
const data = JSON.parse(fs.readFileSync(dataPath).toString() || "{}");
|
||||||
// 导入数据
|
// 导入数据
|
||||||
@ -107,7 +157,12 @@ export class CoolModuleImport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const endTime = new Date().getTime();
|
const endTime = new Date().getTime();
|
||||||
fs.writeFileSync(lockPath, `time consuming:${endTime - startTime}ms`);
|
await this.lockImportData(
|
||||||
|
module,
|
||||||
|
metadatas,
|
||||||
|
lockPath,
|
||||||
|
endTime - startTime
|
||||||
|
);
|
||||||
this.coreLogger.info(
|
this.coreLogger.info(
|
||||||
"\x1B[36m [cool:core] midwayjs cool core init " +
|
"\x1B[36m [cool:core] midwayjs cool core init " +
|
||||||
module +
|
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
|
* @param metadatas
|
||||||
|
|||||||
@ -9,11 +9,14 @@ import {
|
|||||||
Scope,
|
Scope,
|
||||||
ScopeEnum,
|
ScopeEnum,
|
||||||
} from "@midwayjs/core";
|
} from "@midwayjs/core";
|
||||||
|
import { InjectDataSource, TypeORMDataSourceManager } from "@midwayjs/typeorm";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { CoolModuleConfig } from "./config";
|
import * as _ from "lodash";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { CoolConfig } from "../interface";
|
import { DataSource, Equal } from "typeorm";
|
||||||
import { CoolEventManager } from "../event";
|
import { CoolEventManager } from "../event";
|
||||||
|
import { CoolConfig } from "../interface";
|
||||||
|
import { CoolModuleConfig } from "./config";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜单
|
* 菜单
|
||||||
@ -36,29 +39,43 @@ export class CoolModuleMenu {
|
|||||||
@Inject()
|
@Inject()
|
||||||
coolEventManager: CoolEventManager;
|
coolEventManager: CoolEventManager;
|
||||||
|
|
||||||
|
initJudge: "file" | "db";
|
||||||
|
|
||||||
|
@Config("typeorm.dataSource")
|
||||||
|
ormConfig;
|
||||||
|
|
||||||
|
@InjectDataSource("default")
|
||||||
|
defaultDataSource: DataSource;
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
typeORMDataSourceManager: TypeORMDataSourceManager;
|
||||||
|
|
||||||
datas = {};
|
datas = {};
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
|
this.initJudge = this.coolConfig.initJudge;
|
||||||
|
if (!this.initJudge) {
|
||||||
|
this.initJudge = "file";
|
||||||
|
}
|
||||||
// 是否需要导入
|
// 是否需要导入
|
||||||
if (this.coolConfig.initMenu) {
|
if (this.coolConfig.initMenu) {
|
||||||
const modules = this.coolModuleConfig.modules;
|
const modules = this.coolModuleConfig.modules;
|
||||||
const importLockPath = path.join(
|
const metadatas = await this.getDbMetadatas();
|
||||||
`${this.app.getBaseDir()}`,
|
|
||||||
"..",
|
|
||||||
"lock",
|
|
||||||
"menu"
|
|
||||||
);
|
|
||||||
if (!fs.existsSync(importLockPath)) {
|
|
||||||
fs.mkdirSync(importLockPath, { recursive: true });
|
|
||||||
}
|
|
||||||
for (const module of modules) {
|
for (const module of modules) {
|
||||||
const lockPath = path.join(importLockPath, module + ".menu.lock");
|
if (this.initJudge == "file") {
|
||||||
if (!fs.existsSync(lockPath)) {
|
const { exist, lockPath } = this.checkFileExist(module);
|
||||||
await this.importMenu(module, lockPath);
|
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("onMenuImport", this.datas);
|
||||||
// this.coolEventManager.emit("onMenuInit", {});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +84,7 @@ export class CoolModuleMenu {
|
|||||||
* @param module
|
* @param module
|
||||||
* @param lockPath
|
* @param lockPath
|
||||||
*/
|
*/
|
||||||
async importMenu(module: string, lockPath: string) {
|
async importMenu(module: string, metadatas, lockPath?: string) {
|
||||||
// 模块路径
|
// 模块路径
|
||||||
const modulePath = `${this.app.getBaseDir()}/modules/${module}`;
|
const modulePath = `${this.app.getBaseDir()}/modules/${module}`;
|
||||||
// json 路径
|
// json 路径
|
||||||
@ -78,7 +95,7 @@ export class CoolModuleMenu {
|
|||||||
try {
|
try {
|
||||||
// this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString()));
|
// this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString()));
|
||||||
this.datas[module] = JSON.parse(data.toString());
|
this.datas[module] = JSON.parse(data.toString());
|
||||||
fs.writeFileSync(lockPath, data);
|
await this.lockImportData(module, metadatas, lockPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.coreLogger.error(error);
|
this.coreLogger.error(error);
|
||||||
this.coreLogger.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",
|
"name": "@cool-midway/core",
|
||||||
"version": "7.1.19",
|
"version": "7.1.20",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"typings": "index.d.ts",
|
"typings": "index.d.ts",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user