diff --git a/index.mjs b/index.mjs index 5486903..5ede8b2 100644 --- a/index.mjs +++ b/index.mjs @@ -1,6 +1,6 @@ // 备份 -export * from "./backupHelper.mjs"; -export { default as BackupHelper } from "./backupHelper.mjs"; +// export * from "./backupHelper.mjs"; +// export { default as BackupHelper } from "./backupHelper.mjs"; // 配置 export * from "./ConfigHelper.mjs"; @@ -21,3 +21,7 @@ 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"; diff --git a/lib/funjia-server.mjs b/lib/funjia-server.mjs new file mode 100644 index 0000000..14d7ceb --- /dev/null +++ b/lib/funjia-server.mjs @@ -0,0 +1,382 @@ +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 +}; diff --git a/lib/funjiaserver.mjs b/lib/funjiaserver.mjs deleted file mode 100644 index ab12e4e..0000000 --- a/lib/funjiaserver.mjs +++ /dev/null @@ -1,464 +0,0 @@ -var F = Object.defineProperty; -var j = (o, t, a) => t in o ? F(o, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : o[t] = a; -var d = (o, t, a) => j(o, typeof t != "symbol" ? t + "" : t, a); -import f from "fs"; -import v from "dayjs"; -import R from "lodash"; -import H from "cuid"; -import { dirname as q, join as w } from "path"; -import { JSONFile as m, Low as D } from "lowdb"; -import { fileURLToPath as U } from "url"; -import x from "yaml"; -class y { - static initConfig() { - this.config = x.parse(f.readFileSync("./config.yml").toString()); - } -} -d(y, "config", null); -const b = q(U( - import.meta.url -)), h = (o, t) => { - let a = ""; - return y.config && y.config.db && y.config.db[o] ? a = y.config.path + y.config.db[o] : a = t, a; -}, E = async () => { - let o = h("db", w(b, "db.json")); - const t = new m(o), a = new D(t); - return await a.read(), a; -}, N = async () => { - let o = h("resourceDB", w(b, "resourceDB.json")); - const t = new m(o), a = new D(t); - return await a.read(), a; -}, P = async () => { - let o = h("backupDB", w(b, "backupDB.json")); - const t = new m(o), a = new D(t); - return await a.read(), a; -}, _ = async () => { - let o = h("logDB", w(b, "logDB.json")); - const t = new m(o), a = new D(t); - return await a.read(), a; -}; -class g { - static async init() { - this.baseDB = await E(), this.resourceDB = await N(), this.backupDB = await P(), this.logDB = await _(); - } - static getBackupDb() { - return this.backupDB; - } - static getLogDb() { - return this.logDB; - } -} -d(g, "baseDB", null), d(g, "resourceDB", null), d(g, "backupDB", null), d(g, "logDB", null); -class k { - static async addDataLog({ - tableName: t, - type: a, - data: i - }) { - const e = await g.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 g.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 L { - 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 k.addDataLog({ - tableName: a, - type: i, - data: t - }); - this.id = e.id; - } - } - async modifyDataLogStatus() { - const { - name: t, - id: a - } = this; - t != "dataLog" && a && await k.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((r) => { - let u = !0; - for (let c in i) - if (!["page", "pageSize"].includes(c) && (typeof r[c] == "string" ? u = r[c].indexOf(i[c]) > -1 : typeof r[c] == "number" ? u = r[c] == i[c] : r[c] instanceof Array && (u = r[c].includes(i[c])), !u)) - break; - return u; - }), { - page: s, - pageSize: n - } = i; - let l = e; - return s && n && (l = e.slice((s - 1) * n, s * n)), { - code: 0, - data: l.sort((r, u) => r.sort - u.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 n = {}; - return s.id && e && (n = e.find((l) => l.id == s.id)), { - code: 0, - data: n || {} - }; - } - /** - * 修改数据 - * @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 n = e.find((r) => r.id == s); - n && R.merge(n, i); - const l = new L({ - name: a, - type: "upd" - }); - await l.addDataLog({ - data: n - }), await t.write(), await l.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 = H(), i.createTime = /* @__PURE__ */ new Date(), e.push(i); - const s = new L({ - 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 n = null; - const l = []; - for (let u = 0; u < s.length; u++) { - n = s[u]; - const c = e.findIndex((S) => S.id == n); - c != -1 && (l.push(e[c]), e.splice(c, 1)); - } - const r = new L({ - name: a, - type: "del" - }); - await r.addDataLog({ - data: l - }), await t.write(), await r.modifyDataLogStatus(); - } - return { - code: 0 - }; - } -} -const B = {}; -class $ { - constructor() { - d(this, "addBackUPRecord", async (t) => p.create({ - db: await g.getBackupDb(), - name: "backupHistory", - data: t - })); - d(this, "updateBackUPRecord", async (t) => p.update({ - db: await g.getBackupDb(), - name: "backupHistory", - data: t - })); - } - /** - * 获取文件名称 fileName.ext - * @param {*} path - * @returns - */ - getFileName(t) { - const a = t.split("/"); - return a.length >= 2 ? a.reverse()[0] : t; - } - /** - * 获取文件后缀和文件名称 - * @param {*} path - * @returns - */ - getFileExt(t) { - let a = t.split("."); - return a.length >= 2 ? (a = a.reverse(), { - name: a[1], - ext: "." + a[0] - }) : { - name: t, - ext: "" - }; - } - /** - * 创建文件夹(指定文件夹路径不存在时才创建) - * @param {*} filePath - */ - mkDir(t) { - f.existsSync(t) || f.mkdirSync(t); - } - // backupToBaiduYunPan = () => { - // let cmd = 'python'; - // let args = ('F:\\sourceCode\\2021\\test-baiduPan\\python\\getInfo.py').split(' '); - // let task = child_process.spawnSync(cmd, args); - // console.log("用户信息:", task.stdout.toString()); - // } - /** - * 根据配置备份文件 - * @param {*} data:[{original:string,dest:string}] - */ - async bakFile(t) { - for (const a of t || []) - if (f.existsSync(a.original)) { - this.mkDir(a.dest); - const e = this.getFileName(a.original), s = this.getFileExt(e), n = v().format("YYYYMMDDHHmmss"), l = s.name + n + s.ext, { - data: r, - code: u - } = await this.addBackUPRecord({ - name: l, - category: a.category - }); - u == 0 && (f.writeFileSync(a.dest + "/" + l, f.readFileSync(a.original)), await this.updateBackUPRecord({ - id: r.id, - status: "1" - })); - } - } -} -const G = () => { - let o = "pm2", t = "start computer-cms-server".split(" "), a = B.spawnSync(o, t); - console.log("pm2 log:", a); -}, Y = () => { - let o = "pm2", t = "stop computer-cms-server".split(" "), a = B.spawnSync(o, t); - console.log("pm2 log:", a); -}, A = new $(); -(async () => (y.initConfig(), await g.init(), Y(), await A.bakFile(y.config.dbBak), G()))(); -class Q { - 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, n = await p.retrieveList({ - db: a, - name: t, - params: s - }); - e.body = n; - }); - } - /** - * 创建详情路由 - */ - createGetDetail() { - const { - name: t, - db: a, - router: i - } = this; - i.get(`/api/${t}`, async (e) => { - e.type = "application/json"; - const s = e.request.query, n = await p.retrieveSingle({ - db: a, - name: t, - params: s - }); - e.body = n; - }); - } - /** - * 修改数据 - */ - 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, n = await p.update({ - db: a, - name: t, - data: s - }); - e.body = n; - }); - } - /** - * 新增数据 - */ - 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, n = await p.create({ - db: a, - name: t, - data: s - }); - e.body = n; - }); - } - /** - * 删除数据 - */ - 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, n = await p.delete({ - db: a, - name: t, - data: s - }); - e.body = n; - }); - } - creaetBaseRouter() { - this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete(); - } -} -export { - $ as BackupHelper, - p as CURDHelper, - y as ConfigHelper, - k as LogHelper, - Q as RouterFactory, - P as getBackupDb, - E as getDb, - _ as getLogDb, - N as getResourceDb -}; diff --git a/test.mjs b/test.mjs index 3b35ef8..712d98c 100644 --- a/test.mjs +++ b/test.mjs @@ -1 +1,4 @@ -import { BackupHelper } from "./index.mjs"; +// import { BackupHelper } from "./index.mjs"; + + +import { } from "./lib/funjia-server.mjs"; \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index a87c456..72d1041 100644 --- a/vite.config.js +++ b/vite.config.js @@ -10,7 +10,7 @@ export default defineConfig({ }, rollupOptions: { // 确保不包含 Node.js 特定的模块,避免将 Node.js 模块错误地打包到浏览器代码中 - external: ['fs', 'path','url','node','node-cron','cuid','dayjs','koa','koa-body', + 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',