industrial-model 0.2.0 → 0.4.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.d.cts CHANGED
@@ -1,7 +1,46 @@
1
1
  import { CogniteClient } from '@cognite/sdk';
2
+ import { z } from 'zod';
2
3
 
4
+ interface ViewReference {
5
+ type: "view";
6
+ space: string;
7
+ externalId: string;
8
+ version: string;
9
+ }
10
+ interface ViewPropertyType {
11
+ type?: string;
12
+ source?: ViewReference;
13
+ list?: boolean;
14
+ }
15
+ interface ViewPropertyDefinition {
16
+ container: unknown;
17
+ containerPropertyIdentifier: string;
18
+ type: ViewPropertyType;
19
+ }
20
+ interface ReverseDirectRelationConnection {
21
+ through: {
22
+ source: ViewReference;
23
+ identifier: string;
24
+ };
25
+ source: ViewReference;
26
+ connectionType?: string;
27
+ targetsList?: boolean;
28
+ }
29
+ interface EdgeConnection {
30
+ type: unknown;
31
+ source: ViewReference;
32
+ direction?: "outwards" | "inwards";
33
+ }
34
+ type ViewDefinitionProperty = ViewPropertyDefinition | ReverseDirectRelationConnection | EdgeConnection;
35
+ interface ViewDefinition {
36
+ space: string;
37
+ externalId: string;
38
+ version: string;
39
+ properties: Record<string, ViewDefinitionProperty>;
40
+ }
3
41
  interface NodeDefinition {
4
42
  instanceType: "node";
43
+ version?: number;
5
44
  space: string;
6
45
  externalId: string;
7
46
  properties?: Record<string, Record<string, Record<string, unknown>>>;
@@ -18,6 +57,9 @@ type DataModelId = NodeId & {
18
57
  version: string;
19
58
  };
20
59
  type SortDirection = "ascending" | "descending";
60
+ interface IndustrialModelClientOptions {
61
+ validateResults?: boolean;
62
+ }
21
63
  type Simplify<T> = {
22
64
  [K in keyof T]: T[K];
23
65
  } & {};
@@ -61,11 +103,12 @@ interface QueryOptions<TModel, TSelect extends QuerySelect<TModel> | undefined =
61
103
  limit?: number;
62
104
  cursor?: string | null;
63
105
  }
64
- type QueryResultMetadata = Pick<NodeDefinition, "space" | "externalId" | "createdTime" | "deletedTime" | "lastUpdatedTime">;
106
+ type QueryResultMetadata = Pick<NodeDefinition, "space" | "externalId" | "version" | "createdTime" | "deletedTime" | "lastUpdatedTime">;
65
107
  type ResultShapeForKey<TModel, K extends PropertyKey> = K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : K extends RelationKeys<TModel> ? ModelRelations<TModel>[K] : never;
66
108
  type ResultEntityForKey<TModel, K extends PropertyKey> = K extends RelationKeys<TModel> ? ModelRelations<TModel>[K] : K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never;
67
109
  type WrapResultValue<TShape, TValue> = [NonNull<TShape>] extends [readonly unknown[]] ? TValue[] : TValue;
68
- type SelectedValue<TModel, K extends PropertyKey, TValue, TDepth extends QueryDepth> = TValue extends true ? K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never : TDepth extends 0 ? never : TValue extends object ? WrapResultValue<ResultShapeForKey<TModel, K>, QueryResultItem<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, TValue & QuerySelect<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, PrevDepth[TDepth]>, PrevDepth[TDepth]>> : never;
110
+ type AsQuerySelect<TModel, TSelect> = TSelect extends QuerySelect<TModel> ? TSelect : never;
111
+ type SelectedValue<TModel, K extends PropertyKey, TValue, TDepth extends QueryDepth> = TValue extends true ? K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never : TDepth extends 0 ? never : TValue extends object ? WrapResultValue<ResultShapeForKey<TModel, K>, QueryResultItem<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, AsQuerySelect<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, TValue>, PrevDepth[TDepth]>> : never;
69
112
  type ExplicitSelectionResult<TModel, TSelect, TDepth extends QueryDepth> = Simplify<{
70
113
  [K in keyof NonNull<TSelect> as K extends "_all" ? never : SelectedValue<TModel, K, NonNull<TSelect>[K], TDepth> extends never ? never : IsOptionalKey<ModelProps<TModel>, K> extends true ? never : IsOptionalKey<ModelRelations<TModel>, K> extends true ? never : K]-?: SelectedValue<TModel, K, NonNull<TSelect>[K], TDepth>;
71
114
  } & {
@@ -78,6 +121,82 @@ interface QueryResult<TItem = Record<string, unknown>> {
78
121
  items: TItem[];
79
122
  cursor: string | null;
80
123
  }
124
+ type QueryExecutor<TModel> = {
125
+ <const TSelect extends QuerySelect<TModel>>(options: Omit<QueryOptions<TModel, TSelect>, "select"> & {
126
+ select: TSelect & QuerySelect<TModel>;
127
+ }): Promise<QueryResult<QueryResultItem<TModel, TSelect>>>;
128
+ (options: Omit<QueryOptions<TModel, undefined>, "select"> & {
129
+ select?: undefined;
130
+ }): Promise<QueryResult<QueryResultItem<TModel, undefined>>>;
131
+ };
132
+ type GroupableValue<T> = [NonNull<T>] extends [NodeId] ? true : [NonNull<T>] extends [string | number | boolean] ? true : false;
133
+ type GroupByKey<TModel> = {
134
+ [K in keyof ModelProps<TModel>]: GroupableValue<ModelProps<TModel>[K]> extends true ? K : never;
135
+ }[keyof ModelProps<TModel>];
136
+ type AggregateGroupBy<TModel> = {
137
+ [K in GroupByKey<TModel>]?: true;
138
+ };
139
+ type NumericKey<TModel> = {
140
+ [K in keyof ModelProps<TModel>]: ModelProps<TModel>[K] extends number ? K : never;
141
+ }[keyof ModelProps<TModel>];
142
+ type CountableKey<TModel> = GroupByKey<TModel> | "externalId" | "space";
143
+ type AggregateDefinition<TModel> = {
144
+ avg: NumericKey<TModel>;
145
+ } | {
146
+ min: NumericKey<TModel>;
147
+ } | {
148
+ max: NumericKey<TModel>;
149
+ } | {
150
+ sum: NumericKey<TModel>;
151
+ } | {
152
+ count: CountableKey<TModel> | Record<string, never>;
153
+ };
154
+ type SelectedGroupKeys<TGroupBy> = Extract<{
155
+ [K in keyof TGroupBy & string]: TGroupBy[K] extends true ? K : never;
156
+ }[keyof TGroupBy & string], string>;
157
+ type GroupValues<TModel, TGroupBy extends AggregateGroupBy<TModel> | undefined> = TGroupBy extends AggregateGroupBy<TModel> ? Simplify<Pick<ModelProps<TModel>, SelectedGroupKeys<TGroupBy> & keyof ModelProps<TModel>>> : undefined;
158
+ type AggregateValue<TDef> = TDef extends {
159
+ avg: infer P extends PropertyKey;
160
+ } ? {
161
+ property: P;
162
+ value: number;
163
+ } : TDef extends {
164
+ min: infer P extends PropertyKey;
165
+ } ? {
166
+ property: P;
167
+ value: number;
168
+ } : TDef extends {
169
+ max: infer P extends PropertyKey;
170
+ } ? {
171
+ property: P;
172
+ value: number;
173
+ } : TDef extends {
174
+ sum: infer P extends PropertyKey;
175
+ } ? {
176
+ property: P;
177
+ value: number;
178
+ } : TDef extends {
179
+ count: infer P;
180
+ } ? Record<string, never> extends P ? {
181
+ value: number;
182
+ } : {
183
+ property: P;
184
+ value: number;
185
+ } : never;
186
+ interface AggregateOptions<TModel> {
187
+ viewExternalId: string;
188
+ filters?: WhereInput<TModel>;
189
+ groupBy?: AggregateGroupBy<TModel>;
190
+ aggregate?: AggregateDefinition<TModel>;
191
+ }
192
+ type AggregateResultItem<TModel, TGroupBy extends AggregateGroupBy<TModel> | undefined = undefined, TAggregate extends AggregateDefinition<TModel> | undefined = undefined> = {
193
+ group?: GroupValues<TModel, TGroupBy>;
194
+ aggregate?: AggregateValue<TAggregate>;
195
+ };
196
+ interface AggregateResult<TItem = Record<string, unknown>> {
197
+ items: TItem[];
198
+ }
199
+ type AggregateExecutor<TModel> = <const TOptions extends AggregateOptions<TModel>>(options: TOptions) => Promise<AggregateResult<AggregateResultItem<TModel, TOptions["groupBy"], TOptions["aggregate"]>>>;
81
200
  type StringFilters = {
82
201
  eq?: string;
83
202
  in?: string[];
@@ -130,11 +249,26 @@ type WhereInput<TModel> = {
130
249
  declare class IndustrialModelClient {
131
250
  private readonly cognite;
132
251
  private readonly queryMapper;
252
+ private readonly aggregateMapper;
253
+ private readonly aggregateResultMapper;
133
254
  private readonly resultMapper;
134
- constructor(client: CogniteClient, dataModelId: DataModelId);
135
- query<TModel>(): <const TSelect extends QuerySelect<TModel> | undefined = undefined>(options: QueryOptions<TModel, TSelect>) => Promise<QueryResult<QueryResultItem<TModel, TSelect>>>;
255
+ private readonly resultValidator;
256
+ private readonly validateResults;
257
+ constructor(client: CogniteClient, dataModelId: DataModelId, options?: IndustrialModelClientOptions);
258
+ query<TModel>(): QueryExecutor<TModel>;
259
+ aggregate<TModel>(): AggregateExecutor<TModel>;
260
+ private aggregateInternal;
136
261
  private queryInternal;
137
262
  private queryDependenciesPages;
138
263
  }
139
264
 
140
- export { type DataModelId, type IndustrialModel, IndustrialModelClient, type ModelProps, type ModelRelations, type NodeId, type QueryOptions, type QueryResult, type QueryResultItem, type QueryResultMetadata, type QuerySelect };
265
+ interface BuildViewSchemaOptions {
266
+ dateMode?: "preserve" | "coerce";
267
+ }
268
+ declare const nodeIdSchema: z.ZodObject<{
269
+ space: z.ZodString;
270
+ externalId: z.ZodString;
271
+ }, z.core.$strip>;
272
+ declare function buildViewSchema(view: ViewDefinition, options?: BuildViewSchemaOptions): z.ZodObject<Record<string, z.ZodType>>;
273
+
274
+ export { type AggregateDefinition, type AggregateGroupBy, type AggregateOptions, type AggregateResult, type AggregateResultItem, type BuildViewSchemaOptions, type DataModelId, type GroupByKey, type IndustrialModel, IndustrialModelClient, type IndustrialModelClientOptions, type ModelProps, type ModelRelations, type NodeId, type QueryOptions, type QueryResult, type QueryResultItem, type QueryResultMetadata, type QuerySelect, buildViewSchema, nodeIdSchema };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,46 @@
1
1
  import { CogniteClient } from '@cognite/sdk';
2
+ import { z } from 'zod';
2
3
 
4
+ interface ViewReference {
5
+ type: "view";
6
+ space: string;
7
+ externalId: string;
8
+ version: string;
9
+ }
10
+ interface ViewPropertyType {
11
+ type?: string;
12
+ source?: ViewReference;
13
+ list?: boolean;
14
+ }
15
+ interface ViewPropertyDefinition {
16
+ container: unknown;
17
+ containerPropertyIdentifier: string;
18
+ type: ViewPropertyType;
19
+ }
20
+ interface ReverseDirectRelationConnection {
21
+ through: {
22
+ source: ViewReference;
23
+ identifier: string;
24
+ };
25
+ source: ViewReference;
26
+ connectionType?: string;
27
+ targetsList?: boolean;
28
+ }
29
+ interface EdgeConnection {
30
+ type: unknown;
31
+ source: ViewReference;
32
+ direction?: "outwards" | "inwards";
33
+ }
34
+ type ViewDefinitionProperty = ViewPropertyDefinition | ReverseDirectRelationConnection | EdgeConnection;
35
+ interface ViewDefinition {
36
+ space: string;
37
+ externalId: string;
38
+ version: string;
39
+ properties: Record<string, ViewDefinitionProperty>;
40
+ }
3
41
  interface NodeDefinition {
4
42
  instanceType: "node";
43
+ version?: number;
5
44
  space: string;
6
45
  externalId: string;
7
46
  properties?: Record<string, Record<string, Record<string, unknown>>>;
@@ -18,6 +57,9 @@ type DataModelId = NodeId & {
18
57
  version: string;
19
58
  };
20
59
  type SortDirection = "ascending" | "descending";
60
+ interface IndustrialModelClientOptions {
61
+ validateResults?: boolean;
62
+ }
21
63
  type Simplify<T> = {
22
64
  [K in keyof T]: T[K];
23
65
  } & {};
@@ -61,11 +103,12 @@ interface QueryOptions<TModel, TSelect extends QuerySelect<TModel> | undefined =
61
103
  limit?: number;
62
104
  cursor?: string | null;
63
105
  }
64
- type QueryResultMetadata = Pick<NodeDefinition, "space" | "externalId" | "createdTime" | "deletedTime" | "lastUpdatedTime">;
106
+ type QueryResultMetadata = Pick<NodeDefinition, "space" | "externalId" | "version" | "createdTime" | "deletedTime" | "lastUpdatedTime">;
65
107
  type ResultShapeForKey<TModel, K extends PropertyKey> = K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : K extends RelationKeys<TModel> ? ModelRelations<TModel>[K] : never;
66
108
  type ResultEntityForKey<TModel, K extends PropertyKey> = K extends RelationKeys<TModel> ? ModelRelations<TModel>[K] : K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never;
67
109
  type WrapResultValue<TShape, TValue> = [NonNull<TShape>] extends [readonly unknown[]] ? TValue[] : TValue;
68
- type SelectedValue<TModel, K extends PropertyKey, TValue, TDepth extends QueryDepth> = TValue extends true ? K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never : TDepth extends 0 ? never : TValue extends object ? WrapResultValue<ResultShapeForKey<TModel, K>, QueryResultItem<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, TValue & QuerySelect<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, PrevDepth[TDepth]>, PrevDepth[TDepth]>> : never;
110
+ type AsQuerySelect<TModel, TSelect> = TSelect extends QuerySelect<TModel> ? TSelect : never;
111
+ type SelectedValue<TModel, K extends PropertyKey, TValue, TDepth extends QueryDepth> = TValue extends true ? K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never : TDepth extends 0 ? never : TValue extends object ? WrapResultValue<ResultShapeForKey<TModel, K>, QueryResultItem<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, AsQuerySelect<UnwrapRelationTarget<ResultEntityForKey<TModel, K>>, TValue>, PrevDepth[TDepth]>> : never;
69
112
  type ExplicitSelectionResult<TModel, TSelect, TDepth extends QueryDepth> = Simplify<{
70
113
  [K in keyof NonNull<TSelect> as K extends "_all" ? never : SelectedValue<TModel, K, NonNull<TSelect>[K], TDepth> extends never ? never : IsOptionalKey<ModelProps<TModel>, K> extends true ? never : IsOptionalKey<ModelRelations<TModel>, K> extends true ? never : K]-?: SelectedValue<TModel, K, NonNull<TSelect>[K], TDepth>;
71
114
  } & {
@@ -78,6 +121,82 @@ interface QueryResult<TItem = Record<string, unknown>> {
78
121
  items: TItem[];
79
122
  cursor: string | null;
80
123
  }
124
+ type QueryExecutor<TModel> = {
125
+ <const TSelect extends QuerySelect<TModel>>(options: Omit<QueryOptions<TModel, TSelect>, "select"> & {
126
+ select: TSelect & QuerySelect<TModel>;
127
+ }): Promise<QueryResult<QueryResultItem<TModel, TSelect>>>;
128
+ (options: Omit<QueryOptions<TModel, undefined>, "select"> & {
129
+ select?: undefined;
130
+ }): Promise<QueryResult<QueryResultItem<TModel, undefined>>>;
131
+ };
132
+ type GroupableValue<T> = [NonNull<T>] extends [NodeId] ? true : [NonNull<T>] extends [string | number | boolean] ? true : false;
133
+ type GroupByKey<TModel> = {
134
+ [K in keyof ModelProps<TModel>]: GroupableValue<ModelProps<TModel>[K]> extends true ? K : never;
135
+ }[keyof ModelProps<TModel>];
136
+ type AggregateGroupBy<TModel> = {
137
+ [K in GroupByKey<TModel>]?: true;
138
+ };
139
+ type NumericKey<TModel> = {
140
+ [K in keyof ModelProps<TModel>]: ModelProps<TModel>[K] extends number ? K : never;
141
+ }[keyof ModelProps<TModel>];
142
+ type CountableKey<TModel> = GroupByKey<TModel> | "externalId" | "space";
143
+ type AggregateDefinition<TModel> = {
144
+ avg: NumericKey<TModel>;
145
+ } | {
146
+ min: NumericKey<TModel>;
147
+ } | {
148
+ max: NumericKey<TModel>;
149
+ } | {
150
+ sum: NumericKey<TModel>;
151
+ } | {
152
+ count: CountableKey<TModel> | Record<string, never>;
153
+ };
154
+ type SelectedGroupKeys<TGroupBy> = Extract<{
155
+ [K in keyof TGroupBy & string]: TGroupBy[K] extends true ? K : never;
156
+ }[keyof TGroupBy & string], string>;
157
+ type GroupValues<TModel, TGroupBy extends AggregateGroupBy<TModel> | undefined> = TGroupBy extends AggregateGroupBy<TModel> ? Simplify<Pick<ModelProps<TModel>, SelectedGroupKeys<TGroupBy> & keyof ModelProps<TModel>>> : undefined;
158
+ type AggregateValue<TDef> = TDef extends {
159
+ avg: infer P extends PropertyKey;
160
+ } ? {
161
+ property: P;
162
+ value: number;
163
+ } : TDef extends {
164
+ min: infer P extends PropertyKey;
165
+ } ? {
166
+ property: P;
167
+ value: number;
168
+ } : TDef extends {
169
+ max: infer P extends PropertyKey;
170
+ } ? {
171
+ property: P;
172
+ value: number;
173
+ } : TDef extends {
174
+ sum: infer P extends PropertyKey;
175
+ } ? {
176
+ property: P;
177
+ value: number;
178
+ } : TDef extends {
179
+ count: infer P;
180
+ } ? Record<string, never> extends P ? {
181
+ value: number;
182
+ } : {
183
+ property: P;
184
+ value: number;
185
+ } : never;
186
+ interface AggregateOptions<TModel> {
187
+ viewExternalId: string;
188
+ filters?: WhereInput<TModel>;
189
+ groupBy?: AggregateGroupBy<TModel>;
190
+ aggregate?: AggregateDefinition<TModel>;
191
+ }
192
+ type AggregateResultItem<TModel, TGroupBy extends AggregateGroupBy<TModel> | undefined = undefined, TAggregate extends AggregateDefinition<TModel> | undefined = undefined> = {
193
+ group?: GroupValues<TModel, TGroupBy>;
194
+ aggregate?: AggregateValue<TAggregate>;
195
+ };
196
+ interface AggregateResult<TItem = Record<string, unknown>> {
197
+ items: TItem[];
198
+ }
199
+ type AggregateExecutor<TModel> = <const TOptions extends AggregateOptions<TModel>>(options: TOptions) => Promise<AggregateResult<AggregateResultItem<TModel, TOptions["groupBy"], TOptions["aggregate"]>>>;
81
200
  type StringFilters = {
82
201
  eq?: string;
83
202
  in?: string[];
@@ -130,11 +249,26 @@ type WhereInput<TModel> = {
130
249
  declare class IndustrialModelClient {
131
250
  private readonly cognite;
132
251
  private readonly queryMapper;
252
+ private readonly aggregateMapper;
253
+ private readonly aggregateResultMapper;
133
254
  private readonly resultMapper;
134
- constructor(client: CogniteClient, dataModelId: DataModelId);
135
- query<TModel>(): <const TSelect extends QuerySelect<TModel> | undefined = undefined>(options: QueryOptions<TModel, TSelect>) => Promise<QueryResult<QueryResultItem<TModel, TSelect>>>;
255
+ private readonly resultValidator;
256
+ private readonly validateResults;
257
+ constructor(client: CogniteClient, dataModelId: DataModelId, options?: IndustrialModelClientOptions);
258
+ query<TModel>(): QueryExecutor<TModel>;
259
+ aggregate<TModel>(): AggregateExecutor<TModel>;
260
+ private aggregateInternal;
136
261
  private queryInternal;
137
262
  private queryDependenciesPages;
138
263
  }
139
264
 
140
- export { type DataModelId, type IndustrialModel, IndustrialModelClient, type ModelProps, type ModelRelations, type NodeId, type QueryOptions, type QueryResult, type QueryResultItem, type QueryResultMetadata, type QuerySelect };
265
+ interface BuildViewSchemaOptions {
266
+ dateMode?: "preserve" | "coerce";
267
+ }
268
+ declare const nodeIdSchema: z.ZodObject<{
269
+ space: z.ZodString;
270
+ externalId: z.ZodString;
271
+ }, z.core.$strip>;
272
+ declare function buildViewSchema(view: ViewDefinition, options?: BuildViewSchemaOptions): z.ZodObject<Record<string, z.ZodType>>;
273
+
274
+ export { type AggregateDefinition, type AggregateGroupBy, type AggregateOptions, type AggregateResult, type AggregateResultItem, type BuildViewSchemaOptions, type DataModelId, type GroupByKey, type IndustrialModel, IndustrialModelClient, type IndustrialModelClientOptions, type ModelProps, type ModelRelations, type NodeId, type QueryOptions, type QueryResult, type QueryResultItem, type QueryResultMetadata, type QuerySelect, buildViewSchema, nodeIdSchema };