import F from "yaml"; import B from "fs"; import G from "cuid"; import U from "lodash"; import w, { dirname as R, join as h } from "path"; import { JSONFile as m, Low as D } from "lowdb"; import { fileURLToPath as $ } from "url"; import A from "koa"; import M from "koa-router"; import T from "koa-body"; import z from "koa-static"; import C from "koa-mount"; const j = class j { static initConfig() { this.config = F.parse(B.readFileSync("./config.yml").toString()); } }; j.config = null; let u = j; const b = R($( import.meta.url )), L = (n, a) => { let t = ""; return u.config && u.config.db && u.config.db[n] ? t = u.config.path + u.config.db[n] : t = a, t; }, E = async () => { let n = L("db", h(b, "db.json")); const a = new m(n), t = new D(a); return await t.read(), t; }, I = async () => { let n = L("resourceDB", h(b, "resourceDB.json")); const a = new m(n), t = new D(a); return await t.read(), t; }, O = async () => { let n = L("backupDB", h(b, "backupDB.json")); const a = new m(n), t = new D(a); return await t.read(), t; }, J = async () => { let n = L("logDB", h(b, "logDB.json")); const a = new m(n), t = new D(a); return await t.read(), t; }, y = class y { static async init() { this.baseDB = await E(), this.resourceDB = await I(), this.backupDB = await O(), this.logDB = await J(); } static getBackupDb() { return this.backupDB; } static getLogDb() { return this.logDB; } }; y.baseDB = null, y.resourceDB = null, y.backupDB = null, y.logDB = null; let g = y; class v { static async addDataLog({ tableName: a, type: t, data: i }) { const e = await g.getLogDb(); return p.create({ db: e, name: "dataLog", data: { dataId: i.id, type: t, tableName: a, data: i, // 待生效 status: 0, createTime: /* @__PURE__ */ new Date() } }); } static async modifyDataLogStatus({ id: a }) { const t = await g.getLogDb(), { data: i, code: e } = await p.retrieveSingle({ db: t, name: "dataLog", params: { id: a } }); e == 0 && await p.update({ db: t, name: "dataLog", data: { ...i, // 生效 status: 1 } }); } } class S { constructor({ name: a, type: t }) { this.name = "", this.type = "", this.id = null, this.name = a, this.type = t; } async addDataLog({ data: a }) { const { name: t, type: i } = this; if (t != "dataLog") { const { data: e } = await v.addDataLog({ tableName: t, type: i, data: a }); this.id = e.id; } } async modifyDataLogStatus() { const { name: a, id: t } = this; a != "dataLog" && t && await v.modifyDataLogStatus({ id: t }); } } class p { /** * 读取列表数据 * @param {*} param0 * @returns */ static async retrieveList({ db: a, name: t, params: i }) { await a.read(); const e = a.data[t].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: o, pageSize: s } = i; let d = e; return o && s && (d = e.slice((o - 1) * s, o * s)), { code: 0, data: d.sort((c, l) => c.sort - l.sort) || [], total: e.length }; } /** * 读取单条数据 * @param {*} param0 */ static async retrieveSingle({ db: a, name: t, params: i }) { await a.read(); const e = a.data[t], o = i; let s = {}; return o.id && e && (s = e.find((d) => d.id == o.id)), { code: 0, data: s || {} }; } /** * 修改数据 * @param {*} param0 */ static async update({ db: a, name: t, data: i }) { await a.read(), a.data[t] || (a.data[t] = []); const e = a.data[t], o = i.id; if (o && e) { const s = e.find((c) => c.id == o); s && U.merge(s, i); const d = new S({ name: t, type: "upd" }); await d.addDataLog({ data: s }), await a.write(), await d.modifyDataLogStatus(); } return { code: 0 }; } /** * 创建数据 * @param {*} param0 */ static async create({ db: a, name: t, data: i }) { await a.read(), a.data[t] || (a.data[t] = []); const e = a.data[t]; i.id = G(), i.createTime = /* @__PURE__ */ new Date(), e.push(i); const o = new S({ name: t, type: "add" }); return await o.addDataLog({ data: i }), await a.write(), await o.modifyDataLogStatus(), { code: 0, data: i }; } /** * 删除数据 * @param {*} param0 * @returns */ static async delete({ db: a, name: t, data: i }) { await a.read(), a.data[t] || (a.data[t] = []); const e = a.data[t], o = i.ids; if (o.length > 0 && e) { let s = null; const d = []; for (let l = 0; l < o.length; l++) { s = o[l]; const r = e.findIndex((f) => f.id == s); r != -1 && (d.push(e[r]), e.splice(r, 1)); } const c = new S({ name: t, type: "del" }); await c.addDataLog({ data: d }), await a.write(), await c.modifyDataLogStatus(); } return { code: 0 }; } } class et { constructor({ router: a, db: t, name: i }) { this.name = "", this.db = null, this.router = null, this.router = a, this.db = t, this.name = i; } /** * 创建列表数据路由 */ createGetList() { const { name: a, db: t, router: i } = this; i.post(`/api/${a}/list`, async (e) => { e.type = "application/json"; const o = e.request.body, s = await p.retrieveList({ db: t, name: a, params: o }); e.body = s; }); } /** * 创建详情路由 */ createGetDetail() { const { name: a, db: t, router: i } = this; i.get(`/api/${a}`, async (e) => { e.type = "application/json"; const o = e.request.query, s = await p.retrieveSingle({ db: t, name: a, params: o }); e.body = s; }); } /** * 修改数据 */ createUpdate() { const { name: a, db: t, router: i } = this; i.post(`/api/${a}/upd`, async (e) => { e.type = "application/json"; const o = e.request.body, s = await p.update({ db: t, name: a, data: o }); e.body = s; }); } /** * 新增数据 */ createAdd() { const { name: a, db: t, router: i } = this; i.post(`/api/${a}/add`, async (e) => { e.type = "application/json"; const o = e.request.body, s = await p.create({ db: t, name: a, data: o }); e.body = s; }); } /** * 删除数据 */ createDelete() { const { name: a, db: t, router: i } = this; i.post(`/api/${a}/delete`, async (e) => { e.type = "application/json"; const o = e.request.body, s = await p.delete({ db: t, name: a, data: o }); e.body = s; }); } creaetBaseRouter() { this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete(); } } const q = R($( import.meta.url )); u.initConfig(); (async () => await g.init())(); function it(n = 4873) { const a = new M(), t = new A(); t.use(T()), t.use( C("/resource/static", z( w.join(q, "./static"), {} )) ), t.use(a.routes()), t.use(a.allowedMethods()); const e = (d) => { B.readdir(d, async (c, l) => { if (c) { console.error("Error reading directory:", c); return; } for (const r of l) try { const f = w.join(d, r), k = await B.statSync(f); if (k.isDirectory()) await e(f); else if (k.isFile()) { const P = w.join(d, r); (await import("./" + w.relative(q, P))).default(a); } } catch (f) { console.error(`Error importing ${r}:`, f); } }); }; (async () => await e("./routers"))(); let o = null; (() => { o = t.listen(n, () => { console.log("server is listen in ", n); }), o.on("close", (d, c) => { o = null, console.log("server is close"); }); })(); } export { p as CURDHelper, u as ConfigHelper, it as CreateServer, g as DBHelper, v as LogHelper, et as RouterFactor, O as getBackupDb, E as getDb, J as getLogDb, I as getResourceDb };