industrial-model 0.6.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/README.md +195 -1
- package/dist/cognite-core/index.cjs +366 -0
- package/dist/cognite-core/index.cjs.map +1 -1
- package/dist/cognite-core/index.d.cts +5 -2
- package/dist/cognite-core/index.d.ts +5 -2
- package/dist/cognite-core/index.js +366 -0
- package/dist/cognite-core/index.js.map +1 -1
- package/dist/index.cjs +358 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -3
- package/dist/index.d.ts +8 -3
- package/dist/index.js +358 -0
- package/dist/index.js.map +1 -1
- package/dist/{types-DCP5GMi3.d.cts → types-2jjbs2i7.d.cts} +52 -1
- package/dist/{types-DCP5GMi3.d.ts → types-2jjbs2i7.d.ts} +52 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CogniteClient } from '@cognite/sdk';
|
|
2
|
-
import {
|
|
2
|
+
import { i as IndustrialModel, N as NodeId, s as AggregateOptions, b as AggregateResult, c as AggregateResultItem, m as QuerySelect, t as QueryOptions, k as QueryResult, l as QueryResultItem, o as UpsertOptions, q as UpsertResult, I as IndustrialModelClientOptions, a as DeleteResult } from '../types-2jjbs2i7.cjs';
|
|
3
3
|
|
|
4
4
|
type CogniteCoreViewExternalId = "CogniteDescribable" | "CogniteSourceable" | "CogniteSourceSystem" | "CogniteSchedulable" | "CogniteVisualizable" | "Cognite3DTransformation" | "CogniteCubeMap" | "Cognite3DObject" | "Cognite3DModel" | "CogniteCADModel" | "Cognite3DRevision" | "CognitePointCloudModel" | "Cognite360ImageModel" | "CogniteCADRevision" | "CognitePointCloudRevision" | "Cognite360ImageCollection" | "CogniteCADNode" | "CognitePointCloudVolume" | "Cognite360Image" | "Cognite360ImageStation" | "Cognite360ImageAnnotation" | "CogniteAsset" | "CogniteAssetClass" | "CogniteAssetType" | "CogniteEquipment" | "CogniteEquipmentType" | "CogniteFile" | "CogniteFileCategory" | "CogniteActivity" | "CogniteTimeSeries" | "CogniteAnnotation" | "CogniteDiagramAnnotation" | "CogniteUnit";
|
|
5
5
|
type CogniteDescribable = IndustrialModel<{
|
|
@@ -515,6 +515,7 @@ type CogniteCoreQueryExecutor<TView extends CogniteCoreViewExternalId> = {
|
|
|
515
515
|
}): Promise<QueryResult<QueryResultItem<CogniteCoreModel<TView>, undefined>>>;
|
|
516
516
|
};
|
|
517
517
|
type CogniteCoreAggregateExecutor<TView extends CogniteCoreViewExternalId> = <const TOptions extends Omit<AggregateOptions<CogniteCoreModel<TView>>, "viewExternalId">>(options?: TOptions) => Promise<AggregateResult<AggregateResultItem<CogniteCoreModel<TView>, TOptions["groupBy"], TOptions["aggregate"]>>>;
|
|
518
|
+
type CogniteCoreUpsertExecutor<TView extends CogniteCoreViewExternalId> = (options: Omit<UpsertOptions<CogniteCoreModel<TView>>, "viewExternalId">) => Promise<UpsertResult>;
|
|
518
519
|
|
|
519
520
|
/** Data model id for Cognite Core v1. */
|
|
520
521
|
declare const COGNITE_CORE_DATA_MODEL: {
|
|
@@ -527,6 +528,8 @@ declare class CogniteCoreClient {
|
|
|
527
528
|
constructor(client: CogniteClient, options?: IndustrialModelClientOptions);
|
|
528
529
|
query<TView extends CogniteCoreViewExternalId>(viewExternalId: TView): CogniteCoreQueryExecutor<TView>;
|
|
529
530
|
aggregate<TView extends CogniteCoreViewExternalId>(viewExternalId: TView): CogniteCoreAggregateExecutor<TView>;
|
|
531
|
+
upsert<TView extends CogniteCoreViewExternalId>(viewExternalId: TView): CogniteCoreUpsertExecutor<TView>;
|
|
532
|
+
delete<TItem extends NodeId>(items: TItem[]): Promise<DeleteResult>;
|
|
530
533
|
}
|
|
531
534
|
|
|
532
|
-
export { COGNITE_CORE_DATA_MODEL, type Cognite360Image, type Cognite360ImageAnnotation, type Cognite360ImageCollection, type Cognite360ImageModel, type Cognite360ImageStation, type Cognite3DModel, type Cognite3DObject, type Cognite3DRevision, type Cognite3DTransformation, type CogniteActivity, type CogniteAnnotation, type CogniteAsset, type CogniteAssetClass, type CogniteAssetType, type CogniteCADModel, type CogniteCADNode, type CogniteCADRevision, type CogniteCoreAggregateExecutor, CogniteCoreClient, type CogniteCoreModel, type CogniteCoreModelByView, type CogniteCoreQueryExecutor, type CogniteCoreViewExternalId, type CogniteCubeMap, type CogniteDescribable, type CogniteDiagramAnnotation, type CogniteEquipment, type CogniteEquipmentType, type CogniteFile, type CogniteFileCategory, type CognitePointCloudModel, type CognitePointCloudRevision, type CognitePointCloudVolume, type CogniteSchedulable, type CogniteSourceSystem, type CogniteSourceable, type CogniteTimeSeries, type CogniteUnit, type CogniteVisualizable };
|
|
535
|
+
export { COGNITE_CORE_DATA_MODEL, type Cognite360Image, type Cognite360ImageAnnotation, type Cognite360ImageCollection, type Cognite360ImageModel, type Cognite360ImageStation, type Cognite3DModel, type Cognite3DObject, type Cognite3DRevision, type Cognite3DTransformation, type CogniteActivity, type CogniteAnnotation, type CogniteAsset, type CogniteAssetClass, type CogniteAssetType, type CogniteCADModel, type CogniteCADNode, type CogniteCADRevision, type CogniteCoreAggregateExecutor, CogniteCoreClient, type CogniteCoreModel, type CogniteCoreModelByView, type CogniteCoreQueryExecutor, type CogniteCoreUpsertExecutor, type CogniteCoreViewExternalId, type CogniteCubeMap, type CogniteDescribable, type CogniteDiagramAnnotation, type CogniteEquipment, type CogniteEquipmentType, type CogniteFile, type CogniteFileCategory, type CognitePointCloudModel, type CognitePointCloudRevision, type CognitePointCloudVolume, type CogniteSchedulable, type CogniteSourceSystem, type CogniteSourceable, type CogniteTimeSeries, type CogniteUnit, type CogniteVisualizable };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CogniteClient } from '@cognite/sdk';
|
|
2
|
-
import {
|
|
2
|
+
import { i as IndustrialModel, N as NodeId, s as AggregateOptions, b as AggregateResult, c as AggregateResultItem, m as QuerySelect, t as QueryOptions, k as QueryResult, l as QueryResultItem, o as UpsertOptions, q as UpsertResult, I as IndustrialModelClientOptions, a as DeleteResult } from '../types-2jjbs2i7.js';
|
|
3
3
|
|
|
4
4
|
type CogniteCoreViewExternalId = "CogniteDescribable" | "CogniteSourceable" | "CogniteSourceSystem" | "CogniteSchedulable" | "CogniteVisualizable" | "Cognite3DTransformation" | "CogniteCubeMap" | "Cognite3DObject" | "Cognite3DModel" | "CogniteCADModel" | "Cognite3DRevision" | "CognitePointCloudModel" | "Cognite360ImageModel" | "CogniteCADRevision" | "CognitePointCloudRevision" | "Cognite360ImageCollection" | "CogniteCADNode" | "CognitePointCloudVolume" | "Cognite360Image" | "Cognite360ImageStation" | "Cognite360ImageAnnotation" | "CogniteAsset" | "CogniteAssetClass" | "CogniteAssetType" | "CogniteEquipment" | "CogniteEquipmentType" | "CogniteFile" | "CogniteFileCategory" | "CogniteActivity" | "CogniteTimeSeries" | "CogniteAnnotation" | "CogniteDiagramAnnotation" | "CogniteUnit";
|
|
5
5
|
type CogniteDescribable = IndustrialModel<{
|
|
@@ -515,6 +515,7 @@ type CogniteCoreQueryExecutor<TView extends CogniteCoreViewExternalId> = {
|
|
|
515
515
|
}): Promise<QueryResult<QueryResultItem<CogniteCoreModel<TView>, undefined>>>;
|
|
516
516
|
};
|
|
517
517
|
type CogniteCoreAggregateExecutor<TView extends CogniteCoreViewExternalId> = <const TOptions extends Omit<AggregateOptions<CogniteCoreModel<TView>>, "viewExternalId">>(options?: TOptions) => Promise<AggregateResult<AggregateResultItem<CogniteCoreModel<TView>, TOptions["groupBy"], TOptions["aggregate"]>>>;
|
|
518
|
+
type CogniteCoreUpsertExecutor<TView extends CogniteCoreViewExternalId> = (options: Omit<UpsertOptions<CogniteCoreModel<TView>>, "viewExternalId">) => Promise<UpsertResult>;
|
|
518
519
|
|
|
519
520
|
/** Data model id for Cognite Core v1. */
|
|
520
521
|
declare const COGNITE_CORE_DATA_MODEL: {
|
|
@@ -527,6 +528,8 @@ declare class CogniteCoreClient {
|
|
|
527
528
|
constructor(client: CogniteClient, options?: IndustrialModelClientOptions);
|
|
528
529
|
query<TView extends CogniteCoreViewExternalId>(viewExternalId: TView): CogniteCoreQueryExecutor<TView>;
|
|
529
530
|
aggregate<TView extends CogniteCoreViewExternalId>(viewExternalId: TView): CogniteCoreAggregateExecutor<TView>;
|
|
531
|
+
upsert<TView extends CogniteCoreViewExternalId>(viewExternalId: TView): CogniteCoreUpsertExecutor<TView>;
|
|
532
|
+
delete<TItem extends NodeId>(items: TItem[]): Promise<DeleteResult>;
|
|
530
533
|
}
|
|
531
534
|
|
|
532
|
-
export { COGNITE_CORE_DATA_MODEL, type Cognite360Image, type Cognite360ImageAnnotation, type Cognite360ImageCollection, type Cognite360ImageModel, type Cognite360ImageStation, type Cognite3DModel, type Cognite3DObject, type Cognite3DRevision, type Cognite3DTransformation, type CogniteActivity, type CogniteAnnotation, type CogniteAsset, type CogniteAssetClass, type CogniteAssetType, type CogniteCADModel, type CogniteCADNode, type CogniteCADRevision, type CogniteCoreAggregateExecutor, CogniteCoreClient, type CogniteCoreModel, type CogniteCoreModelByView, type CogniteCoreQueryExecutor, type CogniteCoreViewExternalId, type CogniteCubeMap, type CogniteDescribable, type CogniteDiagramAnnotation, type CogniteEquipment, type CogniteEquipmentType, type CogniteFile, type CogniteFileCategory, type CognitePointCloudModel, type CognitePointCloudRevision, type CognitePointCloudVolume, type CogniteSchedulable, type CogniteSourceSystem, type CogniteSourceable, type CogniteTimeSeries, type CogniteUnit, type CogniteVisualizable };
|
|
535
|
+
export { COGNITE_CORE_DATA_MODEL, type Cognite360Image, type Cognite360ImageAnnotation, type Cognite360ImageCollection, type Cognite360ImageModel, type Cognite360ImageStation, type Cognite3DModel, type Cognite3DObject, type Cognite3DRevision, type Cognite3DTransformation, type CogniteActivity, type CogniteAnnotation, type CogniteAsset, type CogniteAssetClass, type CogniteAssetType, type CogniteCADModel, type CogniteCADNode, type CogniteCADRevision, type CogniteCoreAggregateExecutor, CogniteCoreClient, type CogniteCoreModel, type CogniteCoreModelByView, type CogniteCoreQueryExecutor, type CogniteCoreUpsertExecutor, type CogniteCoreViewExternalId, type CogniteCubeMap, type CogniteDescribable, type CogniteDiagramAnnotation, type CogniteEquipment, type CogniteEquipmentType, type CogniteFile, type CogniteFileCategory, type CognitePointCloudModel, type CognitePointCloudRevision, type CognitePointCloudVolume, type CogniteSchedulable, type CogniteSourceSystem, type CogniteSourceable, type CogniteTimeSeries, type CogniteUnit, type CogniteVisualizable };
|
|
@@ -41,6 +41,13 @@ var CogniteSdkAdapter = class {
|
|
|
41
41
|
items: response.items
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
|
+
async applyInstances(request) {
|
|
45
|
+
const apply = this.client.instances.apply;
|
|
46
|
+
const response = await apply(request);
|
|
47
|
+
return {
|
|
48
|
+
items: response.items
|
|
49
|
+
};
|
|
50
|
+
}
|
|
44
51
|
};
|
|
45
52
|
|
|
46
53
|
// src/constants.ts
|
|
@@ -794,6 +801,85 @@ ${result.error.issues.map((issue) => `- ${issue.path.map(String).join(".")}: ${i
|
|
|
794
801
|
return (isList ? z.array(nestedSchema) : nestedSchema).optional();
|
|
795
802
|
}
|
|
796
803
|
};
|
|
804
|
+
var strictNodeIdSchema = z.object({
|
|
805
|
+
space: z.string().min(1),
|
|
806
|
+
externalId: z.string().min(1)
|
|
807
|
+
}).strict();
|
|
808
|
+
var nodeIdLikeSchema = z.object({
|
|
809
|
+
space: z.string().min(1),
|
|
810
|
+
externalId: z.string().min(1)
|
|
811
|
+
}).loose();
|
|
812
|
+
var optionsSchema = z.object({
|
|
813
|
+
viewExternalId: z.string().min(1),
|
|
814
|
+
items: z.array(z.record(z.string(), z.unknown())),
|
|
815
|
+
onEdgeCreation: z.record(z.string(), z.function()).optional(),
|
|
816
|
+
replace: z.boolean().optional(),
|
|
817
|
+
edgeMode: z.enum(["append", "replace"]).optional()
|
|
818
|
+
}).strict();
|
|
819
|
+
function issuePath3(path) {
|
|
820
|
+
return path.length === 0 ? "upsert" : path.map(String).join(".");
|
|
821
|
+
}
|
|
822
|
+
function formatZodIssues3(error, path) {
|
|
823
|
+
return error.issues.map((issue) => `${issuePath3([...path, ...issue.path])}: ${issue.message}`);
|
|
824
|
+
}
|
|
825
|
+
function relationValueSchema(property) {
|
|
826
|
+
if (isReverseDirectRelation(property) && property.targetsList === true) {
|
|
827
|
+
return z.never();
|
|
828
|
+
}
|
|
829
|
+
return z.union([nodeIdLikeSchema, z.array(nodeIdLikeSchema)]);
|
|
830
|
+
}
|
|
831
|
+
var UpsertValidator = class {
|
|
832
|
+
validate(options, rootView) {
|
|
833
|
+
const errors = [];
|
|
834
|
+
const optionsResult = optionsSchema.safeParse(options);
|
|
835
|
+
if (!optionsResult.success) {
|
|
836
|
+
errors.push(...formatZodIssues3(optionsResult.error, []));
|
|
837
|
+
}
|
|
838
|
+
if (options.viewExternalId !== rootView.externalId) {
|
|
839
|
+
errors.push(
|
|
840
|
+
`viewExternalId: expected "${rootView.externalId}", received "${options.viewExternalId}"`
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
for (const [index, item] of options.items.entries()) {
|
|
844
|
+
errors.push(
|
|
845
|
+
...this.validateItem(item, rootView, ["items", index])
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
if (errors.length > 0) {
|
|
849
|
+
throw new Error(`Invalid upsert options:
|
|
850
|
+
${errors.map((error) => `- ${error}`).join("\n")}`);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
validateItem(item, view, path) {
|
|
854
|
+
const errors = [];
|
|
855
|
+
const identityResult = strictNodeIdSchema.safeParse({
|
|
856
|
+
space: item.space,
|
|
857
|
+
externalId: item.externalId
|
|
858
|
+
});
|
|
859
|
+
if (!identityResult.success) {
|
|
860
|
+
errors.push(...formatZodIssues3(identityResult.error, path));
|
|
861
|
+
}
|
|
862
|
+
for (const [name, value] of Object.entries(item)) {
|
|
863
|
+
if (name === "space" || name === "externalId") continue;
|
|
864
|
+
const property = view.properties[name];
|
|
865
|
+
if (!property) {
|
|
866
|
+
errors.push(`${issuePath3([...path, name])}: unknown view property`);
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
if (isViewPropertyDefinition(property)) {
|
|
870
|
+
const schema = property.type.type === "direct" ? property.type.list === true ? z.array(nodeIdLikeSchema) : nodeIdLikeSchema : propertyValueSchema(property);
|
|
871
|
+
const result = schema.safeParse(value);
|
|
872
|
+
if (!result.success) errors.push(...formatZodIssues3(result.error, [...path, name]));
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
if (isReverseDirectRelation(property) || isEdgeConnection(property)) {
|
|
876
|
+
const result = relationValueSchema(property).safeParse(value);
|
|
877
|
+
if (!result.success) errors.push(...formatZodIssues3(result.error, [...path, name]));
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
return errors;
|
|
881
|
+
}
|
|
882
|
+
};
|
|
797
883
|
|
|
798
884
|
// src/mappers/filter-mapper.ts
|
|
799
885
|
var LEAF_OPS = /* @__PURE__ */ new Set([
|
|
@@ -1361,6 +1447,216 @@ var QueryResultMapper = class {
|
|
|
1361
1447
|
}
|
|
1362
1448
|
};
|
|
1363
1449
|
|
|
1450
|
+
// src/mappers/upsert-mapper.ts
|
|
1451
|
+
var IDENTITY_KEYS = /* @__PURE__ */ new Set(["space", "externalId"]);
|
|
1452
|
+
var EDGE_QUERY_LIMIT = 1e3;
|
|
1453
|
+
var UpsertMapper = class {
|
|
1454
|
+
constructor(viewMapper, cognite) {
|
|
1455
|
+
this.viewMapper = viewMapper;
|
|
1456
|
+
this.cognite = cognite;
|
|
1457
|
+
this.validator = new UpsertValidator();
|
|
1458
|
+
}
|
|
1459
|
+
async map(options) {
|
|
1460
|
+
const rootView = await this.viewMapper.getView(options.viewExternalId);
|
|
1461
|
+
this.validator.validate(options, rootView);
|
|
1462
|
+
const edgeMode = options.edgeMode ?? "append";
|
|
1463
|
+
const mappedItems = options.items.map(
|
|
1464
|
+
(item) => this.mapItem(item, rootView, options.onEdgeCreation, edgeMode)
|
|
1465
|
+
);
|
|
1466
|
+
const items = mappedItems.flatMap((item) => item.writes);
|
|
1467
|
+
const edgeReplacements = mappedItems.flatMap((item) => item.edgeReplacements);
|
|
1468
|
+
const deleteItems = edgeMode === "replace" ? await this.mapEdgeReplacementDeletes(edgeReplacements) : [];
|
|
1469
|
+
return {
|
|
1470
|
+
items,
|
|
1471
|
+
...deleteItems.length > 0 ? { delete: deleteItems } : {},
|
|
1472
|
+
...options.replace === true ? { replace: true } : {}
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
mapItem(item, rootView, onEdgeCreation, edgeMode) {
|
|
1476
|
+
const node = { space: item.space, externalId: item.externalId };
|
|
1477
|
+
const nodeProperties = {};
|
|
1478
|
+
const inferredItems = [];
|
|
1479
|
+
const edgeReplacements = [];
|
|
1480
|
+
for (const [name, value] of Object.entries(item)) {
|
|
1481
|
+
if (IDENTITY_KEYS.has(name)) continue;
|
|
1482
|
+
const property = rootView.properties[name];
|
|
1483
|
+
if (!property) continue;
|
|
1484
|
+
if (isViewPropertyDefinition(property)) {
|
|
1485
|
+
nodeProperties[name] = normalizeViewPropertyValue(value, property);
|
|
1486
|
+
} else if (isReverseDirectRelation(property)) {
|
|
1487
|
+
inferredItems.push(...this.mapReverseDirectRelation(node, value, property));
|
|
1488
|
+
} else if (isEdgeConnection(property)) {
|
|
1489
|
+
const desiredEdges = this.mapEdgeConnection(node, name, value, property, onEdgeCreation);
|
|
1490
|
+
inferredItems.push(...desiredEdges);
|
|
1491
|
+
if (edgeMode === "replace") {
|
|
1492
|
+
edgeReplacements.push({ rootNode: node, propertyName: name, property, desiredEdges });
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
const applyNode = {
|
|
1497
|
+
instanceType: "node",
|
|
1498
|
+
...node
|
|
1499
|
+
};
|
|
1500
|
+
if (Object.keys(nodeProperties).length > 0) {
|
|
1501
|
+
applyNode.sources = [{ source: toViewReference(rootView), properties: nodeProperties }];
|
|
1502
|
+
}
|
|
1503
|
+
return { writes: [applyNode, ...inferredItems], edgeReplacements };
|
|
1504
|
+
}
|
|
1505
|
+
async mapEdgeReplacementDeletes(replacements) {
|
|
1506
|
+
const deletes = await Promise.all(
|
|
1507
|
+
replacements.map(async (replacement) => {
|
|
1508
|
+
const existingEdges = await this.queryExistingEdges(replacement);
|
|
1509
|
+
const desiredEdgeKeys = new Set(replacement.desiredEdges.map((edge) => instanceKey(edge)));
|
|
1510
|
+
return existingEdges.filter((edge) => !desiredEdgeKeys.has(instanceKey(edge))).map((edge) => ({
|
|
1511
|
+
instanceType: "edge",
|
|
1512
|
+
space: edge.space,
|
|
1513
|
+
externalId: edge.externalId
|
|
1514
|
+
}));
|
|
1515
|
+
})
|
|
1516
|
+
);
|
|
1517
|
+
return uniqueDeletes(deletes.flat());
|
|
1518
|
+
}
|
|
1519
|
+
async queryExistingEdges(replacement) {
|
|
1520
|
+
const rootKey = `${replacement.propertyName}Root`;
|
|
1521
|
+
const edgeKey = `${replacement.propertyName}Edges`;
|
|
1522
|
+
const direction = replacement.property.direction ?? "outwards";
|
|
1523
|
+
const query = {
|
|
1524
|
+
with: {
|
|
1525
|
+
[rootKey]: {
|
|
1526
|
+
nodes: {
|
|
1527
|
+
filter: {
|
|
1528
|
+
instanceReferences: [replacement.rootNode]
|
|
1529
|
+
}
|
|
1530
|
+
},
|
|
1531
|
+
limit: 1
|
|
1532
|
+
},
|
|
1533
|
+
[edgeKey]: {
|
|
1534
|
+
edges: {
|
|
1535
|
+
from: rootKey,
|
|
1536
|
+
maxDistance: 1,
|
|
1537
|
+
direction,
|
|
1538
|
+
filter: {
|
|
1539
|
+
equals: { property: ["edge", "type"], value: replacement.property.type }
|
|
1540
|
+
}
|
|
1541
|
+
},
|
|
1542
|
+
limit: EDGE_QUERY_LIMIT
|
|
1543
|
+
}
|
|
1544
|
+
},
|
|
1545
|
+
select: {
|
|
1546
|
+
[rootKey]: {},
|
|
1547
|
+
[edgeKey]: {}
|
|
1548
|
+
}
|
|
1549
|
+
};
|
|
1550
|
+
const edges = [];
|
|
1551
|
+
let cursor;
|
|
1552
|
+
do {
|
|
1553
|
+
const response = await this.cognite.queryInstances({
|
|
1554
|
+
...query,
|
|
1555
|
+
...cursor ? { cursors: { [edgeKey]: cursor } } : {}
|
|
1556
|
+
});
|
|
1557
|
+
edges.push(
|
|
1558
|
+
...(response.items[edgeKey] ?? []).filter(
|
|
1559
|
+
(item) => item.instanceType === "edge"
|
|
1560
|
+
)
|
|
1561
|
+
);
|
|
1562
|
+
cursor = response.nextCursor[edgeKey];
|
|
1563
|
+
} while (cursor);
|
|
1564
|
+
return edges;
|
|
1565
|
+
}
|
|
1566
|
+
mapReverseDirectRelation(node, value, property) {
|
|
1567
|
+
const targets = asNodeIdArray(value);
|
|
1568
|
+
return targets.map((target) => ({
|
|
1569
|
+
instanceType: "node",
|
|
1570
|
+
...target,
|
|
1571
|
+
sources: [
|
|
1572
|
+
{
|
|
1573
|
+
source: property.through.source,
|
|
1574
|
+
properties: {
|
|
1575
|
+
[property.through.identifier]: normalizeReverseDirectRelationValue(node, property)
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
]
|
|
1579
|
+
}));
|
|
1580
|
+
}
|
|
1581
|
+
mapEdgeConnection(node, propertyName, value, property, onEdgeCreation) {
|
|
1582
|
+
const direction = property.direction ?? "outwards";
|
|
1583
|
+
return asNodeIdArray(value).map((target) => {
|
|
1584
|
+
const startNode = direction === "inwards" ? target : node;
|
|
1585
|
+
const endNode = direction === "inwards" ? node : target;
|
|
1586
|
+
const edgeType = toNodeId(property.type, `edge type for "${propertyName}"`);
|
|
1587
|
+
const createEdgeId = onEdgeCreation?.[propertyName];
|
|
1588
|
+
if (!createEdgeId) {
|
|
1589
|
+
throw new Error(
|
|
1590
|
+
`Invalid upsert options:
|
|
1591
|
+
- onEdgeCreation.${propertyName}: required when ingesting edge connection "${propertyName}"`
|
|
1592
|
+
);
|
|
1593
|
+
}
|
|
1594
|
+
const edgeId = createEdgeId({
|
|
1595
|
+
startNode,
|
|
1596
|
+
endNode,
|
|
1597
|
+
edgeType
|
|
1598
|
+
});
|
|
1599
|
+
assertNodeId(edgeId, `onEdgeCreation(${propertyName})`);
|
|
1600
|
+
return {
|
|
1601
|
+
instanceType: "edge",
|
|
1602
|
+
...edgeId,
|
|
1603
|
+
type: property.type,
|
|
1604
|
+
startNode,
|
|
1605
|
+
endNode
|
|
1606
|
+
};
|
|
1607
|
+
});
|
|
1608
|
+
}
|
|
1609
|
+
};
|
|
1610
|
+
function normalizeReverseDirectRelationValue(node, property) {
|
|
1611
|
+
return property.targetsList === true ? [node] : node;
|
|
1612
|
+
}
|
|
1613
|
+
function normalizeViewPropertyValue(value, property) {
|
|
1614
|
+
if (property.type.type !== "direct") return normalizePropertyValue(value);
|
|
1615
|
+
if (Array.isArray(value)) return value.map((item) => toNodeId(item));
|
|
1616
|
+
return toNodeId(value);
|
|
1617
|
+
}
|
|
1618
|
+
function normalizePropertyValue(value) {
|
|
1619
|
+
if (value instanceof Date) return value.toISOString();
|
|
1620
|
+
if (Array.isArray(value)) return value.map(normalizePropertyValue);
|
|
1621
|
+
if (isPlainObject(value)) {
|
|
1622
|
+
return Object.fromEntries(
|
|
1623
|
+
Object.entries(value).map(([key, nestedValue]) => [key, normalizePropertyValue(nestedValue)])
|
|
1624
|
+
);
|
|
1625
|
+
}
|
|
1626
|
+
return value;
|
|
1627
|
+
}
|
|
1628
|
+
function asNodeIdArray(value) {
|
|
1629
|
+
return Array.isArray(value) ? value.map((item) => toNodeId(item)) : [toNodeId(value)];
|
|
1630
|
+
}
|
|
1631
|
+
function toNodeId(value, label = "relation reference") {
|
|
1632
|
+
if (!isPlainObject(value) || typeof value.space !== "string" || typeof value.externalId !== "string") {
|
|
1633
|
+
throw new Error(`Invalid upsert options:
|
|
1634
|
+
- ${label}: expected a NodeId`);
|
|
1635
|
+
}
|
|
1636
|
+
return { space: value.space, externalId: value.externalId };
|
|
1637
|
+
}
|
|
1638
|
+
function instanceKey(instance) {
|
|
1639
|
+
return `${instance.space}\0${instance.externalId}`;
|
|
1640
|
+
}
|
|
1641
|
+
function uniqueDeletes(deletes) {
|
|
1642
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1643
|
+
return deletes.filter((item) => {
|
|
1644
|
+
const key = instanceKey(item);
|
|
1645
|
+
if (seen.has(key)) return false;
|
|
1646
|
+
seen.add(key);
|
|
1647
|
+
return true;
|
|
1648
|
+
});
|
|
1649
|
+
}
|
|
1650
|
+
function assertNodeId(value, label) {
|
|
1651
|
+
if (!isPlainObject(value) || typeof value.space !== "string" || typeof value.externalId !== "string") {
|
|
1652
|
+
throw new Error(`Invalid upsert options:
|
|
1653
|
+
- ${label}: expected a NodeId`);
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
function isPlainObject(value) {
|
|
1657
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1364
1660
|
// src/mappers/view-mapper.ts
|
|
1365
1661
|
var ViewMapper = class {
|
|
1366
1662
|
constructor(cognite, dataModelId) {
|
|
@@ -1408,6 +1704,7 @@ var ViewMapper = class {
|
|
|
1408
1704
|
};
|
|
1409
1705
|
|
|
1410
1706
|
// src/client.ts
|
|
1707
|
+
var APPLY_ITEM_LIMIT = 1e3;
|
|
1411
1708
|
var IndustrialModelClient = class {
|
|
1412
1709
|
constructor(client, dataModelId, options = {}) {
|
|
1413
1710
|
const cognite = createCogniteAdapter(client);
|
|
@@ -1415,6 +1712,7 @@ var IndustrialModelClient = class {
|
|
|
1415
1712
|
const viewMapper = new ViewMapper(cognite, dataModelId);
|
|
1416
1713
|
this.queryMapper = new QueryMapper(viewMapper, cognite);
|
|
1417
1714
|
this.aggregateMapper = new AggregateMapper(viewMapper, cognite);
|
|
1715
|
+
this.upsertMapper = new UpsertMapper(viewMapper, cognite);
|
|
1418
1716
|
this.aggregateResultMapper = new AggregateResultMapper();
|
|
1419
1717
|
this.resultMapper = new QueryResultMapper(viewMapper);
|
|
1420
1718
|
this.resultValidator = new QueryResultValidator(viewMapper);
|
|
@@ -1428,6 +1726,54 @@ var IndustrialModelClient = class {
|
|
|
1428
1726
|
const execute = (options) => this.aggregateInternal(options);
|
|
1429
1727
|
return execute;
|
|
1430
1728
|
}
|
|
1729
|
+
upsert() {
|
|
1730
|
+
const execute = (options) => this.upsertInternal(options);
|
|
1731
|
+
return execute;
|
|
1732
|
+
}
|
|
1733
|
+
async delete(items) {
|
|
1734
|
+
const deleteItems = items.map((item) => {
|
|
1735
|
+
assertNodeId2(item);
|
|
1736
|
+
return {
|
|
1737
|
+
instanceType: "node",
|
|
1738
|
+
space: item.space,
|
|
1739
|
+
externalId: item.externalId
|
|
1740
|
+
};
|
|
1741
|
+
});
|
|
1742
|
+
const response = await this.applyInstancesInChunks({
|
|
1743
|
+
items: [],
|
|
1744
|
+
delete: deleteItems
|
|
1745
|
+
});
|
|
1746
|
+
return { items: response.items };
|
|
1747
|
+
}
|
|
1748
|
+
async upsertInternal(options) {
|
|
1749
|
+
const cogniteRequest = await this.upsertMapper.map(options);
|
|
1750
|
+
const response = await this.applyInstancesInChunks(cogniteRequest);
|
|
1751
|
+
return { items: response.items };
|
|
1752
|
+
}
|
|
1753
|
+
async applyInstancesInChunks(request) {
|
|
1754
|
+
const deleteItems = request.delete ?? [];
|
|
1755
|
+
const totalItems = request.items.length + deleteItems.length;
|
|
1756
|
+
if (totalItems === 0) return { items: [] };
|
|
1757
|
+
if (totalItems <= APPLY_ITEM_LIMIT) {
|
|
1758
|
+
return this.cognite.applyInstances(request);
|
|
1759
|
+
}
|
|
1760
|
+
const responses = [];
|
|
1761
|
+
for (const deleteChunk of chunks(deleteItems, APPLY_ITEM_LIMIT)) {
|
|
1762
|
+
const response = await this.cognite.applyInstances({
|
|
1763
|
+
items: [],
|
|
1764
|
+
delete: deleteChunk
|
|
1765
|
+
});
|
|
1766
|
+
responses.push(...response.items);
|
|
1767
|
+
}
|
|
1768
|
+
for (const itemChunk of chunks(request.items, APPLY_ITEM_LIMIT)) {
|
|
1769
|
+
const response = await this.cognite.applyInstances({
|
|
1770
|
+
items: itemChunk,
|
|
1771
|
+
...request.replace === true ? { replace: true } : {}
|
|
1772
|
+
});
|
|
1773
|
+
responses.push(...response.items);
|
|
1774
|
+
}
|
|
1775
|
+
return { items: responses };
|
|
1776
|
+
}
|
|
1431
1777
|
async aggregateInternal(options) {
|
|
1432
1778
|
const cogniteRequest = await this.aggregateMapper.map(options);
|
|
1433
1779
|
const response = await this.cognite.aggregateInstances(cogniteRequest);
|
|
@@ -1484,6 +1830,18 @@ var IndustrialModelClient = class {
|
|
|
1484
1830
|
return appendNodesAndEdges(result, nestedResults);
|
|
1485
1831
|
}
|
|
1486
1832
|
};
|
|
1833
|
+
function chunks(items, size) {
|
|
1834
|
+
const result = [];
|
|
1835
|
+
for (let index = 0; index < items.length; index += size) {
|
|
1836
|
+
result.push(items.slice(index, index + size));
|
|
1837
|
+
}
|
|
1838
|
+
return result;
|
|
1839
|
+
}
|
|
1840
|
+
function assertNodeId2(value) {
|
|
1841
|
+
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) {
|
|
1842
|
+
throw new Error("Invalid delete options:\n- items: expected NodeId values");
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1487
1845
|
|
|
1488
1846
|
// src/cognite-core/client.ts
|
|
1489
1847
|
var COGNITE_CORE_DATA_MODEL = {
|
|
@@ -1506,6 +1864,14 @@ var CogniteCoreClient = class {
|
|
|
1506
1864
|
const execute = (options = {}) => aggregate({ ...options, viewExternalId });
|
|
1507
1865
|
return execute;
|
|
1508
1866
|
}
|
|
1867
|
+
upsert(viewExternalId) {
|
|
1868
|
+
const upsert = this.model.upsert();
|
|
1869
|
+
const execute = (options) => upsert({ ...options, viewExternalId });
|
|
1870
|
+
return execute;
|
|
1871
|
+
}
|
|
1872
|
+
delete(items) {
|
|
1873
|
+
return this.model.delete(items);
|
|
1874
|
+
}
|
|
1509
1875
|
};
|
|
1510
1876
|
|
|
1511
1877
|
export { COGNITE_CORE_DATA_MODEL, CogniteCoreClient };
|