industrial-model 0.3.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
@@ -129,6 +129,74 @@ type QueryExecutor<TModel> = {
129
129
  select?: undefined;
130
130
  }): Promise<QueryResult<QueryResultItem<TModel, undefined>>>;
131
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"]>>>;
132
200
  type StringFilters = {
133
201
  eq?: string;
134
202
  in?: string[];
@@ -181,11 +249,15 @@ type WhereInput<TModel> = {
181
249
  declare class IndustrialModelClient {
182
250
  private readonly cognite;
183
251
  private readonly queryMapper;
252
+ private readonly aggregateMapper;
253
+ private readonly aggregateResultMapper;
184
254
  private readonly resultMapper;
185
255
  private readonly resultValidator;
186
256
  private readonly validateResults;
187
257
  constructor(client: CogniteClient, dataModelId: DataModelId, options?: IndustrialModelClientOptions);
188
258
  query<TModel>(): QueryExecutor<TModel>;
259
+ aggregate<TModel>(): AggregateExecutor<TModel>;
260
+ private aggregateInternal;
189
261
  private queryInternal;
190
262
  private queryDependenciesPages;
191
263
  }
@@ -199,4 +271,4 @@ declare const nodeIdSchema: z.ZodObject<{
199
271
  }, z.core.$strip>;
200
272
  declare function buildViewSchema(view: ViewDefinition, options?: BuildViewSchemaOptions): z.ZodObject<Record<string, z.ZodType>>;
201
273
 
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 };
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
@@ -129,6 +129,74 @@ type QueryExecutor<TModel> = {
129
129
  select?: undefined;
130
130
  }): Promise<QueryResult<QueryResultItem<TModel, undefined>>>;
131
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"]>>>;
132
200
  type StringFilters = {
133
201
  eq?: string;
134
202
  in?: string[];
@@ -181,11 +249,15 @@ type WhereInput<TModel> = {
181
249
  declare class IndustrialModelClient {
182
250
  private readonly cognite;
183
251
  private readonly queryMapper;
252
+ private readonly aggregateMapper;
253
+ private readonly aggregateResultMapper;
184
254
  private readonly resultMapper;
185
255
  private readonly resultValidator;
186
256
  private readonly validateResults;
187
257
  constructor(client: CogniteClient, dataModelId: DataModelId, options?: IndustrialModelClientOptions);
188
258
  query<TModel>(): QueryExecutor<TModel>;
259
+ aggregate<TModel>(): AggregateExecutor<TModel>;
260
+ private aggregateInternal;
189
261
  private queryInternal;
190
262
  private queryDependenciesPages;
191
263
  }
@@ -199,4 +271,4 @@ declare const nodeIdSchema: z.ZodObject<{
199
271
  }, z.core.$strip>;
200
272
  declare function buildViewSchema(view: ViewDefinition, options?: BuildViewSchemaOptions): z.ZodObject<Record<string, z.ZodType>>;
201
273
 
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 };
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 };