industrial-model 0.4.0 → 0.5.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 +52 -2
- package/dist/index.cjs +64 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +64 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -197,10 +197,15 @@ interface AggregateResult<TItem = Record<string, unknown>> {
|
|
|
197
197
|
items: TItem[];
|
|
198
198
|
}
|
|
199
199
|
type AggregateExecutor<TModel> = <const TOptions extends AggregateOptions<TModel>>(options: TOptions) => Promise<AggregateResult<AggregateResultItem<TModel, TOptions["groupBy"], TOptions["aggregate"]>>>;
|
|
200
|
+
type SearchFilter = {
|
|
201
|
+
query: string;
|
|
202
|
+
operator?: "OR" | "AND";
|
|
203
|
+
};
|
|
200
204
|
type StringFilters = {
|
|
201
205
|
eq?: string;
|
|
202
206
|
in?: string[];
|
|
203
207
|
prefix?: string;
|
|
208
|
+
search?: SearchFilter;
|
|
204
209
|
exists?: boolean;
|
|
205
210
|
};
|
|
206
211
|
type NumberFilters = {
|
|
@@ -234,7 +239,9 @@ type ListFilters<T> = {
|
|
|
234
239
|
containsAny?: T[];
|
|
235
240
|
containsAll?: T[];
|
|
236
241
|
exists?: boolean;
|
|
237
|
-
}
|
|
242
|
+
} & (T extends string ? {
|
|
243
|
+
search?: SearchFilter;
|
|
244
|
+
} : unknown);
|
|
238
245
|
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;
|
|
239
246
|
type RelationFilterFor<T> = UnwrapRelationTarget<T> extends object ? WhereInput<UnwrapRelationTarget<T>> : never;
|
|
240
247
|
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;
|
package/dist/index.d.ts
CHANGED
|
@@ -197,10 +197,15 @@ interface AggregateResult<TItem = Record<string, unknown>> {
|
|
|
197
197
|
items: TItem[];
|
|
198
198
|
}
|
|
199
199
|
type AggregateExecutor<TModel> = <const TOptions extends AggregateOptions<TModel>>(options: TOptions) => Promise<AggregateResult<AggregateResultItem<TModel, TOptions["groupBy"], TOptions["aggregate"]>>>;
|
|
200
|
+
type SearchFilter = {
|
|
201
|
+
query: string;
|
|
202
|
+
operator?: "OR" | "AND";
|
|
203
|
+
};
|
|
200
204
|
type StringFilters = {
|
|
201
205
|
eq?: string;
|
|
202
206
|
in?: string[];
|
|
203
207
|
prefix?: string;
|
|
208
|
+
search?: SearchFilter;
|
|
204
209
|
exists?: boolean;
|
|
205
210
|
};
|
|
206
211
|
type NumberFilters = {
|
|
@@ -234,7 +239,9 @@ type ListFilters<T> = {
|
|
|
234
239
|
containsAny?: T[];
|
|
235
240
|
containsAll?: T[];
|
|
236
241
|
exists?: boolean;
|
|
237
|
-
}
|
|
242
|
+
} & (T extends string ? {
|
|
243
|
+
search?: SearchFilter;
|
|
244
|
+
} : unknown);
|
|
238
245
|
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;
|
|
239
246
|
type RelationFilterFor<T> = UnwrapRelationTarget<T> extends object ? WhereInput<UnwrapRelationTarget<T>> : never;
|
|
240
247
|
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;
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,13 @@ var CogniteSdkAdapter = class {
|
|
|
26
26
|
nextCursor: response.nextCursor
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
+
async searchInstances(request) {
|
|
30
|
+
const search = this.client.instances.search;
|
|
31
|
+
const response = await search(request);
|
|
32
|
+
return {
|
|
33
|
+
items: response.items
|
|
34
|
+
};
|
|
35
|
+
}
|
|
29
36
|
async aggregateInstances(request) {
|
|
30
37
|
const response = await this.client.instances.aggregate(
|
|
31
38
|
request
|
|
@@ -184,6 +191,7 @@ var leafOps = /* @__PURE__ */ new Set([
|
|
|
184
191
|
"lte",
|
|
185
192
|
"exists",
|
|
186
193
|
"prefix",
|
|
194
|
+
"search",
|
|
187
195
|
"containsAny",
|
|
188
196
|
"containsAll"
|
|
189
197
|
]);
|
|
@@ -236,19 +244,33 @@ function leafFilterSchema(property) {
|
|
|
236
244
|
const value = baseValueSchema(property);
|
|
237
245
|
const isList = typeof property !== "string" && property.type.list === true;
|
|
238
246
|
if (isList) {
|
|
239
|
-
|
|
247
|
+
const shape = {
|
|
240
248
|
containsAny: z.array(value).optional(),
|
|
241
249
|
containsAll: z.array(value).optional(),
|
|
242
250
|
exists: z.boolean().optional()
|
|
243
|
-
}
|
|
251
|
+
};
|
|
252
|
+
if (property.type.type === "text") {
|
|
253
|
+
shape.search = z.object({
|
|
254
|
+
query: z.string(),
|
|
255
|
+
operator: z.enum(["OR", "AND"]).optional()
|
|
256
|
+
}).strict().optional();
|
|
257
|
+
}
|
|
258
|
+
return z.object(shape).strict();
|
|
244
259
|
}
|
|
245
260
|
if (property === "node-string" || typeof property !== "string" && property.type.type === "text") {
|
|
246
|
-
|
|
261
|
+
const shape = {
|
|
247
262
|
eq: z.string().optional(),
|
|
248
263
|
in: z.array(z.string()).optional(),
|
|
249
264
|
prefix: z.string().optional(),
|
|
250
265
|
exists: z.boolean().optional()
|
|
251
|
-
}
|
|
266
|
+
};
|
|
267
|
+
if (property !== "node-string") {
|
|
268
|
+
shape.search = z.object({
|
|
269
|
+
query: z.string(),
|
|
270
|
+
operator: z.enum(["OR", "AND"]).optional()
|
|
271
|
+
}).strict().optional();
|
|
272
|
+
}
|
|
273
|
+
return z.object(shape).strict();
|
|
252
274
|
}
|
|
253
275
|
if (typeof property !== "string" && property.type.type === "enum") {
|
|
254
276
|
return z.object({
|
|
@@ -608,11 +630,13 @@ function isLeafFilter2(value) {
|
|
|
608
630
|
return Object.keys(value).some((k) => LEAF_OPS.has(k));
|
|
609
631
|
}
|
|
610
632
|
var FilterMapper = class {
|
|
611
|
-
constructor(viewMapper) {
|
|
633
|
+
constructor(viewMapper, cognite) {
|
|
612
634
|
this.viewMapper = viewMapper;
|
|
635
|
+
this.cognite = cognite;
|
|
613
636
|
}
|
|
614
637
|
async map(input, rootView) {
|
|
615
638
|
const result = [];
|
|
639
|
+
const searchQueries = {};
|
|
616
640
|
for (const [key, value] of Object.entries(input)) {
|
|
617
641
|
if (value == null) continue;
|
|
618
642
|
if (key === "AND") {
|
|
@@ -633,17 +657,44 @@ var FilterMapper = class {
|
|
|
633
657
|
} else {
|
|
634
658
|
const filterValue = value;
|
|
635
659
|
const property = getPropertyRef(key, rootView);
|
|
636
|
-
|
|
660
|
+
const hasLeafFilter = isLeafFilter2(filterValue);
|
|
661
|
+
if (hasLeafFilter) {
|
|
637
662
|
result.push(...this.leafToFilterDefs(property, filterValue));
|
|
638
|
-
}
|
|
663
|
+
}
|
|
664
|
+
if ("search" in filterValue && filterValue.search != null) {
|
|
665
|
+
searchQueries[key] = filterValue.search;
|
|
666
|
+
} else if (!hasLeafFilter) {
|
|
639
667
|
const targetView = await this.getNestedTargetView(key, rootView);
|
|
640
668
|
const innerFilter = await this.whereInputToSingle(filterValue, targetView);
|
|
641
669
|
result.push({ nested: { scope: property, filter: innerFilter } });
|
|
642
670
|
}
|
|
643
671
|
}
|
|
644
672
|
}
|
|
673
|
+
if (Object.keys(searchQueries).length > 0) {
|
|
674
|
+
const searchFilters = await Promise.all(
|
|
675
|
+
Object.entries(searchQueries).map(
|
|
676
|
+
([key, filterValue]) => this.searchToFilterDef(key, filterValue, rootView)
|
|
677
|
+
)
|
|
678
|
+
);
|
|
679
|
+
result.push(...searchFilters);
|
|
680
|
+
}
|
|
645
681
|
return result;
|
|
646
682
|
}
|
|
683
|
+
async searchToFilterDef(propertyName, search, rootView) {
|
|
684
|
+
const response = await this.cognite.searchInstances({
|
|
685
|
+
view: toViewReference(rootView),
|
|
686
|
+
query: search.query,
|
|
687
|
+
instanceType: "node",
|
|
688
|
+
properties: [propertyName],
|
|
689
|
+
operator: search.operator ?? "OR",
|
|
690
|
+
limit: 1e3
|
|
691
|
+
});
|
|
692
|
+
const instanceRefs = response.items.map((item) => ({
|
|
693
|
+
space: item.space,
|
|
694
|
+
externalId: item.externalId
|
|
695
|
+
}));
|
|
696
|
+
return { instanceReferences: instanceRefs };
|
|
697
|
+
}
|
|
647
698
|
async whereInputToSingle(input, rootView) {
|
|
648
699
|
const filters = await this.map(input, rootView);
|
|
649
700
|
const [firstFilter, ...restFilters] = filters;
|
|
@@ -722,9 +773,9 @@ var FilterMapper = class {
|
|
|
722
773
|
|
|
723
774
|
// src/mappers/aggregate-mapper.ts
|
|
724
775
|
var AggregateMapper = class {
|
|
725
|
-
constructor(viewMapper) {
|
|
776
|
+
constructor(viewMapper, cognite) {
|
|
726
777
|
this.viewMapper = viewMapper;
|
|
727
|
-
this.filterMapper = new FilterMapper(viewMapper);
|
|
778
|
+
this.filterMapper = new FilterMapper(viewMapper, cognite);
|
|
728
779
|
this.validator = new AggregateValidator(viewMapper);
|
|
729
780
|
}
|
|
730
781
|
async map(options) {
|
|
@@ -819,9 +870,9 @@ var SortMapper = class {
|
|
|
819
870
|
|
|
820
871
|
// src/mappers/query-mapper.ts
|
|
821
872
|
var QueryMapper = class {
|
|
822
|
-
constructor(viewMapper) {
|
|
873
|
+
constructor(viewMapper, cognite) {
|
|
823
874
|
this.viewMapper = viewMapper;
|
|
824
|
-
this.filterMapper = new FilterMapper(viewMapper);
|
|
875
|
+
this.filterMapper = new FilterMapper(viewMapper, cognite);
|
|
825
876
|
this.sortMapper = new SortMapper();
|
|
826
877
|
this.validator = new QueryValidator(viewMapper);
|
|
827
878
|
}
|
|
@@ -1367,8 +1418,8 @@ var IndustrialModelClient = class {
|
|
|
1367
1418
|
const cognite = createCogniteAdapter(client);
|
|
1368
1419
|
this.cognite = cognite;
|
|
1369
1420
|
const viewMapper = new ViewMapper(cognite, dataModelId);
|
|
1370
|
-
this.queryMapper = new QueryMapper(viewMapper);
|
|
1371
|
-
this.aggregateMapper = new AggregateMapper(viewMapper);
|
|
1421
|
+
this.queryMapper = new QueryMapper(viewMapper, cognite);
|
|
1422
|
+
this.aggregateMapper = new AggregateMapper(viewMapper, cognite);
|
|
1372
1423
|
this.aggregateResultMapper = new AggregateResultMapper();
|
|
1373
1424
|
this.resultMapper = new QueryResultMapper(viewMapper);
|
|
1374
1425
|
this.resultValidator = new QueryResultValidator(viewMapper);
|