md #41
This commit is contained in:
@ -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
|
||||
|
@ -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
|
||||
|
@ -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>("fund"),
|
||||
act: db.collection<Act>("act"),
|
||||
aut: db.collection<Aut>("aut"),
|
||||
|
||||
wsl: db.collection<Wsl>("wsl"),
|
||||
lit: db.collection<Lit>("lit"),
|
||||
}
|
||||
|
||||
if (reset) {
|
||||
|
@ -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
|
||||
}
|
||||
|
74
ismism.ts/src/eid/md.ts
Normal file
74
ismism.ts/src/eid/md.ts
Normal file
@ -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<Md>
|
||||
): Promise<Md["_id"]> {
|
||||
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>,
|
||||
md: Omit<Md, "_id" | "utc" | "utcp">,
|
||||
): DocC<Md["_id"]> {
|
||||
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<Md>,
|
||||
_id: Md["_id"],
|
||||
): DocR<Md> {
|
||||
if (!is_id(_id)) return null
|
||||
return await c.findOne({ _id }) ?? null
|
||||
}
|
||||
|
||||
export async function md_f(
|
||||
c: Coll<Md>,
|
||||
id: Md["_id"],
|
||||
): DocR<Md[]> {
|
||||
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<Md>,
|
||||
_id: Md["_id"],
|
||||
u: Update<Md>
|
||||
): 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<Md>,
|
||||
_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 }
|
||||
}
|
@ -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<
|
||||
|
@ -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
|
||||
|
@ -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))
|
||||
})
|
||||
|
Reference in New Issue
Block a user