industrial-model 0.4.0 → 0.6.0
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.
- package/README.md +571 -537
- package/dist/cognite-core/index.cjs +1516 -0
- package/dist/cognite-core/index.cjs.map +1 -0
- package/dist/cognite-core/index.d.cts +532 -0
- package/dist/cognite-core/index.d.ts +532 -0
- package/dist/cognite-core/index.js +1513 -0
- package/dist/cognite-core/index.js.map +1 -0
- package/dist/index.cjs +260 -216
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -256
- package/dist/index.d.ts +3 -256
- package/dist/index.js +261 -215
- package/dist/index.js.map +1 -1
- package/dist/types-DCP5GMi3.d.cts +216 -0
- package/dist/types-DCP5GMi3.d.ts +216 -0
- package/package.json +11 -1
package/dist/index.cjs
CHANGED
|
@@ -28,6 +28,13 @@ var CogniteSdkAdapter = class {
|
|
|
28
28
|
nextCursor: response.nextCursor
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
|
+
async searchInstances(request) {
|
|
32
|
+
const search = this.client.instances.search;
|
|
33
|
+
const response = await search(request);
|
|
34
|
+
return {
|
|
35
|
+
items: response.items
|
|
36
|
+
};
|
|
37
|
+
}
|
|
31
38
|
async aggregateInstances(request) {
|
|
32
39
|
const response = await this.client.instances.aggregate(
|
|
33
40
|
request
|
|
@@ -47,7 +54,108 @@ var MAX_DEPENDENCY_DEPTH = 3;
|
|
|
47
54
|
var AGGREGATE_LIMIT = 1e3;
|
|
48
55
|
var MAX_GROUP_BY = 5;
|
|
49
56
|
|
|
50
|
-
// src/
|
|
57
|
+
// src/utils/query.ts
|
|
58
|
+
function mapNodesAndEdges(queryResult, _query) {
|
|
59
|
+
return queryResult.items;
|
|
60
|
+
}
|
|
61
|
+
function appendNodesAndEdges(initial, additional) {
|
|
62
|
+
if (!additional) return initial;
|
|
63
|
+
for (const [key, items] of Object.entries(additional)) {
|
|
64
|
+
const existing = initial[key];
|
|
65
|
+
if (existing) {
|
|
66
|
+
existing.push(...items);
|
|
67
|
+
} else {
|
|
68
|
+
initial[key] = [...items];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return initial;
|
|
72
|
+
}
|
|
73
|
+
function getQueryForDependenciesPagination(query, queryResult, viewExternalId) {
|
|
74
|
+
const cursorKeys = new Set(Object.keys(queryResult.nextCursor));
|
|
75
|
+
const { nodesParent, nodesChildren } = getParentAndChildrenNodes(cursorKeys);
|
|
76
|
+
const leafCursors = getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren);
|
|
77
|
+
if (Object.keys(leafCursors).length === 0) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return buildDependenciesQuery(query, nodesParent, nodesChildren, leafCursors);
|
|
81
|
+
}
|
|
82
|
+
function getParentAndChildrenNodes(keys) {
|
|
83
|
+
const nodesParent = /* @__PURE__ */ new Map();
|
|
84
|
+
const nodesChildren = /* @__PURE__ */ new Map();
|
|
85
|
+
for (const key of keys) {
|
|
86
|
+
const keyParts = key.split(NESTED_SEP);
|
|
87
|
+
const validParents = /* @__PURE__ */ new Set();
|
|
88
|
+
for (let i = keyParts.length - 1; i > 0; i--) {
|
|
89
|
+
const parentPath = keyParts.slice(0, i).join(NESTED_SEP);
|
|
90
|
+
const parentWithEdgeMarker = `${parentPath}${NESTED_SEP}${EDGE_MARKER}`;
|
|
91
|
+
if (keys.has(parentWithEdgeMarker)) {
|
|
92
|
+
validParents.add(parentWithEdgeMarker);
|
|
93
|
+
const children = nodesChildren.get(parentWithEdgeMarker) ?? /* @__PURE__ */ new Set();
|
|
94
|
+
children.add(key);
|
|
95
|
+
nodesChildren.set(parentWithEdgeMarker, children);
|
|
96
|
+
}
|
|
97
|
+
if (keys.has(parentPath)) {
|
|
98
|
+
validParents.add(parentPath);
|
|
99
|
+
const children = nodesChildren.get(parentPath) ?? /* @__PURE__ */ new Set();
|
|
100
|
+
children.add(key);
|
|
101
|
+
nodesChildren.set(parentPath, children);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
nodesParent.set(key, validParents);
|
|
105
|
+
}
|
|
106
|
+
return { nodesParent, nodesChildren };
|
|
107
|
+
}
|
|
108
|
+
function getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren) {
|
|
109
|
+
const targetCursors = {};
|
|
110
|
+
const targetCursorKeys = /* @__PURE__ */ new Set();
|
|
111
|
+
for (const [cursorKey, cursorValue] of Object.entries(queryResult.nextCursor)) {
|
|
112
|
+
if (cursorKey === viewExternalId || !cursorValue || (queryResult.items[cursorKey]?.length ?? 0) !== MAX_LIMIT) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
116
|
+
let skipDueToChild = false;
|
|
117
|
+
for (const c of children) {
|
|
118
|
+
if (targetCursorKeys.has(c)) {
|
|
119
|
+
skipDueToChild = true;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (skipDueToChild) continue;
|
|
124
|
+
const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
125
|
+
for (const key of parent) {
|
|
126
|
+
if (targetCursorKeys.has(key)) {
|
|
127
|
+
delete targetCursors[key];
|
|
128
|
+
targetCursorKeys.delete(key);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
targetCursors[cursorKey] = cursorValue;
|
|
132
|
+
targetCursorKeys.add(cursorKey);
|
|
133
|
+
}
|
|
134
|
+
return targetCursors;
|
|
135
|
+
}
|
|
136
|
+
function buildDependenciesQuery(previousQuery, nodesParent, nodesChildren, leafCursors) {
|
|
137
|
+
const withExprs = {};
|
|
138
|
+
const selectExprs = {};
|
|
139
|
+
const finalCursors = {};
|
|
140
|
+
for (const [cursorKey, cursorValue] of Object.entries(leafCursors)) {
|
|
141
|
+
const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
142
|
+
const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
143
|
+
const validKeys = /* @__PURE__ */ new Set([...parent, ...children, cursorKey]);
|
|
144
|
+
for (const [k, v] of Object.entries(previousQuery.with)) {
|
|
145
|
+
if (validKeys.has(k)) withExprs[k] = v;
|
|
146
|
+
}
|
|
147
|
+
for (const [k, v] of Object.entries(previousQuery.select)) {
|
|
148
|
+
if (validKeys.has(k)) selectExprs[k] = v;
|
|
149
|
+
}
|
|
150
|
+
for (const [k, v] of Object.entries(previousQuery.cursors ?? {})) {
|
|
151
|
+
if (parent.has(k) && v) finalCursors[k] = v;
|
|
152
|
+
}
|
|
153
|
+
finalCursors[cursorKey] = cursorValue;
|
|
154
|
+
}
|
|
155
|
+
return { with: withExprs, select: selectExprs, cursors: finalCursors };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/utils/view.ts
|
|
51
159
|
var NODE_PROPERTIES = /* @__PURE__ */ new Set([
|
|
52
160
|
"externalId",
|
|
53
161
|
"space",
|
|
@@ -111,8 +219,6 @@ function isNumericProperty(property) {
|
|
|
111
219
|
function getSelectedGroupByKeys(groupBy) {
|
|
112
220
|
return Object.entries(groupBy).filter((entry) => entry[1] === true).map(([key]) => key);
|
|
113
221
|
}
|
|
114
|
-
|
|
115
|
-
// src/validation.ts
|
|
116
222
|
var nodeIdSchema = zod.z.object({
|
|
117
223
|
space: zod.z.string().min(1),
|
|
118
224
|
externalId: zod.z.string().min(1)
|
|
@@ -161,17 +267,8 @@ function propertyValueSchema(property, options = {}) {
|
|
|
161
267
|
}
|
|
162
268
|
return type.list === true ? zod.z.array(schema) : schema;
|
|
163
269
|
}
|
|
164
|
-
function buildViewSchema(view, options = {}) {
|
|
165
|
-
const shape = {};
|
|
166
|
-
for (const [name, property] of Object.entries(view.properties)) {
|
|
167
|
-
if (isViewPropertyDefinition(property)) {
|
|
168
|
-
shape[name] = propertyValueSchema(property, options).optional();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return zod.z.object(shape).strict();
|
|
172
|
-
}
|
|
173
270
|
|
|
174
|
-
// src/
|
|
271
|
+
// src/validators/query-validator.ts
|
|
175
272
|
var NODE_STRING_PROPERTIES = ["externalId", "space"];
|
|
176
273
|
var NODE_NUMBER_PROPERTIES = ["createdTime", "deletedTime", "lastUpdatedTime"];
|
|
177
274
|
var NODE_PROPERTIES2 = /* @__PURE__ */ new Set([...NODE_STRING_PROPERTIES, ...NODE_NUMBER_PROPERTIES]);
|
|
@@ -186,6 +283,7 @@ var leafOps = /* @__PURE__ */ new Set([
|
|
|
186
283
|
"lte",
|
|
187
284
|
"exists",
|
|
188
285
|
"prefix",
|
|
286
|
+
"search",
|
|
189
287
|
"containsAny",
|
|
190
288
|
"containsAll"
|
|
191
289
|
]);
|
|
@@ -238,19 +336,33 @@ function leafFilterSchema(property) {
|
|
|
238
336
|
const value = baseValueSchema(property);
|
|
239
337
|
const isList = typeof property !== "string" && property.type.list === true;
|
|
240
338
|
if (isList) {
|
|
241
|
-
|
|
339
|
+
const shape = {
|
|
242
340
|
containsAny: zod.z.array(value).optional(),
|
|
243
341
|
containsAll: zod.z.array(value).optional(),
|
|
244
342
|
exists: zod.z.boolean().optional()
|
|
245
|
-
}
|
|
343
|
+
};
|
|
344
|
+
if (property.type.type === "text") {
|
|
345
|
+
shape.search = zod.z.object({
|
|
346
|
+
query: zod.z.string(),
|
|
347
|
+
operator: zod.z.enum(["OR", "AND"]).optional()
|
|
348
|
+
}).strict().optional();
|
|
349
|
+
}
|
|
350
|
+
return zod.z.object(shape).strict();
|
|
246
351
|
}
|
|
247
352
|
if (property === "node-string" || typeof property !== "string" && property.type.type === "text") {
|
|
248
|
-
|
|
353
|
+
const shape = {
|
|
249
354
|
eq: zod.z.string().optional(),
|
|
250
355
|
in: zod.z.array(zod.z.string()).optional(),
|
|
251
356
|
prefix: zod.z.string().optional(),
|
|
252
357
|
exists: zod.z.boolean().optional()
|
|
253
|
-
}
|
|
358
|
+
};
|
|
359
|
+
if (property !== "node-string") {
|
|
360
|
+
shape.search = zod.z.object({
|
|
361
|
+
query: zod.z.string(),
|
|
362
|
+
operator: zod.z.enum(["OR", "AND"]).optional()
|
|
363
|
+
}).strict().optional();
|
|
364
|
+
}
|
|
365
|
+
return zod.z.object(shape).strict();
|
|
254
366
|
}
|
|
255
367
|
if (typeof property !== "string" && property.type.type === "enum") {
|
|
256
368
|
return zod.z.object({
|
|
@@ -367,6 +479,12 @@ ${errors.map((error) => `- ${error}`).join("\n")}`);
|
|
|
367
479
|
);
|
|
368
480
|
continue;
|
|
369
481
|
}
|
|
482
|
+
if (isReverseDirectRelation(property) && property.targetsList) {
|
|
483
|
+
errors.push(
|
|
484
|
+
`${issuePath([...path, name])}: cannot select "${name}" \u2014 Cognite does not support inward traversal of list direct relations. Query "${property.source.externalId}" directly and filter by the "${property.through.identifier}" field instead.`
|
|
485
|
+
);
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
370
488
|
const targetView = await this.viewMapper.getView(target);
|
|
371
489
|
errors.push(...await this.validateSelect(value, targetView, [...path, name]));
|
|
372
490
|
}
|
|
@@ -456,7 +574,7 @@ ${errors.map((error) => `- ${error}`).join("\n")}`);
|
|
|
456
574
|
}
|
|
457
575
|
};
|
|
458
576
|
|
|
459
|
-
// src/
|
|
577
|
+
// src/validators/aggregate-validator.ts
|
|
460
578
|
var NODE_COUNT_PROPERTIES = /* @__PURE__ */ new Set(["externalId", "space"]);
|
|
461
579
|
function issuePath2(path) {
|
|
462
580
|
return path.length === 0 ? "aggregate" : path.map(String).join(".");
|
|
@@ -592,6 +710,92 @@ ${errors.map((error) => `- ${error}`).join("\n")}`
|
|
|
592
710
|
return [];
|
|
593
711
|
}
|
|
594
712
|
};
|
|
713
|
+
var nodeMetadataSchema = {
|
|
714
|
+
instanceType: zod.z.literal("node").optional(),
|
|
715
|
+
space: zod.z.string(),
|
|
716
|
+
externalId: zod.z.string(),
|
|
717
|
+
version: zod.z.number().optional(),
|
|
718
|
+
createdTime: zod.z.number().optional(),
|
|
719
|
+
deletedTime: zod.z.number().optional(),
|
|
720
|
+
lastUpdatedTime: zod.z.number().optional(),
|
|
721
|
+
_edges: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
|
|
722
|
+
};
|
|
723
|
+
function isListRelation(property) {
|
|
724
|
+
if (isViewPropertyDefinition(property)) {
|
|
725
|
+
return isListDirectRelation(property);
|
|
726
|
+
}
|
|
727
|
+
if (isReverseDirectRelation(property)) {
|
|
728
|
+
return property.connectionType === "multi_reverse_direct_relation" || property.targetsList === true;
|
|
729
|
+
}
|
|
730
|
+
return isEdgeConnection(property);
|
|
731
|
+
}
|
|
732
|
+
function isRecord2(value) {
|
|
733
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
734
|
+
}
|
|
735
|
+
var QueryResultValidator = class {
|
|
736
|
+
constructor(viewMapper) {
|
|
737
|
+
this.viewMapper = viewMapper;
|
|
738
|
+
}
|
|
739
|
+
async parseItems(rootViewExternalId, items, select) {
|
|
740
|
+
const rootView = await this.viewMapper.getView(rootViewExternalId);
|
|
741
|
+
const schema = await this.buildResultSchema(rootView, MAX_DEPENDENCY_DEPTH, select);
|
|
742
|
+
const result = zod.z.array(schema).safeParse(items);
|
|
743
|
+
if (!result.success) {
|
|
744
|
+
throw new Error(
|
|
745
|
+
`Invalid query result:
|
|
746
|
+
${result.error.issues.map((issue) => `- ${issue.path.map(String).join(".")}: ${issue.message}`).join("\n")}`
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
return result.data;
|
|
750
|
+
}
|
|
751
|
+
async buildResultSchema(view, remainingDepth, select) {
|
|
752
|
+
const shape = { ...nodeMetadataSchema };
|
|
753
|
+
const includeAllProperties = select == null || select._all === true;
|
|
754
|
+
for (const [name, property] of Object.entries(view.properties)) {
|
|
755
|
+
const isSelected = includeAllProperties || name in select;
|
|
756
|
+
if (!isSelected) continue;
|
|
757
|
+
const nestedSelect = isRecord2(select?.[name]) ? select[name] : void 0;
|
|
758
|
+
if (isViewPropertyDefinition(property)) {
|
|
759
|
+
const relationSource = getDirectRelationSource(property);
|
|
760
|
+
if (relationSource) {
|
|
761
|
+
shape[name] = await this.buildRelationSchema(
|
|
762
|
+
property,
|
|
763
|
+
relationSource.externalId,
|
|
764
|
+
remainingDepth,
|
|
765
|
+
nestedSelect
|
|
766
|
+
);
|
|
767
|
+
} else {
|
|
768
|
+
shape[name] = propertyValueSchema(property, { dateMode: "coerce" }).optional();
|
|
769
|
+
}
|
|
770
|
+
continue;
|
|
771
|
+
}
|
|
772
|
+
if (isReverseDirectRelation(property) || isEdgeConnection(property)) {
|
|
773
|
+
shape[name] = await this.buildRelationSchema(
|
|
774
|
+
property,
|
|
775
|
+
property.source.externalId,
|
|
776
|
+
remainingDepth,
|
|
777
|
+
nestedSelect
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
const schema = zod.z.object(shape);
|
|
782
|
+
return includeAllProperties ? schema.strict() : schema;
|
|
783
|
+
}
|
|
784
|
+
async buildRelationSchema(property, targetViewExternalId, remainingDepth, select) {
|
|
785
|
+
const isList = isListRelation(property);
|
|
786
|
+
const fallbackSchema = isViewPropertyDefinition(property) ? propertyValueSchema(property, { dateMode: "coerce" }) : zod.z.unknown();
|
|
787
|
+
if (remainingDepth <= 0 || select == null) {
|
|
788
|
+
return fallbackSchema.optional();
|
|
789
|
+
}
|
|
790
|
+
const targetView = await this.viewMapper.getView(targetViewExternalId);
|
|
791
|
+
const nestedSchema = await this.buildResultSchema(targetView, remainingDepth - 1, select);
|
|
792
|
+
if (isViewPropertyDefinition(property)) {
|
|
793
|
+
const nestedRelationSchema = isList ? zod.z.array(nestedSchema) : nestedSchema;
|
|
794
|
+
return zod.z.union([nestedRelationSchema, fallbackSchema]).optional();
|
|
795
|
+
}
|
|
796
|
+
return (isList ? zod.z.array(nestedSchema) : nestedSchema).optional();
|
|
797
|
+
}
|
|
798
|
+
};
|
|
595
799
|
|
|
596
800
|
// src/mappers/filter-mapper.ts
|
|
597
801
|
var LEAF_OPS = /* @__PURE__ */ new Set([
|
|
@@ -610,11 +814,13 @@ function isLeafFilter2(value) {
|
|
|
610
814
|
return Object.keys(value).some((k) => LEAF_OPS.has(k));
|
|
611
815
|
}
|
|
612
816
|
var FilterMapper = class {
|
|
613
|
-
constructor(viewMapper) {
|
|
817
|
+
constructor(viewMapper, cognite) {
|
|
614
818
|
this.viewMapper = viewMapper;
|
|
819
|
+
this.cognite = cognite;
|
|
615
820
|
}
|
|
616
821
|
async map(input, rootView) {
|
|
617
822
|
const result = [];
|
|
823
|
+
const searchQueries = {};
|
|
618
824
|
for (const [key, value] of Object.entries(input)) {
|
|
619
825
|
if (value == null) continue;
|
|
620
826
|
if (key === "AND") {
|
|
@@ -635,17 +841,44 @@ var FilterMapper = class {
|
|
|
635
841
|
} else {
|
|
636
842
|
const filterValue = value;
|
|
637
843
|
const property = getPropertyRef(key, rootView);
|
|
638
|
-
|
|
844
|
+
const hasLeafFilter = isLeafFilter2(filterValue);
|
|
845
|
+
if (hasLeafFilter) {
|
|
639
846
|
result.push(...this.leafToFilterDefs(property, filterValue));
|
|
640
|
-
}
|
|
847
|
+
}
|
|
848
|
+
if ("search" in filterValue && filterValue.search != null) {
|
|
849
|
+
searchQueries[key] = filterValue.search;
|
|
850
|
+
} else if (!hasLeafFilter) {
|
|
641
851
|
const targetView = await this.getNestedTargetView(key, rootView);
|
|
642
852
|
const innerFilter = await this.whereInputToSingle(filterValue, targetView);
|
|
643
853
|
result.push({ nested: { scope: property, filter: innerFilter } });
|
|
644
854
|
}
|
|
645
855
|
}
|
|
646
856
|
}
|
|
857
|
+
if (Object.keys(searchQueries).length > 0) {
|
|
858
|
+
const searchFilters = await Promise.all(
|
|
859
|
+
Object.entries(searchQueries).map(
|
|
860
|
+
([key, filterValue]) => this.searchToFilterDef(key, filterValue, rootView)
|
|
861
|
+
)
|
|
862
|
+
);
|
|
863
|
+
result.push(...searchFilters);
|
|
864
|
+
}
|
|
647
865
|
return result;
|
|
648
866
|
}
|
|
867
|
+
async searchToFilterDef(propertyName, search, rootView) {
|
|
868
|
+
const response = await this.cognite.searchInstances({
|
|
869
|
+
view: toViewReference(rootView),
|
|
870
|
+
query: search.query,
|
|
871
|
+
instanceType: "node",
|
|
872
|
+
properties: [propertyName],
|
|
873
|
+
operator: search.operator ?? "OR",
|
|
874
|
+
limit: 1e3
|
|
875
|
+
});
|
|
876
|
+
const instanceRefs = response.items.map((item) => ({
|
|
877
|
+
space: item.space,
|
|
878
|
+
externalId: item.externalId
|
|
879
|
+
}));
|
|
880
|
+
return { instanceReferences: instanceRefs };
|
|
881
|
+
}
|
|
649
882
|
async whereInputToSingle(input, rootView) {
|
|
650
883
|
const filters = await this.map(input, rootView);
|
|
651
884
|
const [firstFilter, ...restFilters] = filters;
|
|
@@ -724,9 +957,9 @@ var FilterMapper = class {
|
|
|
724
957
|
|
|
725
958
|
// src/mappers/aggregate-mapper.ts
|
|
726
959
|
var AggregateMapper = class {
|
|
727
|
-
constructor(viewMapper) {
|
|
960
|
+
constructor(viewMapper, cognite) {
|
|
728
961
|
this.viewMapper = viewMapper;
|
|
729
|
-
this.filterMapper = new FilterMapper(viewMapper);
|
|
962
|
+
this.filterMapper = new FilterMapper(viewMapper, cognite);
|
|
730
963
|
this.validator = new AggregateValidator(viewMapper);
|
|
731
964
|
}
|
|
732
965
|
async map(options) {
|
|
@@ -821,9 +1054,9 @@ var SortMapper = class {
|
|
|
821
1054
|
|
|
822
1055
|
// src/mappers/query-mapper.ts
|
|
823
1056
|
var QueryMapper = class {
|
|
824
|
-
constructor(viewMapper) {
|
|
1057
|
+
constructor(viewMapper, cognite) {
|
|
825
1058
|
this.viewMapper = viewMapper;
|
|
826
|
-
this.filterMapper = new FilterMapper(viewMapper);
|
|
1059
|
+
this.filterMapper = new FilterMapper(viewMapper, cognite);
|
|
827
1060
|
this.sortMapper = new SortMapper();
|
|
828
1061
|
this.validator = new QueryValidator(viewMapper);
|
|
829
1062
|
}
|
|
@@ -1129,92 +1362,6 @@ var QueryResultMapper = class {
|
|
|
1129
1362
|
return entry;
|
|
1130
1363
|
}
|
|
1131
1364
|
};
|
|
1132
|
-
var nodeMetadataSchema = {
|
|
1133
|
-
instanceType: zod.z.literal("node").optional(),
|
|
1134
|
-
space: zod.z.string(),
|
|
1135
|
-
externalId: zod.z.string(),
|
|
1136
|
-
version: zod.z.number().optional(),
|
|
1137
|
-
createdTime: zod.z.number().optional(),
|
|
1138
|
-
deletedTime: zod.z.number().optional(),
|
|
1139
|
-
lastUpdatedTime: zod.z.number().optional(),
|
|
1140
|
-
_edges: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
|
|
1141
|
-
};
|
|
1142
|
-
function isListRelation(property) {
|
|
1143
|
-
if (isViewPropertyDefinition(property)) {
|
|
1144
|
-
return isListDirectRelation(property);
|
|
1145
|
-
}
|
|
1146
|
-
if (isReverseDirectRelation(property)) {
|
|
1147
|
-
return property.connectionType === "multi_reverse_direct_relation" || property.targetsList === true;
|
|
1148
|
-
}
|
|
1149
|
-
return isEdgeConnection(property);
|
|
1150
|
-
}
|
|
1151
|
-
function isRecord2(value) {
|
|
1152
|
-
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
1153
|
-
}
|
|
1154
|
-
var QueryResultValidator = class {
|
|
1155
|
-
constructor(viewMapper) {
|
|
1156
|
-
this.viewMapper = viewMapper;
|
|
1157
|
-
}
|
|
1158
|
-
async parseItems(rootViewExternalId, items, select) {
|
|
1159
|
-
const rootView = await this.viewMapper.getView(rootViewExternalId);
|
|
1160
|
-
const schema = await this.buildResultSchema(rootView, MAX_DEPENDENCY_DEPTH, select);
|
|
1161
|
-
const result = zod.z.array(schema).safeParse(items);
|
|
1162
|
-
if (!result.success) {
|
|
1163
|
-
throw new Error(
|
|
1164
|
-
`Invalid query result:
|
|
1165
|
-
${result.error.issues.map((issue) => `- ${issue.path.map(String).join(".")}: ${issue.message}`).join("\n")}`
|
|
1166
|
-
);
|
|
1167
|
-
}
|
|
1168
|
-
return result.data;
|
|
1169
|
-
}
|
|
1170
|
-
async buildResultSchema(view, remainingDepth, select) {
|
|
1171
|
-
const shape = { ...nodeMetadataSchema };
|
|
1172
|
-
const includeAllProperties = select == null || select._all === true;
|
|
1173
|
-
for (const [name, property] of Object.entries(view.properties)) {
|
|
1174
|
-
const isSelected = includeAllProperties || name in select;
|
|
1175
|
-
if (!isSelected) continue;
|
|
1176
|
-
const nestedSelect = isRecord2(select?.[name]) ? select[name] : void 0;
|
|
1177
|
-
if (isViewPropertyDefinition(property)) {
|
|
1178
|
-
const relationSource = getDirectRelationSource(property);
|
|
1179
|
-
if (relationSource) {
|
|
1180
|
-
shape[name] = await this.buildRelationSchema(
|
|
1181
|
-
property,
|
|
1182
|
-
relationSource.externalId,
|
|
1183
|
-
remainingDepth,
|
|
1184
|
-
nestedSelect
|
|
1185
|
-
);
|
|
1186
|
-
} else {
|
|
1187
|
-
shape[name] = propertyValueSchema(property, { dateMode: "coerce" }).optional();
|
|
1188
|
-
}
|
|
1189
|
-
continue;
|
|
1190
|
-
}
|
|
1191
|
-
if (isReverseDirectRelation(property) || isEdgeConnection(property)) {
|
|
1192
|
-
shape[name] = await this.buildRelationSchema(
|
|
1193
|
-
property,
|
|
1194
|
-
property.source.externalId,
|
|
1195
|
-
remainingDepth,
|
|
1196
|
-
nestedSelect
|
|
1197
|
-
);
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
const schema = zod.z.object(shape);
|
|
1201
|
-
return includeAllProperties ? schema.strict() : schema;
|
|
1202
|
-
}
|
|
1203
|
-
async buildRelationSchema(property, targetViewExternalId, remainingDepth, select) {
|
|
1204
|
-
const isList = isListRelation(property);
|
|
1205
|
-
const fallbackSchema = isViewPropertyDefinition(property) ? propertyValueSchema(property, { dateMode: "coerce" }) : zod.z.unknown();
|
|
1206
|
-
if (remainingDepth <= 0 || select == null) {
|
|
1207
|
-
return fallbackSchema.optional();
|
|
1208
|
-
}
|
|
1209
|
-
const targetView = await this.viewMapper.getView(targetViewExternalId);
|
|
1210
|
-
const nestedSchema = await this.buildResultSchema(targetView, remainingDepth - 1, select);
|
|
1211
|
-
if (isViewPropertyDefinition(property)) {
|
|
1212
|
-
const nestedRelationSchema = isList ? zod.z.array(nestedSchema) : nestedSchema;
|
|
1213
|
-
return zod.z.union([nestedRelationSchema, fallbackSchema]).optional();
|
|
1214
|
-
}
|
|
1215
|
-
return (isList ? zod.z.array(nestedSchema) : nestedSchema).optional();
|
|
1216
|
-
}
|
|
1217
|
-
};
|
|
1218
1365
|
|
|
1219
1366
|
// src/mappers/view-mapper.ts
|
|
1220
1367
|
var ViewMapper = class {
|
|
@@ -1262,115 +1409,14 @@ var ViewMapper = class {
|
|
|
1262
1409
|
}
|
|
1263
1410
|
};
|
|
1264
1411
|
|
|
1265
|
-
// src/utils/query.ts
|
|
1266
|
-
function mapNodesAndEdges(queryResult, _query) {
|
|
1267
|
-
return queryResult.items;
|
|
1268
|
-
}
|
|
1269
|
-
function appendNodesAndEdges(initial, additional) {
|
|
1270
|
-
if (!additional) return initial;
|
|
1271
|
-
for (const [key, items] of Object.entries(additional)) {
|
|
1272
|
-
const existing = initial[key];
|
|
1273
|
-
if (existing) {
|
|
1274
|
-
existing.push(...items);
|
|
1275
|
-
} else {
|
|
1276
|
-
initial[key] = [...items];
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
1279
|
-
return initial;
|
|
1280
|
-
}
|
|
1281
|
-
function getQueryForDependenciesPagination(query, queryResult, viewExternalId) {
|
|
1282
|
-
const cursorKeys = new Set(Object.keys(queryResult.nextCursor));
|
|
1283
|
-
const { nodesParent, nodesChildren } = getParentAndChildrenNodes(cursorKeys);
|
|
1284
|
-
const leafCursors = getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren);
|
|
1285
|
-
if (Object.keys(leafCursors).length === 0) {
|
|
1286
|
-
return null;
|
|
1287
|
-
}
|
|
1288
|
-
return buildDependenciesQuery(query, nodesParent, nodesChildren, leafCursors);
|
|
1289
|
-
}
|
|
1290
|
-
function getParentAndChildrenNodes(keys) {
|
|
1291
|
-
const nodesParent = /* @__PURE__ */ new Map();
|
|
1292
|
-
const nodesChildren = /* @__PURE__ */ new Map();
|
|
1293
|
-
for (const key of keys) {
|
|
1294
|
-
const keyParts = key.split(NESTED_SEP);
|
|
1295
|
-
const validParents = /* @__PURE__ */ new Set();
|
|
1296
|
-
for (let i = keyParts.length - 1; i > 0; i--) {
|
|
1297
|
-
const parentPath = keyParts.slice(0, i).join(NESTED_SEP);
|
|
1298
|
-
const parentWithEdgeMarker = `${parentPath}${NESTED_SEP}${EDGE_MARKER}`;
|
|
1299
|
-
if (keys.has(parentWithEdgeMarker)) {
|
|
1300
|
-
validParents.add(parentWithEdgeMarker);
|
|
1301
|
-
const children = nodesChildren.get(parentWithEdgeMarker) ?? /* @__PURE__ */ new Set();
|
|
1302
|
-
children.add(key);
|
|
1303
|
-
nodesChildren.set(parentWithEdgeMarker, children);
|
|
1304
|
-
}
|
|
1305
|
-
if (keys.has(parentPath)) {
|
|
1306
|
-
validParents.add(parentPath);
|
|
1307
|
-
const children = nodesChildren.get(parentPath) ?? /* @__PURE__ */ new Set();
|
|
1308
|
-
children.add(key);
|
|
1309
|
-
nodesChildren.set(parentPath, children);
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
nodesParent.set(key, validParents);
|
|
1313
|
-
}
|
|
1314
|
-
return { nodesParent, nodesChildren };
|
|
1315
|
-
}
|
|
1316
|
-
function getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren) {
|
|
1317
|
-
const targetCursors = {};
|
|
1318
|
-
const targetCursorKeys = /* @__PURE__ */ new Set();
|
|
1319
|
-
for (const [cursorKey, cursorValue] of Object.entries(queryResult.nextCursor)) {
|
|
1320
|
-
if (cursorKey === viewExternalId || !cursorValue || (queryResult.items[cursorKey]?.length ?? 0) !== MAX_LIMIT) {
|
|
1321
|
-
continue;
|
|
1322
|
-
}
|
|
1323
|
-
const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
1324
|
-
let skipDueToChild = false;
|
|
1325
|
-
for (const c of children) {
|
|
1326
|
-
if (targetCursorKeys.has(c)) {
|
|
1327
|
-
skipDueToChild = true;
|
|
1328
|
-
break;
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
if (skipDueToChild) continue;
|
|
1332
|
-
const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
1333
|
-
for (const key of parent) {
|
|
1334
|
-
if (targetCursorKeys.has(key)) {
|
|
1335
|
-
delete targetCursors[key];
|
|
1336
|
-
targetCursorKeys.delete(key);
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
targetCursors[cursorKey] = cursorValue;
|
|
1340
|
-
targetCursorKeys.add(cursorKey);
|
|
1341
|
-
}
|
|
1342
|
-
return targetCursors;
|
|
1343
|
-
}
|
|
1344
|
-
function buildDependenciesQuery(previousQuery, nodesParent, nodesChildren, leafCursors) {
|
|
1345
|
-
const withExprs = {};
|
|
1346
|
-
const selectExprs = {};
|
|
1347
|
-
const finalCursors = {};
|
|
1348
|
-
for (const [cursorKey, cursorValue] of Object.entries(leafCursors)) {
|
|
1349
|
-
const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
1350
|
-
const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
|
|
1351
|
-
const validKeys = /* @__PURE__ */ new Set([...parent, ...children, cursorKey]);
|
|
1352
|
-
for (const [k, v] of Object.entries(previousQuery.with)) {
|
|
1353
|
-
if (validKeys.has(k)) withExprs[k] = v;
|
|
1354
|
-
}
|
|
1355
|
-
for (const [k, v] of Object.entries(previousQuery.select)) {
|
|
1356
|
-
if (validKeys.has(k)) selectExprs[k] = v;
|
|
1357
|
-
}
|
|
1358
|
-
for (const [k, v] of Object.entries(previousQuery.cursors ?? {})) {
|
|
1359
|
-
if (parent.has(k) && v) finalCursors[k] = v;
|
|
1360
|
-
}
|
|
1361
|
-
finalCursors[cursorKey] = cursorValue;
|
|
1362
|
-
}
|
|
1363
|
-
return { with: withExprs, select: selectExprs, cursors: finalCursors };
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
1412
|
// src/client.ts
|
|
1367
1413
|
var IndustrialModelClient = class {
|
|
1368
1414
|
constructor(client, dataModelId, options = {}) {
|
|
1369
1415
|
const cognite = createCogniteAdapter(client);
|
|
1370
1416
|
this.cognite = cognite;
|
|
1371
1417
|
const viewMapper = new ViewMapper(cognite, dataModelId);
|
|
1372
|
-
this.queryMapper = new QueryMapper(viewMapper);
|
|
1373
|
-
this.aggregateMapper = new AggregateMapper(viewMapper);
|
|
1418
|
+
this.queryMapper = new QueryMapper(viewMapper, cognite);
|
|
1419
|
+
this.aggregateMapper = new AggregateMapper(viewMapper, cognite);
|
|
1374
1420
|
this.aggregateResultMapper = new AggregateResultMapper();
|
|
1375
1421
|
this.resultMapper = new QueryResultMapper(viewMapper);
|
|
1376
1422
|
this.resultValidator = new QueryResultValidator(viewMapper);
|
|
@@ -1442,7 +1488,5 @@ var IndustrialModelClient = class {
|
|
|
1442
1488
|
};
|
|
1443
1489
|
|
|
1444
1490
|
exports.IndustrialModelClient = IndustrialModelClient;
|
|
1445
|
-
exports.buildViewSchema = buildViewSchema;
|
|
1446
|
-
exports.nodeIdSchema = nodeIdSchema;
|
|
1447
1491
|
//# sourceMappingURL=index.cjs.map
|
|
1448
1492
|
//# sourceMappingURL=index.cjs.map
|