@travetto/model-query 3.1.6 → 3.1.7

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
@@ -158,6 +158,7 @@ One of the complexities of abstracting multiple storage mechanisms, is providing
158
158
  ### Array Fields
159
159
 
160
160
  * `field: { $all: T[]] }` checks to see if the records value contains everything within `$all`
161
+ * `field: { $empty: boolean }` to determine if an array is missing or is of zero length. Due to the nature of this operation, this is only supported on lists at the top level of the document.
161
162
 
162
163
  ### String Fields
163
164
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-query",
3
- "version": "3.1.6",
3
+ "version": "3.1.7",
4
4
  "description": "Datastore abstraction for advanced query support.",
5
5
  "keywords": [
6
6
  "datastore",
@@ -3,10 +3,10 @@ import { Class } from '@travetto/base';
3
3
 
4
4
  import { PointImpl } from '../model/point';
5
5
 
6
- const st = (t: string | string[], arr: boolean = false): Set<string> =>
7
- new Set((Array.isArray(t) ? t : [t]).map(v => arr ? `${v}[]` : v));
6
+ const st = (t: string | string[], isArr: boolean = false): Set<string> =>
7
+ new Set((Array.isArray(t) ? t : [t]).map(v => isArr ? `${v}[]` : v));
8
8
 
9
- const basic = (types: Set<string>): Record<string, Set<string>> => ({ $ne: types, $eq: types, $exists: st('boolean') });
9
+ const basic = (types: Set<string>): Record<string, Set<string>> => ({ $ne: types, $eq: types, $exists: st('boolean'), $empty: st('boolean') });
10
10
  const scalar = (types: Set<string>): Record<string, Set<string>> => ({ $in: types, $nin: types });
11
11
  const str = (): Record<string, Set<string>> => ({ $regex: st(['RegExp', 'string']) });
12
12
  const comp = (types: Set<string>): Record<string, Set<string>> => ({ $lt: types, $lte: types, $gt: types, $gte: types });
@@ -44,6 +44,7 @@ type ArrayField<T> =
44
44
  { $ne?: T | T[] } |
45
45
  { $all?: T[] } |
46
46
  { $in?: T[] } |
47
+ { $empty?: boolean } |
47
48
  PropWhereClause<RetainFields<T>> |
48
49
  T | T[];
49
50
 
@@ -5,7 +5,7 @@ import { BaseModelSuite } from '@travetto/model/support/test/base';
5
5
  import { ModelCrudSupport } from '@travetto/model/src/service/crud';
6
6
  import { TimeUtil } from '@travetto/base';
7
7
 
8
- import { Aged, Location, Names, Note, Person, SimpleList } from './types';
8
+ import { Aged, Location, Names, Note, Person, SimpleList, WithNestedLists } from './types';
9
9
 
10
10
  import { ModelQuerySupport } from '../../src/service/query';
11
11
 
@@ -296,8 +296,77 @@ export abstract class ModelQuerySuite extends BaseModelSuite<ModelQuerySupport &
296
296
  }
297
297
  });
298
298
  assert(simple4 === 3);
299
+ }
300
+
301
+ @Test()
302
+ async verifyArrayEmptyVsNot() {
303
+ const service = await this.service;
304
+ await service.create(WithNestedLists, {
305
+ tags: ['a', 'b']
306
+ });
307
+
308
+ await service.create(WithNestedLists, {
309
+ names: ['c', 'd'],
310
+ });
311
+
312
+ await service.create(WithNestedLists, {
313
+ names: ['c', 'd'],
314
+ tags: ['e', 'f']
315
+ });
299
316
 
317
+ await service.create(WithNestedLists, {
318
+ names: ['g', 'h'],
319
+ tags: []
320
+ });
321
+
322
+ await service.create(WithNestedLists, {
323
+ names: [],
324
+ tags: []
325
+ });
326
+
327
+ let total = await service.queryCount(WithNestedLists, {
328
+ where: {
329
+ names: { $empty: false }
330
+ }
331
+ });
332
+ assert(total === 3);
333
+ total = await service.queryCount(WithNestedLists, {
334
+ where: {
335
+ names: { $empty: true }
336
+ }
337
+ });
338
+ assert(total === 2);
339
+
340
+ total = await service.queryCount(WithNestedLists, {
341
+ where: {
342
+ tags: { $empty: true }
343
+ }
344
+ });
345
+ assert(total === 3);
346
+
347
+ total = await service.queryCount(WithNestedLists, {
348
+ where: {
349
+ tags: { $empty: false }
350
+ }
351
+ });
352
+ assert(total === 2);
353
+
354
+ total = await service.queryCount(WithNestedLists, {
355
+ where: {
356
+ tags: { $empty: true },
357
+ names: { $empty: true }
358
+ }
359
+ });
360
+ assert(total === 1);
361
+
362
+ total = await service.queryCount(WithNestedLists, {
363
+ where: {
364
+ tags: { $empty: false },
365
+ names: { $empty: false }
366
+ }
367
+ });
300
368
 
369
+ assert(total === 1);
301
370
  }
302
371
  }
303
372
 
@@ -74,4 +74,11 @@ export class Region {
74
74
  export class Aged {
75
75
  id: string;
76
76
  createdAt: Date;
77
+ }
78
+
79
+ @Model()
80
+ export class WithNestedLists {
81
+ id: string;
82
+ tags?: string[] = [];
83
+ names?: string[] = [];
77
84
  }