feat: 添加类型注释

This commit is contained in:
2025-01-20 16:05:50 +08:00
parent e5a4381b04
commit 60724a923e
27 changed files with 1364 additions and 249 deletions
+43
View File
@@ -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;
}>;
}
+7
View File
@@ -0,0 +1,7 @@
/**
* 配置信息帮助类
*/
export default class ConfigHelper {
static config: any;
static initConfig(): void;
}
+9
View File
@@ -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;
}
+7
View File
@@ -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>;
}
+27
View File
@@ -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;
}
+6
View File
@@ -0,0 +1,6 @@
export default CreateServer;
/**
* 创建服务
* port: 服务端口号
*/
declare function CreateServer(port?: number): void;
+22
View File
@@ -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;
+215 -174
View File
@@ -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 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 = k.parse(q.readFileSync("./config.yml").toString());
this.config = F.parse(B.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 {
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 G(), this.resourceDB = await U(), this.backupDB = await _(), this.logDB = await A();
this.baseDB = await E(), this.resourceDB = await I(), this.backupDB = await O(), this.logDB = await J();
}
static getBackupDb() {
return this.backupDB;
@@ -46,22 +48,23 @@ 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 {
};
y.baseDB = null, y.resourceDB = null, y.backupDB = null, y.logDB = null;
let g = y;
class v {
static async addDataLog({
tableName: t,
type: a,
tableName: a,
type: t,
data: i
}) {
const e = await y.getLogDb();
const e = await g.getLogDb();
return p.create({
db: e,
name: "dataLog",
data: {
dataId: i.id,
type: a,
tableName: t,
type: t,
tableName: a,
data: i,
// 待生效
status: 0,
@@ -70,20 +73,20 @@ class m {
});
}
static async modifyDataLogStatus({
id: t
id: a
}) {
const a = await y.getLogDb(), {
const t = await g.getLogDb(), {
data: i,
code: e
} = await p.retrieveSingle({
db: a,
db: t,
name: "dataLog",
params: {
id: t
id: a
}
});
e == 0 && await p.update({
db: a,
db: t,
name: "dataLog",
data: {
...i,
@@ -93,41 +96,38 @@ class m {
});
}
}
class h {
class S {
constructor({
name: t,
type: a
name: a,
type: t
}) {
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 = a, this.type = t;
}
async addDataLog({
data: t
data: a
}) {
const {
name: a,
name: t,
type: i
} = this;
if (a != "dataLog") {
if (t != "dataLog") {
const {
data: e
} = await m.addDataLog({
tableName: a,
} = await v.addDataLog({
tableName: t,
type: i,
data: t
data: a
});
this.id = e.id;
}
}
async modifyDataLogStatus() {
const {
name: t,
id: a
name: a,
id: t
} = this;
t != "dataLog" && a && await m.modifyDataLogStatus({
id: a
a != "dataLog" && t && await v.modifyDataLogStatus({
id: t
});
}
}
@@ -138,25 +138,25 @@ class p {
* @returns
*/
static async retrieveList({
db: t,
name: a,
db: a,
name: t,
params: i
}) {
await t.read();
const e = t.data[a].filter((c) => {
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: s,
pageSize: o
page: o,
pageSize: s
} = i;
let u = e;
return s && o && (u = e.slice((s - 1) * o, s * o)), {
let d = e;
return o && s && (d = e.slice((o - 1) * s, o * s)), {
code: 0,
data: u.sort((c, l) => c.sort - l.sort) || [],
data: d.sort((c, l) => c.sort - l.sort) || [],
total: e.length
};
}
@@ -165,16 +165,16 @@ class p {
* @param {*} param0
*/
static async retrieveSingle({
db: t,
name: a,
db: a,
name: t,
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)), {
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: o || {}
data: s || {}
};
}
/**
@@ -182,22 +182,22 @@ class p {
* @param {*} param0
*/
static async update({
db: t,
name: a,
db: a,
name: t,
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,
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 u.addDataLog({
data: o
}), await t.write(), await u.modifyDataLogStatus();
await d.addDataLog({
data: s
}), await a.write(), await d.modifyDataLogStatus();
}
return {
code: 0
@@ -208,20 +208,20 @@ class p {
* @param {*} param0
*/
static async create({
db: t,
name: a,
db: a,
name: t,
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,
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 s.addDataLog({
return await o.addDataLog({
data: i
}), await t.write(), await s.modifyDataLogStatus(), {
}), await a.write(), await o.modifyDataLogStatus(), {
code: 0,
data: i
};
@@ -232,61 +232,58 @@ class p {
* @returns
*/
static async delete({
db: t,
name: a,
db: a,
name: t,
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));
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 h({
name: a,
const c = new S({
name: t,
type: "del"
});
await c.addDataLog({
data: u
}), await t.write(), await c.modifyDataLogStatus();
data: d
}), await a.write(), await c.modifyDataLogStatus();
}
return {
code: 0
};
}
}
class M {
class et {
constructor({
router: t,
db: a,
router: a,
db: t,
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 = a, this.db = t, this.name = i;
}
/**
* 创建列表数据路由
*/
createGetList() {
const {
name: t,
db: a,
name: a,
db: t,
router: i
} = this;
i.post(`/api/${t}/list`, async (e) => {
i.post(`/api/${a}/list`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.retrieveList({
db: a,
name: t,
params: s
const o = e.request.body, s = await p.retrieveList({
db: t,
name: a,
params: o
});
e.body = o;
e.body = s;
});
}
/**
@@ -294,18 +291,18 @@ class M {
*/
createGetDetail() {
const {
name: t,
db: a,
name: a,
db: t,
router: i
} = this;
i.get(`/api/${t}`, async (e) => {
i.get(`/api/${a}`, async (e) => {
e.type = "application/json";
const s = e.request.query, o = await p.retrieveSingle({
db: a,
name: t,
params: s
const o = e.request.query, s = await p.retrieveSingle({
db: t,
name: a,
params: o
});
e.body = o;
e.body = s;
});
}
/**
@@ -313,18 +310,18 @@ class M {
*/
createUpdate() {
const {
name: t,
db: a,
name: a,
db: t,
router: i
} = this;
i.post(`/api/${t}/upd`, async (e) => {
i.post(`/api/${a}/upd`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.update({
db: a,
name: t,
data: s
const o = e.request.body, s = await p.update({
db: t,
name: a,
data: o
});
e.body = o;
e.body = s;
});
}
/**
@@ -332,18 +329,18 @@ class M {
*/
createAdd() {
const {
name: t,
db: a,
name: a,
db: t,
router: i
} = this;
i.post(`/api/${t}/add`, async (e) => {
i.post(`/api/${a}/add`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.create({
db: a,
name: t,
data: s
const o = e.request.body, s = await p.create({
db: t,
name: a,
data: o
});
e.body = o;
e.body = s;
});
}
/**
@@ -351,32 +348,76 @@ class M {
*/
createDelete() {
const {
name: t,
db: a,
name: a,
db: t,
router: i
} = this;
i.post(`/api/${t}/delete`, async (e) => {
i.post(`/api/${a}/delete`, async (e) => {
e.type = "application/json";
const s = e.request.body, o = await p.delete({
db: a,
name: t,
data: s
const o = e.request.body, s = await p.delete({
db: t,
name: a,
data: o
});
e.body = 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,
g as ConfigHelper,
y as DBHelper,
m as LogHelper,
M as RouterFactory,
_ as getBackupDb,
G as getDb,
A as getLogDb,
U as getResourceDb
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
};
+13
View File
@@ -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';
+1
View File
@@ -0,0 +1 @@
export default function sleep(timeout: number): Promise<unknown>;