industrial-model 0.1.0 → 0.3.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.ts CHANGED
@@ -1,4 +1,53 @@
1
1
  import { CogniteClient } from '@cognite/sdk';
2
+ import { z } from 'zod';
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
+ }
41
+ interface NodeDefinition {
42
+ instanceType: "node";
43
+ version?: number;
44
+ space: string;
45
+ externalId: string;
46
+ properties?: Record<string, Record<string, Record<string, unknown>>>;
47
+ createdTime?: number;
48
+ deletedTime?: number;
49
+ lastUpdatedTime?: number;
50
+ }
2
51
 
3
52
  type NodeId = {
4
53
  externalId: string;
@@ -8,31 +57,78 @@ type DataModelId = NodeId & {
8
57
  version: string;
9
58
  };
10
59
  type SortDirection = "ascending" | "descending";
11
- type SelectFor<T, TRelation = never> = T extends Array<infer U> ? boolean | (U extends object ? QuerySelect<U, TRelation> : never) : T extends object ? boolean | QuerySelect<T, TRelation> : boolean;
12
- type QuerySelect<T, TRelation = never> = {
13
- _all?: boolean;
14
- } & ([TRelation] extends [never] ? {
15
- [K in keyof T]?: SelectFor<T[K], TRelation>;
16
- } : {
17
- [K in Exclude<keyof T, keyof TRelation>]?: SelectFor<T[K], TRelation>;
18
- }) & ([TRelation] extends [never] ? {} : {
19
- [K in keyof TRelation]?: SelectFor<TRelation[K], TRelation>;
20
- });
21
- type SortInput<T> = {
22
- [K in keyof T as T[K] extends string | number | boolean | Date | NodeId ? K : never]?: SortDirection;
60
+ interface IndustrialModelClientOptions {
61
+ validateResults?: boolean;
62
+ }
63
+ type Simplify<T> = {
64
+ [K in keyof T]: T[K];
65
+ } & {};
66
+ type Merge<A, B> = Simplify<Omit<A, keyof B> & B>;
67
+ type NonNull<T> = Exclude<T, null | undefined>;
68
+ type ArrayItem<T> = T extends readonly (infer U)[] ? U : never;
69
+ type QueryDepth = 0 | 1 | 2 | 3;
70
+ type PrevDepth = {
71
+ 0: 0;
72
+ 1: 0;
73
+ 2: 1;
74
+ 3: 2;
75
+ };
76
+ declare const MODEL_RELATIONS: unique symbol;
77
+ type IndustrialModel<TProps, TRelations = {}> = Simplify<TProps & {
78
+ readonly [MODEL_RELATIONS]?: TRelations;
79
+ }>;
80
+ type ModelProps<TModel> = Simplify<Omit<TModel, typeof MODEL_RELATIONS>>;
81
+ type ModelRelations<TModel> = TModel extends {
82
+ readonly [MODEL_RELATIONS]?: infer TRelations;
83
+ } ? NonNull<TRelations> : never;
84
+ type RelationKeys<TModel> = [ModelRelations<TModel>] extends [never] ? never : keyof ModelRelations<TModel>;
85
+ type IsOptionalKey<T, K extends PropertyKey> = K extends keyof T ? {} extends Pick<T, K> ? true : false : false;
86
+ type UnwrapRelationTarget<T> = NonNull<T> extends readonly unknown[] ? ArrayItem<NonNull<T>> : NonNull<T>;
87
+ type BaseSelectFor<T, TDepth extends QueryDepth = 3> = [NonNull<T>] extends [NodeId] ? boolean : NonNull<T> extends readonly unknown[] ? [ArrayItem<NonNull<T>>] extends [NodeId] ? boolean : TDepth extends 0 ? boolean : NonNull<ArrayItem<NonNull<T>>> extends object ? boolean | QuerySelect<NonNull<ArrayItem<NonNull<T>>>, PrevDepth[TDepth]> : boolean : TDepth extends 0 ? boolean : NonNull<T> extends object ? boolean | QuerySelect<NonNull<T>, PrevDepth[TDepth]> : boolean;
88
+ type RelationSelectFor<T, TDepth extends QueryDepth = 3> = TDepth extends 0 ? never : QuerySelect<UnwrapRelationTarget<T>, PrevDepth[TDepth]>;
89
+ type QuerySelectValue<TModel, K extends PropertyKey, TDepth extends QueryDepth> = K extends RelationKeys<TModel> ? K extends keyof ModelProps<TModel> ? boolean | RelationSelectFor<ModelRelations<TModel>[K], TDepth> : RelationSelectFor<ModelRelations<TModel>[K], TDepth> : K extends keyof ModelProps<TModel> ? BaseSelectFor<ModelProps<TModel>[K], TDepth> : never;
90
+ type QuerySelect<TModel, TDepth extends QueryDepth = 3> = {
91
+ _all?: true;
92
+ } & {
93
+ [K in keyof ModelProps<TModel> | RelationKeys<TModel>]?: QuerySelectValue<TModel, K, TDepth>;
23
94
  };
24
- interface QueryOptions<T, TRelation = never> {
95
+ type SortInput<TModel> = {
96
+ [K in keyof ModelProps<TModel> as NonNull<ModelProps<TModel>[K]> extends string | number | boolean | Date | NodeId ? K : never]?: SortDirection;
97
+ };
98
+ interface QueryOptions<TModel, TSelect extends QuerySelect<TModel> | undefined = QuerySelect<TModel> | undefined> {
25
99
  viewExternalId: string;
26
- select?: QuerySelect<T, TRelation>;
27
- filters?: WhereInput<T, TRelation>;
28
- sortClauses?: SortInput<T>;
100
+ select?: TSelect;
101
+ filters?: WhereInput<TModel>;
102
+ sort?: SortInput<TModel>;
29
103
  limit?: number;
30
104
  cursor?: string | null;
31
105
  }
32
- interface QueryResult {
33
- items: Record<string, unknown>[];
106
+ type QueryResultMetadata = Pick<NodeDefinition, "space" | "externalId" | "version" | "createdTime" | "deletedTime" | "lastUpdatedTime">;
107
+ type ResultShapeForKey<TModel, K extends PropertyKey> = K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : K extends RelationKeys<TModel> ? ModelRelations<TModel>[K] : never;
108
+ type ResultEntityForKey<TModel, K extends PropertyKey> = K extends RelationKeys<TModel> ? ModelRelations<TModel>[K] : K extends keyof ModelProps<TModel> ? ModelProps<TModel>[K] : never;
109
+ type WrapResultValue<TShape, TValue> = [NonNull<TShape>] extends [readonly unknown[]] ? TValue[] : TValue;
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;
112
+ type ExplicitSelectionResult<TModel, TSelect, TDepth extends QueryDepth> = Simplify<{
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>;
114
+ } & {
115
+ [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 ? K : IsOptionalKey<ModelRelations<TModel>, K> extends true ? K : never]?: SelectedValue<TModel, K, NonNull<TSelect>[K], TDepth>;
116
+ }>;
117
+ type QueryResultItem<TModel, TSelect extends QuerySelect<TModel> | undefined = undefined, TDepth extends QueryDepth = 3> = [TSelect] extends [undefined] ? Merge<QueryResultMetadata, ModelProps<TModel>> : Merge<TSelect extends {
118
+ _all: true;
119
+ } ? Merge<QueryResultMetadata, ModelProps<TModel>> : QueryResultMetadata, ExplicitSelectionResult<TModel, TSelect, TDepth>>;
120
+ interface QueryResult<TItem = Record<string, unknown>> {
121
+ items: TItem[];
34
122
  cursor: string | null;
35
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
+ };
36
132
  type StringFilters = {
37
133
  eq?: string;
38
134
  in?: string[];
@@ -71,26 +167,36 @@ type ListFilters<T> = {
71
167
  containsAll?: T[];
72
168
  exists?: boolean;
73
169
  };
74
- type FilterFor<T, TRelation = never> = T extends NodeId ? NodeIdFilters : T extends string ? StringFilters : T extends number ? NumberFilters : T extends boolean ? BooleanFilters : T extends Date ? DateFilters : T extends Array<infer U> ? ListFilters<U> : T extends object ? NodeIdFilters | WhereInput<T, TRelation> : never;
75
- type WhereInput<T, TRelation = never> = {
76
- AND?: WhereInput<T, TRelation> | WhereInput<T, TRelation>[];
77
- OR?: WhereInput<T, TRelation>[];
78
- NOT?: WhereInput<T, TRelation> | WhereInput<T, TRelation>[];
79
- } & ([TRelation] extends [never] ? {
80
- [K in keyof T]?: FilterFor<T[K], TRelation>;
81
- } : {
82
- [K in Exclude<keyof T, keyof TRelation>]?: FilterFor<T[K], TRelation>;
83
- }) & ([TRelation] extends [never] ? {} : {
84
- [K in keyof TRelation]?: FilterFor<TRelation[K], TRelation>;
85
- });
170
+ type BaseFilterFor<T> = T extends NodeId ? NodeIdFilters : T extends string ? StringFilters : T extends number ? NumberFilters : T extends boolean ? BooleanFilters : T extends Date ? DateFilters : T extends Array<infer U> ? ListFilters<U> : T extends object ? NodeIdFilters | WhereInput<T> : never;
171
+ type RelationFilterFor<T> = UnwrapRelationTarget<T> extends object ? WhereInput<UnwrapRelationTarget<T>> : never;
172
+ type QueryFilterValue<TModel, K extends PropertyKey> = K extends RelationKeys<TModel> ? K extends keyof ModelProps<TModel> ? BaseFilterFor<ModelProps<TModel>[K]> | RelationFilterFor<ModelRelations<TModel>[K]> : RelationFilterFor<ModelRelations<TModel>[K]> : K extends keyof ModelProps<TModel> ? BaseFilterFor<ModelProps<TModel>[K]> : never;
173
+ type WhereInput<TModel> = {
174
+ AND?: WhereInput<TModel> | WhereInput<TModel>[];
175
+ OR?: WhereInput<TModel>[];
176
+ NOT?: WhereInput<TModel> | WhereInput<TModel>[];
177
+ } & {
178
+ [K in keyof ModelProps<TModel> | RelationKeys<TModel>]?: QueryFilterValue<TModel, K>;
179
+ };
86
180
 
87
- declare class IndustrialModel {
181
+ declare class IndustrialModelClient {
88
182
  private readonly cognite;
89
183
  private readonly queryMapper;
90
184
  private readonly resultMapper;
91
- constructor(client: CogniteClient, dataModelId: DataModelId);
92
- query<T, TRelation = never>(options: QueryOptions<T, TRelation>): Promise<QueryResult>;
185
+ private readonly resultValidator;
186
+ private readonly validateResults;
187
+ constructor(client: CogniteClient, dataModelId: DataModelId, options?: IndustrialModelClientOptions);
188
+ query<TModel>(): QueryExecutor<TModel>;
189
+ private queryInternal;
93
190
  private queryDependenciesPages;
94
191
  }
95
192
 
96
- export { type DataModelId, IndustrialModel, type NodeId, type QueryOptions, type QueryResult };
193
+ interface BuildViewSchemaOptions {
194
+ dateMode?: "preserve" | "coerce";
195
+ }
196
+ declare const nodeIdSchema: z.ZodObject<{
197
+ space: z.ZodString;
198
+ externalId: z.ZodString;
199
+ }, z.core.$strip>;
200
+ declare function buildViewSchema(view: ViewDefinition, options?: BuildViewSchemaOptions): z.ZodObject<Record<string, z.ZodType>>;
201
+
202
+ export { type BuildViewSchemaOptions, type DataModelId, type IndustrialModel, IndustrialModelClient, type IndustrialModelClientOptions, type ModelProps, type ModelRelations, type NodeId, type QueryOptions, type QueryResult, type QueryResultItem, type QueryResultMetadata, type QuerySelect, buildViewSchema, nodeIdSchema };