@karmaniverous/entity-manager 6.11.2 → 6.12.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.
@@ -3,6 +3,7 @@
3
3
  var tslib = require('tslib');
4
4
  var radash = require('radash');
5
5
  var addKeys = require('./addKeys.js');
6
+ var findIndexToken = require('./findIndexToken.js');
6
7
  var ParsedConfig = require('./ParsedConfig.js');
7
8
  var query = require('./query.js');
8
9
  var removeKeys = require('./removeKeys.js');
@@ -54,7 +55,7 @@ class EntityManager {
54
55
  * @param item - {@link EntityItem | `EntityItem`} object.
55
56
  * @param overwrite - Overwrite existing properties (default `false`).
56
57
  *
57
- * @returns Shallow clone of `item` with updated properties.
58
+ * @returns {@link EntityRecord | `EntityRecord`} object with updated properties.
58
59
  *
59
60
  * @throws `Error` if `entityToken` is invalid.
60
61
  */
@@ -79,18 +80,29 @@ class EntityManager {
79
80
  : addKeys.addKeys(this, entityToken, item, overwrite), [this.config.hashKey, this.config.rangeKey]);
80
81
  }
81
82
  /**
82
- * Strips generated properties, hash key, and range key from an {@link EntityItem | `EntityItem`} object.
83
+ * Strips generated properties, hash key, and range key from an {@link EntityRecord | `EntityRecord`} object.
83
84
  *
84
85
  * @param entityToken - {@link Config | `Config`} `entities` key.
85
- * @param item - {@link EntityItem | `EntityItem`} object.
86
+ * @param item - {@link EntityRecord | `EntityRecord`} object.
86
87
  *
87
- * @returns Shallow clone of `item` without generated properties, hash key or range key.
88
+ * @returns {@link EntityItem | `EntityItem`} with generated properties, hash key & range key removed.
88
89
  *
89
90
  * @throws `Error` if `entityToken` is invalid.
90
91
  */
91
92
  removeKeys(entityToken, item) {
92
93
  return removeKeys.removeKeys(this, entityToken, item);
93
94
  }
95
+ /**
96
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
97
+ *
98
+ * @param hashKeyToken - Index hash key.
99
+ * @param rangeKeyToken - Index range key.
100
+ *
101
+ * @returns Index token if found.
102
+ */
103
+ findIndexToken(hashKeyToken, rangeKeyToken) {
104
+ return findIndexToken.findIndexToken(this, hashKeyToken, rangeKeyToken);
105
+ }
94
106
  /**
95
107
  * Query a database entity across shards in a provider-generic fashion.
96
108
  *
@@ -236,6 +236,7 @@ const configSchema = zod.z
236
236
  path: ['generatedProperties', 'unsharded', property],
237
237
  });
238
238
  // Validate indexes.
239
+ // TODO: Vaidate no two indexes have the same hashKey & rangeKey.
239
240
  for (const [indexKey, { hashKey, rangeKey, projections }] of Object.entries(data.indexes)) {
240
241
  // Validate hash key is sharded.
241
242
  if (![data.hashKey, ...shardedKeys].includes(hashKey)) {
@@ -14,7 +14,7 @@ var validateEntityToken = require('./validateEntityToken.js');
14
14
  * @param item - {@link EntityItem | `EntityItem`} object.
15
15
  * @param overwrite - Overwrite existing properties (default `false`).
16
16
  *
17
- * @returns Shallow clone of `item` with updated properties.
17
+ * @returns {@link EntityRecord | `EntityRecord`} object with updated properties.
18
18
  *
19
19
  * @throws `Error` if `entityToken` is invalid.
20
20
  */
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
5
+ *
6
+ * @param entityManager - {@link EntityManager | `EntityManager`} instance.
7
+ * @param hashKeyToken - Index hash key.
8
+ * @param rangeKeyToken - Index range key.
9
+ *
10
+ * @returns Index token if found.
11
+ */
12
+ function findIndexToken(entityManager, hashKeyToken, rangeKeyToken) {
13
+ return Object.entries(entityManager.config.indexes).find(([, index]) => index.hashKey === hashKeyToken && index.rangeKey === rangeKeyToken)?.[0];
14
+ }
15
+
16
+ exports.findIndexToken = findIndexToken;
@@ -3,13 +3,13 @@
3
3
  var validateEntityToken = require('./validateEntityToken.js');
4
4
 
5
5
  /**
6
- * Strips generated properties, hash key, and range key from an {@link ItemMap | `ItemMap`} object.
6
+ * Strips generated properties, hash key, and range key from an {@link EntityRecord | `EntityRecord`} object.
7
7
  *
8
8
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
9
9
  * @param entityToken - {@link Config.entities | `entityManager.config.entities`} key.
10
- * @param item - {@link ItemMap | `ItemMap`} object.
10
+ * @param item - {@link EntityRecord | `EntityRecord`} object.
11
11
  *
12
- * @returns Shallow clone of `item` without generated properties, hash key or range key.
12
+ * @returns {@link EntityItem | `EntityItem`} with generated properties, hash key & range key removed.
13
13
  *
14
14
  * @throws `Error` if `entityToken` is invalid.
15
15
  */
package/dist/index.d.ts CHANGED
@@ -96,6 +96,16 @@ type EntityItem<C extends BaseConfigMap> = Partial<FlattenEntityMap<C['EntityMap
96
96
  */
97
97
  type EntityKey<C extends BaseConfigMap> = Record<C['HashKey'] | C['RangeKey'], string>;
98
98
 
99
+ /**
100
+ * Database-facing record type from a {@link BaseConfigMap | `ConfigMap`} with required hash & range keys.
101
+ *
102
+ * @typeParam C - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeMap | `TranscodeMap`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
103
+ *
104
+ * @category EntityManager
105
+ * @protected
106
+ */
107
+ type EntityRecord<C extends BaseConfigMap> = EntityItem<C> & EntityKey<C>;
108
+
99
109
  /**
100
110
  * Extracts entity tokens from a {@link ConfigMap | `ConfigMap`}.
101
111
  *
@@ -538,11 +548,11 @@ declare class EntityManager<C extends BaseConfigMap> {
538
548
  * @param item - {@link EntityItem | `EntityItem`} object.
539
549
  * @param overwrite - Overwrite existing properties (default `false`).
540
550
  *
541
- * @returns Shallow clone of `item` with updated properties.
551
+ * @returns {@link EntityRecord | `EntityRecord`} object with updated properties.
542
552
  *
543
553
  * @throws `Error` if `entityToken` is invalid.
544
554
  */
545
- addKeys(entityToken: EntityToken<C>, item: EntityItem<C>, overwrite?: boolean): EntityItem<C>;
555
+ addKeys(entityToken: EntityToken<C>, item: EntityItem<C>, overwrite?: boolean): EntityRecord<C>;
546
556
  /**
547
557
  * Convert an {@link EntityItem | `EntityItem`} into an {@link EntityKey | `EntityKey`}.
548
558
  *
@@ -556,16 +566,25 @@ declare class EntityManager<C extends BaseConfigMap> {
556
566
  */
557
567
  getPrimaryKey(entityToken: EntityToken<C>, item: EntityItem<C>, overwrite?: boolean): EntityKey<C>;
558
568
  /**
559
- * Strips generated properties, hash key, and range key from an {@link EntityItem | `EntityItem`} object.
569
+ * Strips generated properties, hash key, and range key from an {@link EntityRecord | `EntityRecord`} object.
560
570
  *
561
571
  * @param entityToken - {@link Config | `Config`} `entities` key.
562
- * @param item - {@link EntityItem | `EntityItem`} object.
572
+ * @param item - {@link EntityRecord | `EntityRecord`} object.
563
573
  *
564
- * @returns Shallow clone of `item` without generated properties, hash key or range key.
574
+ * @returns {@link EntityItem | `EntityItem`} with generated properties, hash key & range key removed.
565
575
  *
566
576
  * @throws `Error` if `entityToken` is invalid.
567
577
  */
568
- removeKeys(entityToken: EntityToken<C>, item: EntityItem<C>): EntityItem<C>;
578
+ removeKeys(entityToken: EntityToken<C>, item: EntityRecord<C>): EntityItem<C>;
579
+ /**
580
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
581
+ *
582
+ * @param hashKeyToken - Index hash key.
583
+ * @param rangeKeyToken - Index range key.
584
+ *
585
+ * @returns Index token if found.
586
+ */
587
+ findIndexToken(hashKeyToken: C['HashKey'] | C['ShardedKeys'], rangeKeyToken: C['RangeKey'] | C['UnshardedKeys'] | C['TranscodedProperties']): string | undefined;
569
588
  /**
570
589
  * Query a database entity across shards in a provider-generic fashion.
571
590
  *
@@ -736,14 +755,4 @@ type ConfigMap<M extends Partial<BaseConfigMap> = Partial<BaseConfigMap>> = Vali
736
755
  TranscodeMap: 'TranscodeMap' extends keyof M ? NonNullable<M['TranscodeMap']> : DefaultTranscodeMap;
737
756
  }>;
738
757
 
739
- /**
740
- * Database-facing record type from a {@link BaseConfigMap | `ConfigMap`} with required hash & range keys.
741
- *
742
- * @typeParam C - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeMap | `TranscodeMap`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
743
- *
744
- * @category EntityManager
745
- * @protected
746
- */
747
- type EntityRecord<C extends BaseConfigMap> = EntityItem<C> & EntityKey<C>;
748
-
749
758
  export { type BaseConfigMap, BaseEntityClient, type BaseEntityClientOptions, BaseQueryBuilder, type BaseQueryBuilderOptions, type Config, type ConfigMap, type EntityItem, type EntityKey, EntityManager, type EntityRecord, type EntityToken, type PageKey, type ParsedConfig, type QueryBuilderQueryOptions, type QueryOptions, type QueryResult, type ShardBump, type ShardQueryFunction, type ShardQueryMap, type ShardQueryResult, type ValidateConfigMap };
@@ -1,6 +1,7 @@
1
1
  import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
2
2
  import { pick } from 'radash';
3
3
  import { addKeys } from './addKeys.js';
4
+ import { findIndexToken } from './findIndexToken.js';
4
5
  import { configSchema } from './ParsedConfig.js';
5
6
  import { query } from './query.js';
6
7
  import { removeKeys } from './removeKeys.js';
@@ -52,7 +53,7 @@ class EntityManager {
52
53
  * @param item - {@link EntityItem | `EntityItem`} object.
53
54
  * @param overwrite - Overwrite existing properties (default `false`).
54
55
  *
55
- * @returns Shallow clone of `item` with updated properties.
56
+ * @returns {@link EntityRecord | `EntityRecord`} object with updated properties.
56
57
  *
57
58
  * @throws `Error` if `entityToken` is invalid.
58
59
  */
@@ -77,18 +78,29 @@ class EntityManager {
77
78
  : addKeys(this, entityToken, item, overwrite), [this.config.hashKey, this.config.rangeKey]);
78
79
  }
79
80
  /**
80
- * Strips generated properties, hash key, and range key from an {@link EntityItem | `EntityItem`} object.
81
+ * Strips generated properties, hash key, and range key from an {@link EntityRecord | `EntityRecord`} object.
81
82
  *
82
83
  * @param entityToken - {@link Config | `Config`} `entities` key.
83
- * @param item - {@link EntityItem | `EntityItem`} object.
84
+ * @param item - {@link EntityRecord | `EntityRecord`} object.
84
85
  *
85
- * @returns Shallow clone of `item` without generated properties, hash key or range key.
86
+ * @returns {@link EntityItem | `EntityItem`} with generated properties, hash key & range key removed.
86
87
  *
87
88
  * @throws `Error` if `entityToken` is invalid.
88
89
  */
89
90
  removeKeys(entityToken, item) {
90
91
  return removeKeys(this, entityToken, item);
91
92
  }
93
+ /**
94
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
95
+ *
96
+ * @param hashKeyToken - Index hash key.
97
+ * @param rangeKeyToken - Index range key.
98
+ *
99
+ * @returns Index token if found.
100
+ */
101
+ findIndexToken(hashKeyToken, rangeKeyToken) {
102
+ return findIndexToken(this, hashKeyToken, rangeKeyToken);
103
+ }
92
104
  /**
93
105
  * Query a database entity across shards in a provider-generic fashion.
94
106
  *
@@ -234,6 +234,7 @@ const configSchema = z
234
234
  path: ['generatedProperties', 'unsharded', property],
235
235
  });
236
236
  // Validate indexes.
237
+ // TODO: Vaidate no two indexes have the same hashKey & rangeKey.
237
238
  for (const [indexKey, { hashKey, rangeKey, projections }] of Object.entries(data.indexes)) {
238
239
  // Validate hash key is sharded.
239
240
  if (![data.hashKey, ...shardedKeys].includes(hashKey)) {
@@ -12,7 +12,7 @@ import { validateEntityToken } from './validateEntityToken.js';
12
12
  * @param item - {@link EntityItem | `EntityItem`} object.
13
13
  * @param overwrite - Overwrite existing properties (default `false`).
14
14
  *
15
- * @returns Shallow clone of `item` with updated properties.
15
+ * @returns {@link EntityRecord | `EntityRecord`} object with updated properties.
16
16
  *
17
17
  * @throws `Error` if `entityToken` is invalid.
18
18
  */
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
3
+ *
4
+ * @param entityManager - {@link EntityManager | `EntityManager`} instance.
5
+ * @param hashKeyToken - Index hash key.
6
+ * @param rangeKeyToken - Index range key.
7
+ *
8
+ * @returns Index token if found.
9
+ */
10
+ function findIndexToken(entityManager, hashKeyToken, rangeKeyToken) {
11
+ return Object.entries(entityManager.config.indexes).find(([, index]) => index.hashKey === hashKeyToken && index.rangeKey === rangeKeyToken)?.[0];
12
+ }
13
+
14
+ export { findIndexToken };
@@ -1,13 +1,13 @@
1
1
  import { validateEntityToken } from './validateEntityToken.js';
2
2
 
3
3
  /**
4
- * Strips generated properties, hash key, and range key from an {@link ItemMap | `ItemMap`} object.
4
+ * Strips generated properties, hash key, and range key from an {@link EntityRecord | `EntityRecord`} object.
5
5
  *
6
6
  * @param entityManager - {@link EntityManager | `EntityManager`} instance.
7
7
  * @param entityToken - {@link Config.entities | `entityManager.config.entities`} key.
8
- * @param item - {@link ItemMap | `ItemMap`} object.
8
+ * @param item - {@link EntityRecord | `EntityRecord`} object.
9
9
  *
10
- * @returns Shallow clone of `item` without generated properties, hash key or range key.
10
+ * @returns {@link EntityItem | `EntityItem`} with generated properties, hash key & range key removed.
11
11
  *
12
12
  * @throws `Error` if `entityToken` is invalid.
13
13
  */
package/package.json CHANGED
@@ -132,5 +132,5 @@
132
132
  },
133
133
  "type": "module",
134
134
  "types": "dist/index.d.ts",
135
- "version": "6.11.2"
135
+ "version": "6.12.0"
136
136
  }