@travetto/model-query 5.0.4 → 5.0.6

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 CHANGED
@@ -33,7 +33,7 @@ export interface ModelQuerySupport {
33
33
  * Find one by query, fail if not found
34
34
  * @param cls The model class
35
35
  * @param query The query to search for
36
- * @param failOnMany Should the query fail on more than one result found
36
+ * @param failOnMany Should the query fail on more than one result found, defaults to true
37
37
  */
38
38
  queryOne<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>, failOnMany?: boolean): Promise<T>;
39
39
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-query",
3
- "version": "5.0.4",
3
+ "version": "5.0.6",
4
4
  "description": "Datastore abstraction for advanced query support.",
5
5
  "keywords": [
6
6
  "datastore",
@@ -26,12 +26,12 @@
26
26
  "directory": "module/model-query"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/di": "^5.0.4",
30
- "@travetto/model": "^5.0.4",
31
- "@travetto/schema": "^5.0.4"
29
+ "@travetto/di": "^5.0.6",
30
+ "@travetto/model": "^5.0.6",
31
+ "@travetto/schema": "^5.0.6"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/test": "^5.0.4"
34
+ "@travetto/test": "^5.0.6"
35
35
  },
36
36
  "peerDependenciesMeta": {
37
37
  "@travetto/test": {
@@ -26,12 +26,13 @@ export class ModelQueryUtil {
26
26
  /**
27
27
  * Verify result set is singular, and decide if failing on many should happen
28
28
  */
29
- static verifyGetSingleCounts<T>(cls: Class<T>, res?: T[], failOnMany = true): T {
29
+ static verifyGetSingleCounts<T extends ModelType>(cls: Class<T>, failOnMany: boolean, res?: T[], where?: WhereClause<T>): T {
30
30
  res = res ?? [];
31
31
  if (res.length === 1 || res.length > 1 && !failOnMany) {
32
32
  return res[0]!;
33
33
  }
34
- throw res.length === 0 ? new NotFoundError(cls, 'none') : new AppError(`Invalid number of results for find by id: ${res.length}`, 'data');
34
+ const requestedId = ((where && 'id' in where && typeof where.id === 'string') ? where.id : undefined) ?? 'unknown';
35
+ throw res.length === 0 ? new NotFoundError(cls, requestedId, { where }) : new AppError(`Invalid number of results for find by id: ${res.length}`, 'data');
35
36
  }
36
37
 
37
38
  /**
@@ -18,7 +18,7 @@ export interface ModelQuerySupport {
18
18
  * Find one by query, fail if not found
19
19
  * @param cls The model class
20
20
  * @param query The query to search for
21
- * @param failOnMany Should the query fail on more than one result found
21
+ * @param failOnMany Should the query fail on more than one result found, defaults to true
22
22
  */
23
23
  queryOne<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>, failOnMany?: boolean): Promise<T>;
24
24
  /**
@@ -4,6 +4,7 @@ import { Suite, Test } from '@travetto/test';
4
4
  import { BaseModelSuite } from '@travetto/model/support/test/base';
5
5
  import { ModelCrudSupport } from '@travetto/model/src/service/crud';
6
6
  import { TimeUtil } from '@travetto/runtime';
7
+ import { NotFoundError } from '@travetto/model';
7
8
 
8
9
  import { Aged, Location, Names, Note, Person, SimpleList, WithNestedLists, WithNestedNestedLists } from './types';
9
10
 
@@ -37,6 +38,33 @@ export abstract class ModelQuerySuite extends BaseModelSuite<ModelQuerySupport &
37
38
  assert(results3.length === 0);
38
39
  }
39
40
 
41
+
42
+ @Test('verify query one behavior')
43
+ async testQueryOne() {
44
+ const service = await this.service;
45
+ await this.saveAll(Person, [1, 2, 3, 8].map(x => Person.from({
46
+ name: 'Bob Omber',
47
+ age: 20 + x,
48
+ gender: 'm',
49
+ address: {
50
+ street1: 'a',
51
+ ...(x === 1 ? { street2: 'b' } : {})
52
+ }
53
+ })));
54
+
55
+ await assert.rejects(() => service.queryOne(Person, { where: { gender: 'm' } }), /Invalid number of results/);
56
+ await assert.rejects(() => service.queryOne(Person, { where: { gender: 'z' } }), NotFoundError);
57
+ await assert.rejects(() => service.queryOne(Person, { where: { gender: 'z' } }), /unknown/);
58
+ assert.deepStrictEqual(
59
+ (await service.queryOne(Person, { where: { gender: 'z' } }).catch(e => e)).details.where,
60
+ {
61
+ gender: 'z'
62
+ }
63
+ );
64
+ await assert.rejects(() => service.queryOne(Person, { where: { id: 'orange' } }), NotFoundError);
65
+ await assert.rejects(() => service.queryOne(Person, { where: { id: 'orange' } }), /orange/);
66
+ }
67
+
40
68
  @Test('Verify array $in queries work properly')
41
69
  async testArrayContains() {
42
70
  const svc = await this.service;