@karmaniverous/entity-manager 6.5.1 → 6.7.0

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/dist/index.d.ts CHANGED
@@ -1,19 +1,6 @@
1
1
  import { Entity, Exactify, TranscodeMap, PropertiesOfType, TranscodableProperties, Transcodes, DefaultTranscodeMap, SortOrder } from '@karmaniverous/entity-tools';
2
2
  import { z } from 'zod';
3
3
 
4
- /**
5
- * Transforms a function such that it only executes when `condition` is truthy.
6
- *
7
- * @param fn - The function to conditionally execute.
8
- * @param condition - The condition to check before executing `fn`.
9
- *
10
- * @typeParam F - The type of the function to conditionally execute.
11
- *
12
- * @returns The conditionalized function with the same signature as `fn`.
13
- *
14
- */
15
- declare function conditionalize<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, condition?: unknown): (...args: Parameters<F>) => ReturnType<F> | undefined;
16
-
17
4
  /**
18
5
  * The base EntityMap type. All EntityMaps should extend this type.
19
6
  *
@@ -81,7 +68,7 @@ interface ShardBump {
81
68
  chars: number;
82
69
  }
83
70
  /**
84
- * Returns a Config entity index components type.
71
+ * Returns a Config entity index component type.
85
72
  *
86
73
  * @typeParam EntityToken - The {@link Entity | `Entity`} token.
87
74
  * @typeParam M - The {@link EntityMap | `EntityMap`}.
@@ -688,53 +675,11 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
688
675
  type ParsedConfig = z.infer<typeof configSchema>;
689
676
 
690
677
  /**
691
- * A result returned by a {@link ShardQueryFunction | `ShardQueryFunction`} querying an individual shard.
692
- *
693
- * @typeParam Item - The {@link Item | `Item`} type being queried.
694
-
695
- * @category Query
696
- */
697
- interface ShardQueryResult<Item extends Entity> {
698
- /** The number of records returned. */
699
- count: number;
700
- /** The returned records. */
701
- items: Item[];
702
- /** The page key for the next query on this shard. */
703
- pageKey?: Partial<Item>;
704
- }
705
-
706
- /**
707
- * A query function that returns a single page of results from an individual
708
- * shard. This function will typically be composed dynamically to express a
709
- * specific query index & logic. The arguments to this function will be
710
- * provided by the {@link EntityManager.query | `EntityManager.query`} method, which assembles many returned
711
- * pages queried across multiple shards into a single query result.
712
- *
713
- * @typeParam Item - The {@link Item | `Item`} type being queried.
714
-
715
- * @param hashKey - The hash key value of the shard being queried.
716
- * @param pageKey - The page key returned by the previous query on this shard.
717
- * @param pageSize - The maximum number of items to return from this query.
718
- *
719
- * @category Query
720
- */
721
- type ShardQueryFunction<Item extends Entity> = (hashKey: string, pageKey?: Partial<Item>, pageSize?: number) => Promise<ShardQueryResult<Item>>;
722
-
723
- type ShardQueryMap<Item extends Entity> = Record<string, ShardQueryFunction<Item>>;
724
-
725
- /**
726
- * Options passed to the {@link EntityManager.query | `EntityManager.query`} method.
678
+ * Base type of options passed to the {@link EntityManager.query | `EntityManager.query`} method.
727
679
  *
728
680
  * @category Query
729
681
  */
730
- interface QueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string> {
731
- /** Identifies the entity to be queried. Key of {@link Config | `EntityManager.config.entities`}. */
732
- entityToken: EntityToken;
733
- /**
734
- * Identifies the entity key across which the query will be sharded. Key of
735
- * {@link Config | `EntityManager.config.entities.<entityToken>.keys`}.
736
- */
737
- hashKey: string;
682
+ interface BaseQueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string> {
738
683
  /**
739
684
  * The target maximum number of records to be returned by the query across
740
685
  * all shards.
@@ -744,10 +689,6 @@ interface QueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken],
744
689
  * shard.
745
690
  */
746
691
  limit?: number;
747
- /**
748
- * {@link QueryResult.pageKeyMap | `pageKeyMap`} returned by the previous iteration of this query.
749
- */
750
- pageKeyMap?: string;
751
692
  /**
752
693
  * The maximum number of records to be returned by each individual query to a
753
694
  * single shard (i.e. {@link ShardQueryFunction | `ShardQueryFunction`} execution).
@@ -757,16 +698,6 @@ interface QueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken],
757
698
  * reached.
758
699
  */
759
700
  pageSize?: number;
760
- /**
761
- * Each key in this object is a valid entity index token. Each value is a valid
762
- * {@link ShardQueryFunction | 'ShardQueryFunction'} that specifies the query of a single page of data on a
763
- * single shard for the mapped index.
764
- *
765
- * This allows simultaneous queries on multiple sort keys to share a single
766
- * page key, e.g. to match the same string against `firstName` and `lastName`
767
- * properties without performing a table scan for either.
768
- */
769
- shardQueryMap: ShardQueryMap<Item>;
770
701
  /**
771
702
  * A {@link SortOrder | `SortOrder`} object specifying the sort order of the result set. Defaults to `[]`.
772
703
  */
@@ -799,6 +730,16 @@ interface QueryOptions<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken],
799
730
  throttle?: number;
800
731
  }
801
732
 
733
+ /**
734
+ * Options passed to the {@link EntityManager.query | `EntityManager.query`} method.
735
+ *
736
+ * @category Query
737
+ */
738
+ interface QueryOptions<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> extends BaseQueryOptions<Item, EntityToken, M, HashKey, RangeKey> {
739
+ /** Instance of class extending {@link BaseShardQueryMapBuilder | `BaseShardQueryMapBuilder`}. */
740
+ shardQueryMapBuilder: BaseShardQueryMapBuilder<IndexParams, Item, EntityToken, M, HashKey, RangeKey, T>;
741
+ }
742
+
802
743
  /**
803
744
  * A result returned by a query across multiple shards, where each shard may
804
745
  * receive multiple page queries via a dynamically-generated {@link ShardQueryFunction | `ShardQueryFunction`}.
@@ -848,31 +789,31 @@ declare class EntityManager<M extends EntityMap, HashKey extends string, RangeKe
848
789
  /**
849
790
  * Update generated properties, hash key, and range key on an {@link ItemMap | `ItemMap`} object.
850
791
  *
851
- * @param item - {@link ItemMap | `ItemMap`} object.
852
792
  * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
793
+ * @param item - {@link ItemMap | `ItemMap`} object.
853
794
  * @param overwrite - Overwrite existing properties (default `false`).
854
795
  *
855
796
  * @returns Shallow clone of `item` with updated properties.
856
797
  *
857
798
  * @throws `Error` if `entityToken` is invalid.
858
799
  */
859
- addKeys<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(item: Partial<Item>, entityToken: EntityToken, overwrite?: boolean): Partial<Item>;
800
+ addKeys<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(entityToken: EntityToken, item: Partial<Item>, overwrite?: boolean): Partial<Item>;
860
801
  /**
861
802
  * Strips generated properties, hash key, and range key from an {@link ItemMap | `ItemMap`} object.
862
803
  *
863
- * @param item - {@link ItemMap | `ItemMap`} object.
864
804
  * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
805
+ * @param item - {@link ItemMap | `ItemMap`} object.
865
806
  *
866
807
  * @returns Shallow clone of `item` without generated properties, hash key or range key.
867
808
  *
868
809
  * @throws `Error` if `entityToken` is invalid.
869
810
  */
870
- removeKeys<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(item: Partial<Item>, entityToken: EntityToken): Partial<Item>;
811
+ removeKeys<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(entityToken: EntityToken, item: Partial<Item>): Partial<Item>;
871
812
  /**
872
813
  * Query a database entity across shards in a provider-generic fashion.
873
814
  *
874
815
  * @remarks
875
- * The provided {@link ShardQueryFunction | `ShardQueryFunction`} performs the actual query of individual data pages on individual shards. This function is presumed to express provider-specific query logic, including any necessary indexing or search constraints.
816
+ * The provided `shardQueryMap` performs the actual query of individual data pages on individual index/shard combinations.
876
817
  *
877
818
  * Individual shard query results will be combined, deduped by {@link ConfigEntity.uniqueProperty} property value, and sorted by {@link QueryOptions.sortOrder | `sortOrder`}.
878
819
  *
@@ -884,9 +825,84 @@ declare class EntityManager<M extends EntityMap, HashKey extends string, RangeKe
884
825
  *
885
826
  * @returns {@link QueryResult} object.
886
827
  *
887
- * @throws Error if {@link QueryOptions.pageKeyMap | `pageKeyMap`} keys do not match {@link QueryOptions.shardQueryMap | `shardQueryMap`} keys.
828
+ * @throws Error if {@link QueryOptions.shardQueryMapBuilder | `shardQueryMapBuilder`} `pageKeyMap` keys do not match its `shardQueryMap` keys.
829
+ */
830
+ query<IndexParams, Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(options: QueryOptions<IndexParams, Item, EntityToken, M, HashKey, RangeKey, T>): Promise<QueryResult<Item, EntityToken, M, HashKey, RangeKey>>;
831
+ }
832
+
833
+ /**
834
+ * A result returned by a {@link ShardQueryFunction | `ShardQueryFunction`} querying an individual shard.
835
+ *
836
+ * @typeParam Item - The {@link Item | `Item`} type being queried.
837
+
838
+ * @category Query
839
+ */
840
+ interface ShardQueryResult<Item extends Entity> {
841
+ /** The number of records returned. */
842
+ count: number;
843
+ /** The returned records. */
844
+ items: Item[];
845
+ /** The page key for the next query on this shard. */
846
+ pageKey?: Partial<Item>;
847
+ }
848
+
849
+ /**
850
+ * A query function that returns a single page of results from an individual
851
+ * shard. This function will typically be composed dynamically to express a
852
+ * specific query index & logic. The arguments to this function will be
853
+ * provided by the {@link EntityManager.query | `EntityManager.query`} method, which assembles many returned
854
+ * pages queried across multiple shards into a single query result.
855
+ *
856
+ * @typeParam Item - The {@link Item | `Item`} type being queried.
857
+
858
+ * @param hashKey - The hash key value of the shard being queried.
859
+ * @param pageKey - The page key returned by the previous query on this shard.
860
+ * @param pageSize - The maximum number of items to return from this query.
861
+ *
862
+ * @category Query
863
+ */
864
+ type ShardQueryFunction<Item extends Entity> = (hashKey: string, pageKey?: Partial<Item>, pageSize?: number) => Promise<ShardQueryResult<Item>>;
865
+
866
+ type ShardQueryMap<Item extends Entity> = Record<string, ShardQueryFunction<Item>>;
867
+
868
+ /**
869
+ * Abstract base class supporting a fluent API for building a {@link ShardQueryMap | `ShardQueryMap`} using a database client.
870
+ *
871
+ * @category ShardQueryMapBuilder
872
+ */
873
+ 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> {
874
+ readonly entityManager: EntityManager<M, HashKey, RangeKey, T>;
875
+ readonly entityToken: EntityToken;
876
+ readonly hashKeyToken: PropertiesOfType<M[EntityToken], never> | HashKey;
877
+ readonly pageKeyMap?: string | undefined;
878
+ /**
879
+ * Maps `indexToken` values to database platform-specific parameters.
880
+ *
881
+ * @protected
882
+ */
883
+ readonly indexParamsMap: Record<string, IndexParams>;
884
+ /** BaseShardQueryMapBuilder constructor. */
885
+ constructor(entityManager: EntityManager<M, HashKey, RangeKey, T>, entityToken: EntityToken, hashKeyToken: PropertiesOfType<M[EntityToken], never> | HashKey, pageKeyMap?: string | undefined);
886
+ protected abstract getShardQueryFunction(indexToken: string): ShardQueryFunction<Item>;
887
+ /**
888
+ * Builds a {@link ShardQueryMap | `ShardQueryMap`} object.
889
+ *
890
+ * @returns - The {@link ShardQueryMap | `ShardQueryMap`} object.
888
891
  */
889
- 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>>;
892
+ build(): ShardQueryMap<Item>;
890
893
  }
891
894
 
892
- export { 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 };
895
+ /**
896
+ * Transforms a function such that it only executes when `condition` is truthy.
897
+ *
898
+ * @param fn - The function to conditionally execute.
899
+ * @param condition - The condition to check before executing `fn`.
900
+ *
901
+ * @typeParam F - The type of the function to conditionally execute.
902
+ *
903
+ * @returns The conditionalized function with the same signature as `fn`.
904
+ *
905
+ */
906
+ declare function conditionalize<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, condition?: unknown): (...args: Parameters<F>) => ReturnType<F> | undefined;
907
+
908
+ 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 };
@@ -0,0 +1,32 @@
1
+ import { mapValues } from 'radash';
2
+
3
+ /**
4
+ * Abstract base class supporting a fluent API for building a {@link ShardQueryMap | `ShardQueryMap`} using a database client.
5
+ *
6
+ * @category ShardQueryMapBuilder
7
+ */
8
+ class BaseShardQueryMapBuilder {
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;
15
+ /**
16
+ * Maps `indexToken` values to database platform-specific parameters.
17
+ *
18
+ * @protected
19
+ */
20
+ this.indexParamsMap = {};
21
+ }
22
+ /**
23
+ * Builds a {@link ShardQueryMap | `ShardQueryMap`} object.
24
+ *
25
+ * @returns - The {@link ShardQueryMap | `ShardQueryMap`} object.
26
+ */
27
+ build() {
28
+ return mapValues(this.indexParamsMap, (indexConfig, indexToken) => this.getShardQueryFunction(indexToken));
29
+ }
30
+ }
31
+
32
+ export { BaseShardQueryMapBuilder };
@@ -42,35 +42,35 @@ class EntityManager {
42
42
  /**
43
43
  * Update generated properties, hash key, and range key on an {@link ItemMap | `ItemMap`} object.
44
44
  *
45
- * @param item - {@link ItemMap | `ItemMap`} object.
46
45
  * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
46
+ * @param item - {@link ItemMap | `ItemMap`} object.
47
47
  * @param overwrite - Overwrite existing properties (default `false`).
48
48
  *
49
49
  * @returns Shallow clone of `item` with updated properties.
50
50
  *
51
51
  * @throws `Error` if `entityToken` is invalid.
52
52
  */
53
- addKeys(item, entityToken, overwrite = false) {
54
- return addKeys(this, item, entityToken, overwrite);
53
+ addKeys(entityToken, item, overwrite = false) {
54
+ return addKeys(this, entityToken, item, overwrite);
55
55
  }
56
56
  /**
57
57
  * Strips generated properties, hash key, and range key from an {@link ItemMap | `ItemMap`} object.
58
58
  *
59
- * @param item - {@link ItemMap | `ItemMap`} object.
60
59
  * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
60
+ * @param item - {@link ItemMap | `ItemMap`} object.
61
61
  *
62
62
  * @returns Shallow clone of `item` without generated properties, hash key or range key.
63
63
  *
64
64
  * @throws `Error` if `entityToken` is invalid.
65
65
  */
66
- removeKeys(item, entityToken) {
67
- return removeKeys(this, item, entityToken);
66
+ removeKeys(entityToken, item) {
67
+ return removeKeys(this, entityToken, item);
68
68
  }
69
69
  /**
70
70
  * Query a database entity across shards in a provider-generic fashion.
71
71
  *
72
72
  * @remarks
73
- * The provided {@link ShardQueryFunction | `ShardQueryFunction`} performs the actual query of individual data pages on individual shards. This function is presumed to express provider-specific query logic, including any necessary indexing or search constraints.
73
+ * The provided `shardQueryMap` performs the actual query of individual data pages on individual index/shard combinations.
74
74
  *
75
75
  * Individual shard query results will be combined, deduped by {@link ConfigEntity.uniqueProperty} property value, and sorted by {@link QueryOptions.sortOrder | `sortOrder`}.
76
76
  *
@@ -82,10 +82,19 @@ class EntityManager {
82
82
  *
83
83
  * @returns {@link QueryResult} object.
84
84
  *
85
- * @throws Error if {@link QueryOptions.pageKeyMap | `pageKeyMap`} keys do not match {@link QueryOptions.shardQueryMap | `shardQueryMap`} keys.
85
+ * @throws Error if {@link QueryOptions.shardQueryMapBuilder | `shardQueryMapBuilder`} `pageKeyMap` keys do not match its `shardQueryMap` keys.
86
86
  */
87
87
  async query(options) {
88
- return await query(this, options);
88
+ const { shardQueryMapBuilder, ...baseOptions } = options;
89
+ const { entityToken, hashKeyToken, pageKeyMap } = shardQueryMapBuilder;
90
+ const shardQueryMap = shardQueryMapBuilder.build();
91
+ return await query(this, {
92
+ entityToken,
93
+ hashKeyToken,
94
+ pageKeyMap,
95
+ shardQueryMap,
96
+ ...baseOptions,
97
+ });
89
98
  }
90
99
  }
91
100
  _EntityManager_config = new WeakMap();
@@ -8,27 +8,27 @@ import { validateEntityToken } from './validateEntityToken.js';
8
8
  * Update generated properties, hash key, and range key on an {@link ItemMap | `ItemMap`} object.
9
9
  *
10
10
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
11
- * @param item - {@link ItemMap | `ItemMap`} object.
12
11
  * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
12
+ * @param item - {@link ItemMap | `ItemMap`} object.
13
13
  * @param overwrite - Overwrite existing properties (default `false`).
14
14
  *
15
15
  * @returns Shallow clone of `item` with updated properties.
16
16
  *
17
17
  * @throws `Error` if `entityToken` is invalid.
18
18
  */
19
- function addKeys(entityManager, item, entityToken, overwrite = false) {
19
+ function addKeys(entityManager, entityToken, item, overwrite = false) {
20
20
  try {
21
21
  // Validate params.
22
22
  validateEntityToken(entityManager, entityToken);
23
23
  // Update hash key.
24
- let newItem = updateItemHashKey(entityManager, item, entityToken, overwrite);
24
+ let newItem = updateItemHashKey(entityManager, entityToken, item, overwrite);
25
25
  // Update range key.
26
- newItem = updateItemRangeKey(entityManager, newItem, entityToken, overwrite);
26
+ newItem = updateItemRangeKey(entityManager, entityToken, newItem, overwrite);
27
27
  // Update generated properties.
28
28
  for (const property in entityManager.config.entities[entityToken]
29
29
  .generated) {
30
30
  if (overwrite || isNil(item[property])) {
31
- const encoded = encodeGeneratedProperty(entityManager, newItem, entityToken, property);
31
+ const encoded = encodeGeneratedProperty(entityManager, entityToken, property, newItem);
32
32
  if (encoded)
33
33
  Object.assign(newItem, { [property]: encoded });
34
34
  else
@@ -8,15 +8,15 @@ import { validateEntityToken } from './validateEntityToken.js';
8
8
  * If `element` is the {@link Config.hashKey | `hashKey`} or {@link Config.rangeKey | `rangeKey`}, returns the value as-is.
9
9
  *
10
10
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
11
- * @param item - Partial {@link ItemMap | `ItemMap`} object.
12
11
  * @param entityToken - {@link ConfigKeys.entities | `entityManager.config.entities`} key.
13
12
  * @param element - The {@link Entity | `Entity`} generated property element or ungenerated index component to encode.
13
+ * @param value - Encoded entity element.
14
14
  *
15
15
  * @returns Decoded value.
16
16
  *
17
17
  * @throws `Error` if `entityToken` is invalid.
18
18
  */
19
- function decodeEntityElement(entityManager, value, entityToken, element) {
19
+ function decodeEntityElement(entityManager, entityToken, element, value) {
20
20
  try {
21
21
  validateEntityToken(entityManager, entityToken);
22
22
  const { entities, hashKey, rangeKey, transcodes } = entityManager.config;
@@ -26,9 +26,9 @@ function decodeEntityElement(entityManager, value, entityToken, element) {
26
26
  return value;
27
27
  const decoded = transcodes[entities[entityToken].elementTranscodes[element]].decode(value);
28
28
  entityManager.logger.debug('decoded entity element', {
29
- value,
30
29
  entityToken,
31
30
  element,
31
+ value,
32
32
  decoded,
33
33
  });
34
34
  return decoded;
@@ -36,9 +36,9 @@ function decodeEntityElement(entityManager, value, entityToken, element) {
36
36
  catch (error) {
37
37
  if (error instanceof Error)
38
38
  entityManager.logger.error(error.message, {
39
- value,
40
39
  entityToken,
41
40
  element,
41
+ value,
42
42
  });
43
43
  throw error;
44
44
  }
@@ -6,14 +6,14 @@ import { validateEntityToken } from './validateEntityToken.js';
6
6
  * Decode a generated property value. Returns a partial ItemMap.
7
7
  *
8
8
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
9
- * @param encoded - Encoded generated property value.
10
9
  * @param entityToken - `entityManager.config.entities` key.
10
+ * @param encoded - Encoded generated property value.
11
11
  *
12
12
  * @returns Partial {@link ItemMap | `ItemMap`} object with updated properties decoded from `encoded`.
13
13
  *
14
14
  * @throws `Error` if `entityToken` is invalid.
15
15
  */
16
- function decodeGeneratedProperty(entityManager, encoded, entityToken) {
16
+ function decodeGeneratedProperty(entityManager, entityToken, encoded) {
17
17
  try {
18
18
  const { generatedKeyDelimiter, generatedValueDelimiter, hashKey, shardKeyDelimiter, } = entityManager.config;
19
19
  // Validate params.
@@ -35,17 +35,17 @@ function decodeGeneratedProperty(entityManager, encoded, entityToken) {
35
35
  return pair;
36
36
  });
37
37
  // Assign decoded properties.
38
- Object.assign(decoded, objectify(values, ([key]) => key, ([key, value]) => decodeEntityElement(entityManager, value, entityToken, key)));
38
+ Object.assign(decoded, objectify(values, ([key]) => key, ([key, value]) => decodeEntityElement(entityManager, entityToken, key, value)));
39
39
  entityManager.logger.debug('decoded generated property', {
40
- encoded,
41
40
  entityToken,
41
+ encoded,
42
42
  decoded,
43
43
  });
44
44
  return decoded;
45
45
  }
46
46
  catch (error) {
47
47
  if (error instanceof Error)
48
- entityManager.logger.error(error.message, { encoded, entityToken });
48
+ entityManager.logger.error(error.message, { entityToken, encoded });
49
49
  throw error;
50
50
  }
51
51
  }
@@ -16,9 +16,9 @@ import { validateEntityIndexToken } from './validateEntityIndexToken.js';
16
16
  * `item` must be populated with all required index component elements!
17
17
  *
18
18
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
19
- * @param item - Partial {@link ItemMap | `ItemMap`} object.
20
19
  * @param entityToken - {@link ConfigKeys.entities | `entityManager.config.entities`} key.
21
20
  * @param indexToken - {@link ConfigEntity.indexes | `entityManager.config.entities.<entityToken>.indexes`} key.
21
+ * @param item - Partial {@link ItemMap | `ItemMap`} object.
22
22
  * @param omit - Array of index components to omit from the output value.
23
23
  *
24
24
  * @returns Dehydrated index value.
@@ -26,7 +26,7 @@ import { validateEntityIndexToken } from './validateEntityIndexToken.js';
26
26
  * @throws `Error` if `entityToken` is invalid.
27
27
  * @throws `Error` if `indexToken` is invalid.
28
28
  */
29
- function dehydrateIndexItem(entityManager, item, entityToken, indexToken, omit = []) {
29
+ function dehydrateIndexItem(entityManager, entityToken, indexToken, item, omit = []) {
30
30
  try {
31
31
  const { generatedKeyDelimiter } = entityManager.config;
32
32
  // Validate params.
@@ -38,7 +38,7 @@ function dehydrateIndexItem(entityManager, item, entityToken, indexToken, omit =
38
38
  const elements = unwrapIndex(entityManager, entityToken, indexToken).filter((element) => !omit.includes(element));
39
39
  // Join index element values.
40
40
  const dehydrated = elements
41
- .map((element) => encodeEntityElement(entityManager, item, entityToken, element))
41
+ .map((element) => encodeEntityElement(entityManager, entityToken, element, item))
42
42
  .join(generatedKeyDelimiter);
43
43
  entityManager.logger.debug('dehydrated index', {
44
44
  item,
@@ -9,8 +9,8 @@ import { validateEntityToken } from './validateEntityToken.js';
9
9
  * Reverses {@link EntityManager.rehydratePageKeyMap | `rehydratePageKeyMap`}.
10
10
  *
11
11
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
12
- * @param pageKeyMap - {@link PageKeyMap | `PageKeyMap`} object to dehydrate.
13
12
  * @param entityToken - {@link ConfigKeys.entities | `entityManager.config.entities`} key.
13
+ * @param pageKeyMap - {@link PageKeyMap | `PageKeyMap`} object to dehydrate.
14
14
  *
15
15
  * @returns Array of dehydrated page keys.
16
16
  *
@@ -22,7 +22,7 @@ import { validateEntityToken } from './validateEntityToken.js';
22
22
  *
23
23
  * An empty returned array indicates all page keys are `undefined`.
24
24
  */
25
- function dehydratePageKeyMap(entityManager, pageKeyMap, entityToken) {
25
+ function dehydratePageKeyMap(entityManager, entityToken, pageKeyMap) {
26
26
  try {
27
27
  // Validate params.
28
28
  validateEntityToken(entityManager, entityToken);
@@ -30,8 +30,8 @@ function dehydratePageKeyMap(entityManager, pageKeyMap, entityToken) {
30
30
  if (!Object.keys(pageKeyMap).length) {
31
31
  const dehydrated = [];
32
32
  entityManager.logger.debug('dehydrated empty page key map', {
33
- pageKeyMap,
34
33
  entityToken,
34
+ pageKeyMap,
35
35
  dehydrated,
36
36
  });
37
37
  return dehydrated;
@@ -54,13 +54,13 @@ function dehydratePageKeyMap(entityManager, pageKeyMap, entityToken) {
54
54
  const item = Object.entries(pageKeyMap[index][hashKey]).reduce((item, [property, value]) => {
55
55
  if (property in entityManager.config.entities[entityToken].generated ||
56
56
  property === entityManager.config.rangeKey)
57
- Object.assign(item, decodeGeneratedProperty(entityManager, value, entityToken));
57
+ Object.assign(item, decodeGeneratedProperty(entityManager, entityToken, value));
58
58
  else
59
59
  Object.assign(item, { [property]: value });
60
60
  return item;
61
61
  }, {});
62
62
  // Dehydrate index from item.
63
- dehydrated.push(dehydrateIndexItem(entityManager, item, entityToken, index, [
63
+ dehydrated.push(dehydrateIndexItem(entityManager, entityToken, index, item, [
64
64
  entityManager.config.hashKey,
65
65
  ]));
66
66
  }
@@ -69,8 +69,8 @@ function dehydratePageKeyMap(entityManager, pageKeyMap, entityToken) {
69
69
  if (dehydrated.every((pageKey) => pageKey === ''))
70
70
  dehydrated = [];
71
71
  entityManager.logger.debug('dehydrated page key map', {
72
- pageKeyMap,
73
72
  entityToken,
73
+ pageKeyMap,
74
74
  indexes,
75
75
  hashKeys,
76
76
  dehydrated,
@@ -8,15 +8,15 @@ import { validateEntityToken } from './validateEntityToken.js';
8
8
  * If `element` is the {@link Config.hashKey | `hashKey`} or {@link Config.rangeKey | `rangeKey`}, returns the value as-is.
9
9
  *
10
10
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
11
- * @param item - Partial {@link ItemMap | `ItemMap`} object.
12
11
  * @param entityToken - {@link ConfigKeys.entities | `entityManager.config.entities`} key.
13
12
  * @param element - The {@link Entity | `Entity`} generated property element or ungenerated index component to encode.
13
+ * @param item - Partial {@link ItemMap | `ItemMap`} object.
14
14
  *
15
15
  * @returns Encoded value.
16
16
  *
17
17
  * @throws `Error` if `entityToken` is invalid.
18
18
  */
19
- function encodeEntityElement(entityManager, item, entityToken, element) {
19
+ function encodeEntityElement(entityManager, entityToken, element, item) {
20
20
  try {
21
21
  validateEntityToken(entityManager, entityToken);
22
22
  const { entities, hashKey, rangeKey, transcodes } = entityManager.config;
@@ -25,16 +25,16 @@ function encodeEntityElement(entityManager, item, entityToken, element) {
25
25
  return value;
26
26
  const encoded = transcodes[entities[entityToken].elementTranscodes[element]].encode(item[element]) || undefined;
27
27
  entityManager.logger.debug('encoded entity element', {
28
- item,
29
28
  entityToken,
30
29
  element,
30
+ item,
31
31
  encoded,
32
32
  });
33
33
  return encoded;
34
34
  }
35
35
  catch (error) {
36
36
  if (error instanceof Error)
37
- entityManager.logger.error(error.message, { item, entityToken, element });
37
+ entityManager.logger.error(error.message, { entityToken, element, item });
38
38
  throw error;
39
39
  }
40
40
  }
@@ -5,16 +5,16 @@ import { validateEntityGeneratedProperty } from './validateEntityGeneratedProper
5
5
  * Encode a generated property value. Returns a string or undefined if atomicity requirement not met.
6
6
  *
7
7
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
8
- * @param item - Partial {@link ItemMap | `ItemMap`} object.
9
8
  * @param entityToken - `entityManager.config.entities` key.
10
9
  * @param property - {@link ConfigEntityGenerated | `entityManager.config.entities.<entityToken>.generated`} key.
10
+ * @param item - Partial {@link ItemMap | `ItemMap`} object.
11
11
  *
12
12
  * @returns Encoded generated property value.
13
13
  *
14
14
  * @throws `Error` if `entityToken` is invalid.
15
15
  * @throws `Error` if `property` is invalid.
16
16
  */
17
- function encodeGeneratedProperty(entityManager, item, entityToken, property) {
17
+ function encodeGeneratedProperty(entityManager, entityToken, property, item) {
18
18
  try {
19
19
  // Validate params.
20
20
  validateEntityGeneratedProperty(entityManager, entityToken, property);
@@ -33,9 +33,9 @@ function encodeGeneratedProperty(entityManager, item, entityToken, property) {
33
33
  ...elementMap.map(([element, value]) => [element, (value ?? '').toString()].join(entityManager.config.generatedValueDelimiter)),
34
34
  ].join(entityManager.config.generatedKeyDelimiter);
35
35
  entityManager.logger.debug('encoded generated property', {
36
- item,
37
36
  entityToken,
38
37
  property,
38
+ item,
39
39
  encoded,
40
40
  });
41
41
  return encoded;
@@ -43,9 +43,9 @@ function encodeGeneratedProperty(entityManager, item, entityToken, property) {
43
43
  catch (error) {
44
44
  if (error instanceof Error)
45
45
  entityManager.logger.error(error.message, {
46
- item,
47
46
  entityToken,
48
47
  property,
48
+ item,
49
49
  });
50
50
  throw error;
51
51
  }
package/dist/mjs/index.js CHANGED
@@ -1,2 +1,3 @@
1
+ export { BaseShardQueryMapBuilder } from './BaseShardQueryMapBuilder.js';
1
2
  export { conditionalize } from './conditionalize.js';
2
3
  export { EntityManager } from './EntityManager.js';