industrial-model 0.7.0 → 0.9.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 CHANGED
@@ -13,6 +13,8 @@ TypeScript SDK for querying [Cognite Flexible Data Models (FDM)](https://docs.co
13
13
  - **Pagination support** - use cursors manually or fetch all root pages with `limit: -1`.
14
14
  - **Aggregation support** - count, group, list distinct values, and aggregate numeric properties.
15
15
  - **Mutation support** - upsert model-shaped node patches and delete nodes by identity.
16
+ - **Datapoints support** - read, write, and delete time series datapoints through model-friendly option objects.
17
+ - **Files support** - upload files and retrieve pre-signed download URLs, keyed to model node identities.
16
18
  - **Runtime validation option** - parse query results with Zod schemas derived from Cognite view metadata.
17
19
  - **CJS and ESM builds** - works in Node.js and common bundler setups.
18
20
 
@@ -627,6 +629,122 @@ await core.delete([{ space: "asset-space", externalId: "pump-1" }]);
627
629
 
628
630
  Deletes are sent through Cognite apply. When more than 1000 nodes are provided, the SDK splits them into multiple Cognite calls.
629
631
 
632
+ ## Datapoints
633
+
634
+ Use `model.datapoints` for Cognite time series data. The public API talks in terms of `timeSeries`, ranges, and datapoint batches; the Cognite SDK `items` and `instanceId` payload shape stays internal.
635
+
636
+ ```ts
637
+ const { items } = await model.datapoints.retrieve({
638
+ timeSeries: [{ space: "ts-space", externalId: "temperature", targetUnit: "degC" }],
639
+ start: new Date("2024-01-01T00:00:00.000Z"),
640
+ end: new Date("2024-01-02T00:00:00.000Z"),
641
+ limit: 100,
642
+ });
643
+
644
+ items[0]?.timeSeries.externalId;
645
+ items[0]?.datapoints;
646
+ items[0]?.cursor; // next cursor, or null
647
+ ```
648
+
649
+ Pass `limit: -1` to follow datapoint cursors and return all pages for each requested time series.
650
+
651
+ ```ts
652
+ const history = await model.datapoints.retrieve({
653
+ timeSeries: [{ space: "ts-space", externalId: "temperature" }],
654
+ start: new Date("2024-01-01T00:00:00.000Z"),
655
+ limit: -1,
656
+ });
657
+ ```
658
+
659
+ Read the latest datapoint before a given timestamp:
660
+
661
+ ```ts
662
+ const latest = await model.datapoints.latest({
663
+ timeSeries: [{ space: "ts-space", externalId: "temperature" }],
664
+ ignoreUnknownIds: true,
665
+ });
666
+ ```
667
+
668
+ Write datapoints by passing an array of insert items directly:
669
+
670
+ ```ts
671
+ await model.datapoints.insert([
672
+ {
673
+ timeSeries: { space: "ts-space", externalId: "temperature" },
674
+ datapoints: [{ timestamp: new Date(), value: 42 }],
675
+ },
676
+ ]);
677
+ ```
678
+
679
+ Delete datapoints by passing an array of ranges directly:
680
+
681
+ ```ts
682
+ await model.datapoints.delete([
683
+ {
684
+ timeSeries: { space: "ts-space", externalId: "temperature" },
685
+ start: new Date("2024-01-01T00:00:00.000Z"),
686
+ end: new Date("2024-01-02T00:00:00.000Z"),
687
+ },
688
+ ]);
689
+ ```
690
+
691
+ ## Files
692
+
693
+ Use `model.files` to upload files to Cognite and retrieve pre-signed download URLs. Both operations use `space` and `externalId` node identities as keys; the Cognite `instanceId` payload shape stays internal.
694
+
695
+ Upload a file and optionally pass the binary content as a second argument to complete the upload in one call. When content is provided, Cognite uploads it inline and `uploaded` is `true` on the result.
696
+
697
+ ```ts
698
+ const result = await model.files.upload(
699
+ {
700
+ space: "file-space",
701
+ externalId: "report-2024",
702
+ name: "annual-report.pdf",
703
+ mimeType: "application/pdf",
704
+ directory: "/reports",
705
+ source: "erp",
706
+ metadata: { department: "finance", year: "2024" },
707
+ },
708
+ pdfContent, // Buffer, Blob, ArrayBuffer, ReadableStream, …
709
+ );
710
+
711
+ result.space; // "file-space"
712
+ result.externalId; // "report-2024"
713
+ result.name; // "annual-report.pdf"
714
+ result.uploaded; // true — content was provided
715
+ result.createdTime; // Date
716
+ result.lastUpdatedTime; // Date
717
+ ```
718
+
719
+ Omit the content argument to receive a pre-signed `uploadUrl` instead. Use it to push the file binary from the client side without routing it through your server:
720
+
721
+ ```ts
722
+ const result = await model.files.upload({
723
+ space: "file-space",
724
+ externalId: "report-2024",
725
+ name: "annual-report.pdf",
726
+ mimeType: "application/pdf",
727
+ });
728
+
729
+ result.uploaded; // false — content not yet uploaded
730
+ result.uploadUrl; // "https://storage.example.com/…" — use to PUT the file
731
+ ```
732
+
733
+ Retrieve pre-signed download URLs for one or more files by node identity:
734
+
735
+ ```ts
736
+ const urls = await model.files.getDownloadUrls([
737
+ { space: "file-space", externalId: "report-2024" },
738
+ { space: "file-space", externalId: "manual-v3" },
739
+ ]);
740
+
741
+ urls[0]?.downloadUrl; // pre-signed URL ready to use
742
+ urls[0]?.space;
743
+ urls[0]?.externalId;
744
+ ```
745
+
746
+ The returned array preserves the input order. Passing an empty array returns `[]` without calling Cognite.
747
+
630
748
  ## Aggregation
631
749
 
632
750
  Use `aggregate()` when you need grouped counts, distinct values, or numeric summaries without loading every instance.
@@ -826,6 +944,15 @@ Deletes are view-independent:
826
944
  await core.delete([{ space: "asset-space", externalId: "pump-1" }]);
827
945
  ```
828
946
 
947
+ `core.datapoints` exposes the same executor API as `model.datapoints` on `IndustrialModelClient`:
948
+
949
+ ```ts
950
+ const { items } = await core.datapoints.retrieve({
951
+ timeSeries: [{ space: "ts-space", externalId: "temperature" }],
952
+ limit: 100,
953
+ });
954
+ ```
955
+
829
956
  All Cognite Core view types are exported from `industrial-model` and can be imported directly for use with `IndustrialModelClient` if needed:
830
957
 
831
958
  ```ts
@@ -987,6 +1114,10 @@ Same as `model.upsert<TModel>()(options)` on `IndustrialModelClient`, with the v
987
1114
 
988
1115
  Same as `model.delete(items)` on `IndustrialModelClient`. Deletes nodes by `space` and `externalId`; no view name is required.
989
1116
 
1117
+ ### `core.datapoints`
1118
+
1119
+ Same as `model.datapoints` on `IndustrialModelClient`. All four methods — `retrieve`, `latest`, `insert`, and `delete` — are available without a view name.
1120
+
990
1121
  ### `new IndustrialModelClient(client, dataModelId, options?)`
991
1122
 
992
1123
  | Parameter | Type | Description |
@@ -1093,6 +1224,50 @@ Provide at least one of `groupBy` or `aggregate`. Omit `aggregate` to fetch dist
1093
1224
  | `max` | `{ max: "volume" }` | Maximum numeric value. |
1094
1225
  | `sum` | `{ sum: "volume" }` | Sum of a numeric property. |
1095
1226
 
1227
+ ### `model.files.upload(fileInfo, content?)`
1228
+
1229
+ Uploads a file to Cognite. `fileInfo` fields:
1230
+
1231
+ | Field | Type | Description |
1232
+ | --- | --- | --- |
1233
+ | `space` | `string` | Node space. |
1234
+ | `externalId` | `string` | Node external ID. |
1235
+ | `name` | `string` | File name. |
1236
+ | `mimeType` | `string` | Optional MIME type. |
1237
+ | `directory` | `string` | Optional directory path within the project. |
1238
+ | `source` | `string` | Optional source system identifier. |
1239
+ | `metadata` | `Record<string, string>` | Optional key-value metadata. |
1240
+
1241
+ `content` accepts any value accepted by the Cognite SDK file upload (`Buffer`, `Blob`, `ArrayBuffer`, `ReadableStream`, …). When provided, the file is uploaded inline and `uploaded` is `true`. When omitted, Cognite returns a pre-signed `uploadUrl` for a separate upload and `uploaded` is `false`.
1242
+
1243
+ Returns `FileUploadResult`:
1244
+
1245
+ ```ts
1246
+ type FileUploadResult = NodeId & {
1247
+ name: string;
1248
+ uploaded: boolean;
1249
+ mimeType?: string;
1250
+ directory?: string;
1251
+ source?: string;
1252
+ uploadedTime?: Date;
1253
+ createdTime: Date;
1254
+ lastUpdatedTime: Date;
1255
+ uploadUrl?: string;
1256
+ };
1257
+ ```
1258
+
1259
+ ### `model.files.getDownloadUrls(nodeIds)`
1260
+
1261
+ Retrieves one pre-signed download URL per node, in the same order as the input. Passing an empty array returns `[]` without calling Cognite.
1262
+
1263
+ Returns `FileDownloadUrl[]`:
1264
+
1265
+ ```ts
1266
+ type FileDownloadUrl = NodeId & {
1267
+ downloadUrl: string;
1268
+ };
1269
+ ```
1270
+
1096
1271
  ### Filter Operators
1097
1272
 
1098
1273
  | Field type | Operators |
@@ -1132,6 +1307,8 @@ Logical combinators `AND`, `OR`, and `NOT` are supported at any nesting level, i
1132
1307
  | `UpsertResult`, `UpsertResultItem` | Upsert output types. |
1133
1308
  | `DeleteExecutor`, `DeleteResult`, `DeleteResultItem` | Delete helper and output types. |
1134
1309
  | `EdgeCreationContext`, `EdgeCreationCallback`, `EdgeCreationCallbacks`, `EdgeMode` | Edge upsert helper types. |
1310
+ | `DatapointsExecutor`, `DatapointsRetrieveOptions`, `DatapointsLatestOptions`, `DatapointsLatestSeries`, `DatapointsInsertItem`, `DatapointsDeleteRange`, `DatapointsResult`, `DatapointSeriesResult`, `DatapointAggregate` | Datapoints types. |
1311
+ | `FilesExecutor`, `FileUploadInfo`, `FileUploadResult`, `FileDownloadUrl` | Files types. |
1135
1312
  | `IndustrialModelClientOptions` | Client configuration options. |
1136
1313
 
1137
1314
  **Cognite Core**
@@ -1156,6 +1333,10 @@ Logical combinators `AND`, `OR`, and `NOT` are supported at any nesting level, i
1156
1333
  | `Cognite360Image`, `Cognite360ImageModel`, `Cognite360ImageCollection`, `Cognite360ImageStation`, `Cognite360ImageAnnotation` | 360 image views. |
1157
1334
  | `CogniteCubeMap` | Cube map view. |
1158
1335
 
1336
+ ## Code Generator
1337
+
1338
+ The package includes a CLI to generate typed models and client code from a Cognite data model. See the [CLI documentation](./src/cli/README.md).
1339
+
1159
1340
  ## Releasing
1160
1341
 
1161
1342
  This project uses [Changesets](https://github.com/changesets/changesets) to manage versions and changelogs.