@adaas/a-concept 0.2.4 → 0.2.5

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": "@adaas/a-concept",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "A-Concept is a framework of the new generation that is tailored to use AI, enabling developers to create AI-powered applications with ease. It provides a structured approach to building, managing, and deploying AI-driven solutions.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.cjs",
@@ -16,12 +16,7 @@ import { A_TYPES__EntityMetaKey } from "../A-Entity/A-Entity.constants";
16
16
  /**
17
17
  * Should indicate which All is required
18
18
  */
19
- export function A_Dependency_All<T extends A_Entity>(
20
- /**
21
- * Constructor Parameters that will be used to create the default instance
22
- */
23
- entity: A_TYPES__Entity_Constructor<T>
24
- ): A_TYPES__A_Dependency_AllDecoratorReturn {
19
+ export function A_Dependency_All(): A_TYPES__A_Dependency_AllDecoratorReturn {
25
20
 
26
21
  return function (
27
22
  target: A_TYPES__InjectableTargets,
@@ -175,6 +175,7 @@ export class A_Dependency<
175
175
  ...strategy,
176
176
  pagination: {
177
177
  ...this._defaultPagination,
178
+ ...(this._resolutionStrategy || {}).pagination,
178
179
  ...(strategy.pagination || {})
179
180
  },
180
181
  };
@@ -766,20 +766,36 @@ export class A_Scope<
766
766
 
767
767
 
768
768
  // 5) apply pagination
769
+
769
770
  const count = dependency.pagination.count;
770
771
  const from = dependency.pagination.from; // from start or from end
771
772
 
772
773
 
774
+
773
775
  const startSliceIndex = from === 'end'
774
- ? Math.max(result.length - count, 0)
776
+ ? (count === -1 ? 0 : Math.max(result.length - count, 0))
775
777
  : 0;
778
+
779
+ // end slice should handle -1 for all items
776
780
  const endSliceIndex = from === 'end'
777
781
  ? result.length
778
- : Math.min(count, result.length);
782
+ : (count === -1 ? result.length : Math.min(count, result.length));
779
783
 
780
784
  const slice = result.slice(startSliceIndex, endSliceIndex);
781
785
 
782
- return slice.length === 1 ? slice[0] : slice.length ? slice : undefined;
786
+ /**
787
+ * Default behavior is to return single instance if count is 1
788
+ *
789
+ * If Directive All (-1) is provided or count > 1, an array is returned
790
+ *
791
+ * If no instances found, undefined is returned
792
+ */
793
+ return slice.length === 1
794
+ && count !== -1
795
+ ? slice[0]
796
+ : slice.length
797
+ ? slice
798
+ : undefined;
783
799
  }
784
800
 
785
801
 
@@ -940,17 +956,20 @@ export class A_Scope<
940
956
  param1: A_TYPES__Ctor<A_TYPES__A_DependencyInjectable> | string
941
957
  ): Array<T> {
942
958
 
943
- const results: Array<T> = [];
959
+ const results: Set<T> = new Set();
944
960
 
945
961
  // 1) Resolve all in the current scope
946
962
  const currentResults = this.resolveFlatAll<T>(param1 as any);
947
- results.push(...currentResults);
963
+ currentResults.forEach(result => results.add(result));
948
964
 
949
965
  // 2) resolve all in the imported scopes
950
966
  this._imports.forEach(importedScope => {
967
+
951
968
  if (importedScope.has(param1 as any)) {
952
969
  const importedResults = importedScope.resolveFlatAll<T>(param1 as any);
953
- results.push(...importedResults);
970
+
971
+
972
+ importedResults.forEach(result => results.add(result));
954
973
  }
955
974
  });
956
975
 
@@ -958,8 +977,9 @@ export class A_Scope<
958
977
  let parentScope = this._parent;
959
978
 
960
979
  while (parentScope && parentScope.has(param1 as any)) {
961
- const parentResults = parentScope.resolveFlatAll<T>(param1 as any);
962
- results.push(...parentResults);
980
+
981
+ const parentResults = parentScope.resolveAll<T>(param1 as any);
982
+ parentResults.forEach(result => results.add(result));
963
983
 
964
984
  // Move to the next parent scope
965
985
  parentScope = parentScope._parent;
@@ -967,7 +987,7 @@ export class A_Scope<
967
987
 
968
988
 
969
989
 
970
- return results;
990
+ return Array.from(results);
971
991
  }
972
992
 
973
993
 
@@ -1128,7 +1148,6 @@ export class A_Scope<
1128
1148
  param1 as A_Dependency<T> :
1129
1149
  new A_Dependency<T>(param1)
1130
1150
 
1131
-
1132
1151
  return this.resolveDependency<T>(dependency);
1133
1152
  }
1134
1153
 
@@ -49,7 +49,7 @@ describe('A-Dependency tests', () => {
49
49
  }
50
50
 
51
51
  class testEntity extends A_Entity {
52
-
52
+
53
53
  @A_Feature.Extend({
54
54
  name: 'test'
55
55
  })
@@ -357,5 +357,97 @@ describe('A-Dependency tests', () => {
357
357
  // });
358
358
 
359
359
  });
360
+ it('Should support directive All for query', async () => {
361
+
362
+ const executionChain: number[] = [];
363
+
364
+ class MyEntity_A extends A_Entity<{ name: string }> {
365
+ name!: string;
366
+
367
+ fromNew(newEntity: { name: string; }): void {
368
+ super.fromNew(newEntity);
369
+ this.name = newEntity.name;
370
+ }
371
+ }
372
+
373
+ class MyComponent extends A_Component {
374
+
375
+
376
+ @A_Feature.Extend()
377
+ async allEntities(
378
+ @A_Dependency.All()
379
+ @A_Inject(MyEntity_A) entities: MyEntity_A[],
380
+ @A_Inject(A_Scope) scope: A_Scope,
381
+ ) {
382
+ executionChain.push(entities.length);
383
+ }
384
+
385
+ @A_Feature.Extend()
386
+ async allFlatEntities(
387
+ @A_Dependency.Flat()
388
+ // That's important because current scope is feature scope, and plat applies for current scope only
389
+ @A_Dependency.All()
390
+ @A_Dependency.Parent(-1)
391
+ @A_Inject(MyEntity_A) entities: MyEntity_A[],
392
+ @A_Inject(A_Scope) scope: A_Scope,
393
+
394
+ ) {
395
+ executionChain.push(entities.length);
396
+ }
397
+
398
+ @A_Feature.Extend()
399
+ async withImportedEntities(
400
+ @A_Dependency.All()
401
+ @A_Inject(MyEntity_A) entities: MyEntity_A[],
402
+ ) {
403
+ executionChain.push(entities.length);
404
+ }
405
+ }
406
+
407
+ const parentScope = new A_Scope({
408
+ name: 'Parent Scope',
409
+ entities: [
410
+ new MyEntity_A({ name: 'Entity 1' }),
411
+ new MyEntity_A({ name: 'Entity 2' }),
412
+ new MyEntity_A({ name: 'Entity 3' }),
413
+ ]
414
+ });
415
+
416
+ const childScope = new A_Scope({
417
+ name: 'Child Scope',
418
+ components: [MyComponent],
419
+ entities: [
420
+ new MyEntity_A({ name: 'Entity 4' }),
421
+ new MyEntity_A({ name: 'Entity 5' }),
422
+ ]
423
+ }, { parent: parentScope });
424
+
425
+
426
+ const importScope = new A_Scope({
427
+ name: 'Import Scope',
428
+ entities: [
429
+ new MyEntity_A({ name: 'Entity 6' }),
430
+ ]
431
+ });
432
+
433
+
434
+
435
+ const componentInstance = childScope.resolve(MyComponent);
436
+
437
+ await componentInstance?.call('allEntities');
438
+ await componentInstance?.call('allFlatEntities');
439
+ await componentInstance?.call('withImportedEntities');
440
+
441
+ expect(executionChain).toEqual([5, 2, 5]);
442
+
443
+ childScope.import(importScope);
444
+
445
+ await componentInstance?.call('withImportedEntities');
446
+
447
+
448
+ expect(executionChain).toEqual([5, 2, 5, 6]);
449
+
450
+
451
+ });
360
452
 
361
453
  });