@travetto/model-query 8.0.0-alpha.16 → 8.0.0-alpha.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-query",
3
- "version": "8.0.0-alpha.16",
3
+ "version": "8.0.0-alpha.19",
4
4
  "type": "module",
5
5
  "description": "Datastore abstraction for advanced query support.",
6
6
  "keywords": [
@@ -27,12 +27,12 @@
27
27
  "directory": "module/model-query"
28
28
  },
29
29
  "dependencies": {
30
- "@travetto/di": "^8.0.0-alpha.14",
31
- "@travetto/model": "^8.0.0-alpha.15",
32
- "@travetto/schema": "^8.0.0-alpha.15"
30
+ "@travetto/di": "^8.0.0-alpha.17",
31
+ "@travetto/model": "^8.0.0-alpha.18",
32
+ "@travetto/schema": "^8.0.0-alpha.18"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/test": "^8.0.0-alpha.14"
35
+ "@travetto/test": "^8.0.0-alpha.17"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@travetto/test": {
@@ -1,11 +1,11 @@
1
- import type { Primitive, ValidFields, TimeSpan, ValidTypedFields } from '@travetto/runtime';
1
+ import type { Primitive, ValidFields, TimeSpan, KeyPaths } from '@travetto/runtime';
2
2
  import type { Point } from '@travetto/schema';
3
3
 
4
4
  export type QueryPrimitive = Primitive | Date | Point;
5
5
  export type QueryPrimitiveArray = QueryPrimitive[];
6
6
  export type DistanceUnit = 'mi' | 'm' | 'km' | 'ft' | 'rad';
7
7
  export type RetainQueryPrimitiveFields<T> = Pick<T, ValidFields<T, QueryPrimitive>>;
8
- export type ValidStringFields<T> = ValidTypedFields<T, String | string | string[] | String[] | undefined>;
8
+ export type ValidStringFields<T> = KeyPaths<T, String | string | string[] | String[] | undefined>;
9
9
 
10
10
  type General<T> = {
11
11
  $eq?: T;
@@ -1,5 +1,5 @@
1
1
  import { ModelRegistryIndex, type ModelType } from '@travetto/model';
2
- import { castTo, type Class, hasFunction } from '@travetto/runtime';
2
+ import { castKey, castTo, type Class, hasFunction } from '@travetto/runtime';
3
3
  import { SchemaRegistryIndex } from '@travetto/schema';
4
4
 
5
5
  import type { PageableModelQuery, Query } from '../model/query.ts';
@@ -28,7 +28,15 @@ export class ModelQuerySuggestUtil {
28
28
  */
29
29
  static getSuggestQuery<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, prefix?: string, query?: Query<T>): Query<T> {
30
30
  const limit = query?.limit ?? 10;
31
- const clauses: WhereClauseRaw<ModelType>[] = prefix ? [{ [field]: { $regex: this.getSuggestRegex(prefix) } }] : [];
31
+ const clauses: WhereClauseRaw<ModelType>[] = [];
32
+ if (prefix) {
33
+ const parts = `${field}`.split('.');
34
+ let o: WhereClauseRaw<ModelType> = { [parts.at(-1)!]: { $regex: this.getSuggestRegex(prefix) } };
35
+ for (let i = parts.length - 2; i >= 0; i -= 1) {
36
+ o = { [parts[i]]: o };
37
+ }
38
+ clauses.push(o);
39
+ }
32
40
  const select: Query<T>['select'] = {
33
41
  ...query?.select
34
42
  };
@@ -75,8 +83,13 @@ export class ModelQuerySuggestUtil {
75
83
  const pattern = this.getSuggestRegex(prefix);
76
84
 
77
85
  const out: ([string, U] | readonly [string, U])[] = [];
86
+ const parts = `${field}`.split('.');
87
+
78
88
  for (const result of results) {
79
- const resultValue = result[field];
89
+ let resultValue = result[castKey<T>(parts[0])];
90
+ for (let i = 1; i < parts.length; i++) {
91
+ resultValue = resultValue[castKey(parts[i])];
92
+ }
80
93
  if (Array.isArray(resultValue)) {
81
94
  out.push(...resultValue.filter(item => pattern.test(item)).map((item: string) => [item, transform(item, result)] as const));
82
95
  } else if (typeof resultValue === 'string') {