@sneat/dto 0.27.16 → 0.27.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,9 +23,19 @@ export const addRelatedItem = (related, key, rolesOfItem) => {
23
23
  let collectionRelated = related[key.module] || {};
24
24
  let relatedItems = collectionRelated[key.collection] || {};
25
25
  if (!hasRelated(related, key)) {
26
+ const storedKey = relatedStorageKey(relatedItems, key);
27
+ const parent = relatedItems[storedKey];
26
28
  relatedItems = {
27
29
  ...relatedItems,
28
- [key.itemID]: { rolesOfItem },
30
+ [storedKey]: key.subPath
31
+ ? {
32
+ ...parent,
33
+ subPaths: { ...parent?.subPaths, [key.subPath]: { rolesOfItem } },
34
+ }
35
+ : {
36
+ ...parent,
37
+ rolesOfItem: rolesOfItem ?? (parent?.subPaths ? {} : undefined),
38
+ },
29
39
  };
30
40
  collectionRelated = {
31
41
  ...collectionRelated,
@@ -49,7 +59,29 @@ export const removeRelatedItem = (related, key) => {
49
59
  }
50
60
  if (hasRelated(related, key)) {
51
61
  const collectionItems = { ...relatedItems };
52
- delete collectionItems[key.itemID];
62
+ const storedKey = relatedStorageKey(relatedItems, key);
63
+ const parent = collectionItems[storedKey];
64
+ if (key.subPath && parent?.subPaths) {
65
+ const subPaths = { ...parent.subPaths };
66
+ delete subPaths[key.subPath];
67
+ const { subPaths: previousPaths, ...documentLink } = parent;
68
+ void previousPaths;
69
+ if (Object.keys(subPaths).length) {
70
+ collectionItems[storedKey] = { ...documentLink, subPaths };
71
+ }
72
+ else if (documentLink.rolesOfItem || documentLink.rolesToItem) {
73
+ collectionItems[storedKey] = documentLink;
74
+ }
75
+ else {
76
+ delete collectionItems[storedKey];
77
+ }
78
+ }
79
+ else if (parent?.subPaths && Object.keys(parent.subPaths).length) {
80
+ collectionItems[storedKey] = { subPaths: parent.subPaths };
81
+ }
82
+ else {
83
+ delete collectionItems[storedKey];
84
+ }
53
85
  collectionRelated = {
54
86
  ...collectionRelated,
55
87
  [key.collection]: collectionItems,
@@ -69,7 +101,8 @@ export const getRelatedItemByIDs = (relatedItems, itemID, spaceID) => relatedIte
69
101
  export const getRelatedItemByKey = (related, key) => {
70
102
  const items = related?.[key.module]?.[key.collection];
71
103
  const { itemID, spaceID } = key;
72
- return getRelatedItemByIDs(items, itemID, spaceID);
104
+ const item = getRelatedItemByIDs(items, itemID, spaceID);
105
+ return key.subPath ? item?.subPaths?.[key.subPath] : documentRelationship(item);
73
106
  };
74
107
  export const getRelatedItemIDs = (related, module, collection, spaceID) => {
75
108
  if (!related) {
@@ -78,7 +111,11 @@ export const getRelatedItemIDs = (related, module, collection, spaceID) => {
78
111
  // console.log('getRelatedItemIDs', module, collection, spaceID, related);
79
112
  const collectionRelated = (related || {})[module] || {};
80
113
  const relatedItems = collectionRelated[collection];
81
- const keys = Object.keys(relatedItems);
114
+ // A subPaths-only entry shares its storage key with the containing document
115
+ // without being a relationship to the document itself (see
116
+ // `documentRelationship`); exclude it here so this list stays consistent
117
+ // with `hasRelated`/`getRelatedItemByKey` for the same, no-subPath key.
118
+ const keys = Object.keys(relatedItems).filter((k) => documentRelationship(relatedItems[k]));
82
119
  return spaceID
83
120
  ? keys.filter((k) => !k.includes('@') || k.endsWith(`@${spaceID}`))
84
121
  : keys;
@@ -93,6 +130,16 @@ export const hasRelated = (related, key) => {
93
130
  };
94
131
  const hasRelatedItem = (relatedItems, itemKey) => {
95
132
  const { itemID, spaceID } = itemKey;
96
- return !!getRelatedItemByIDs(relatedItems, itemID, spaceID);
133
+ const item = getRelatedItemByIDs(relatedItems, itemID, spaceID);
134
+ return !!(itemKey.subPath
135
+ ? item?.subPaths?.[itemKey.subPath]
136
+ : documentRelationship(item));
137
+ };
138
+ const documentRelationship = (item) => item?.subPaths && !item.rolesOfItem && !item.rolesToItem ? undefined : item;
139
+ const relatedStorageKey = (items, key) => {
140
+ if (items[key.itemID])
141
+ return key.itemID;
142
+ const qualified = getLongRelatedItemID(key.itemID, key.spaceID);
143
+ return items[qualified] || key.subPath ? qualified : key.itemID;
97
144
  };
98
145
  //# sourceMappingURL=with-related.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"with-related.js","sourceRoot":"","sources":["../../../../../libs/dto/src/lib/with-related.ts"],"names":[],"mappings":"AA8CA,MAAM,UAAU,eAAe,CAC7B,QAAgB,EAChB,YAAoB,EACpB,OAAyB;IAEzB,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,IAAI,EAAE,CAAC;AAC9D,CAAC;AAMD,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;IACT,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,EAAE,EAAE;QAClD,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE;YAChD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;YACT,CAAC;YACD,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE;gBACzC,IAAI,CAAC,MAAM,EAAE,CAAC;oBACZ,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;gBACvC,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAMD,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,OAAoC,EACpC,GAAwB,EACxB,WAAgC,EAChC,EAAE;IACF,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;IACxB,IAAI,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAClD,IAAI,YAAY,GAAG,iBAAiB,CAAC,GA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AI,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACvC,OAAO,OAAO;QACZ,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,OAAO,EAAE,CAAC,CAAC;QACnE,CAAC,CAAC,IAAI,CAAC;AACX,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,OAAoC,EACpC,GAAwB,EACf,EAAE;IACX,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,iBAAiB,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5D,MAAM,YAAY,GAAG,iBAAiB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACvD,OAAO,cAAc,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;AAC3C,CAAC,CAAC;AAEF,MAAM,cAAc,GAAG,CACrB,YAA2B,EAC3B,OAAwB,EACf,EAAE;IACX,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACpC,OAAO,CAAC,CAAC,mBAAmB,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAC9D,CAAC,CAAC","sourcesContent":["import { IWithCreatedShort } from './dto-with-modified';\n\nexport interface IRelatedItemKey {\n readonly itemID: string;\n readonly spaceID?: string;\n}\n\n// specscore: decisions/0002-reserved-extension-space-ids\n// See sneat-specs Decision 0002:\n// https://github.com/sneat-co/sneat-specs/blob/main/spec/decisions/0002-reserved-extension-space-ids.md\nexport interface ISpaceModuleItemRef {\n readonly module: string;\n readonly collection: string;\n // spaceID is optional: an empty/undefined spaceID denotes the spaceless\n // system namespace (record stored at /ext/{ext-id}/...). A non-empty spaceID\n // denotes a space-bound record at /spaces/{space-id}/ext/{ext-id}/...\n // (sneat-specs Decision 0002).\n readonly spaceID?: string;\n readonly itemID: string;\n}\n\nexport interface IRelationshipRole {\n readonly created: IWithCreatedShort;\n}\n\nexport type WritableRelationshipRoles = Record<string, IRelationshipRole>;\n// {\n// \t// -readonly [K in keyof IRelationshipRoles]: IRelationshipRoles[K];\n// };\nexport type IRelationshipRoles = Readonly<WritableRelationshipRoles>;\n\nexport interface IRelatedItem {\n // readonly keys: readonly IRelatedItemKey[];\n readonly rolesOfItem?: IRelationshipRoles; // if related item is a child of the current record, then rolesOfItem = {\"child\": ...}\n readonly rolesToItem?: IRelationshipRoles; // if related item is a child of the current contact, then rolesToItem = {\"parent\": ...}\n}\n\nexport type IRelatedItems = Readonly<Record<string, IRelatedItem>>;\nexport type IRelatedCollections = Readonly<Record<string, IRelatedItems>>;\nexport type IRelatedModules = Readonly<Record<string, IRelatedCollections>>;\n\nexport interface IRelatedTo extends IWithRelatedOnly {\n readonly key: ISpaceModuleItemRef;\n readonly title: string; // pass empty string if you don't want to display name\n}\n\nexport function getRelatedItems(\n moduleId: string,\n collectionId: string,\n related?: IRelatedModules,\n): IRelatedItems {\n return (related && related[moduleId]?.[collectionId]) || {};\n}\n\nexport interface IWithRelatedOnly {\n readonly related?: IRelatedModules;\n}\n\nexport function validateRelated(related?: IRelatedModules): void {\n if (!related) {\n return;\n }\n Object.entries(related).forEach(([, collections]) => {\n Object.entries(collections).forEach(([, items]) => {\n if (!items) {\n return;\n }\n Object.entries(items).forEach(([itemID]) => {\n if (!itemID) {\n throw new Error('ItemID is not set');\n }\n });\n });\n });\n}\n\nexport interface IWithRelatedAndRelatedIDs extends IWithRelatedOnly {\n readonly relatedIDs?: readonly string[];\n}\n\nexport const addRelatedItem = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n rolesOfItem?: IRelationshipRoles,\n) => {\n related = related || {};\n let collectionRelated = related[key.module] || {};\n let relatedItems = collectionRelated[key.collection] || {};\n if (!hasRelated(related, key)) {\n relatedItems = {\n ...relatedItems,\n [key.itemID]: { rolesOfItem },\n };\n collectionRelated = {\n ...collectionRelated,\n [key.collection]: relatedItems,\n };\n related = { ...related, [key.module]: collectionRelated };\n }\n return related;\n};\n\nexport const removeRelatedItem = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n) => {\n if (!related) {\n return related;\n }\n let collectionRelated = related[key.module];\n if (!collectionRelated) {\n return related;\n }\n const relatedItems = collectionRelated[key.collection];\n if (!relatedItems) {\n return related;\n }\n if (hasRelated(related, key)) {\n const collectionItems = { ...relatedItems };\n delete collectionItems[key.itemID];\n collectionRelated = {\n ...collectionRelated,\n [key.collection]: collectionItems,\n };\n related = { ...related, [key.module]: collectionRelated };\n }\n return related;\n};\n\n// specscore: decisions/0002-reserved-extension-space-ids\n// Omit the \"@{spaceID}\" suffix for the spaceless system namespace. See\n// sneat-specs Decision 0002:\n// https://github.com/sneat-co/sneat-specs/blob/main/spec/decisions/0002-reserved-extension-space-ids.md\nexport const getLongRelatedItemID = (itemID: string, spaceID?: string) =>\n spaceID ? `${itemID}@${spaceID}` : itemID;\n\nexport const getRelatedItemByIDs = (\n relatedItems: Readonly<Record<string, IRelatedItem>> | undefined,\n itemID: string,\n spaceID?: string,\n) =>\n relatedItems &&\n (relatedItems[itemID] ||\n (spaceID && relatedItems[getLongRelatedItemID(itemID, spaceID)]));\n\nexport const getRelatedItemByKey = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n): IRelatedItem | undefined => {\n const items = related?.[key.module]?.[key.collection];\n const { itemID, spaceID } = key;\n return getRelatedItemByIDs(items, itemID, spaceID);\n};\n\nexport const getRelatedItemIDs = (\n related: IRelatedModules | undefined,\n module: string,\n collection: string,\n spaceID?: string,\n): readonly string[] => {\n if (!related) {\n return [];\n }\n // console.log('getRelatedItemIDs', module, collection, spaceID, related);\n const collectionRelated = (related || {})[module] || {};\n const relatedItems = collectionRelated[collection];\n const keys = Object.keys(relatedItems);\n return spaceID\n ? keys.filter((k) => !k.includes('@') || k.endsWith(`@${spaceID}`))\n : keys;\n};\n\nexport const hasRelated = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n): boolean => {\n if (!related) {\n return false;\n }\n const collectionRelated = (related || {})[key.module] || {};\n const relatedItems = collectionRelated[key.collection];\n return hasRelatedItem(relatedItems, key);\n};\n\nconst hasRelatedItem = (\n relatedItems: IRelatedItems,\n itemKey: IRelatedItemKey,\n): boolean => {\n const { itemID, spaceID } = itemKey;\n return !!getRelatedItemByIDs(relatedItems, itemID, spaceID);\n};\n"]}
1
+ {"version":3,"file":"with-related.js","sourceRoot":"","sources":["../../../../../libs/dto/src/lib/with-related.ts"],"names":[],"mappings":"AAqDA,MAAM,UAAU,eAAe,CAC7B,QAAgB,EAChB,YAAoB,EACpB,OAAyB;IAEzB,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,IAAI,EAAE,CAAC;AAC9D,CAAC;AAMD,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;IACT,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,EAAE,EAAE;QAClD,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE;YAChD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;YACT,CAAC;YACD,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE;gBACzC,IAAI,CAAC,MAAM,EAAE,CAAC;oBACZ,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;gBACvC,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CA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AAM,CAAC;AAClE,CAAC,CAAC","sourcesContent":["import { IWithCreatedShort } from './dto-with-modified';\n\nexport interface IRelatedItemKey {\n readonly itemID: string;\n readonly spaceID?: string;\n readonly subPath?: string;\n}\n\n// specscore: decisions/0002-reserved-extension-space-ids\n// See sneat-specs Decision 0002:\n// https://github.com/sneat-co/sneat-specs/blob/main/spec/decisions/0002-reserved-extension-space-ids.md\nexport interface ISpaceModuleItemRef {\n readonly module: string;\n readonly collection: string;\n // spaceID is optional: an empty/undefined spaceID denotes the spaceless\n // system namespace (record stored at /ext/{ext-id}/...). A non-empty spaceID\n // denotes a space-bound record at /spaces/{space-id}/ext/{ext-id}/...\n // (sneat-specs Decision 0002).\n readonly spaceID?: string;\n readonly itemID: string;\n // Optional stable-key path inside the document, e.g. /items/@id=item-123.\n // The owner resolves and authorizes it; array positions are never identities.\n readonly subPath?: string;\n}\n\nexport interface IRelationshipRole {\n readonly created: IWithCreatedShort;\n}\n\nexport type WritableRelationshipRoles = Record<string, IRelationshipRole>;\n// {\n// \t// -readonly [K in keyof IRelationshipRoles]: IRelationshipRoles[K];\n// };\nexport type IRelationshipRoles = Readonly<WritableRelationshipRoles>;\n\nexport interface IRelatedItem {\n // readonly keys: readonly IRelatedItemKey[];\n readonly rolesOfItem?: IRelationshipRoles; // if related item is a child of the current record, then rolesOfItem = {\"child\": ...}\n readonly rolesToItem?: IRelationshipRoles; // if related item is a child of the current contact, then rolesToItem = {\"parent\": ...}\n // Embedded relationships share the containing document key without being\n // mistaken for a relationship to the document itself.\n readonly subPaths?: Readonly<Record<string, IRelatedItem>>;\n}\n\nexport type IRelatedItems = Readonly<Record<string, IRelatedItem>>;\nexport type IRelatedCollections = Readonly<Record<string, IRelatedItems>>;\nexport type IRelatedModules = Readonly<Record<string, IRelatedCollections>>;\n\nexport interface IRelatedTo extends IWithRelatedOnly {\n readonly key: ISpaceModuleItemRef;\n readonly title: string; // pass empty string if you don't want to display name\n}\n\nexport function getRelatedItems(\n moduleId: string,\n collectionId: string,\n related?: IRelatedModules,\n): IRelatedItems {\n return (related && related[moduleId]?.[collectionId]) || {};\n}\n\nexport interface IWithRelatedOnly {\n readonly related?: IRelatedModules;\n}\n\nexport function validateRelated(related?: IRelatedModules): void {\n if (!related) {\n return;\n }\n Object.entries(related).forEach(([, collections]) => {\n Object.entries(collections).forEach(([, items]) => {\n if (!items) {\n return;\n }\n Object.entries(items).forEach(([itemID]) => {\n if (!itemID) {\n throw new Error('ItemID is not set');\n }\n });\n });\n });\n}\n\nexport interface IWithRelatedAndRelatedIDs extends IWithRelatedOnly {\n readonly relatedIDs?: readonly string[];\n}\n\nexport const addRelatedItem = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n rolesOfItem?: IRelationshipRoles,\n) => {\n related = related || {};\n let collectionRelated = related[key.module] || {};\n let relatedItems = collectionRelated[key.collection] || {};\n if (!hasRelated(related, key)) {\n const storedKey = relatedStorageKey(relatedItems, key);\n const parent = relatedItems[storedKey];\n relatedItems = {\n ...relatedItems,\n [storedKey]: key.subPath\n ? {\n ...parent,\n subPaths: { ...parent?.subPaths, [key.subPath]: { rolesOfItem } },\n }\n : {\n ...parent,\n rolesOfItem: rolesOfItem ?? (parent?.subPaths ? {} : undefined),\n },\n };\n collectionRelated = {\n ...collectionRelated,\n [key.collection]: relatedItems,\n };\n related = { ...related, [key.module]: collectionRelated };\n }\n return related;\n};\n\nexport const removeRelatedItem = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n) => {\n if (!related) {\n return related;\n }\n let collectionRelated = related[key.module];\n if (!collectionRelated) {\n return related;\n }\n const relatedItems = collectionRelated[key.collection];\n if (!relatedItems) {\n return related;\n }\n if (hasRelated(related, key)) {\n const collectionItems = { ...relatedItems };\n const storedKey = relatedStorageKey(relatedItems, key);\n const parent = collectionItems[storedKey];\n if (key.subPath && parent?.subPaths) {\n const subPaths = { ...parent.subPaths };\n delete subPaths[key.subPath];\n const { subPaths: previousPaths, ...documentLink } = parent;\n void previousPaths;\n if (Object.keys(subPaths).length) {\n collectionItems[storedKey] = { ...documentLink, subPaths };\n } else if (documentLink.rolesOfItem || documentLink.rolesToItem) {\n collectionItems[storedKey] = documentLink;\n } else {\n delete collectionItems[storedKey];\n }\n } else if (parent?.subPaths && Object.keys(parent.subPaths).length) {\n collectionItems[storedKey] = { subPaths: parent.subPaths };\n } else {\n delete collectionItems[storedKey];\n }\n collectionRelated = {\n ...collectionRelated,\n [key.collection]: collectionItems,\n };\n related = { ...related, [key.module]: collectionRelated };\n }\n return related;\n};\n\n// specscore: decisions/0002-reserved-extension-space-ids\n// Omit the \"@{spaceID}\" suffix for the spaceless system namespace. See\n// sneat-specs Decision 0002:\n// https://github.com/sneat-co/sneat-specs/blob/main/spec/decisions/0002-reserved-extension-space-ids.md\nexport const getLongRelatedItemID = (itemID: string, spaceID?: string) =>\n spaceID ? `${itemID}@${spaceID}` : itemID;\n\nexport const getRelatedItemByIDs = (\n relatedItems: Readonly<Record<string, IRelatedItem>> | undefined,\n itemID: string,\n spaceID?: string,\n) =>\n relatedItems &&\n (relatedItems[itemID] ||\n (spaceID && relatedItems[getLongRelatedItemID(itemID, spaceID)]));\n\nexport const getRelatedItemByKey = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n): IRelatedItem | undefined => {\n const items = related?.[key.module]?.[key.collection];\n const { itemID, spaceID } = key;\n const item = getRelatedItemByIDs(items, itemID, spaceID);\n return key.subPath ? item?.subPaths?.[key.subPath] : documentRelationship(item);\n};\n\nexport const getRelatedItemIDs = (\n related: IRelatedModules | undefined,\n module: string,\n collection: string,\n spaceID?: string,\n): readonly string[] => {\n if (!related) {\n return [];\n }\n // console.log('getRelatedItemIDs', module, collection, spaceID, related);\n const collectionRelated = (related || {})[module] || {};\n const relatedItems = collectionRelated[collection];\n // A subPaths-only entry shares its storage key with the containing document\n // without being a relationship to the document itself (see\n // `documentRelationship`); exclude it here so this list stays consistent\n // with `hasRelated`/`getRelatedItemByKey` for the same, no-subPath key.\n const keys = Object.keys(relatedItems).filter((k) =>\n documentRelationship(relatedItems[k]),\n );\n return spaceID\n ? keys.filter((k) => !k.includes('@') || k.endsWith(`@${spaceID}`))\n : keys;\n};\n\nexport const hasRelated = (\n related: IRelatedModules | undefined,\n key: ISpaceModuleItemRef,\n): boolean => {\n if (!related) {\n return false;\n }\n const collectionRelated = (related || {})[key.module] || {};\n const relatedItems = collectionRelated[key.collection];\n return hasRelatedItem(relatedItems, key);\n};\n\nconst hasRelatedItem = (\n relatedItems: IRelatedItems,\n itemKey: IRelatedItemKey,\n): boolean => {\n const { itemID, spaceID } = itemKey;\n const item = getRelatedItemByIDs(relatedItems, itemID, spaceID);\n return !!(itemKey.subPath\n ? item?.subPaths?.[itemKey.subPath]\n : documentRelationship(item));\n};\n\nconst documentRelationship = (item: IRelatedItem | undefined) =>\n item?.subPaths && !item.rolesOfItem && !item.rolesToItem ? undefined : item;\n\nconst relatedStorageKey = (items: IRelatedItems, key: IRelatedItemKey): string => {\n if (items[key.itemID]) return key.itemID;\n const qualified = getLongRelatedItemID(key.itemID, key.spaceID);\n return items[qualified] || key.subPath ? qualified : key.itemID;\n};\n"]}
@@ -2,12 +2,14 @@ import { IWithCreatedShort } from './dto-with-modified';
2
2
  export interface IRelatedItemKey {
3
3
  readonly itemID: string;
4
4
  readonly spaceID?: string;
5
+ readonly subPath?: string;
5
6
  }
6
7
  export interface ISpaceModuleItemRef {
7
8
  readonly module: string;
8
9
  readonly collection: string;
9
10
  readonly spaceID?: string;
10
11
  readonly itemID: string;
12
+ readonly subPath?: string;
11
13
  }
12
14
  export interface IRelationshipRole {
13
15
  readonly created: IWithCreatedShort;
@@ -17,6 +19,7 @@ export type IRelationshipRoles = Readonly<WritableRelationshipRoles>;
17
19
  export interface IRelatedItem {
18
20
  readonly rolesOfItem?: IRelationshipRoles;
19
21
  readonly rolesToItem?: IRelationshipRoles;
22
+ readonly subPaths?: Readonly<Record<string, IRelatedItem>>;
20
23
  }
21
24
  export type IRelatedItems = Readonly<Record<string, IRelatedItem>>;
22
25
  export type IRelatedCollections = Readonly<Record<string, IRelatedItems>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sneat/dto",
3
- "version": "0.27.16",
3
+ "version": "0.27.17",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },