@travetto/model-query 8.0.0-alpha.0 → 8.0.0-alpha.10

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/__index__.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './src/model/query.ts';
2
2
  export * from './src/model/where-clause.ts';
3
+ export * from './src/model/indexes.ts';
3
4
  export * from './src/types/crud.ts';
4
5
  export * from './src/types/query.ts';
5
6
  export * from './src/types/facet.ts';
@@ -10,4 +11,5 @@ export * from './src/util/suggest.ts';
10
11
  export * from './src/util/facet.ts';
11
12
  export * from './src/util/crud.ts';
12
13
 
14
+
13
15
  export * from './src/verifier.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-query",
3
- "version": "8.0.0-alpha.0",
3
+ "version": "8.0.0-alpha.10",
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.0",
31
- "@travetto/model": "^8.0.0-alpha.0",
32
- "@travetto/schema": "^8.0.0-alpha.0"
30
+ "@travetto/di": "^8.0.0-alpha.10",
31
+ "@travetto/model": "^8.0.0-alpha.10",
32
+ "@travetto/schema": "^8.0.0-alpha.10"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/test": "^8.0.0-alpha.0"
35
+ "@travetto/test": "^8.0.0-alpha.10"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@travetto/test": {
@@ -0,0 +1,42 @@
1
+ import { ModelRegistryIndex, type IndexConfig, type ModelType } from '@travetto/model';
2
+ import { RuntimeError, type Class, type Primitive, type ValidFields } from '@travetto/runtime';
3
+
4
+ type RetainPrimitiveFields<T> = Pick<T, ValidFields<T, Primitive | Date>>;
5
+
6
+ type IndexClauseRaw<T> = {
7
+ [P in keyof T]?:
8
+ T[P] extends object ? IndexClauseRaw<RetainPrimitiveFields<T[P]>> : 1 | -1 | true;
9
+ };
10
+
11
+ export type IndexField<T extends ModelType> = IndexClauseRaw<RetainPrimitiveFields<T>>;
12
+
13
+ /**
14
+ * Supported index types
15
+ */
16
+ export type QueryIndexType = 'query:unique' | 'query:unsorted' | 'query:sorted';
17
+
18
+ /**
19
+ * Index options
20
+ */
21
+ export interface QueryIndexConfig<T extends ModelType> extends IndexConfig<QueryIndexType> {
22
+ /**
23
+ * Fields and sort order
24
+ */
25
+ fields: IndexClauseRaw<RetainPrimitiveFields<T>>[];
26
+ };
27
+
28
+ /**
29
+ * Defines an index on a model
30
+ * @kind decorator
31
+ */
32
+ export function QueryIndex<T extends ModelType>(index: Omit<QueryIndexConfig<T>, 'class'>) {
33
+ if (index.fields.some(field => field === 'id')) {
34
+ throw new RuntimeError('Cannot create an index with the id field');
35
+ }
36
+ return function (cls: Class<T>): void {
37
+ ModelRegistryIndex.getForRegister(cls).register({ indices: { [index.name]: { ...index, class: cls } } });
38
+ };
39
+ }
40
+
41
+ export const isModelQueryIndex = (idx: unknown): idx is QueryIndexConfig<ModelType> =>
42
+ typeof idx === 'object' && idx !== null && 'type' in idx && typeof idx.type === 'string' && idx.type.startsWith('query:');