Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4cb0d7cdad | ||
|
|
8795337a11 | ||
|
|
c0b667de46 | ||
|
|
60724a923e |
@@ -3,21 +3,21 @@ import _ from "lodash";
|
||||
import LogHelper from "./LogHelper.mjs";
|
||||
|
||||
class DataLogHelper {
|
||||
name = "";
|
||||
type = "";
|
||||
id = null;
|
||||
name: string = "";
|
||||
type: string = "";
|
||||
id: any = null;
|
||||
|
||||
constructor({
|
||||
name,
|
||||
type
|
||||
}) {
|
||||
}: any) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
async addDataLog({
|
||||
data
|
||||
}) {
|
||||
}: any) {
|
||||
const {
|
||||
name,
|
||||
type
|
||||
@@ -61,10 +61,10 @@ export default class CURDHelper {
|
||||
db,
|
||||
name,
|
||||
params
|
||||
}) {
|
||||
}: any) {
|
||||
await db.read();
|
||||
/******************************参数过滤**********************************/
|
||||
const data = db.data[name].filter((item) => {
|
||||
const data = db.data[name].filter((item: any) => {
|
||||
let isMatch = true;
|
||||
for (let field in params) {
|
||||
if (["page", "pageSize"].includes(field)) {
|
||||
@@ -101,7 +101,7 @@ export default class CURDHelper {
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
data: batchData.sort((a, b) => {
|
||||
data: batchData.sort((a:any, b:any) => {
|
||||
return a.sort - b.sort;
|
||||
}) || [],
|
||||
total: data.length
|
||||
@@ -116,14 +116,14 @@ export default class CURDHelper {
|
||||
db,
|
||||
name,
|
||||
params
|
||||
}) {
|
||||
}:any) {
|
||||
await db.read();
|
||||
const data = db.data[name];
|
||||
const query = params;
|
||||
|
||||
let detail = {};
|
||||
if (query.id && data) {
|
||||
detail = data.find((item) => {
|
||||
detail = data.find((item:any) => {
|
||||
return item.id == query.id;
|
||||
});
|
||||
}
|
||||
@@ -142,7 +142,7 @@ export default class CURDHelper {
|
||||
db,
|
||||
name,
|
||||
data
|
||||
}) {
|
||||
}:any) {
|
||||
await db.read();
|
||||
|
||||
// You can also use this syntax if you prefer
|
||||
@@ -154,7 +154,7 @@ export default class CURDHelper {
|
||||
const id = data.id;
|
||||
|
||||
if (id && dataList) {
|
||||
const currentDetail = dataList.find(item => item.id == id);
|
||||
const currentDetail = dataList.find((item:any) => item.id == id);
|
||||
if (currentDetail) {
|
||||
_.merge(currentDetail, data);
|
||||
}
|
||||
@@ -187,7 +187,7 @@ export default class CURDHelper {
|
||||
db,
|
||||
name,
|
||||
data
|
||||
}) {
|
||||
}:any) {
|
||||
await db.read();
|
||||
|
||||
if (!db.data[name])
|
||||
@@ -228,7 +228,7 @@ export default class CURDHelper {
|
||||
db,
|
||||
name,
|
||||
data
|
||||
}) {
|
||||
}: any) {
|
||||
await db.read();
|
||||
|
||||
// You can also use this syntax if you prefer
|
||||
@@ -239,12 +239,12 @@ export default class CURDHelper {
|
||||
const ids = data.ids;
|
||||
|
||||
if (ids.length > 0 && dataList) {
|
||||
let id = null;
|
||||
let id: any = null;
|
||||
const delDataList = [];
|
||||
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
id = ids[i];
|
||||
const idx = dataList.findIndex(item => item.id == id);
|
||||
const idx = dataList.findIndex((item: any) => item.id == id);
|
||||
if (idx != -1) {
|
||||
delDataList.push(dataList[idx]);
|
||||
dataList.splice(idx, 1);
|
||||
@@ -5,7 +5,7 @@ import fs from "fs";
|
||||
* 配置信息帮助类
|
||||
*/
|
||||
export default class ConfigHelper {
|
||||
static config = null;
|
||||
static config: any = null;
|
||||
static initConfig() {
|
||||
this.config = yaml.parse(fs.readFileSync("./config.yml").toString());
|
||||
}
|
||||
@@ -6,10 +6,10 @@ import {
|
||||
} from "./dbServer.mjs";
|
||||
|
||||
export default class DBHelper {
|
||||
static baseDB = null;
|
||||
static resourceDB = null;
|
||||
static backupDB = null;
|
||||
static logDB = null;
|
||||
static baseDB: any = null;
|
||||
static resourceDB: any = null;
|
||||
static backupDB: any = null;
|
||||
static logDB: any = null;
|
||||
|
||||
static async init() {
|
||||
this.baseDB = await getDb();
|
||||
@@ -6,7 +6,7 @@ export default class LogHelper {
|
||||
tableName,
|
||||
type,
|
||||
data
|
||||
}) {
|
||||
}:any) {
|
||||
const db = await DBHelper.getLogDb();
|
||||
return CURDHelper.create({
|
||||
db,
|
||||
@@ -25,7 +25,7 @@ export default class LogHelper {
|
||||
|
||||
static async modifyDataLogStatus({
|
||||
id
|
||||
}) {
|
||||
}:any) {
|
||||
const db = await DBHelper.getLogDb();
|
||||
const {
|
||||
data,
|
||||
@@ -1,14 +1,14 @@
|
||||
import CURDHelper from "./CURDHelper.mjs";
|
||||
|
||||
export default class RouterFactor {
|
||||
name = "";
|
||||
db = null;
|
||||
router = null;
|
||||
name: string = "";
|
||||
db: any = null;
|
||||
router: any = null;
|
||||
constructor({
|
||||
router: _router,
|
||||
db: _db,
|
||||
name: _name
|
||||
}) {
|
||||
}: any) {
|
||||
this.router = _router;
|
||||
this.db = _db;
|
||||
this.name = _name;
|
||||
@@ -24,7 +24,7 @@ export default class RouterFactor {
|
||||
router
|
||||
} = this;
|
||||
|
||||
router.post(`/api/${name}/list`, async (ctx) => {
|
||||
router.post(`/api/${name}/list`, async (ctx: any) => {
|
||||
// 设置头类型, 如果不设置,会直接下载该页面
|
||||
ctx.type = 'application/json';
|
||||
const body = ctx.request.body;
|
||||
@@ -47,7 +47,7 @@ export default class RouterFactor {
|
||||
db,
|
||||
router
|
||||
} = this;
|
||||
router.get(`/api/${name}`, async (ctx) => {
|
||||
router.get(`/api/${name}`, async (ctx:any) => {
|
||||
// 设置头类型, 如果不设置,会直接下载该页面
|
||||
ctx.type = 'application/json';
|
||||
const query = ctx.request.query;
|
||||
@@ -70,7 +70,7 @@ export default class RouterFactor {
|
||||
db,
|
||||
router
|
||||
} = this;
|
||||
router.post(`/api/${name}/upd`, async (ctx) => {
|
||||
router.post(`/api/${name}/upd`, async (ctx:any) => {
|
||||
// 设置头类型, 如果不设置,会直接下载该页面
|
||||
ctx.type = 'application/json';
|
||||
const body = ctx.request.body;
|
||||
@@ -93,7 +93,7 @@ export default class RouterFactor {
|
||||
db,
|
||||
router
|
||||
} = this;
|
||||
router.post(`/api/${name}/add`, async (ctx) => {
|
||||
router.post(`/api/${name}/add`, async (ctx:any) => {
|
||||
// 设置头类型, 如果不设置,会直接下载该页面
|
||||
ctx.type = 'application/json';
|
||||
const body = ctx.request.body;
|
||||
@@ -116,7 +116,7 @@ export default class RouterFactor {
|
||||
db,
|
||||
router
|
||||
} = this;
|
||||
router.post(`/api/${name}/delete`, async (ctx) => {
|
||||
router.post(`/api/${name}/delete`, async (ctx:any) => {
|
||||
// 设置头类型, 如果不设置,会直接下载该页面
|
||||
ctx.type = 'application/json';
|
||||
const body = ctx.request.body;
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
import path from 'path';
|
||||
import {
|
||||
dirname
|
||||
} from 'path';
|
||||
import {
|
||||
fileURLToPath
|
||||
} from 'url';
|
||||
import Koa from 'koa';
|
||||
import getRouter from 'koa-router';
|
||||
import koaBody from 'koa-body';
|
||||
import fs from "fs";
|
||||
// 静态文件服务
|
||||
import koaStatic from 'koa-static'
|
||||
// 虚拟路径
|
||||
import koaMount from 'koa-mount';
|
||||
// import { ConfigHelper, DBHelper } from "funjia-server/lib/funjia-server.mjs";
|
||||
import ConfigHelper from './ConfigHelper.mjs';
|
||||
import DBHelper from './DBHelper.mjs';
|
||||
|
||||
ConfigHelper.initConfig();
|
||||
|
||||
(async () => await DBHelper.init())();
|
||||
|
||||
|
||||
export default CreateServer
|
||||
|
||||
/**
|
||||
* 创建服务
|
||||
* port: 服务端口号
|
||||
*/
|
||||
async function CreateServer(port = 4873) {
|
||||
const __dirname = dirname(fileURLToPath(
|
||||
import.meta.url));
|
||||
const rootDir = process.cwd();
|
||||
|
||||
// console.log(__dirname, rootDir);
|
||||
|
||||
const router = new getRouter();
|
||||
const app = new Koa();
|
||||
app.use(koaBody());
|
||||
const staticPath = './static'
|
||||
app.use(
|
||||
koaMount('/resource/static', koaStatic(
|
||||
path.join(rootDir, staticPath), {}
|
||||
))
|
||||
)
|
||||
|
||||
// app.use(koaStatic('.'));
|
||||
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
|
||||
// 初始化路由
|
||||
const initRouter = async (folderPath: string) => {
|
||||
// 读取文件夹中的文件列表
|
||||
fs.readdir(folderPath, async (err, files) => {
|
||||
if (err) {
|
||||
console.error('Error reading directory:', err);
|
||||
return;
|
||||
}
|
||||
|
||||
// 筛选出.js 文件
|
||||
// const jsFiles = files.filter(file => path.extname(file) === '.mjs');
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
const filePath = path.join(folderPath, file);
|
||||
const stats = await fs.statSync(filePath);
|
||||
if (stats.isDirectory()) {
|
||||
await initRouter(filePath);
|
||||
}
|
||||
else if (stats.isFile()) {
|
||||
// 构建文件的完整路径
|
||||
const filePath = path.join(folderPath, file);
|
||||
// 动态导入文件
|
||||
const module = await import("./" + path.relative(__dirname, filePath));
|
||||
// console.log(path.relative(__dirname, filePath),__dirname,filePath);
|
||||
// const module = await import("./" +filePath);
|
||||
// 在这里可以对导入的模块进行处理,例如调用其中的函数或使用其导出的对象
|
||||
module.default(router);
|
||||
// console.log(module);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error importing ${file}:`, error);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
await (async () => {
|
||||
// 目标文件夹的路径
|
||||
const folderPath = './routers';
|
||||
|
||||
await initRouter(folderPath);
|
||||
})();
|
||||
|
||||
let server = null;
|
||||
|
||||
const startServer = () => {
|
||||
server = app.listen(port, () => {
|
||||
console.log('server is listen in ', port);
|
||||
});
|
||||
|
||||
server.on('close', (err: any, socket: any) => {
|
||||
server = null;
|
||||
console.log('server is close');
|
||||
});
|
||||
}
|
||||
|
||||
startServer();
|
||||
return app;
|
||||
}
|
||||
|
||||
// const bH = new BackupHelper();
|
||||
|
||||
// 调度任务 (该模块内存占用会持续上升不建议使用,建议使用系统本身的调度任务)
|
||||
// const task = cron.schedule('* */1 * * * Sunday', () => {
|
||||
// console.log('running a task every 10 seconds');
|
||||
// task.stop();
|
||||
// server.close(async () => {
|
||||
// await bH.bakFile(ConfigHelper.config.dbBak);
|
||||
// startServer();
|
||||
// });
|
||||
// }, {
|
||||
// scheduled: false,
|
||||
// // https://timezonedb.com/download
|
||||
// timezone: "Asia/Shanghai"
|
||||
// });
|
||||
|
||||
// task.start();
|
||||
|
||||
// const delay = () => new Promise((resolve) => {
|
||||
// setTimeout(() => {
|
||||
// resolve(true);
|
||||
// }, 1000 * 10);
|
||||
// });
|
||||
|
||||
// (async () => {
|
||||
// for (let i = 0; i < 20; i++) {
|
||||
// console.log('running a task every 10 seconds');
|
||||
// await delay();
|
||||
// await bH.bakFile(ConfigHelper.config.dbBak);
|
||||
// }
|
||||
// })();
|
||||
@@ -0,0 +1,22 @@
|
||||
db: # 数据库配置
|
||||
db: &db C:\workspace\anki_admin\QuestionMakingSystem\server\db\db.json
|
||||
resourceDB: &resourceDB C:\workspace\anki_admin\QuestionMakingSystem\server\db\resourceDB.json
|
||||
backupDB: &backupDB C:\workspace\anki_admin\QuestionMakingSystem\server\db\backupDB.json
|
||||
logDB: &logDB C:\workspace\anki_admin\QuestionMakingSystem\server\db\logDB.json
|
||||
dbBak: # 数据库备份配置
|
||||
- #
|
||||
original: *db
|
||||
dest: F:/sourceCode/2022/compute-cms/bak/db
|
||||
category: db
|
||||
- #
|
||||
original: *resourceDB
|
||||
dest: F:/sourceCode/2022/compute-cms/bak/resourceDB
|
||||
category: resourceDB
|
||||
- #
|
||||
original: *backupDB
|
||||
dest: F:/sourceCode/2022/compute-cms/bak/backupDB
|
||||
category: backupDB
|
||||
- #
|
||||
original: *logDB
|
||||
dest: F:/sourceCode/2022/compute-cms/bak/logDB
|
||||
category: logDB
|
||||
@@ -15,7 +15,7 @@ const __dirname = dirname(fileURLToPath(
|
||||
import.meta.url));
|
||||
|
||||
|
||||
const getDbPath = (dbName, defPath) => {
|
||||
const getDbPath = (dbName:string, defPath:string) => {
|
||||
let file = "";
|
||||
if (ConfigHelper.config && ConfigHelper.config.db && ConfigHelper.config.db[dbName]) {
|
||||
file = ConfigHelper.config.path + ConfigHelper.config.db[dbName];
|
||||
@@ -1,27 +0,0 @@
|
||||
// 备份
|
||||
// export * from "./backupHelper.mjs";
|
||||
// export { default as BackupHelper } from "./backupHelper.mjs";
|
||||
|
||||
// 配置
|
||||
export * from "./ConfigHelper.mjs";
|
||||
export { default as ConfigHelper } from "./ConfigHelper.mjs";
|
||||
|
||||
// 增删改查抽象方法
|
||||
export * from "./CURDHelper.mjs";
|
||||
export { default as CURDHelper } from "./CURDHelper.mjs";
|
||||
|
||||
// 数据库服务
|
||||
export * from "./dbServer.mjs";
|
||||
export { default as getDb } from "./dbServer.mjs";
|
||||
|
||||
// 日志服务
|
||||
export * from "./LogHelper.mjs";
|
||||
export { default as LogHelper } from "./LogHelper.mjs";
|
||||
|
||||
// 路由工厂类
|
||||
export * from "./RouterFactory.mjs";
|
||||
export { default as RouterFactory } from "./RouterFactory.mjs";
|
||||
|
||||
// 数据操作工具类
|
||||
export * from "./DBHelper.mjs";
|
||||
export { default as DBHelper } from "./DBHelper.mjs";
|
||||
@@ -0,0 +1,30 @@
|
||||
// 备份
|
||||
// export * from "./backupHelper.mjs";
|
||||
// export { default as BackupHelper } from "./backupHelper.mjs";
|
||||
|
||||
// 配置
|
||||
export * from "./ConfigHelper.mts";
|
||||
export { default as ConfigHelper } from "./ConfigHelper.mts";
|
||||
|
||||
// 增删改查抽象方法
|
||||
export * from "./CURDHelper.mts";
|
||||
export { default as CURDHelper } from "./CURDHelper.mts";
|
||||
|
||||
// 数据库服务
|
||||
export * from "./dbServer.mts";
|
||||
export { default as getDb } from "./dbServer.mts";
|
||||
|
||||
// 日志服务
|
||||
export * from "./LogHelper.mts";
|
||||
export { default as LogHelper } from "./LogHelper.mts";
|
||||
|
||||
// 路由工厂类
|
||||
export * from "./RouterFactory.mts";
|
||||
export { default as RouterFactor } from "./RouterFactory.mts";
|
||||
|
||||
// 数据操作工具类
|
||||
export * from "./DBHelper.mts";
|
||||
export { default as DBHelper } from "./DBHelper.mts";
|
||||
|
||||
// 服务启动类
|
||||
export { default as CreateServer } from "./Server.mts";
|
||||
@@ -0,0 +1,43 @@
|
||||
export default class CURDHelper {
|
||||
/**
|
||||
* 读取列表数据
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
static retrieveList({ db, name, params }: any): Promise<{
|
||||
code: number;
|
||||
data: any;
|
||||
total: any;
|
||||
}>;
|
||||
/**
|
||||
* 读取单条数据
|
||||
* @param {*} param0
|
||||
*/
|
||||
static retrieveSingle({ db, name, params }: any): Promise<{
|
||||
code: number;
|
||||
data: {};
|
||||
}>;
|
||||
/**
|
||||
* 修改数据
|
||||
* @param {*} param0
|
||||
*/
|
||||
static update({ db, name, data }: any): Promise<{
|
||||
code: number;
|
||||
}>;
|
||||
/**
|
||||
* 创建数据
|
||||
* @param {*} param0
|
||||
*/
|
||||
static create({ db, name, data }: any): Promise<{
|
||||
code: number;
|
||||
data: any;
|
||||
}>;
|
||||
/**
|
||||
* 删除数据
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
static delete({ db, name, data }: any): Promise<{
|
||||
code: number;
|
||||
}>;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 配置信息帮助类
|
||||
*/
|
||||
export default class ConfigHelper {
|
||||
static config: any;
|
||||
static initConfig(): void;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export default class DBHelper {
|
||||
static baseDB: any;
|
||||
static resourceDB: any;
|
||||
static backupDB: any;
|
||||
static logDB: any;
|
||||
static init(): Promise<void>;
|
||||
static getBackupDb(): any;
|
||||
static getLogDb(): any;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export default class LogHelper {
|
||||
static addDataLog({ tableName, type, data }: any): Promise<{
|
||||
code: number;
|
||||
data: any;
|
||||
}>;
|
||||
static modifyDataLogStatus({ id }: any): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
export default class RouterFactor {
|
||||
name: string;
|
||||
db: any;
|
||||
router: any;
|
||||
constructor({ router: _router, db: _db, name: _name }: any);
|
||||
/**
|
||||
* 创建列表数据路由
|
||||
*/
|
||||
createGetList(): void;
|
||||
/**
|
||||
* 创建详情路由
|
||||
*/
|
||||
createGetDetail(): void;
|
||||
/**
|
||||
* 修改数据
|
||||
*/
|
||||
createUpdate(): void;
|
||||
/**
|
||||
* 新增数据
|
||||
*/
|
||||
createAdd(): void;
|
||||
/**
|
||||
* 删除数据
|
||||
*/
|
||||
createDelete(): void;
|
||||
creaetBaseRouter(): void;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { default as Koa } from 'koa';
|
||||
export default CreateServer;
|
||||
/**
|
||||
* 创建服务
|
||||
* port: 服务端口号
|
||||
*/
|
||||
declare function CreateServer(port?: number): Promise<Koa<Koa.DefaultState, Koa.DefaultContext>>;
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Low } from 'lowdb';
|
||||
/**
|
||||
* 基础数据库
|
||||
* @returns
|
||||
*/
|
||||
export declare const getDb: () => Promise<Low<unknown>>;
|
||||
/**
|
||||
* 文件资源库
|
||||
* @returns
|
||||
*/
|
||||
export declare const getResourceDb: () => Promise<Low<unknown>>;
|
||||
/**
|
||||
* 备份记录存储库
|
||||
* @returns
|
||||
*/
|
||||
export declare const getBackupDb: () => Promise<Low<unknown>>;
|
||||
/**
|
||||
* 日志存储库
|
||||
* @returns
|
||||
*/
|
||||
export declare const getLogDb: () => Promise<Low<unknown>>;
|
||||
export default getDb;
|
||||
+119
-77
@@ -1,44 +1,46 @@
|
||||
var S = Object.defineProperty;
|
||||
var j = (n, t, a) => t in n ? S(n, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : n[t] = a;
|
||||
var d = (n, t, a) => j(n, typeof t != "symbol" ? t + "" : t, a);
|
||||
import k from "yaml";
|
||||
import q from "fs";
|
||||
import R from "cuid";
|
||||
import v from "lodash";
|
||||
import { dirname as $, join as f } from "path";
|
||||
import { JSONFile as w, Low as D } from "lowdb";
|
||||
import { fileURLToPath as F } from "url";
|
||||
class g {
|
||||
import G from "yaml";
|
||||
import k from "fs";
|
||||
import U from "cuid";
|
||||
import A from "lodash";
|
||||
import w, { dirname as P, join as h } from "path";
|
||||
import { JSONFile as m, Low as D } from "lowdb";
|
||||
import { fileURLToPath as $ } from "url";
|
||||
import M from "koa";
|
||||
import T from "koa-router";
|
||||
import z from "koa-body";
|
||||
import C from "koa-static";
|
||||
import E from "koa-mount";
|
||||
const v = class v {
|
||||
static initConfig() {
|
||||
this.config = k.parse(q.readFileSync("./config.yml").toString());
|
||||
this.config = G.parse(k.readFileSync("./config.yml").toString());
|
||||
}
|
||||
}
|
||||
d(g, "config", null);
|
||||
const b = $(F(
|
||||
};
|
||||
v.config = null;
|
||||
let u = v;
|
||||
const b = P($(
|
||||
import.meta.url
|
||||
)), L = (n, t) => {
|
||||
let a = "";
|
||||
return g.config && g.config.db && g.config.db[n] ? a = g.config.path + g.config.db[n] : a = t, a;
|
||||
}, G = async () => {
|
||||
let n = L("db", f(b, "db.json"));
|
||||
const t = new w(n), a = new D(t);
|
||||
return u.config && u.config.db && u.config.db[n] ? a = u.config.path + u.config.db[n] : a = t, a;
|
||||
}, I = async () => {
|
||||
let n = L("db", h(b, "db.json"));
|
||||
const t = new m(n), a = new D(t);
|
||||
return await a.read(), a;
|
||||
}, U = async () => {
|
||||
let n = L("resourceDB", f(b, "resourceDB.json"));
|
||||
const t = new w(n), a = new D(t);
|
||||
}, O = async () => {
|
||||
let n = L("resourceDB", h(b, "resourceDB.json"));
|
||||
const t = new m(n), a = new D(t);
|
||||
return await a.read(), a;
|
||||
}, _ = async () => {
|
||||
let n = L("backupDB", f(b, "backupDB.json"));
|
||||
const t = new w(n), a = new D(t);
|
||||
}, J = async () => {
|
||||
let n = L("backupDB", h(b, "backupDB.json"));
|
||||
const t = new m(n), a = new D(t);
|
||||
return await a.read(), a;
|
||||
}, A = async () => {
|
||||
let n = L("logDB", f(b, "logDB.json"));
|
||||
const t = new w(n), a = new D(t);
|
||||
}, K = async () => {
|
||||
let n = L("logDB", h(b, "logDB.json"));
|
||||
const t = new m(n), a = new D(t);
|
||||
return await a.read(), a;
|
||||
};
|
||||
class y {
|
||||
}, f = class f {
|
||||
static async init() {
|
||||
this.baseDB = await G(), this.resourceDB = await U(), this.backupDB = await _(), this.logDB = await A();
|
||||
this.baseDB = await I(), this.resourceDB = await O(), this.backupDB = await J(), this.logDB = await K();
|
||||
}
|
||||
static getBackupDb() {
|
||||
return this.backupDB;
|
||||
@@ -46,9 +48,10 @@ class y {
|
||||
static getLogDb() {
|
||||
return this.logDB;
|
||||
}
|
||||
}
|
||||
d(y, "baseDB", null), d(y, "resourceDB", null), d(y, "backupDB", null), d(y, "logDB", null);
|
||||
class m {
|
||||
};
|
||||
f.baseDB = null, f.resourceDB = null, f.backupDB = null, f.logDB = null;
|
||||
let y = f;
|
||||
class R {
|
||||
static async addDataLog({
|
||||
tableName: t,
|
||||
type: a,
|
||||
@@ -93,15 +96,12 @@ class m {
|
||||
});
|
||||
}
|
||||
}
|
||||
class h {
|
||||
class j {
|
||||
constructor({
|
||||
name: t,
|
||||
type: a
|
||||
}) {
|
||||
d(this, "name", "");
|
||||
d(this, "type", "");
|
||||
d(this, "id", null);
|
||||
this.name = t, this.type = a;
|
||||
this.name = "", this.type = "", this.id = null, this.name = t, this.type = a;
|
||||
}
|
||||
async addDataLog({
|
||||
data: t
|
||||
@@ -113,7 +113,7 @@ class h {
|
||||
if (a != "dataLog") {
|
||||
const {
|
||||
data: e
|
||||
} = await m.addDataLog({
|
||||
} = await R.addDataLog({
|
||||
tableName: a,
|
||||
type: i,
|
||||
data: t
|
||||
@@ -126,7 +126,7 @@ class h {
|
||||
name: t,
|
||||
id: a
|
||||
} = this;
|
||||
t != "dataLog" && a && await m.modifyDataLogStatus({
|
||||
t != "dataLog" && a && await R.modifyDataLogStatus({
|
||||
id: a
|
||||
});
|
||||
}
|
||||
@@ -143,20 +143,20 @@ class p {
|
||||
params: i
|
||||
}) {
|
||||
await t.read();
|
||||
const e = t.data[a].filter((c) => {
|
||||
let l = !0;
|
||||
const e = t.data[a].filter((l) => {
|
||||
let c = !0;
|
||||
for (let r in i)
|
||||
if (!["page", "pageSize"].includes(r) && (typeof c[r] == "string" ? l = c[r].indexOf(i[r]) > -1 : typeof c[r] == "number" ? l = c[r] == i[r] : c[r] instanceof Array && (l = c[r].includes(i[r])), !l))
|
||||
if (!["page", "pageSize"].includes(r) && (typeof l[r] == "string" ? c = l[r].indexOf(i[r]) > -1 : typeof l[r] == "number" ? c = l[r] == i[r] : l[r] instanceof Array && (c = l[r].includes(i[r])), !c))
|
||||
break;
|
||||
return l;
|
||||
return c;
|
||||
}), {
|
||||
page: s,
|
||||
pageSize: o
|
||||
} = i;
|
||||
let u = e;
|
||||
return s && o && (u = e.slice((s - 1) * o, s * o)), {
|
||||
let d = e;
|
||||
return s && o && (d = e.slice((s - 1) * o, s * o)), {
|
||||
code: 0,
|
||||
data: u.sort((c, l) => c.sort - l.sort) || [],
|
||||
data: d.sort((l, c) => l.sort - c.sort) || [],
|
||||
total: e.length
|
||||
};
|
||||
}
|
||||
@@ -172,7 +172,7 @@ class p {
|
||||
await t.read();
|
||||
const e = t.data[a], s = i;
|
||||
let o = {};
|
||||
return s.id && e && (o = e.find((u) => u.id == s.id)), {
|
||||
return s.id && e && (o = e.find((d) => d.id == s.id)), {
|
||||
code: 0,
|
||||
data: o || {}
|
||||
};
|
||||
@@ -189,15 +189,15 @@ class p {
|
||||
await t.read(), t.data[a] || (t.data[a] = []);
|
||||
const e = t.data[a], s = i.id;
|
||||
if (s && e) {
|
||||
const o = e.find((c) => c.id == s);
|
||||
o && v.merge(o, i);
|
||||
const u = new h({
|
||||
const o = e.find((l) => l.id == s);
|
||||
o && A.merge(o, i);
|
||||
const d = new j({
|
||||
name: a,
|
||||
type: "upd"
|
||||
});
|
||||
await u.addDataLog({
|
||||
await d.addDataLog({
|
||||
data: o
|
||||
}), await t.write(), await u.modifyDataLogStatus();
|
||||
}), await t.write(), await d.modifyDataLogStatus();
|
||||
}
|
||||
return {
|
||||
code: 0
|
||||
@@ -214,8 +214,8 @@ class p {
|
||||
}) {
|
||||
await t.read(), t.data[a] || (t.data[a] = []);
|
||||
const e = t.data[a];
|
||||
i.id = R(), i.createTime = /* @__PURE__ */ new Date(), e.push(i);
|
||||
const s = new h({
|
||||
i.id = U(), i.createTime = /* @__PURE__ */ new Date(), e.push(i);
|
||||
const s = new j({
|
||||
name: a,
|
||||
type: "add"
|
||||
});
|
||||
@@ -240,35 +240,32 @@ class p {
|
||||
const e = t.data[a], s = i.ids;
|
||||
if (s.length > 0 && e) {
|
||||
let o = null;
|
||||
const u = [];
|
||||
for (let l = 0; l < s.length; l++) {
|
||||
o = s[l];
|
||||
const r = e.findIndex((B) => B.id == o);
|
||||
r != -1 && (u.push(e[r]), e.splice(r, 1));
|
||||
const d = [];
|
||||
for (let c = 0; c < s.length; c++) {
|
||||
o = s[c];
|
||||
const r = e.findIndex((S) => S.id == o);
|
||||
r != -1 && (d.push(e[r]), e.splice(r, 1));
|
||||
}
|
||||
const c = new h({
|
||||
const l = new j({
|
||||
name: a,
|
||||
type: "del"
|
||||
});
|
||||
await c.addDataLog({
|
||||
data: u
|
||||
}), await t.write(), await c.modifyDataLogStatus();
|
||||
await l.addDataLog({
|
||||
data: d
|
||||
}), await t.write(), await l.modifyDataLogStatus();
|
||||
}
|
||||
return {
|
||||
code: 0
|
||||
};
|
||||
}
|
||||
}
|
||||
class M {
|
||||
class it {
|
||||
constructor({
|
||||
router: t,
|
||||
db: a,
|
||||
name: i
|
||||
}) {
|
||||
d(this, "name", "");
|
||||
d(this, "db", null);
|
||||
d(this, "router", null);
|
||||
this.router = t, this.db = a, this.name = i;
|
||||
this.name = "", this.db = null, this.router = null, this.router = t, this.db = a, this.name = i;
|
||||
}
|
||||
/**
|
||||
* 创建列表数据路由
|
||||
@@ -369,14 +366,59 @@ class M {
|
||||
this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete();
|
||||
}
|
||||
}
|
||||
u.initConfig();
|
||||
(async () => await y.init())();
|
||||
async function st(n = 4873) {
|
||||
const t = P($(
|
||||
import.meta.url
|
||||
)), a = process.cwd(), i = new T(), e = new M();
|
||||
e.use(z()), e.use(
|
||||
E("/resource/static", C(
|
||||
w.join(a, "./static"),
|
||||
{}
|
||||
))
|
||||
), e.use(i.routes()), e.use(i.allowedMethods());
|
||||
const o = async (c) => {
|
||||
k.readdir(c, async (r, S) => {
|
||||
if (r) {
|
||||
console.error("Error reading directory:", r);
|
||||
return;
|
||||
}
|
||||
for (const B of S)
|
||||
try {
|
||||
const g = w.join(c, B), q = await k.statSync(g);
|
||||
if (q.isDirectory())
|
||||
await o(g);
|
||||
else if (q.isFile()) {
|
||||
const F = w.join(c, B);
|
||||
(await import("./" + w.relative(t, F))).default(i);
|
||||
}
|
||||
} catch (g) {
|
||||
console.error(`Error importing ${B}:`, g);
|
||||
}
|
||||
});
|
||||
};
|
||||
await (async () => {
|
||||
await o("./routers");
|
||||
})();
|
||||
let d = null;
|
||||
return (() => {
|
||||
d = e.listen(n, () => {
|
||||
console.log("server is listen in ", n);
|
||||
}), d.on("close", (c, r) => {
|
||||
d = null, console.log("server is close");
|
||||
});
|
||||
})(), e;
|
||||
}
|
||||
export {
|
||||
p as CURDHelper,
|
||||
g as ConfigHelper,
|
||||
u as ConfigHelper,
|
||||
st as CreateServer,
|
||||
y as DBHelper,
|
||||
m as LogHelper,
|
||||
M as RouterFactory,
|
||||
_ as getBackupDb,
|
||||
G as getDb,
|
||||
A as getLogDb,
|
||||
U as getResourceDb
|
||||
R as LogHelper,
|
||||
it as RouterFactor,
|
||||
J as getBackupDb,
|
||||
I as getDb,
|
||||
K as getLogDb,
|
||||
O as getResourceDb
|
||||
};
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
export * from './ConfigHelper.mts';
|
||||
export { default as ConfigHelper } from './ConfigHelper.mts';
|
||||
export * from './CURDHelper.mts';
|
||||
export { default as CURDHelper } from './CURDHelper.mts';
|
||||
export * from './dbServer.mts';
|
||||
export { default as getDb } from './dbServer.mts';
|
||||
export * from './LogHelper.mts';
|
||||
export { default as LogHelper } from './LogHelper.mts';
|
||||
export * from './RouterFactory.mts';
|
||||
export { default as RouterFactor } from './RouterFactory.mts';
|
||||
export * from './DBHelper.mts';
|
||||
export { default as DBHelper } from './DBHelper.mts';
|
||||
export { default as CreateServer } from './Server.mts';
|
||||
@@ -0,0 +1 @@
|
||||
export default function sleep(timeout: number): Promise<unknown>;
|
||||
+9
-2
@@ -2,9 +2,10 @@
|
||||
"name": "funjia-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"main": "lib/funjia-server.mjs",
|
||||
"types": "lib/index.d.mts",
|
||||
"scripts": {
|
||||
"build":"vite build",
|
||||
"build": "vite build",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
@@ -24,9 +25,15 @@
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"node-cron": "^3.0.1",
|
||||
"uuid": "^8.3.2",
|
||||
"vite-plugin-dts": "^4.5.0",
|
||||
"yaml": "^2.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/koa": "^2.15.0",
|
||||
"@types/koa-mount": "^4.0.5",
|
||||
"@types/koa-router": "^7.4.8",
|
||||
"@types/koa-static": "^4.0.4",
|
||||
"@types/lodash": "^4.17.14",
|
||||
"vite": "^6.0.7"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export default async function sleep(timeout) {
|
||||
export default async function sleep(timeout:number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(true);
|
||||
@@ -1,4 +1,3 @@
|
||||
// import { BackupHelper } from "./index.mjs";
|
||||
import { CreateServer,CURDHelper} from "./lib/index.mjs";
|
||||
|
||||
|
||||
import { } from "./lib/funjia-server.mjs";
|
||||
CreateServer()
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2018",
|
||||
"module": "nodenext",
|
||||
"moduleResolution": "nodenext",
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"skipLibCheck": true,
|
||||
"noEmit": true,
|
||||
"allowImportingTsExtensions": true
|
||||
},
|
||||
"include": [
|
||||
"*.mts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
declare namespace Server {
|
||||
/**
|
||||
* 创建服务
|
||||
* @param port 端口号 默认:4873
|
||||
*/
|
||||
export function CreateServer(port: number) { }
|
||||
}
|
||||
+11
-5
@@ -1,25 +1,31 @@
|
||||
import { defineConfig } from 'vite';
|
||||
import dts from 'vite-plugin-dts';
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: './index.mjs', // 你的库的入口文件,这里假设是 src/main.mjs
|
||||
entry: './index.mts', // 你的库的入口文件,这里假设是 src/main.mjs
|
||||
name: 'funjia-server', // 库的全局名称,将在浏览器环境中使用
|
||||
formats: ['es'], // 输出的模块格式,这里包含 ES 模块和 UMD 模块
|
||||
// fileName: (format) => `your-library-name.${format}.js` // 输出的文件名,根据不同的格式生成不同的文件名
|
||||
},
|
||||
rollupOptions: {
|
||||
// 确保不包含 Node.js 特定的模块,避免将 Node.js 模块错误地打包到浏览器代码中
|
||||
external: ['child_process','fs', 'path','url','node','node-cron','cuid','dayjs','koa','koa-body',
|
||||
'koa-mount','koa-router','koa-static','lodash','lowdb','multer','uuid','yaml','@koa/multer','@types/yaml'],
|
||||
external: ['child_process', 'fs', 'path', 'url', 'node', 'node-cron', 'cuid', 'dayjs', 'koa', 'koa-body',
|
||||
'koa-mount', 'koa-router', 'koa-static', 'lodash', 'lowdb', 'multer', 'uuid', 'yaml', '@koa/multer', '@types/yaml'],
|
||||
output: {
|
||||
exports: 'named',
|
||||
// 在 Node.js 环境中不需要全局变量,可将此设置为 undefined
|
||||
globals: undefined
|
||||
}
|
||||
},
|
||||
},
|
||||
// 构建为 Node.js 库时,将输出目录设置为 lib 而不是 dist,可根据需要修改
|
||||
outDir: 'lib'
|
||||
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
dts({
|
||||
insertTypesEntry: true,
|
||||
}),
|
||||
],
|
||||
});
|
||||
Reference in New Issue
Block a user