Files
funjia-server/lib/funjia-server.mjs
2025-01-19 13:41:23 +08:00

383 lines
7.8 KiB
JavaScript

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 {
static initConfig() {
this.config = k.parse(q.readFileSync("./config.yml").toString());
}
}
d(g, "config", null);
const b = $(F(
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 await a.read(), a;
}, U = async () => {
let n = L("resourceDB", f(b, "resourceDB.json"));
const t = new w(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);
return await a.read(), a;
}, A = async () => {
let n = L("logDB", f(b, "logDB.json"));
const t = new w(n), a = new D(t);
return await a.read(), a;
};
class y {
static async init() {
this.baseDB = await G(), this.resourceDB = await U(), this.backupDB = await _(), this.logDB = await A();
}
static getBackupDb() {
return this.backupDB;
}
static getLogDb() {
return this.logDB;
}
}
d(y, "baseDB", null), d(y, "resourceDB", null), d(y, "backupDB", null), d(y, "logDB", null);
class m {
static async addDataLog({
tableName: t,
type: a,
data: i
}) {
const e = await y.getLogDb();
return p.create({
db: e,
name: "dataLog",
data: {
dataId: i.id,
type: a,
tableName: t,
data: i,
// 待生效
status: 0,
createTime: /* @__PURE__ */ new Date()
}
});
}
static async modifyDataLogStatus({
id: t
}) {
const a = await y.getLogDb(), {
data: i,
code: e
} = await p.retrieveSingle({
db: a,
name: "dataLog",
params: {
id: t
}
});
e == 0 && await p.update({
db: a,
name: "dataLog",
data: {
...i,
// 生效
status: 1
}
});
}
}
class h {
constructor({
name: t,
type: a
}) {
d(this, "name", "");
d(this, "type", "");
d(this, "id", null);
this.name = t, this.type = a;
}
async addDataLog({
data: t
}) {
const {
name: a,
type: i
} = this;
if (a != "dataLog") {
const {
data: e
} = await m.addDataLog({
tableName: a,
type: i,
data: t
});
this.id = e.id;
}
}
async modifyDataLogStatus() {
const {
name: t,
id: a
} = this;
t != "dataLog" && a && await m.modifyDataLogStatus({
id: a
});
}
}
class p {
/**
* 读取列表数据
* @param {*} param0
* @returns
*/
static async retrieveList({
db: t,
name: a,
params: i
}) {
await t.read();
const e = t.data[a].filter((c) => {
let l = !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))
break;
return l;
}), {
page: s,
pageSize: o
} = i;
let u = e;
return s && o && (u = e.slice((s - 1) * o, s * o)), {
code: 0,
data: u.sort((c, l) => c.sort - l.sort) || [],
total: e.length
};
}
/**
* 读取单条数据
* @param {*} param0
*/
static async retrieveSingle({
db: t,
name: a,
params: i
}) {
await t.read();
const e = t.data[a], s = i;
let o = {};
return s.id && e && (o = e.find((u) => u.id == s.id)), {
code: 0,
data: o || {}
};
}
/**
* 修改数据
* @param {*} param0
*/
static async update({
db: t,
name: a,
data: i
}) {
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({
name: a,
type: "upd"
});
await u.addDataLog({
data: o
}), await t.write(), await u.modifyDataLogStatus();
}
return {
code: 0
};
}
/**
* 创建数据
* @param {*} param0
*/
static async create({
db: t,
name: a,
data: i
}) {
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({
name: a,
type: "add"
});
return await s.addDataLog({
data: i
}), await t.write(), await s.modifyDataLogStatus(), {
code: 0,
data: i
};
}
/**
* 删除数据
* @param {*} param0
* @returns
*/
static async delete({
db: t,
name: a,
data: i
}) {
await t.read(), t.data[a] || (t.data[a] = []);
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 c = new h({
name: a,
type: "del"
});
await c.addDataLog({
data: u
}), await t.write(), await c.modifyDataLogStatus();
}
return {
code: 0
};
}
}
class M {
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;
}
/**
* 创建列表数据路由
*/
createGetList() {
const {
name: t,
db: a,
router: i
} = this;
i.post(`/api/${t}/list`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.retrieveList({
db: a,
name: t,
params: s
});
e.body = o;
});
}
/**
* 创建详情路由
*/
createGetDetail() {
const {
name: t,
db: a,
router: i
} = this;
i.get(`/api/${t}`, async (e) => {
e.type = "application/json";
const s = e.request.query, o = await p.retrieveSingle({
db: a,
name: t,
params: s
});
e.body = o;
});
}
/**
* 修改数据
*/
createUpdate() {
const {
name: t,
db: a,
router: i
} = this;
i.post(`/api/${t}/upd`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.update({
db: a,
name: t,
data: s
});
e.body = o;
});
}
/**
* 新增数据
*/
createAdd() {
const {
name: t,
db: a,
router: i
} = this;
i.post(`/api/${t}/add`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.create({
db: a,
name: t,
data: s
});
e.body = o;
});
}
/**
* 删除数据
*/
createDelete() {
const {
name: t,
db: a,
router: i
} = this;
i.post(`/api/${t}/delete`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.delete({
db: a,
name: t,
data: s
});
e.body = o;
});
}
creaetBaseRouter() {
this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete();
}
}
export {
p as CURDHelper,
g as ConfigHelper,
y as DBHelper,
m as LogHelper,
M as RouterFactory,
_ as getBackupDb,
G as getDb,
A as getLogDb,
U as getResourceDb
};