@karmaniverous/entity-manager 6.7.1 → 6.7.3

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.
@@ -9,17 +9,18 @@ var radash = require('radash');
9
9
  */
10
10
  class BaseShardQueryMapBuilder {
11
11
  /** BaseShardQueryMapBuilder constructor. */
12
- constructor(entityManager, entityToken, hashKeyToken, pageKeyMap) {
13
- this.entityManager = entityManager;
14
- this.entityToken = entityToken;
15
- this.hashKeyToken = hashKeyToken;
16
- this.pageKeyMap = pageKeyMap;
12
+ constructor(options) {
17
13
  /**
18
- * Maps `indexToken` values to database platform-specific parameters.
14
+ * Maps `indexToken` values to database platform-specific query parameters.
19
15
  *
20
16
  * @protected
21
17
  */
22
18
  this.indexParamsMap = {};
19
+ const { entityManager, entityToken, hashKeyToken, pageKeyMap } = options;
20
+ this.entityManager = entityManager;
21
+ this.entityToken = entityToken;
22
+ this.hashKeyToken = hashKeyToken;
23
+ this.pageKeyMap = pageKeyMap;
23
24
  }
24
25
  /**
25
26
  * Builds a {@link ShardQueryMap | `ShardQueryMap`} object.
@@ -29,6 +30,16 @@ class BaseShardQueryMapBuilder {
29
30
  build() {
30
31
  return radash.mapValues(this.indexParamsMap, (indexConfig, indexToken) => this.getShardQueryFunction(indexToken));
31
32
  }
33
+ async query(options) {
34
+ const { entityManager, entityToken, pageKeyMap } = this;
35
+ const shardQueryMap = this.build();
36
+ return await entityManager.query({
37
+ ...options,
38
+ entityToken,
39
+ pageKeyMap,
40
+ shardQueryMap,
41
+ });
42
+ }
32
43
  }
33
44
 
34
45
  exports.BaseShardQueryMapBuilder = BaseShardQueryMapBuilder;
package/dist/index.d.ts CHANGED
@@ -363,6 +363,24 @@ type ItemMap<M extends EntityMap, HashKey extends string = 'hashKey', RangeKey e
363
363
  } & Partial<Record<HashKey | RangeKey, string>>>;
364
364
  };
365
365
 
366
+ /**
367
+ * A result returned by a query across multiple shards, where each shard may
368
+ * receive multiple page queries via a dynamically-generated {@link ShardQueryFunction | `ShardQueryFunction`}.
369
+ *
370
+ * @category Query
371
+ */
372
+ interface QueryResult<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string> {
373
+ /** Total number of records returned across all shards. */
374
+ count: number;
375
+ /** The returned records. */
376
+ items: Item[];
377
+ /**
378
+ * A compressed, two-layer map of page keys, used to query the next page of
379
+ * data for a given sort key on each shard of a given hash key.
380
+ */
381
+ pageKeyMap: string;
382
+ }
383
+
366
384
  declare const configSchema: z.ZodEffects<z.ZodObject<{
367
385
  entities: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodObject<{
368
386
  defaultLimit: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
@@ -785,24 +803,6 @@ interface QueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken],
785
803
  throttle?: number;
786
804
  }
787
805
 
788
- /**
789
- * A result returned by a query across multiple shards, where each shard may
790
- * receive multiple page queries via a dynamically-generated {@link ShardQueryFunction | `ShardQueryFunction`}.
791
- *
792
- * @category Query
793
- */
794
- interface QueryResult<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string> {
795
- /** Total number of records returned across all shards. */
796
- count: number;
797
- /** The returned records. */
798
- items: Item[];
799
- /**
800
- * A compressed, two-layer map of page keys, used to query the next page of
801
- * data for a given sort key on each shard of a given hash key.
802
- */
803
- pageKeyMap: string;
804
- }
805
-
806
806
  /**
807
807
  * The EntityManager class applies a configuration-driven sharded data model &
808
808
  * query strategy to NoSql data.
@@ -875,24 +875,44 @@ declare class EntityManager<M extends EntityMap, HashKey extends string, RangeKe
875
875
  query<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(options: QueryOptions<Item, EntityToken, M, HashKey, RangeKey>): Promise<QueryResult<Item, EntityToken, M, HashKey, RangeKey>>;
876
876
  }
877
877
 
878
+ /**
879
+ * Constructor options for {@link BaseShardQueryMapBuilder | `BaseShardQueryMapBuilder`}.
880
+ */
881
+ interface BaseShardQueryMapBuilderOptions<EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string, T extends TranscodeMap> {
882
+ /** {@link EntityManager | `EntityManager`} instance. */
883
+ entityManager: EntityManager<M, HashKey, RangeKey, T>;
884
+ /** Entity token. */
885
+ entityToken: EntityToken;
886
+ /** Hash key token. */
887
+ hashKeyToken: PropertiesOfType<M[EntityToken], never> | HashKey;
888
+ /** Dehydrated page key map. */
889
+ pageKeyMap?: string;
890
+ }
891
+
892
+ type ShardQueryMapBuilderQueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string> = Omit<QueryOptions<Item, EntityToken, M, HashKey, RangeKey>, 'entityToken' | 'pageKeyMap' | 'shardQueryMap'>;
893
+
878
894
  /**
879
895
  * Abstract base class supporting a fluent API for building a {@link ShardQueryMap | `ShardQueryMap`} using a database client.
880
896
  *
881
897
  * @category ShardQueryMapBuilder
882
898
  */
883
899
  declare abstract class BaseShardQueryMapBuilder<IndexParams, Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string, T extends TranscodeMap> {
900
+ /** {@link EntityManager | `EntityManager`} instance. */
884
901
  readonly entityManager: EntityManager<M, HashKey, RangeKey, T>;
902
+ /** Entity token. */
885
903
  readonly entityToken: EntityToken;
904
+ /** Hash key token. */
886
905
  readonly hashKeyToken: PropertiesOfType<M[EntityToken], never> | HashKey;
887
- readonly pageKeyMap?: string | undefined;
906
+ /** Dehydrated page key map. */
907
+ readonly pageKeyMap?: string;
888
908
  /**
889
- * Maps `indexToken` values to database platform-specific parameters.
909
+ * Maps `indexToken` values to database platform-specific query parameters.
890
910
  *
891
911
  * @protected
892
912
  */
893
913
  readonly indexParamsMap: Record<string, IndexParams>;
894
914
  /** BaseShardQueryMapBuilder constructor. */
895
- constructor(entityManager: EntityManager<M, HashKey, RangeKey, T>, entityToken: EntityToken, hashKeyToken: PropertiesOfType<M[EntityToken], never> | HashKey, pageKeyMap?: string | undefined);
915
+ constructor(options: BaseShardQueryMapBuilderOptions<EntityToken, M, HashKey, RangeKey, T>);
896
916
  protected abstract getShardQueryFunction(indexToken: string): ShardQueryFunction<Item>;
897
917
  /**
898
918
  * Builds a {@link ShardQueryMap | `ShardQueryMap`} object.
@@ -900,6 +920,7 @@ declare abstract class BaseShardQueryMapBuilder<IndexParams, Item extends ItemMa
900
920
  * @returns - The {@link ShardQueryMap | `ShardQueryMap`} object.
901
921
  */
902
922
  build(): ShardQueryMap<Item>;
923
+ query(options: ShardQueryMapBuilderQueryOptions<Item, EntityToken, M, HashKey, RangeKey>): Promise<QueryResult<Item, EntityToken, M, HashKey, RangeKey>>;
903
924
  }
904
925
 
905
926
  /**
@@ -915,4 +936,4 @@ declare abstract class BaseShardQueryMapBuilder<IndexParams, Item extends ItemMa
915
936
  */
916
937
  declare function conditionalize<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, condition?: unknown): (...args: Parameters<F>) => ReturnType<F> | undefined;
917
938
 
918
- export { BaseShardQueryMapBuilder, type Config, type ConfigEntities, type ConfigEntity, type ConfigEntityGenerated, type ConfigEntityIndexComponent, type ConfigKeys, type ConfigTranscodes, EntityManager, type EntityMap, type ExclusiveKey, type ItemMap, type ParsedConfig, type QueryOptions, type QueryResult, type ShardBump, type ShardQueryFunction, type ShardQueryMap, type ShardQueryResult, type Unwrap, conditionalize };
939
+ export { BaseShardQueryMapBuilder, type BaseShardQueryMapBuilderOptions, type Config, type ConfigEntities, type ConfigEntity, type ConfigEntityGenerated, type ConfigEntityIndexComponent, type ConfigKeys, type ConfigTranscodes, EntityManager, type EntityMap, type ExclusiveKey, type ItemMap, type ParsedConfig, type QueryOptions, type QueryResult, type ShardBump, type ShardQueryFunction, type ShardQueryMap, type ShardQueryMapBuilderQueryOptions, type ShardQueryResult, type Unwrap, conditionalize };
@@ -7,17 +7,18 @@ import { mapValues } from 'radash';
7
7
  */
8
8
  class BaseShardQueryMapBuilder {
9
9
  /** BaseShardQueryMapBuilder constructor. */
10
- constructor(entityManager, entityToken, hashKeyToken, pageKeyMap) {
11
- this.entityManager = entityManager;
12
- this.entityToken = entityToken;
13
- this.hashKeyToken = hashKeyToken;
14
- this.pageKeyMap = pageKeyMap;
10
+ constructor(options) {
15
11
  /**
16
- * Maps `indexToken` values to database platform-specific parameters.
12
+ * Maps `indexToken` values to database platform-specific query parameters.
17
13
  *
18
14
  * @protected
19
15
  */
20
16
  this.indexParamsMap = {};
17
+ const { entityManager, entityToken, hashKeyToken, pageKeyMap } = options;
18
+ this.entityManager = entityManager;
19
+ this.entityToken = entityToken;
20
+ this.hashKeyToken = hashKeyToken;
21
+ this.pageKeyMap = pageKeyMap;
21
22
  }
22
23
  /**
23
24
  * Builds a {@link ShardQueryMap | `ShardQueryMap`} object.
@@ -27,6 +28,16 @@ class BaseShardQueryMapBuilder {
27
28
  build() {
28
29
  return mapValues(this.indexParamsMap, (indexConfig, indexToken) => this.getShardQueryFunction(indexToken));
29
30
  }
31
+ async query(options) {
32
+ const { entityManager, entityToken, pageKeyMap } = this;
33
+ const shardQueryMap = this.build();
34
+ return await entityManager.query({
35
+ ...options,
36
+ entityToken,
37
+ pageKeyMap,
38
+ shardQueryMap,
39
+ });
40
+ }
30
41
  }
31
42
 
32
43
  export { BaseShardQueryMapBuilder };
package/package.json CHANGED
@@ -131,5 +131,5 @@
131
131
  },
132
132
  "type": "module",
133
133
  "types": "dist/index.d.ts",
134
- "version": "6.7.1"
134
+ "version": "6.7.3"
135
135
  }