This commit is contained in:
728
2023-05-31 06:08:59 +08:00
parent 05333e7d40
commit cb9fc9716d
5 changed files with 101 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { Act, Agd, Aut, Fund, Lit, Ord, Soc, Usr, Work, Wsl } from "./eid/typ.ts"
import { Act, Agd, Aut, Dst, Fund, Lit, Ord, 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()
@ -17,6 +17,7 @@ export async function db(
ord: db.collection<Ord>("ord"),
work: db.collection<Work>("work"),
fund: db.collection<Fund>("fund"),
dst: db.collection<Dst>("dst"),
act: db.collection<Act>("act"),
aut: db.collection<Aut>("aut"),
@ -77,6 +78,14 @@ export async function db(
partialFilterExpression: { work: "live" },
}]
})
await c.dst.createIndexes({
indexes: [{
key: { "_id.rd": 1, "_id.aid": 1, "_id.uid": 1 }, name: "rd-aid-uid",
}, {
key: { "_id.rd": 1, "_id.uid": 1 }, name: "rd-uid",
partialFilterExpression: { "_id.uid": { $exists: true } },
}]
})
await Promise.all([c.wsl, c.lit].map(cl => cl.createIndexes({
indexes: [{
key: { pin: 1, utcp: -1 }, name: "pin",

53
ismism.ts/src/eid/dst.ts Normal file
View File

@ -0,0 +1,53 @@
import type { Agd, Dst } from "./typ.ts"
import { DocC, DocR, DocU, coll } from "../db.ts"
import { is_dstid, is_id, is_json, is_lim, lim_rd } from "./is.ts"
export async function dst_c(
dst: Dst
): DocC<Dst["_id"]> {
if (!is_dstid(dst._id) || "json" in dst && !is_json(dst.json!)) return null
try { return await coll.dst.insertOne(dst) as Dst["_id"] }
catch { return null }
}
export async function dst_r(
_id: Dst["_id"]
): DocR<Dst> {
if (!is_dstid(_id)) return null
return await coll.dst.findOne({ _id }) ?? null
}
export async function dst_f(
_id: { rd: Dst["_id"]["rd"], uid?: Agd["_id"] }
) {
if (!is_lim(_id.rd, lim_rd) || "uid" in _id && !is_id(_id.uid!)) return null
const f = {
"_id.rd": _id.rd,
..."uid" in _id ? { "_id.uid": _id.uid } : { "_id.aid": { $exists: true }, "_id.uid": { $exists: false } },
}
return await coll.dst.find(f).toArray()
}
export async function dst_n(
_id: { rd: Dst["_id"]["rd"] } & ({ aid: Agd["_id"] } | { uid: Agd["_id"] })
): DocR<number> {
const f = {
"_id.rd": _id.rd,
..."aid" in _id ? { "_id.aid": _id.aid, "_id.uid": { $exists: true } } : {},
..."uid" in _id ? { "_id.uid": _id.uid } : {},
}
return is_lim(_id.rd, lim_rd) ? await coll.dst.countDocuments(f) : null
}
export async function dst_u(
rd: Dst["_id"]["rd"],
json: NonNullable<Dst["json"]>,
): DocU {
if (!is_dstid({ rd }) || !is_json(json)) return null
try {
// deno-lint-ignore no-explicit-any
const { matchedCount, modifiedCount } = await coll.dst.updateOne({ _id: { rd } } as any, { $set: { json } })
if (matchedCount > 0) return modifiedCount > 0 ? 1 : 0
else return null
} catch { return null }
}

View File

@ -1,4 +1,4 @@
import { Act, Agd, Aut, Id, Md, Ord, Rec, Usr } from "./typ.ts"
import { Act, Agd, Aut, Dst, Id, Md, Ord, Rec, Usr } from "./typ.ts"
export const req_re = 2
export const lim_re = 64
@ -22,6 +22,7 @@ export const lim_img = 9
export const lim_goal = 9
export const lim_url = 128
export const lim_msg = 256
export const lim_json = lim_intro * 8
export const lim_md = lim_intro * 8
export const lim_ord_max = 128
@ -31,6 +32,7 @@ export const lim_nrecday = 90
export const lim_rec_f = 32
export const lim_md_f = 4
export const lim_md_pin = lim_md_f
export const lim_rd = 1
export function is_lim(
n: number,
@ -98,6 +100,11 @@ export function is_msg(
): msg is string {
return typeof msg === "string" && 2 <= msg.length && msg.length <= lim_msg
}
export function is_json(
json: string
): json is string {
return typeof json === "string" && json.length <= lim_json
}
export function is_ordid(
ordid: Ord["_id"]
@ -111,6 +118,14 @@ export function is_recid(
return Object.keys(recid).length === 3 && is_id(recid.uid) && is_id(recid.aid) && recid.utc > 0
}
export function is_dstid(
dstid: Dst["_id"]
): dstid is Dst["_id"] {
const l = Object.keys(dstid).length
return 1 <= l && l <= 3 && is_lim(dstid.rd, lim_rd)
&& (l === 2 && is_id(dstid.aid!) || l === 3 && is_id(dstid.uid!))
}
export function is_aut(
aut: Aut["aut"],
a?: Aut["aut"][0],

View File

@ -65,6 +65,11 @@ export type Fund = Rec & {
msg: string,
}
export type Dst = {
_id: { rd: number, aid?: Agd["_id"], uid?: Usr["_id"] },
json?: string,
}
export type Aut = {
_id: Usr["_id"],
aut: ("sup" | "aud" | "aut" | "wsl" | "lit")[],

View File

@ -10,6 +10,8 @@ import { md_c, md_f, md_r, md_u } from "../src/eid/md.ts"
import { aut_c, aut_d, aut_g } from "../src/eid/aut.ts"
import { utc_h } from "../src/ont/utc.ts"
import { ord_c, ord_d, ord_f, ord_r, ord_u } from "../src/eid/ord.ts"
import { dst_c, dst_f, dst_n } from "../src/eid/dst.ts"
import { lim_rd } from "../src/eid/is.ts"
await db("tst", true)
@ -154,6 +156,21 @@ Deno.test("rec", async () => {
assertEquals(await nrec(), { work: 2, fund: 0 })
})
Deno.test("dst", async () => {
assertEquals(0, await dst_n({ rd: lim_rd, aid: 1 }))
await Promise.all([
dst_c({ _id: { rd: lim_rd }, json: JSON.stringify({ nam: "nam", c: 32 }) }),
dst_c({ _id: { rd: lim_rd, aid: 1 } }),
dst_c({ _id: { rd: lim_rd, aid: 2 } }),
dst_c({ _id: { rd: lim_rd, aid: 1, uid: 1 } }),
dst_c({ _id: { rd: lim_rd, aid: 1, uid: 2 } }),
dst_c({ _id: { rd: lim_rd, aid: 2, uid: 2 } }),
])
assertEquals(2, await dst_n({ rd: lim_rd, aid: 1 }))
assertEquals(1, await dst_n({ rd: lim_rd, uid: 1 }))
assertEquals([{ _id: { rd: lim_rd, aid: 1 } }, { _id: { rd: lim_rd, aid: 2 } }], await dst_f({ rd: lim_rd }))
})
Deno.test("aut", async () => {
assertEquals({}, await aut_g())
await Promise.all([