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 = G.parse(k.readFileSync("./config.yml").toString()); } }; v.config = null; let u = v; const b = P($( import.meta.url )), L = (n, t) => { let a = ""; 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; }, O = async () => { let n = L("resourceDB", h(b, "resourceDB.json")); const t = new m(n), a = new D(t); return await a.read(), a; }, J = async () => { let n = L("backupDB", h(b, "backupDB.json")); const t = new m(n), a = new D(t); return await a.read(), a; }, K = async () => { let n = L("logDB", h(b, "logDB.json")); const t = new m(n), a = new D(t); return await a.read(), a; }, f = class f { static async init() { this.baseDB = await I(), this.resourceDB = await O(), this.backupDB = await J(), this.logDB = await K(); } static getBackupDb() { return this.backupDB; } static getLogDb() { return this.logDB; } }; f.baseDB = null, f.resourceDB = null, f.backupDB = null, f.logDB = null; let y = f; class R { 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 j { constructor({ name: t, type: a }) { this.name = "", this.type = "", 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 R.addDataLog({ tableName: a, type: i, data: t }); this.id = e.id; } } async modifyDataLogStatus() { const { name: t, id: a } = this; t != "dataLog" && a && await R.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((l) => { let c = !0; for (let r in i) 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 c; }), { page: s, pageSize: o } = i; let d = e; return s && o && (d = e.slice((s - 1) * o, s * o)), { code: 0, data: d.sort((l, c) => l.sort - c.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((d) => d.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((l) => l.id == s); o && A.merge(o, i); const d = new j({ name: a, type: "upd" }); await d.addDataLog({ data: o }), await t.write(), await d.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 = U(), i.createTime = /* @__PURE__ */ new Date(), e.push(i); const s = new j({ 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 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 l = new j({ name: a, type: "del" }); await l.addDataLog({ data: d }), await t.write(), await l.modifyDataLogStatus(); } return { code: 0 }; } } class it { constructor({ router: t, db: a, name: i }) { this.name = "", this.db = null, 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(); } } 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, u as ConfigHelper, st as CreateServer, y as DBHelper, R as LogHelper, it as RouterFactor, J as getBackupDb, I as getDb, K as getLogDb, O as getResourceDb };