From 80b910dbdf61db4e99feabb1d121ee1495b4288b Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 19:50:46 +0600 Subject: [PATCH 1/8] Add fix --- imports/client.tsx | 52 ++++++++++++++++++++++------------------------ package-lock.json | 8 +++++++ package.json | 2 ++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/imports/client.tsx b/imports/client.tsx index 6f9d14b8..2ec0a714 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -13,6 +13,7 @@ import { reserve } from './reserve.js'; import Debug from 'debug'; import { corePckg } from './core.js'; import { BoolExpCan, BoolExpHandler, QueryLink, BoolExpSelector, BoolExpTree, BoolExpValue, MutationInputLink, MutationInputLinkPlain, MutationInputValue } from './client_types.js'; +import get from 'get-value'; const debug = Debug('deeplinks:client'); const log = debug.extend('log'); @@ -840,37 +841,34 @@ export class DeepClient> implements DeepClientInstance { } idLocal(start: DeepClientStartItem, ...path: DeepClientPathItem[]): number { - if (_ids?.[start]?.[path[0]]) { - return _ids[start][path[0]]; + // TODO: Remove as any everywhere here when it will be understandable why DeepClientPathItem can be boolean + let paths: Array<[DeepClientStartItem, ...DeepClientPathItem[]]> = [[start, ...path]]; + if (get(_ids, paths as any)) { + return get(_ids, paths as any); } - const containTypeLinkId = _ids['@deep-foundation/core'].Contain; - const result = this.minilinks.query({ - in: { - type_id: containTypeLinkId, - from: { - ...(typeof start === 'number' && {id: start}), - ...(typeof start === 'string' && { - string: { - value: { - _eq: start - } - } - }), - }, - ...(typeof path[0] === 'string' && { - string: { - value: { - _eq: path[0] - } - } - }), - ...(typeof path[0] === 'boolean' && {}), // TODO What should we do? + paths.forEach(path => { + if(typeof path === 'boolean') { + throw new Error('boolean path is not supported') } }) - if(result.length > 0) { - return ((result[0] as unknown) as Link).id; + + let result = {}; + + for (const [start, ...path] of paths) { + this.id(start, ...path).then(id => { + const fullPath = [start, ...path]; + result = fullPath.reduce((acc, key, index, arr) => { + if (index === arr.length - 1) { + acc[key as any] = id; + return acc[key as any]; + } + return acc[key as any] = acc[key as any] || {}; + }, result); + }); } - throw new Error(`Id not found by [${JSON.stringify([start, ...path])}]`); + + return result as number; + }; async guest(options: DeepClientGuestOptions = {}): Promise { diff --git a/package-lock.json b/package-lock.json index 31baf244..a6ba640f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,7 @@ "electron": "^13.1.9", "fix-path": "^3.0.0", "get-port": "^5.1.1", + "get-value": "^3.0.1", "gists": "^2.0.0", "graphql": "^15.8.0", "graphql-playground-middleware-express": "^1.7.23", @@ -66,6 +67,7 @@ "@testing-library/react": "^14.0.0", "@types/debug": "^4.1.8", "@types/fs-extra": "^11.0.1", + "@types/get-value": "^3.0.3", "clean-webpack-plugin": "^4.0.0", "fs-extra": "^11.1.1", "jest": "^29.5.0", @@ -3329,6 +3331,12 @@ "@types/node": "*" } }, + "node_modules/@types/get-value": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@types/get-value/-/get-value-3.0.3.tgz", + "integrity": "sha512-jsTl/XtZumQfYgn50a0fPkpIyF2xvpsyzZmSLrrwXR8NWiDlw4N5XJiGQYrLckZGGUvbIr920Hz2wqmH1ZfjlQ==", + "dev": true + }, "node_modules/@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", diff --git a/package.json b/package.json index f7cb709f..ceb041de 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "electron": "^13.1.9", "fix-path": "^3.0.0", "get-port": "^5.1.1", + "get-value": "^3.0.1", "gists": "^2.0.0", "graphql": "^15.8.0", "graphql-playground-middleware-express": "^1.7.23", @@ -96,6 +97,7 @@ "@testing-library/react": "^14.0.0", "@types/debug": "^4.1.8", "@types/fs-extra": "^11.0.1", + "@types/get-value": "^3.0.3", "clean-webpack-plugin": "^4.0.0", "fs-extra": "^11.1.1", "jest": "^29.5.0", From 711dbd046dfdd8383652c95f335147bef2803094 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 20:31:11 +0600 Subject: [PATCH 2/8] id -> idLocal --- imports/client.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imports/client.tsx b/imports/client.tsx index 2ec0a714..abd21337 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -855,7 +855,7 @@ export class DeepClient> implements DeepClientInstance { let result = {}; for (const [start, ...path] of paths) { - this.id(start, ...path).then(id => { + this.idLocal(start, ...path).then(id => { const fullPath = [start, ...path]; result = fullPath.reduce((acc, key, index, arr) => { if (index === arr.length - 1) { From 7e112f18286b3d3941031222ef78078af206a983 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 22:12:12 +0600 Subject: [PATCH 3/8] Remove then --- imports/client.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/imports/client.tsx b/imports/client.tsx index abd21337..98ffbfde 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -855,8 +855,8 @@ export class DeepClient> implements DeepClientInstance { let result = {}; for (const [start, ...path] of paths) { - this.idLocal(start, ...path).then(id => { - const fullPath = [start, ...path]; + const id = this.idLocal(start, ...path) + const fullPath = [start, ...path]; result = fullPath.reduce((acc, key, index, arr) => { if (index === arr.length - 1) { acc[key as any] = id; @@ -864,7 +864,6 @@ export class DeepClient> implements DeepClientInstance { } return acc[key as any] = acc[key as any] || {}; }, result); - }); } return result as number; From 47be0a78ef2b9602869c421e133a7325cab50bad Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 22:12:26 +0600 Subject: [PATCH 4/8] Update comment --- imports/client.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imports/client.tsx b/imports/client.tsx index 98ffbfde..ad9fca8b 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -841,7 +841,7 @@ export class DeepClient> implements DeepClientInstance { } idLocal(start: DeepClientStartItem, ...path: DeepClientPathItem[]): number { - // TODO: Remove as any everywhere here when it will be understandable why DeepClientPathItem can be boolean + // TODO: Remove "as any" everywhere here when it will be understandable why DeepClientPathItem can be boolean let paths: Array<[DeepClientStartItem, ...DeepClientPathItem[]]> = [[start, ...path]]; if (get(_ids, paths as any)) { return get(_ids, paths as any); From 1594d2d39a61d628868b7a06af999492d885a9be Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 22:14:53 +0600 Subject: [PATCH 5/8] Update `idLocal get from minilinks ` test --- tests/client.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/client.tsx b/tests/client.tsx index a65b3b5a..9975e9ab 100644 --- a/tests/client.tsx +++ b/tests/client.tsx @@ -266,8 +266,9 @@ describe('client', () => { }, {returning: deepClient.selectReturning}); deepClient.minilinks.apply([packageLink,containLink,newTypeTypeLink]); try { + const packageLinkId = deepClient.idLocal(packageName); + assert.equal(packageLinkId, packageLink.id) const newTypeTypeLinkId = deepClient.idLocal(packageName, "Type"); - assert.notEqual(newTypeTypeLinkId, 0); assert.equal(newTypeTypeLinkId, newTypeTypeLink.id); } finally { await deepClient.delete([packageLink.id, newTypeTypeLink.id, containLink.id]) From dc56c62a71d23dab3fd0bc1c8eb3711ab44466d8 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 22:19:30 +0600 Subject: [PATCH 6/8] Copy idLocal from id --- imports/client.tsx | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/imports/client.tsx b/imports/client.tsx index ad9fca8b..dec937d6 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -854,17 +854,15 @@ export class DeepClient> implements DeepClientInstance { let result = {}; - for (const [start, ...path] of paths) { - const id = this.idLocal(start, ...path) - const fullPath = [start, ...path]; - result = fullPath.reduce((acc, key, index, arr) => { - if (index === arr.length - 1) { - acc[key as any] = id; - return acc[key as any]; - } - return acc[key as any] = acc[key as any] || {}; - }, result); - } + const appendPath = (accumulator, keys, value) => { + const lastKey = keys.pop(); + const lastObject = keys.reduce((obj, key) => obj[key] = obj[key] || {}, accumulator); + lastObject[lastKey] = value; + }; + paths.map(([start, ...path]) => { + const id = this.idLocal(start, ...path); + appendPath(result, [start, ...path], id); + }); return result as number; From f15220200f95b2c21f714b4946aeeebc71448875 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 22:34:59 +0600 Subject: [PATCH 7/8] Use for loop for idLocal --- imports/client.tsx | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/imports/client.tsx b/imports/client.tsx index dec937d6..70df556e 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -841,31 +841,21 @@ export class DeepClient> implements DeepClientInstance { } idLocal(start: DeepClientStartItem, ...path: DeepClientPathItem[]): number { - // TODO: Remove "as any" everywhere here when it will be understandable why DeepClientPathItem can be boolean - let paths: Array<[DeepClientStartItem, ...DeepClientPathItem[]]> = [[start, ...path]]; - if (get(_ids, paths as any)) { - return get(_ids, paths as any); + const paths = [start, ...path] as [DeepClientStartItem, ...Array>]; + if (get(_ids, paths.join('.'))) { + return get(_ids, paths.join('.')); } - paths.forEach(path => { - if(typeof path === 'boolean') { - throw new Error('boolean path is not supported') - } - }) - let result = {}; - - const appendPath = (accumulator, keys, value) => { - const lastKey = keys.pop(); - const lastObject = keys.reduce((obj, key) => obj[key] = obj[key] || {}, accumulator); - lastObject[lastKey] = value; - }; - paths.map(([start, ...path]) => { - const id = this.idLocal(start, ...path); - appendPath(result, [start, ...path], id); - }); - - return result as number; + let result = paths[0]; + for (let i = 1; i < paths.length; i++) { + result = this.idLocal(result, paths[i] as Exclude); + } + if(!result) { + throw new Error(`Id not found by ${JSON.stringify([start, ...path])}`); + } else { + return result as number + } }; async guest(options: DeepClientGuestOptions = {}): Promise { From 0853faa85ca1e764782298c598ade35846a2c91f Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Thu, 7 Sep 2023 22:40:05 +0600 Subject: [PATCH 8/8] Use _id --- imports/client.tsx | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/imports/client.tsx b/imports/client.tsx index 70df556e..0300a35c 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -846,11 +846,23 @@ export class DeepClient> implements DeepClientInstance { return get(_ids, paths.join('.')); } - let result = paths[0]; - for (let i = 1; i < paths.length; i++) { - result = this.idLocal(result, paths[i] as Exclude); - } - + // let result: number; + // if(paths.length === 1) { + + // } else { + // result = paths[0] as number; + // for (let i = 1; i < paths.length; i++) { + // result = this.idLocal(result, paths[i] as Exclude); + // } + // } + + const [link] = this.minilinks.query({ + id: { + _id: paths + } + }) + const result = (link as Link).id; + if(!result) { throw new Error(`Id not found by ${JSON.stringify([start, ...path])}`); } else {