@travetto/model-query 8.0.0-alpha.11 → 8.0.0-alpha.13

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.11",
3
+ "version": "8.0.0-alpha.13",
4
4
  "type": "module",
5
5
  "description": "Datastore abstraction for advanced query support.",
6
6
  "keywords": [
@@ -28,8 +28,8 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@travetto/di": "^8.0.0-alpha.11",
31
- "@travetto/model": "^8.0.0-alpha.11",
32
- "@travetto/schema": "^8.0.0-alpha.11"
31
+ "@travetto/model": "^8.0.0-alpha.12",
32
+ "@travetto/schema": "^8.0.0-alpha.12"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@travetto/test": "^8.0.0-alpha.11"
@@ -10,33 +10,28 @@ type IndexClauseRaw<T> = {
10
10
 
11
11
  export type IndexField<T extends ModelType> = IndexClauseRaw<RetainPrimitiveFields<T>>;
12
12
 
13
- /**
14
- * Supported index types
15
- */
16
- export type QueryIndexType = 'query:unique' | 'query:unsorted' | 'query:sorted';
17
-
18
13
  /**
19
14
  * Index options
20
15
  */
21
- export interface QueryIndexConfig<T extends ModelType> extends IndexConfig<QueryIndexType> {
16
+ export interface QueryIndexConfig<T extends ModelType> extends IndexConfig<'query'> {
22
17
  /**
23
18
  * Fields and sort order
24
19
  */
25
20
  fields: IndexClauseRaw<RetainPrimitiveFields<T>>[];
26
- };
21
+ /**
22
+ * Is this a unique index
23
+ */
24
+ unique?: boolean;
25
+ }
27
26
 
28
- /**
29
- * Defines an index on a model
30
- * @kind decorator
31
- */
32
- export function QueryIndex<T extends ModelType>(index: Omit<QueryIndexConfig<T>, 'class'>) {
27
+ export function QueryIndex<T extends ModelType>(index: Omit<QueryIndexConfig<T>, 'class' | 'type'>) {
33
28
  if (index.fields.some(field => field === 'id')) {
34
29
  throw new RuntimeError('Cannot create an index with the id field');
35
30
  }
36
31
  return function (cls: Class<T>): void {
37
- ModelRegistryIndex.getForRegister(cls).register({ indices: { [index.name]: { ...index, class: cls } } });
32
+ ModelRegistryIndex.getForRegister(cls).register({ indices: { [index.name]: { ...index, type: 'query', class: cls } } });
38
33
  };
39
34
  }
40
35
 
41
36
  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:');
37
+ typeof idx === 'object' && idx !== null && 'type' in idx && typeof idx.type === 'string' && idx.type === 'query';
@@ -1,15 +1,39 @@
1
1
  import assert from 'node:assert';
2
2
 
3
3
  import { Suite, Test } from '@travetto/test';
4
- import { type ModelCrudSupport, NotFoundError } from '@travetto/model';
4
+ import { ExistsError, Model, type ModelCrudSupport, NotFoundError } from '@travetto/model';
5
+ import { castTo } from '@travetto/runtime';
5
6
 
6
7
  import { BaseModelSuite } from '@travetto/model/support/test/base.ts';
7
8
 
8
9
  import { Address, Person, Todo, BigIntModel } from './model.ts';
9
10
  import type { ModelQueryCrudSupport } from '../../src/types/crud.ts';
11
+ import { QueryIndex } from '../../__index__.ts';
12
+
13
+
14
+ @Model()
15
+ @QueryIndex({
16
+ name: 'uniqueUser2',
17
+ fields: [{ name: true }],
18
+ unique: true
19
+ })
20
+ class UniqueUser2 {
21
+ id: string;
22
+ name: string;
23
+ }
10
24
 
11
25
  @Suite()
12
26
  export abstract class ModelQueryCrudSuite extends BaseModelSuite<ModelQueryCrudSupport & ModelCrudSupport> {
27
+
28
+ supportsUniqueIndexes = true;
29
+
30
+ @Test({ skip: (self) => !castTo<ModelQueryCrudSuite>(self).supportsUniqueIndexes })
31
+ async testUnique() {
32
+ const svc = await this.service;
33
+ await svc.create(UniqueUser2, UniqueUser2.from({ name: 'bob' }));
34
+ await assert.rejects(() => svc.create(UniqueUser2, UniqueUser2.from({ name: 'bob' })), ExistsError);
35
+ }
36
+
13
37
  @Test()
14
38
  async testUpdateOneWithQuery() {
15
39
  const svc = await this.service;