From 2388255803a9188b6e69a76ca30a751449070ce2 Mon Sep 17 00:00:00 2001 From: 728 Date: Sat, 20 May 2023 13:58:55 +0800 Subject: [PATCH] rel #43 --- ismism.ts/src/eid/is.ts | 4 +--- ismism.ts/src/eid/rel.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ismism.ts/src/eid/rel.ts diff --git a/ismism.ts/src/eid/is.ts b/ismism.ts/src/eid/is.ts index 8af24a3..9746ef1 100644 --- a/ismism.ts/src/eid/is.ts +++ b/ismism.ts/src/eid/is.ts @@ -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 diff --git a/ismism.ts/src/eid/rel.ts b/ismism.ts/src/eid/rel.ts new file mode 100644 index 0000000..ef312eb --- /dev/null +++ b/ismism.ts/src/eid/rel.ts @@ -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 | 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 } } + } + } +}