This commit is contained in:
728
2023-10-02 10:01:47 +08:00
parent 6007e1237c
commit 45a9538060
2 changed files with 57 additions and 2 deletions

42
ismism.ts/src/eid/soc.ts Normal file
View File

@ -0,0 +1,42 @@
import type { Soc } from "./typ.ts"
import { coll, DocC, DocD, DocR, DocU, Updt } from "./db.ts"
import { id_c, id_d, id_n, id_r, id_u } from "./id.ts"
import { is_idl, lim_sec } from "./is.ts"
export async function soc_c(
nam: Soc["nam"],
adm1: string,
adm2: string,
): DocC<Soc["_id"]> {
return id_c(coll.soc, {
_id: await id_n(coll.soc), utc: Date.now(),
nam, adm1, adm2,
msg: "", sec: [], cde: false,
})
}
export function soc_r<
P extends keyof Soc
>(
_id: Soc["_id"],
projection: Partial<{ [K in P]: 1 }>
): DocR<Pick<Soc, "_id" | P>> {
return id_r(coll.soc, { _id }, projection)
}
export async function soc_u(
sid: Soc["_id"],
u: Updt<Soc>,
): DocU {
if (u.$set) {
const s = u.$set
if (s.sec && !is_idl(s.sec, lim_sec)) return null
}
return await id_u(coll.soc, sid, u)
}
export function soc_d(
sid: Soc["_id"]
): DocD {
return id_d(coll.soc, sid)
}

View File

@ -1,4 +1,5 @@
import { db } from "../src/eid/db.ts"
import { soc_c, soc_d, soc_r, soc_u } from "../src/eid/soc.ts"
import { usr_c, usr_d, usr_r, usr_u } from "../src/eid/usr.ts"
import { assertEquals } from "./mod.ts"
@ -10,11 +11,11 @@ Deno.test("usr", async () => {
const uid = await usr_c(nbr, "四川", "成都") as number
assertEquals(1, uid)
assertEquals({
_id: uid, nam: `${uid}`, msg: "", adm2: "成都", nbr
_id: uid, nam: `${uid}`, msg: "", adm2: "成都", nbr,
}, await usr_r(
{ _id: uid }, { nam: 1, msg: 1, adm2: 1, nbr: 1 })
)
await usr_u(uid, { $set: { nam: "中文名", adm1: "广东", adm2: "汕头", rej: [2], msg: "介绍" } })
await usr_u(uid, { $set: { nam: "中文名", adm1: "广东", adm2: "汕头", msg: "介绍" } })
assertEquals({
_id: uid, nam: "中文名", adm2: "汕头", msg: "介绍"
}, await usr_r(
@ -23,3 +24,15 @@ Deno.test("usr", async () => {
assertEquals(1, await usr_d(uid))
assertEquals(null, await usr_r({ _id: 1 }, { nbr: 1 }))
})
Deno.test("soc", async () => {
const nam = "俱乐部"
assertEquals(null, await soc_r(1, {}))
assertEquals(1, await soc_c(nam, "四川", "成都"))
assertEquals(await soc_r(1, { nam: 1, msg: 1, adm1: 1, sec: 1 }), {
_id: 1, nam, adm1: "四川", msg: "", sec: []
})
await soc_u(1, { $set: { msg: "msg", sec: [2] } })
assertEquals(await soc_r(1, { msg: 1, sec: 1 }), { _id: 1, msg: "msg", sec: [2] })
assertEquals(1, await soc_d(1))
})