From 396201a204d15e898a66e7e340788999b6ced5c5 Mon Sep 17 00:00:00 2001 From: Ivan S Glazunov Date: Thu, 18 Jul 2024 13:26:26 +0000 Subject: [PATCH] 0.0.382 Possibility to containerId and name in deep.insert: await deep.insert({ containerId: deep.linkId, type_id: Selector, name: 'my selector', out: { containerId: 2256, type_id: SelectorInclude, to_id: 2258, out: { type_id: SelectorTree, to_id: await deep.id('@deep-foundation/messaging', 'messagingTree'), }, }, }); --- imports/client.tsx | 31 +++++++++++++++++++++++++------ imports/client_types.ts | 2 ++ package.json | 2 +- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/imports/client.tsx b/imports/client.tsx index f1386d04..c7c54bbf 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -620,16 +620,32 @@ export type AsyncSerialParams = { silent?: boolean; }; -export function checkAndFillShorts(obj) { +export function checkAndFillShorts(obj, table, containerId, Contain, field?: string) { + if (obj.hasOwnProperty('containerId') && !obj.containerId) { + throw new Error(`containerId is undefined`); + } + const cId = obj.containerId || containerId; for (var i in obj) { if (!obj.hasOwnProperty(i)) continue; if ((typeof obj[i]) == 'object' && obj[i] !== null) { if (typeof obj[i] === 'object' && i === 'object' && obj[i]?.data?.value === undefined) { obj[i] = { data: { value: obj[i] } }; continue; } - if (typeof obj[i] === 'object' && (i === 'to' || i === 'from' || i === 'in' || i === 'out') && obj[i]?.data === undefined) obj[i] = { data: obj[i] }; - checkAndFillShorts(obj[i]); + if (typeof obj[i] === 'object' && (i === 'to' || i === 'from' || i === 'in' || i === 'out' || i === 'typed' || i === 'type') && obj[i]?.data === undefined) obj[i] = { data: obj[i] }; + checkAndFillShorts(obj[i], _serialize[table]?.relations?.[i] || table, cId, Contain, i); } else if (i === 'string' && typeof obj[i] === 'string' || i === 'number' && typeof obj[i] === 'number') obj[i] = { data: { value: obj[i] } }; } + if (cId && !Array.isArray(obj) && !obj.data && table === 'links' && ((!field || [ + 'to', 'from', 'in', 'out', 'type', 'typed' + ].includes(field)) || obj.type_id || obj.type)) { + obj.in = obj.in || {}; + obj.in.data = obj?.in?.data ? Array.isArray(obj?.in?.data) ? obj?.in?.data : [obj?.in?.data] : []; + obj.in.data.push({ + type_id: Contain, from_id: cId, + ...(typeof(obj.name) === 'string' ? { string: { data: { value: obj.name } } } : {}) + }); + } + delete obj.name; + delete obj.containerId; } export function convertDeepInsertToMinilinksApplyAndPatchVirtualIds(deep, objects, table, result: { id?: number; from_id?: number; to_id?: number; type_id?: number; value?: any }[] = [], errors: string[], patch: any = {}): { objects: any; levelIds: any[]; } { @@ -1157,9 +1173,6 @@ export class DeepClient = Link> implements DeepClientInst * Note: If a link already has value you should update its value, not insert */ async insert(objects: InsertObjects, options?: WriteOptions):Promise> { - let _objects = Object.prototype.toString.call(objects) === '[object Array]' ? objects : [objects]; - checkAndFillShorts(_objects); - const { local, remote } = { local: this.local, remote: this.remote, ...options }; const table = options?.table || this.table; @@ -1168,6 +1181,11 @@ export class DeepClient = Link> implements DeepClientInst const name = options?.name || this.defaultInsertName; let q: any = {}; + const containerId = options?.containerId; + + let _objects = Object.prototype.toString.call(objects) === '[object Array]' ? objects : [objects]; + checkAndFillShorts(_objects, table, containerId, this.idLocal('@deep-foundation/core', 'Contain')); + const toApply: any = []; if (this.minilinks && local !== false) { const errors = []; @@ -2298,5 +2316,6 @@ export type WriteOptions = Options & { silent?: boolean; remote?: boolean; local?: boolean; + containerId?: Id; } diff --git a/imports/client_types.ts b/imports/client_types.ts index 9e603a70..a53c19e5 100644 --- a/imports/client_types.ts +++ b/imports/client_types.ts @@ -221,6 +221,8 @@ export interface MutationInputLink extends MutationInputLinkPlain { string?: { data: MutationInputValue } | string; object?: { data: MutationInputValue } | any; typed?: { data: MutationInputLink | MutationInputLink[] }; + containerId?: Id; + name?: string; } export interface MutationInputValue { link_id?: Id; diff --git a/package.json b/package.json index f36f387e..11c5e899 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@deep-foundation/deeplinks", - "version": "0.0.381", + "version": "0.0.382", "license": "Unlicense", "type": "module", "scripts": {