@nymphjs/nymph 1.0.0-alpha.4 → 1.0.0-alpha.40

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.
@@ -1319,6 +1319,55 @@ This one's zip code is 92064.`;
1319
1319
  expect(testEntity.$inArray(resultEntity)).toEqual(true);
1320
1320
  });
1321
1321
 
1322
+ it('count return', async () => {
1323
+ await createTestEntities();
1324
+
1325
+ const result = await nymph.getEntities({ class: TestModel });
1326
+
1327
+ // Testing count return...
1328
+ const resultCount = await nymph.getEntities({
1329
+ class: TestModel,
1330
+ return: 'count',
1331
+ });
1332
+ expect(resultCount).toBeGreaterThanOrEqual(1);
1333
+ expect(resultCount).toEqual(result.length);
1334
+
1335
+ const resultSelectors = await nymph.getEntities(
1336
+ { class: TestModel },
1337
+ { type: '&', ref: ['reference', refGuid] }
1338
+ );
1339
+
1340
+ // Testing count return with selectors...
1341
+ const resultSelectorsCount = await nymph.getEntities(
1342
+ { class: TestModel, return: 'count' },
1343
+ { type: '&', ref: ['reference', refGuid] }
1344
+ );
1345
+ expect(resultSelectorsCount).toBeGreaterThanOrEqual(1);
1346
+ expect(resultSelectorsCount).toEqual(resultSelectors.length);
1347
+
1348
+ // Testing count return with limit...
1349
+ const resultSelectorsLimit = await nymph.getEntities({
1350
+ class: TestModel,
1351
+ limit: 1,
1352
+ return: 'count',
1353
+ });
1354
+ expect(resultSelectorsLimit).toEqual(1);
1355
+
1356
+ // Testing count return with limit...
1357
+ const resultSelectorsSingle = await nymph.getEntity({
1358
+ class: TestModel,
1359
+ return: 'count',
1360
+ });
1361
+ expect(resultSelectorsSingle).toEqual(1);
1362
+
1363
+ // Testing empty count...
1364
+ const resultSelectorsEmpty = await nymph.getEntities(
1365
+ { class: TestModel, return: 'count' },
1366
+ { type: '&', ref: ['reference', newGUID()] }
1367
+ );
1368
+ expect(resultSelectorsEmpty).toEqual(0);
1369
+ });
1370
+
1322
1371
  it('guid return', async () => {
1323
1372
  await createTestEntities();
1324
1373
 
package/src/index.ts CHANGED
@@ -17,4 +17,7 @@ export * from './Nymph';
17
17
  import Nymph from './Nymph';
18
18
  export { Nymph };
19
19
 
20
+ import nymphJoiProps from './nymphJoiProps';
21
+ export { nymphJoiProps };
22
+
20
23
  export * from './utils';
@@ -0,0 +1,24 @@
1
+ import Joi from 'joi';
2
+
3
+ export default {
4
+ guid: Joi.alternatives()
5
+ .try(
6
+ Joi.any().only().allow(null),
7
+ Joi.string().trim(false).length(24).hex()
8
+ )
9
+ .required(),
10
+ cdate: Joi.alternatives()
11
+ .try(Joi.any().only().allow(null), Joi.number())
12
+ .required(),
13
+ mdate: Joi.alternatives()
14
+ .try(Joi.any().only().allow(null), Joi.number())
15
+ .required(),
16
+ tags: Joi.array()
17
+ .items(
18
+ Joi.string().pattern(/[[\x01-\x1F\x7F]]/, {
19
+ name: 'control characters',
20
+ invert: true,
21
+ })
22
+ )
23
+ .required(),
24
+ };
package/src/utils.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import Entity from './Entity';
2
2
  import { EntityReference } from './Entity.types';
3
3
  import Nymph from './Nymph';
4
+ import { Options, Selector } from './Nymph.types';
4
5
 
5
6
  export function xor(a: any, b: any): boolean {
6
7
  return !!(a && !b) || (!a && b);
@@ -14,6 +15,64 @@ export function uniqueStrings(array: string[]) {
14
15
  return Object.keys(obj);
15
16
  }
16
17
 
18
+ export function classNamesToEntityConstructors(
19
+ nymph: Nymph,
20
+ selectors: Selector[]
21
+ ): Selector[] {
22
+ const newSelectors: Selector[] = [];
23
+
24
+ for (const curSelector of selectors) {
25
+ const newSelector: Selector = { type: curSelector.type };
26
+
27
+ for (const k in curSelector) {
28
+ const key = k as keyof Selector;
29
+ const value = curSelector[key];
30
+
31
+ if (key === 'type') {
32
+ continue;
33
+ }
34
+
35
+ if (value === undefined) {
36
+ continue;
37
+ }
38
+
39
+ if (key === 'qref' || key === '!qref') {
40
+ const tmpArr = (
41
+ Array.isArray(((value as Selector['qref']) ?? [])[0])
42
+ ? value
43
+ : [value]
44
+ ) as [string, [Options, ...Selector[]]][];
45
+ for (let i = 0; i < tmpArr.length; i++) {
46
+ const name = tmpArr[i][0];
47
+ const [qrefOptions, ...selectors] = tmpArr[i][1];
48
+ const QrefEntityClass =
49
+ typeof qrefOptions.class === 'string'
50
+ ? nymph.getEntityClass(qrefOptions.class)
51
+ : qrefOptions.class ?? nymph.getEntityClass('Entity');
52
+ const options = { ...qrefOptions, class: QrefEntityClass };
53
+ if (!newSelector[key]) {
54
+ newSelector[key] = [];
55
+ }
56
+ (newSelector[key] as [string, [Options, ...Selector[]]][]).push([
57
+ name,
58
+ [options, ...classNamesToEntityConstructors(nymph, selectors)],
59
+ ]);
60
+ }
61
+ } else if (key === 'selector' || key === '!selector') {
62
+ const tmpArr = (Array.isArray(value) ? value : [value]) as Selector[];
63
+ newSelector[key] = classNamesToEntityConstructors(nymph, tmpArr);
64
+ } else {
65
+ // @ts-ignore: ts doesn't know what value is here.
66
+ newSelector[key] = value;
67
+ }
68
+ }
69
+
70
+ newSelectors.push(newSelector);
71
+ }
72
+
73
+ return newSelectors;
74
+ }
75
+
17
76
  export function entitiesToReferences(item: any): any {
18
77
  if (item instanceof Entity && typeof item.$toReference === 'function') {
19
78
  // Convert entities to references.