This commit is contained in:
728
2023-05-20 13:58:55 +08:00
parent 57b804e080
commit 2388255803
2 changed files with 29 additions and 3 deletions

View File

@ -11,9 +11,7 @@ export const lim_code = 10 ** len_code
export const lst_re = 2
export const lim_re = 64
export const lim_sec = 8
export const lim_uid = 128
export const lim_res = 64
export const lim_rel = { sec: 8, uid: 128, res: 64 }
export const lim_itm = 16
export const lim_loc = 16

28
ismism.ts/src/eid/rel.ts Normal file
View File

@ -0,0 +1,28 @@
import type { Rel, Usr } from "./typ.ts"
import { Update } from "./db.ts";
import { is_id, is_lim, lim_rel } from "./is.ts";
export type UpdateRel = {
rel: keyof Rel,
op: "add" | "pull",
uid?: Usr["_id"]
}
export function rel_u(
rel: Rel,
u: UpdateRel
): Update<Rel> | null {
if (u.uid !== undefined && !is_id(u.uid)) return null
const r = rel[u.rel]
switch (u.op) {
case "add": {
if (!u.uid) return r ? null : { $set: { [u.rel]: [] } }
if (!r || !is_lim(r.length + 1, lim_rel[u.rel])) return null
return { $addToSet: { [u.rel]: u.uid } }
} case "pull": {
if (!u.uid) return r ? { $unset: { [u.rel]: "" } } : null
if (!r || !r.includes(u.uid)) return null
return { $pull: { [u.rel]: u.uid } }
}
}
}