industrial-model 0.5.0 → 0.7.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/dist/index.cjs CHANGED
@@ -43,6 +43,13 @@ var CogniteSdkAdapter = class {
43
43
  items: response.items
44
44
  };
45
45
  }
46
+ async applyInstances(request) {
47
+ const apply = this.client.instances.apply;
48
+ const response = await apply(request);
49
+ return {
50
+ items: response.items
51
+ };
52
+ }
46
53
  };
47
54
 
48
55
  // src/constants.ts
@@ -54,7 +61,108 @@ var MAX_DEPENDENCY_DEPTH = 3;
54
61
  var AGGREGATE_LIMIT = 1e3;
55
62
  var MAX_GROUP_BY = 5;
56
63
 
57
- // src/mappers/utils.ts
64
+ // src/utils/query.ts
65
+ function mapNodesAndEdges(queryResult, _query) {
66
+ return queryResult.items;
67
+ }
68
+ function appendNodesAndEdges(initial, additional) {
69
+ if (!additional) return initial;
70
+ for (const [key, items] of Object.entries(additional)) {
71
+ const existing = initial[key];
72
+ if (existing) {
73
+ existing.push(...items);
74
+ } else {
75
+ initial[key] = [...items];
76
+ }
77
+ }
78
+ return initial;
79
+ }
80
+ function getQueryForDependenciesPagination(query, queryResult, viewExternalId) {
81
+ const cursorKeys = new Set(Object.keys(queryResult.nextCursor));
82
+ const { nodesParent, nodesChildren } = getParentAndChildrenNodes(cursorKeys);
83
+ const leafCursors = getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren);
84
+ if (Object.keys(leafCursors).length === 0) {
85
+ return null;
86
+ }
87
+ return buildDependenciesQuery(query, nodesParent, nodesChildren, leafCursors);
88
+ }
89
+ function getParentAndChildrenNodes(keys) {
90
+ const nodesParent = /* @__PURE__ */ new Map();
91
+ const nodesChildren = /* @__PURE__ */ new Map();
92
+ for (const key of keys) {
93
+ const keyParts = key.split(NESTED_SEP);
94
+ const validParents = /* @__PURE__ */ new Set();
95
+ for (let i = keyParts.length - 1; i > 0; i--) {
96
+ const parentPath = keyParts.slice(0, i).join(NESTED_SEP);
97
+ const parentWithEdgeMarker = `${parentPath}${NESTED_SEP}${EDGE_MARKER}`;
98
+ if (keys.has(parentWithEdgeMarker)) {
99
+ validParents.add(parentWithEdgeMarker);
100
+ const children = nodesChildren.get(parentWithEdgeMarker) ?? /* @__PURE__ */ new Set();
101
+ children.add(key);
102
+ nodesChildren.set(parentWithEdgeMarker, children);
103
+ }
104
+ if (keys.has(parentPath)) {
105
+ validParents.add(parentPath);
106
+ const children = nodesChildren.get(parentPath) ?? /* @__PURE__ */ new Set();
107
+ children.add(key);
108
+ nodesChildren.set(parentPath, children);
109
+ }
110
+ }
111
+ nodesParent.set(key, validParents);
112
+ }
113
+ return { nodesParent, nodesChildren };
114
+ }
115
+ function getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren) {
116
+ const targetCursors = {};
117
+ const targetCursorKeys = /* @__PURE__ */ new Set();
118
+ for (const [cursorKey, cursorValue] of Object.entries(queryResult.nextCursor)) {
119
+ if (cursorKey === viewExternalId || !cursorValue || (queryResult.items[cursorKey]?.length ?? 0) !== MAX_LIMIT) {
120
+ continue;
121
+ }
122
+ const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
123
+ let skipDueToChild = false;
124
+ for (const c of children) {
125
+ if (targetCursorKeys.has(c)) {
126
+ skipDueToChild = true;
127
+ break;
128
+ }
129
+ }
130
+ if (skipDueToChild) continue;
131
+ const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
132
+ for (const key of parent) {
133
+ if (targetCursorKeys.has(key)) {
134
+ delete targetCursors[key];
135
+ targetCursorKeys.delete(key);
136
+ }
137
+ }
138
+ targetCursors[cursorKey] = cursorValue;
139
+ targetCursorKeys.add(cursorKey);
140
+ }
141
+ return targetCursors;
142
+ }
143
+ function buildDependenciesQuery(previousQuery, nodesParent, nodesChildren, leafCursors) {
144
+ const withExprs = {};
145
+ const selectExprs = {};
146
+ const finalCursors = {};
147
+ for (const [cursorKey, cursorValue] of Object.entries(leafCursors)) {
148
+ const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
149
+ const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
150
+ const validKeys = /* @__PURE__ */ new Set([...parent, ...children, cursorKey]);
151
+ for (const [k, v] of Object.entries(previousQuery.with)) {
152
+ if (validKeys.has(k)) withExprs[k] = v;
153
+ }
154
+ for (const [k, v] of Object.entries(previousQuery.select)) {
155
+ if (validKeys.has(k)) selectExprs[k] = v;
156
+ }
157
+ for (const [k, v] of Object.entries(previousQuery.cursors ?? {})) {
158
+ if (parent.has(k) && v) finalCursors[k] = v;
159
+ }
160
+ finalCursors[cursorKey] = cursorValue;
161
+ }
162
+ return { with: withExprs, select: selectExprs, cursors: finalCursors };
163
+ }
164
+
165
+ // src/utils/view.ts
58
166
  var NODE_PROPERTIES = /* @__PURE__ */ new Set([
59
167
  "externalId",
60
168
  "space",
@@ -118,8 +226,6 @@ function isNumericProperty(property) {
118
226
  function getSelectedGroupByKeys(groupBy) {
119
227
  return Object.entries(groupBy).filter((entry) => entry[1] === true).map(([key]) => key);
120
228
  }
121
-
122
- // src/validation.ts
123
229
  var nodeIdSchema = zod.z.object({
124
230
  space: zod.z.string().min(1),
125
231
  externalId: zod.z.string().min(1)
@@ -168,17 +274,8 @@ function propertyValueSchema(property, options = {}) {
168
274
  }
169
275
  return type.list === true ? zod.z.array(schema) : schema;
170
276
  }
171
- function buildViewSchema(view, options = {}) {
172
- const shape = {};
173
- for (const [name, property] of Object.entries(view.properties)) {
174
- if (isViewPropertyDefinition(property)) {
175
- shape[name] = propertyValueSchema(property, options).optional();
176
- }
177
- }
178
- return zod.z.object(shape).strict();
179
- }
180
277
 
181
- // src/mappers/query-validator.ts
278
+ // src/validators/query-validator.ts
182
279
  var NODE_STRING_PROPERTIES = ["externalId", "space"];
183
280
  var NODE_NUMBER_PROPERTIES = ["createdTime", "deletedTime", "lastUpdatedTime"];
184
281
  var NODE_PROPERTIES2 = /* @__PURE__ */ new Set([...NODE_STRING_PROPERTIES, ...NODE_NUMBER_PROPERTIES]);
@@ -389,6 +486,12 @@ ${errors.map((error) => `- ${error}`).join("\n")}`);
389
486
  );
390
487
  continue;
391
488
  }
489
+ if (isReverseDirectRelation(property) && property.targetsList) {
490
+ errors.push(
491
+ `${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.`
492
+ );
493
+ continue;
494
+ }
392
495
  const targetView = await this.viewMapper.getView(target);
393
496
  errors.push(...await this.validateSelect(value, targetView, [...path, name]));
394
497
  }
@@ -478,7 +581,7 @@ ${errors.map((error) => `- ${error}`).join("\n")}`);
478
581
  }
479
582
  };
480
583
 
481
- // src/mappers/aggregate-validator.ts
584
+ // src/validators/aggregate-validator.ts
482
585
  var NODE_COUNT_PROPERTIES = /* @__PURE__ */ new Set(["externalId", "space"]);
483
586
  function issuePath2(path) {
484
587
  return path.length === 0 ? "aggregate" : path.map(String).join(".");
@@ -614,6 +717,171 @@ ${errors.map((error) => `- ${error}`).join("\n")}`
614
717
  return [];
615
718
  }
616
719
  };
720
+ var nodeMetadataSchema = {
721
+ instanceType: zod.z.literal("node").optional(),
722
+ space: zod.z.string(),
723
+ externalId: zod.z.string(),
724
+ version: zod.z.number().optional(),
725
+ createdTime: zod.z.number().optional(),
726
+ deletedTime: zod.z.number().optional(),
727
+ lastUpdatedTime: zod.z.number().optional(),
728
+ _edges: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
729
+ };
730
+ function isListRelation(property) {
731
+ if (isViewPropertyDefinition(property)) {
732
+ return isListDirectRelation(property);
733
+ }
734
+ if (isReverseDirectRelation(property)) {
735
+ return property.connectionType === "multi_reverse_direct_relation" || property.targetsList === true;
736
+ }
737
+ return isEdgeConnection(property);
738
+ }
739
+ function isRecord2(value) {
740
+ return value != null && typeof value === "object" && !Array.isArray(value);
741
+ }
742
+ var QueryResultValidator = class {
743
+ constructor(viewMapper) {
744
+ this.viewMapper = viewMapper;
745
+ }
746
+ async parseItems(rootViewExternalId, items, select) {
747
+ const rootView = await this.viewMapper.getView(rootViewExternalId);
748
+ const schema = await this.buildResultSchema(rootView, MAX_DEPENDENCY_DEPTH, select);
749
+ const result = zod.z.array(schema).safeParse(items);
750
+ if (!result.success) {
751
+ throw new Error(
752
+ `Invalid query result:
753
+ ${result.error.issues.map((issue) => `- ${issue.path.map(String).join(".")}: ${issue.message}`).join("\n")}`
754
+ );
755
+ }
756
+ return result.data;
757
+ }
758
+ async buildResultSchema(view, remainingDepth, select) {
759
+ const shape = { ...nodeMetadataSchema };
760
+ const includeAllProperties = select == null || select._all === true;
761
+ for (const [name, property] of Object.entries(view.properties)) {
762
+ const isSelected = includeAllProperties || name in select;
763
+ if (!isSelected) continue;
764
+ const nestedSelect = isRecord2(select?.[name]) ? select[name] : void 0;
765
+ if (isViewPropertyDefinition(property)) {
766
+ const relationSource = getDirectRelationSource(property);
767
+ if (relationSource) {
768
+ shape[name] = await this.buildRelationSchema(
769
+ property,
770
+ relationSource.externalId,
771
+ remainingDepth,
772
+ nestedSelect
773
+ );
774
+ } else {
775
+ shape[name] = propertyValueSchema(property, { dateMode: "coerce" }).optional();
776
+ }
777
+ continue;
778
+ }
779
+ if (isReverseDirectRelation(property) || isEdgeConnection(property)) {
780
+ shape[name] = await this.buildRelationSchema(
781
+ property,
782
+ property.source.externalId,
783
+ remainingDepth,
784
+ nestedSelect
785
+ );
786
+ }
787
+ }
788
+ const schema = zod.z.object(shape);
789
+ return includeAllProperties ? schema.strict() : schema;
790
+ }
791
+ async buildRelationSchema(property, targetViewExternalId, remainingDepth, select) {
792
+ const isList = isListRelation(property);
793
+ const fallbackSchema = isViewPropertyDefinition(property) ? propertyValueSchema(property, { dateMode: "coerce" }) : zod.z.unknown();
794
+ if (remainingDepth <= 0 || select == null) {
795
+ return fallbackSchema.optional();
796
+ }
797
+ const targetView = await this.viewMapper.getView(targetViewExternalId);
798
+ const nestedSchema = await this.buildResultSchema(targetView, remainingDepth - 1, select);
799
+ if (isViewPropertyDefinition(property)) {
800
+ const nestedRelationSchema = isList ? zod.z.array(nestedSchema) : nestedSchema;
801
+ return zod.z.union([nestedRelationSchema, fallbackSchema]).optional();
802
+ }
803
+ return (isList ? zod.z.array(nestedSchema) : nestedSchema).optional();
804
+ }
805
+ };
806
+ var strictNodeIdSchema = zod.z.object({
807
+ space: zod.z.string().min(1),
808
+ externalId: zod.z.string().min(1)
809
+ }).strict();
810
+ var nodeIdLikeSchema = zod.z.object({
811
+ space: zod.z.string().min(1),
812
+ externalId: zod.z.string().min(1)
813
+ }).loose();
814
+ var optionsSchema = zod.z.object({
815
+ viewExternalId: zod.z.string().min(1),
816
+ items: zod.z.array(zod.z.record(zod.z.string(), zod.z.unknown())),
817
+ onEdgeCreation: zod.z.record(zod.z.string(), zod.z.function()).optional(),
818
+ replace: zod.z.boolean().optional(),
819
+ edgeMode: zod.z.enum(["append", "replace"]).optional()
820
+ }).strict();
821
+ function issuePath3(path) {
822
+ return path.length === 0 ? "upsert" : path.map(String).join(".");
823
+ }
824
+ function formatZodIssues3(error, path) {
825
+ return error.issues.map((issue) => `${issuePath3([...path, ...issue.path])}: ${issue.message}`);
826
+ }
827
+ function relationValueSchema(property) {
828
+ if (isReverseDirectRelation(property) && property.targetsList === true) {
829
+ return zod.z.never();
830
+ }
831
+ return zod.z.union([nodeIdLikeSchema, zod.z.array(nodeIdLikeSchema)]);
832
+ }
833
+ var UpsertValidator = class {
834
+ validate(options, rootView) {
835
+ const errors = [];
836
+ const optionsResult = optionsSchema.safeParse(options);
837
+ if (!optionsResult.success) {
838
+ errors.push(...formatZodIssues3(optionsResult.error, []));
839
+ }
840
+ if (options.viewExternalId !== rootView.externalId) {
841
+ errors.push(
842
+ `viewExternalId: expected "${rootView.externalId}", received "${options.viewExternalId}"`
843
+ );
844
+ }
845
+ for (const [index, item] of options.items.entries()) {
846
+ errors.push(
847
+ ...this.validateItem(item, rootView, ["items", index])
848
+ );
849
+ }
850
+ if (errors.length > 0) {
851
+ throw new Error(`Invalid upsert options:
852
+ ${errors.map((error) => `- ${error}`).join("\n")}`);
853
+ }
854
+ }
855
+ validateItem(item, view, path) {
856
+ const errors = [];
857
+ const identityResult = strictNodeIdSchema.safeParse({
858
+ space: item.space,
859
+ externalId: item.externalId
860
+ });
861
+ if (!identityResult.success) {
862
+ errors.push(...formatZodIssues3(identityResult.error, path));
863
+ }
864
+ for (const [name, value] of Object.entries(item)) {
865
+ if (name === "space" || name === "externalId") continue;
866
+ const property = view.properties[name];
867
+ if (!property) {
868
+ errors.push(`${issuePath3([...path, name])}: unknown view property`);
869
+ continue;
870
+ }
871
+ if (isViewPropertyDefinition(property)) {
872
+ const schema = property.type.type === "direct" ? property.type.list === true ? zod.z.array(nodeIdLikeSchema) : nodeIdLikeSchema : propertyValueSchema(property);
873
+ const result = schema.safeParse(value);
874
+ if (!result.success) errors.push(...formatZodIssues3(result.error, [...path, name]));
875
+ continue;
876
+ }
877
+ if (isReverseDirectRelation(property) || isEdgeConnection(property)) {
878
+ const result = relationValueSchema(property).safeParse(value);
879
+ if (!result.success) errors.push(...formatZodIssues3(result.error, [...path, name]));
880
+ }
881
+ }
882
+ return errors;
883
+ }
884
+ };
617
885
 
618
886
  // src/mappers/filter-mapper.ts
619
887
  var LEAF_OPS = /* @__PURE__ */ new Set([
@@ -1180,92 +1448,216 @@ var QueryResultMapper = class {
1180
1448
  return entry;
1181
1449
  }
1182
1450
  };
1183
- var nodeMetadataSchema = {
1184
- instanceType: zod.z.literal("node").optional(),
1185
- space: zod.z.string(),
1186
- externalId: zod.z.string(),
1187
- version: zod.z.number().optional(),
1188
- createdTime: zod.z.number().optional(),
1189
- deletedTime: zod.z.number().optional(),
1190
- lastUpdatedTime: zod.z.number().optional(),
1191
- _edges: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
1192
- };
1193
- function isListRelation(property) {
1194
- if (isViewPropertyDefinition(property)) {
1195
- return isListDirectRelation(property);
1196
- }
1197
- if (isReverseDirectRelation(property)) {
1198
- return property.connectionType === "multi_reverse_direct_relation" || property.targetsList === true;
1199
- }
1200
- return isEdgeConnection(property);
1201
- }
1202
- function isRecord2(value) {
1203
- return value != null && typeof value === "object" && !Array.isArray(value);
1204
- }
1205
- var QueryResultValidator = class {
1206
- constructor(viewMapper) {
1451
+
1452
+ // src/mappers/upsert-mapper.ts
1453
+ var IDENTITY_KEYS = /* @__PURE__ */ new Set(["space", "externalId"]);
1454
+ var EDGE_QUERY_LIMIT = 1e3;
1455
+ var UpsertMapper = class {
1456
+ constructor(viewMapper, cognite) {
1207
1457
  this.viewMapper = viewMapper;
1458
+ this.cognite = cognite;
1459
+ this.validator = new UpsertValidator();
1208
1460
  }
1209
- async parseItems(rootViewExternalId, items, select) {
1210
- const rootView = await this.viewMapper.getView(rootViewExternalId);
1211
- const schema = await this.buildResultSchema(rootView, MAX_DEPENDENCY_DEPTH, select);
1212
- const result = zod.z.array(schema).safeParse(items);
1213
- if (!result.success) {
1214
- throw new Error(
1215
- `Invalid query result:
1216
- ${result.error.issues.map((issue) => `- ${issue.path.map(String).join(".")}: ${issue.message}`).join("\n")}`
1217
- );
1218
- }
1219
- return result.data;
1461
+ async map(options) {
1462
+ const rootView = await this.viewMapper.getView(options.viewExternalId);
1463
+ this.validator.validate(options, rootView);
1464
+ const edgeMode = options.edgeMode ?? "append";
1465
+ const mappedItems = options.items.map(
1466
+ (item) => this.mapItem(item, rootView, options.onEdgeCreation, edgeMode)
1467
+ );
1468
+ const items = mappedItems.flatMap((item) => item.writes);
1469
+ const edgeReplacements = mappedItems.flatMap((item) => item.edgeReplacements);
1470
+ const deleteItems = edgeMode === "replace" ? await this.mapEdgeReplacementDeletes(edgeReplacements) : [];
1471
+ return {
1472
+ items,
1473
+ ...deleteItems.length > 0 ? { delete: deleteItems } : {},
1474
+ ...options.replace === true ? { replace: true } : {}
1475
+ };
1220
1476
  }
1221
- async buildResultSchema(view, remainingDepth, select) {
1222
- const shape = { ...nodeMetadataSchema };
1223
- const includeAllProperties = select == null || select._all === true;
1224
- for (const [name, property] of Object.entries(view.properties)) {
1225
- const isSelected = includeAllProperties || name in select;
1226
- if (!isSelected) continue;
1227
- const nestedSelect = isRecord2(select?.[name]) ? select[name] : void 0;
1477
+ mapItem(item, rootView, onEdgeCreation, edgeMode) {
1478
+ const node = { space: item.space, externalId: item.externalId };
1479
+ const nodeProperties = {};
1480
+ const inferredItems = [];
1481
+ const edgeReplacements = [];
1482
+ for (const [name, value] of Object.entries(item)) {
1483
+ if (IDENTITY_KEYS.has(name)) continue;
1484
+ const property = rootView.properties[name];
1485
+ if (!property) continue;
1228
1486
  if (isViewPropertyDefinition(property)) {
1229
- const relationSource = getDirectRelationSource(property);
1230
- if (relationSource) {
1231
- shape[name] = await this.buildRelationSchema(
1232
- property,
1233
- relationSource.externalId,
1234
- remainingDepth,
1235
- nestedSelect
1236
- );
1237
- } else {
1238
- shape[name] = propertyValueSchema(property, { dateMode: "coerce" }).optional();
1487
+ nodeProperties[name] = normalizeViewPropertyValue(value, property);
1488
+ } else if (isReverseDirectRelation(property)) {
1489
+ inferredItems.push(...this.mapReverseDirectRelation(node, value, property));
1490
+ } else if (isEdgeConnection(property)) {
1491
+ const desiredEdges = this.mapEdgeConnection(node, name, value, property, onEdgeCreation);
1492
+ inferredItems.push(...desiredEdges);
1493
+ if (edgeMode === "replace") {
1494
+ edgeReplacements.push({ rootNode: node, propertyName: name, property, desiredEdges });
1239
1495
  }
1240
- continue;
1241
- }
1242
- if (isReverseDirectRelation(property) || isEdgeConnection(property)) {
1243
- shape[name] = await this.buildRelationSchema(
1244
- property,
1245
- property.source.externalId,
1246
- remainingDepth,
1247
- nestedSelect
1248
- );
1249
1496
  }
1250
1497
  }
1251
- const schema = zod.z.object(shape);
1252
- return includeAllProperties ? schema.strict() : schema;
1498
+ const applyNode = {
1499
+ instanceType: "node",
1500
+ ...node
1501
+ };
1502
+ if (Object.keys(nodeProperties).length > 0) {
1503
+ applyNode.sources = [{ source: toViewReference(rootView), properties: nodeProperties }];
1504
+ }
1505
+ return { writes: [applyNode, ...inferredItems], edgeReplacements };
1506
+ }
1507
+ async mapEdgeReplacementDeletes(replacements) {
1508
+ const deletes = await Promise.all(
1509
+ replacements.map(async (replacement) => {
1510
+ const existingEdges = await this.queryExistingEdges(replacement);
1511
+ const desiredEdgeKeys = new Set(replacement.desiredEdges.map((edge) => instanceKey(edge)));
1512
+ return existingEdges.filter((edge) => !desiredEdgeKeys.has(instanceKey(edge))).map((edge) => ({
1513
+ instanceType: "edge",
1514
+ space: edge.space,
1515
+ externalId: edge.externalId
1516
+ }));
1517
+ })
1518
+ );
1519
+ return uniqueDeletes(deletes.flat());
1520
+ }
1521
+ async queryExistingEdges(replacement) {
1522
+ const rootKey = `${replacement.propertyName}Root`;
1523
+ const edgeKey = `${replacement.propertyName}Edges`;
1524
+ const direction = replacement.property.direction ?? "outwards";
1525
+ const query = {
1526
+ with: {
1527
+ [rootKey]: {
1528
+ nodes: {
1529
+ filter: {
1530
+ instanceReferences: [replacement.rootNode]
1531
+ }
1532
+ },
1533
+ limit: 1
1534
+ },
1535
+ [edgeKey]: {
1536
+ edges: {
1537
+ from: rootKey,
1538
+ maxDistance: 1,
1539
+ direction,
1540
+ filter: {
1541
+ equals: { property: ["edge", "type"], value: replacement.property.type }
1542
+ }
1543
+ },
1544
+ limit: EDGE_QUERY_LIMIT
1545
+ }
1546
+ },
1547
+ select: {
1548
+ [rootKey]: {},
1549
+ [edgeKey]: {}
1550
+ }
1551
+ };
1552
+ const edges = [];
1553
+ let cursor;
1554
+ do {
1555
+ const response = await this.cognite.queryInstances({
1556
+ ...query,
1557
+ ...cursor ? { cursors: { [edgeKey]: cursor } } : {}
1558
+ });
1559
+ edges.push(
1560
+ ...(response.items[edgeKey] ?? []).filter(
1561
+ (item) => item.instanceType === "edge"
1562
+ )
1563
+ );
1564
+ cursor = response.nextCursor[edgeKey];
1565
+ } while (cursor);
1566
+ return edges;
1253
1567
  }
1254
- async buildRelationSchema(property, targetViewExternalId, remainingDepth, select) {
1255
- const isList = isListRelation(property);
1256
- const fallbackSchema = isViewPropertyDefinition(property) ? propertyValueSchema(property, { dateMode: "coerce" }) : zod.z.unknown();
1257
- if (remainingDepth <= 0 || select == null) {
1258
- return fallbackSchema.optional();
1259
- }
1260
- const targetView = await this.viewMapper.getView(targetViewExternalId);
1261
- const nestedSchema = await this.buildResultSchema(targetView, remainingDepth - 1, select);
1262
- if (isViewPropertyDefinition(property)) {
1263
- const nestedRelationSchema = isList ? zod.z.array(nestedSchema) : nestedSchema;
1264
- return zod.z.union([nestedRelationSchema, fallbackSchema]).optional();
1265
- }
1266
- return (isList ? zod.z.array(nestedSchema) : nestedSchema).optional();
1568
+ mapReverseDirectRelation(node, value, property) {
1569
+ const targets = asNodeIdArray(value);
1570
+ return targets.map((target) => ({
1571
+ instanceType: "node",
1572
+ ...target,
1573
+ sources: [
1574
+ {
1575
+ source: property.through.source,
1576
+ properties: {
1577
+ [property.through.identifier]: normalizeReverseDirectRelationValue(node, property)
1578
+ }
1579
+ }
1580
+ ]
1581
+ }));
1582
+ }
1583
+ mapEdgeConnection(node, propertyName, value, property, onEdgeCreation) {
1584
+ const direction = property.direction ?? "outwards";
1585
+ return asNodeIdArray(value).map((target) => {
1586
+ const startNode = direction === "inwards" ? target : node;
1587
+ const endNode = direction === "inwards" ? node : target;
1588
+ const edgeType = toNodeId(property.type, `edge type for "${propertyName}"`);
1589
+ const createEdgeId = onEdgeCreation?.[propertyName];
1590
+ if (!createEdgeId) {
1591
+ throw new Error(
1592
+ `Invalid upsert options:
1593
+ - onEdgeCreation.${propertyName}: required when ingesting edge connection "${propertyName}"`
1594
+ );
1595
+ }
1596
+ const edgeId = createEdgeId({
1597
+ startNode,
1598
+ endNode,
1599
+ edgeType
1600
+ });
1601
+ assertNodeId(edgeId, `onEdgeCreation(${propertyName})`);
1602
+ return {
1603
+ instanceType: "edge",
1604
+ ...edgeId,
1605
+ type: property.type,
1606
+ startNode,
1607
+ endNode
1608
+ };
1609
+ });
1267
1610
  }
1268
1611
  };
1612
+ function normalizeReverseDirectRelationValue(node, property) {
1613
+ return property.targetsList === true ? [node] : node;
1614
+ }
1615
+ function normalizeViewPropertyValue(value, property) {
1616
+ if (property.type.type !== "direct") return normalizePropertyValue(value);
1617
+ if (Array.isArray(value)) return value.map((item) => toNodeId(item));
1618
+ return toNodeId(value);
1619
+ }
1620
+ function normalizePropertyValue(value) {
1621
+ if (value instanceof Date) return value.toISOString();
1622
+ if (Array.isArray(value)) return value.map(normalizePropertyValue);
1623
+ if (isPlainObject(value)) {
1624
+ return Object.fromEntries(
1625
+ Object.entries(value).map(([key, nestedValue]) => [key, normalizePropertyValue(nestedValue)])
1626
+ );
1627
+ }
1628
+ return value;
1629
+ }
1630
+ function asNodeIdArray(value) {
1631
+ return Array.isArray(value) ? value.map((item) => toNodeId(item)) : [toNodeId(value)];
1632
+ }
1633
+ function toNodeId(value, label = "relation reference") {
1634
+ if (!isPlainObject(value) || typeof value.space !== "string" || typeof value.externalId !== "string") {
1635
+ throw new Error(`Invalid upsert options:
1636
+ - ${label}: expected a NodeId`);
1637
+ }
1638
+ return { space: value.space, externalId: value.externalId };
1639
+ }
1640
+ function instanceKey(instance) {
1641
+ return `${instance.space}\0${instance.externalId}`;
1642
+ }
1643
+ function uniqueDeletes(deletes) {
1644
+ const seen = /* @__PURE__ */ new Set();
1645
+ return deletes.filter((item) => {
1646
+ const key = instanceKey(item);
1647
+ if (seen.has(key)) return false;
1648
+ seen.add(key);
1649
+ return true;
1650
+ });
1651
+ }
1652
+ function assertNodeId(value, label) {
1653
+ if (!isPlainObject(value) || typeof value.space !== "string" || typeof value.externalId !== "string") {
1654
+ throw new Error(`Invalid upsert options:
1655
+ - ${label}: expected a NodeId`);
1656
+ }
1657
+ }
1658
+ function isPlainObject(value) {
1659
+ return value != null && typeof value === "object" && !Array.isArray(value);
1660
+ }
1269
1661
 
1270
1662
  // src/mappers/view-mapper.ts
1271
1663
  var ViewMapper = class {
@@ -1313,108 +1705,8 @@ var ViewMapper = class {
1313
1705
  }
1314
1706
  };
1315
1707
 
1316
- // src/utils/query.ts
1317
- function mapNodesAndEdges(queryResult, _query) {
1318
- return queryResult.items;
1319
- }
1320
- function appendNodesAndEdges(initial, additional) {
1321
- if (!additional) return initial;
1322
- for (const [key, items] of Object.entries(additional)) {
1323
- const existing = initial[key];
1324
- if (existing) {
1325
- existing.push(...items);
1326
- } else {
1327
- initial[key] = [...items];
1328
- }
1329
- }
1330
- return initial;
1331
- }
1332
- function getQueryForDependenciesPagination(query, queryResult, viewExternalId) {
1333
- const cursorKeys = new Set(Object.keys(queryResult.nextCursor));
1334
- const { nodesParent, nodesChildren } = getParentAndChildrenNodes(cursorKeys);
1335
- const leafCursors = getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren);
1336
- if (Object.keys(leafCursors).length === 0) {
1337
- return null;
1338
- }
1339
- return buildDependenciesQuery(query, nodesParent, nodesChildren, leafCursors);
1340
- }
1341
- function getParentAndChildrenNodes(keys) {
1342
- const nodesParent = /* @__PURE__ */ new Map();
1343
- const nodesChildren = /* @__PURE__ */ new Map();
1344
- for (const key of keys) {
1345
- const keyParts = key.split(NESTED_SEP);
1346
- const validParents = /* @__PURE__ */ new Set();
1347
- for (let i = keyParts.length - 1; i > 0; i--) {
1348
- const parentPath = keyParts.slice(0, i).join(NESTED_SEP);
1349
- const parentWithEdgeMarker = `${parentPath}${NESTED_SEP}${EDGE_MARKER}`;
1350
- if (keys.has(parentWithEdgeMarker)) {
1351
- validParents.add(parentWithEdgeMarker);
1352
- const children = nodesChildren.get(parentWithEdgeMarker) ?? /* @__PURE__ */ new Set();
1353
- children.add(key);
1354
- nodesChildren.set(parentWithEdgeMarker, children);
1355
- }
1356
- if (keys.has(parentPath)) {
1357
- validParents.add(parentPath);
1358
- const children = nodesChildren.get(parentPath) ?? /* @__PURE__ */ new Set();
1359
- children.add(key);
1360
- nodesChildren.set(parentPath, children);
1361
- }
1362
- }
1363
- nodesParent.set(key, validParents);
1364
- }
1365
- return { nodesParent, nodesChildren };
1366
- }
1367
- function getLeafCursors(queryResult, viewExternalId, nodesParent, nodesChildren) {
1368
- const targetCursors = {};
1369
- const targetCursorKeys = /* @__PURE__ */ new Set();
1370
- for (const [cursorKey, cursorValue] of Object.entries(queryResult.nextCursor)) {
1371
- if (cursorKey === viewExternalId || !cursorValue || (queryResult.items[cursorKey]?.length ?? 0) !== MAX_LIMIT) {
1372
- continue;
1373
- }
1374
- const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
1375
- let skipDueToChild = false;
1376
- for (const c of children) {
1377
- if (targetCursorKeys.has(c)) {
1378
- skipDueToChild = true;
1379
- break;
1380
- }
1381
- }
1382
- if (skipDueToChild) continue;
1383
- const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
1384
- for (const key of parent) {
1385
- if (targetCursorKeys.has(key)) {
1386
- delete targetCursors[key];
1387
- targetCursorKeys.delete(key);
1388
- }
1389
- }
1390
- targetCursors[cursorKey] = cursorValue;
1391
- targetCursorKeys.add(cursorKey);
1392
- }
1393
- return targetCursors;
1394
- }
1395
- function buildDependenciesQuery(previousQuery, nodesParent, nodesChildren, leafCursors) {
1396
- const withExprs = {};
1397
- const selectExprs = {};
1398
- const finalCursors = {};
1399
- for (const [cursorKey, cursorValue] of Object.entries(leafCursors)) {
1400
- const children = nodesChildren.get(cursorKey) ?? /* @__PURE__ */ new Set();
1401
- const parent = nodesParent.get(cursorKey) ?? /* @__PURE__ */ new Set();
1402
- const validKeys = /* @__PURE__ */ new Set([...parent, ...children, cursorKey]);
1403
- for (const [k, v] of Object.entries(previousQuery.with)) {
1404
- if (validKeys.has(k)) withExprs[k] = v;
1405
- }
1406
- for (const [k, v] of Object.entries(previousQuery.select)) {
1407
- if (validKeys.has(k)) selectExprs[k] = v;
1408
- }
1409
- for (const [k, v] of Object.entries(previousQuery.cursors ?? {})) {
1410
- if (parent.has(k) && v) finalCursors[k] = v;
1411
- }
1412
- finalCursors[cursorKey] = cursorValue;
1413
- }
1414
- return { with: withExprs, select: selectExprs, cursors: finalCursors };
1415
- }
1416
-
1417
1708
  // src/client.ts
1709
+ var APPLY_ITEM_LIMIT = 1e3;
1418
1710
  var IndustrialModelClient = class {
1419
1711
  constructor(client, dataModelId, options = {}) {
1420
1712
  const cognite = createCogniteAdapter(client);
@@ -1422,6 +1714,7 @@ var IndustrialModelClient = class {
1422
1714
  const viewMapper = new ViewMapper(cognite, dataModelId);
1423
1715
  this.queryMapper = new QueryMapper(viewMapper, cognite);
1424
1716
  this.aggregateMapper = new AggregateMapper(viewMapper, cognite);
1717
+ this.upsertMapper = new UpsertMapper(viewMapper, cognite);
1425
1718
  this.aggregateResultMapper = new AggregateResultMapper();
1426
1719
  this.resultMapper = new QueryResultMapper(viewMapper);
1427
1720
  this.resultValidator = new QueryResultValidator(viewMapper);
@@ -1435,6 +1728,54 @@ var IndustrialModelClient = class {
1435
1728
  const execute = (options) => this.aggregateInternal(options);
1436
1729
  return execute;
1437
1730
  }
1731
+ upsert() {
1732
+ const execute = (options) => this.upsertInternal(options);
1733
+ return execute;
1734
+ }
1735
+ async delete(items) {
1736
+ const deleteItems = items.map((item) => {
1737
+ assertNodeId2(item);
1738
+ return {
1739
+ instanceType: "node",
1740
+ space: item.space,
1741
+ externalId: item.externalId
1742
+ };
1743
+ });
1744
+ const response = await this.applyInstancesInChunks({
1745
+ items: [],
1746
+ delete: deleteItems
1747
+ });
1748
+ return { items: response.items };
1749
+ }
1750
+ async upsertInternal(options) {
1751
+ const cogniteRequest = await this.upsertMapper.map(options);
1752
+ const response = await this.applyInstancesInChunks(cogniteRequest);
1753
+ return { items: response.items };
1754
+ }
1755
+ async applyInstancesInChunks(request) {
1756
+ const deleteItems = request.delete ?? [];
1757
+ const totalItems = request.items.length + deleteItems.length;
1758
+ if (totalItems === 0) return { items: [] };
1759
+ if (totalItems <= APPLY_ITEM_LIMIT) {
1760
+ return this.cognite.applyInstances(request);
1761
+ }
1762
+ const responses = [];
1763
+ for (const deleteChunk of chunks(deleteItems, APPLY_ITEM_LIMIT)) {
1764
+ const response = await this.cognite.applyInstances({
1765
+ items: [],
1766
+ delete: deleteChunk
1767
+ });
1768
+ responses.push(...response.items);
1769
+ }
1770
+ for (const itemChunk of chunks(request.items, APPLY_ITEM_LIMIT)) {
1771
+ const response = await this.cognite.applyInstances({
1772
+ items: itemChunk,
1773
+ ...request.replace === true ? { replace: true } : {}
1774
+ });
1775
+ responses.push(...response.items);
1776
+ }
1777
+ return { items: responses };
1778
+ }
1438
1779
  async aggregateInternal(options) {
1439
1780
  const cogniteRequest = await this.aggregateMapper.map(options);
1440
1781
  const response = await this.cognite.aggregateInstances(cogniteRequest);
@@ -1491,9 +1832,19 @@ var IndustrialModelClient = class {
1491
1832
  return appendNodesAndEdges(result, nestedResults);
1492
1833
  }
1493
1834
  };
1835
+ function chunks(items, size) {
1836
+ const result = [];
1837
+ for (let index = 0; index < items.length; index += size) {
1838
+ result.push(items.slice(index, index + size));
1839
+ }
1840
+ return result;
1841
+ }
1842
+ function assertNodeId2(value) {
1843
+ if (value == null || typeof value !== "object" || Array.isArray(value) || typeof value.space !== "string" || value.space?.length === 0 || typeof value.externalId !== "string" || value.externalId?.length === 0) {
1844
+ throw new Error("Invalid delete options:\n- items: expected NodeId values");
1845
+ }
1846
+ }
1494
1847
 
1495
1848
  exports.IndustrialModelClient = IndustrialModelClient;
1496
- exports.buildViewSchema = buildViewSchema;
1497
- exports.nodeIdSchema = nodeIdSchema;
1498
1849
  //# sourceMappingURL=index.cjs.map
1499
1850
  //# sourceMappingURL=index.cjs.map