diff --git a/cli/dbexport.zsh b/cli/dbexport.zsh index b6b075c..8ce0af1 100644 --- a/cli/dbexport.zsh +++ b/cli/dbexport.zsh @@ -1,6 +1,6 @@ rm -rf dbexport -coll=(usr soc agd work fund act aut) +coll=(usr soc agd work fund act aut wsl lit) for c in $coll; do echo exporting ismism.$c to dbexport/$c.json diff --git a/cli/dbimport.zsh b/cli/dbimport.zsh index 42662f7..9ed3bdc 100644 --- a/cli/dbimport.zsh +++ b/cli/dbimport.zsh @@ -1,4 +1,4 @@ -coll=(usr soc agd work fund act aut) +coll=(usr soc agd work fund act aut wsl lit) for c in $coll; do echo importing $1/$c.json to ismism.$c diff --git a/ismism.ts/src/db.ts b/ismism.ts/src/db.ts index 09f6886..aa19733 100644 --- a/ismism.ts/src/db.ts +++ b/ismism.ts/src/db.ts @@ -1,4 +1,4 @@ -import type { Act, Agd, Aut, Fund, Soc, Usr, Work } from "./eid/typ.ts" +import type { Act, Agd, Aut, Fund, Lit, Soc, Usr, Work, Wsl } from "./eid/typ.ts" import { Collection, MongoClient, UpdateFilter } from "https://deno.land/x/mongo@v0.31.1/mod.ts" const conn = new MongoClient() @@ -18,6 +18,9 @@ export async function db( fund: db.collection("fund"), act: db.collection("act"), aut: db.collection("aut"), + + wsl: db.collection("wsl"), + lit: db.collection("lit"), } if (reset) { diff --git a/ismism.ts/src/eid/is.ts b/ismism.ts/src/eid/is.ts index 9187630..0b94f00 100644 --- a/ismism.ts/src/eid/is.ts +++ b/ismism.ts/src/eid/is.ts @@ -1,4 +1,4 @@ -import { Act, Agd, Aut, Id, Rec, Usr } from "./typ.ts" +import { Act, Agd, Aut, Id, Md, Rec, Usr } from "./typ.ts" export const req_re = 2 export const lim_re = 64 @@ -17,8 +17,10 @@ export const lim_img = 9 export const lim_goal = 9 export const lim_url = 128 export const lim_msg = 256 +export const lim_md = lim_intro * 8 -export const lim_rec = 32 +export const lim_rec_f = 32 +export const lim_md_f = 8 export function is_lim( n: number, @@ -105,3 +107,9 @@ export function is_actid( ): actid is Act["_id"] { return typeof actid === "string" && 6 <= actid.length && actid.length <= lim_url } + +export function is_md( + md: string +): md is Md["md"] { + return typeof md === "string" && md.length <= lim_md +} diff --git a/ismism.ts/src/eid/md.ts b/ismism.ts/src/eid/md.ts new file mode 100644 index 0000000..0ffe14e --- /dev/null +++ b/ismism.ts/src/eid/md.ts @@ -0,0 +1,74 @@ +import type { Md } from "./typ.ts" +import type { Coll, DocC, DocD, DocR, DocU, Update } from "../db.ts" +import { is_id, is_md, is_nam, lim_md_f } from "./is.ts" + +async function md_n( + c: Coll +): Promise { + const l = await c.findOne({}, { projection: { _id: 1 }, sort: { _id: -1 } }) + return l ? l._id + 1 : 1 +} + +export async function md_c( + c: Coll, + md: Omit, +): DocC { + if (!is_id(md.uid) || !is_nam(md.nam) || !is_md(md.md)) return null + const utc = Date.now() + try { + return await c.insertOne({ + _id: await md_n(c), + nam: md.nam, + utc: utc, + utcp: utc, + uid: md.uid, + md: md.md + }) as Md["_id"] + } + catch { return null } +} + +export async function md_r( + c: Coll, + _id: Md["_id"], +): DocR { + if (!is_id(_id)) return null + return await c.findOne({ _id }) ?? null +} + +export async function md_f( + c: Coll, + id: Md["_id"], +): DocR { + const f = is_id(id) ? { _id: { $lt: id } } : {} + return await c.find(f, { sort: { _id: -1 }, limit: lim_md_f }).toArray() as Md[] +} + +export async function md_u( + c: Coll, + _id: Md["_id"], + u: Update +): DocU { + if (!is_id(_id)) return null + if ("$set" in u && u.$set) { + const s = u.$set + if (s.nam && !is_nam(s.nam)) return null + if (s.md && !is_md(s.md)) return null + } + try { + const { matchedCount, modifiedCount } = await c.updateOne({ _id }, u) + if (matchedCount > 0) return modifiedCount > 0 ? 1 : 0 + else return null + } catch { return null } +} + +export async function md_d( + c: Coll, + _id: Md["_id"], +): DocD { + if (!is_id(_id)) return null + try { + const d = await c.deleteOne({ _id }) + return d > 0 ? 1 : 0 + } catch { return null } +} diff --git a/ismism.ts/src/eid/rec.ts b/ismism.ts/src/eid/rec.ts index 4fdfd2f..799f672 100644 --- a/ismism.ts/src/eid/rec.ts +++ b/ismism.ts/src/eid/rec.ts @@ -1,6 +1,6 @@ import type { Rec } from "./typ.ts" import { coll, Coll, DocC, DocD, DocR, DocU, Update } from "../db.ts" -import { is_id, is_idl, is_recid, lim_rec, lim_uid_max } from "./is.ts" +import { is_id, is_idl, is_recid, lim_rec_f, lim_uid_max } from "./is.ts" export async function rec_c< T extends Rec @@ -38,7 +38,7 @@ export async function rec_f< ...id && "uid" in id ? { "_id.uid": { $in: id.uid } } : {}, ...utc > 0 ? { "_id.utc": { $lt: utc } } : {}, } // deno-lint-ignore no-explicit-any - return await c.find(f as any, { sort: { "_id.utc": -1 }, limit: lim_rec }).toArray() as T[] + return await c.find(f as any, { sort: { "_id.utc": -1 }, limit: lim_rec_f }).toArray() as T[] } export async function rec_u< diff --git a/ismism.ts/src/eid/typ.ts b/ismism.ts/src/eid/typ.ts index 1b891ca..3b49732 100644 --- a/ismism.ts/src/eid/typ.ts +++ b/ismism.ts/src/eid/typ.ts @@ -65,3 +65,14 @@ export type Act = { uid: number, }) +export type Md = { + _id: number, + nam: string, + utc: number, + utcp: number, + uid: Usr["_id"], + md: string, +} + +export type Wsl = Md +export type Lit = Md diff --git a/ismism.ts/tst/eid.test.ts b/ismism.ts/tst/eid.test.ts index 1011ad3..4357d2c 100644 --- a/ismism.ts/tst/eid.test.ts +++ b/ismism.ts/tst/eid.test.ts @@ -6,6 +6,7 @@ import { agd_c, agd_d, agd_r, agd_u } from "../src/eid/agd.ts" import { nrec, rec_c, rec_d, rec_f, rec_r, rec_u } from "../src/eid/rec.ts" import { rolref, rol } from "../src/eid/rel.ts" import { nid } from "../src/eid/id.ts" +import { md_c, md_f, md_r, md_u } from "../src/eid/md.ts" await db("tst", true) @@ -113,3 +114,12 @@ Deno.test("rec", async () => { assertEquals(await nrec(), { work: 0, fund: 0 }) }) +Deno.test("md", async () => { + assertEquals([], await md_f(coll.wsl, 0)) + assertEquals(1, await md_c(coll.wsl, { nam: "标题", md: "##md", uid: 1 })) + assertEquals(2, await md_c(coll.wsl, { nam: "标题", md: "##md", uid: 1 })) + assertEquals(1, await md_u(coll.wsl, 1, { $set: { md: "#md2", uid: 2 } })) + const md = await md_r(coll.wsl, 1) + assertEquals(md!.md, "#md2") + assertEquals([md], await md_f(coll.wsl, 2)) +})