@karmaniverous/entity-manager 6.11.4 → 6.13.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,8 @@
3
3
  var tslib = require('tslib');
4
4
  var radash = require('radash');
5
5
  var addKeys = require('./addKeys.js');
6
+ var encodeGeneratedProperty = require('./encodeGeneratedProperty.js');
7
+ var findIndexToken = require('./findIndexToken.js');
6
8
  var ParsedConfig = require('./ParsedConfig.js');
7
9
  var query = require('./query.js');
8
10
  var removeKeys = require('./removeKeys.js');
@@ -47,6 +49,19 @@ class EntityManager {
47
49
  set config(value) {
48
50
  tslib.__classPrivateFieldSet(this, _EntityManager_config, ParsedConfig.configSchema.parse(value), "f");
49
51
  }
52
+ /**
53
+ * Encode a generated property value. Returns a string or undefined if atomicity requirement of sharded properties not met.
54
+ *
55
+ * @param property - {@link Config.generatedProperties | Generated property} key.
56
+ * @param item - Partial {@link ItemMap | `ItemMap`} object.
57
+ *
58
+ * @returns Encoded generated property value.
59
+ *
60
+ * @throws `Error` if `property` is not a {@link Config.generatedProperties | generated property}.
61
+ */
62
+ encodeGeneratedProperty(property, item) {
63
+ return encodeGeneratedProperty.encodeGeneratedProperty(this, property, item);
64
+ }
50
65
  /**
51
66
  * Update generated properties, hash key, and range key on an {@link EntityItem | `EntityItem`} object.
52
67
  *
@@ -91,6 +106,17 @@ class EntityManager {
91
106
  removeKeys(entityToken, item) {
92
107
  return removeKeys.removeKeys(this, entityToken, item);
93
108
  }
109
+ /**
110
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
111
+ *
112
+ * @param hashKeyToken - Index hash key.
113
+ * @param rangeKeyToken - Index range key.
114
+ *
115
+ * @returns Index token if found.
116
+ */
117
+ findIndexToken(hashKeyToken, rangeKeyToken) {
118
+ return findIndexToken.findIndexToken(this, hashKeyToken, rangeKeyToken);
119
+ }
94
120
  /**
95
121
  * Query a database entity across shards in a provider-generic fashion.
96
122
  *
@@ -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)) {
@@ -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;
package/dist/index.d.ts CHANGED
@@ -541,6 +541,17 @@ declare class EntityManager<C extends BaseConfigMap> {
541
541
  * @param value - {@link Config | `Config`} object.
542
542
  */
543
543
  set config(value: ParsedConfig);
544
+ /**
545
+ * Encode a generated property value. Returns a string or undefined if atomicity requirement of sharded properties not met.
546
+ *
547
+ * @param property - {@link Config.generatedProperties | Generated property} key.
548
+ * @param item - Partial {@link ItemMap | `ItemMap`} object.
549
+ *
550
+ * @returns Encoded generated property value.
551
+ *
552
+ * @throws `Error` if `property` is not a {@link Config.generatedProperties | generated property}.
553
+ */
554
+ encodeGeneratedProperty<C extends BaseConfigMap>(property: C['ShardedKeys'] | C['UnshardedKeys'], item: EntityItem<C>): string | undefined;
544
555
  /**
545
556
  * Update generated properties, hash key, and range key on an {@link EntityItem | `EntityItem`} object.
546
557
  *
@@ -576,6 +587,15 @@ declare class EntityManager<C extends BaseConfigMap> {
576
587
  * @throws `Error` if `entityToken` is invalid.
577
588
  */
578
589
  removeKeys(entityToken: EntityToken<C>, item: EntityRecord<C>): EntityItem<C>;
590
+ /**
591
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
592
+ *
593
+ * @param hashKeyToken - Index hash key.
594
+ * @param rangeKeyToken - Index range key.
595
+ *
596
+ * @returns Index token if found.
597
+ */
598
+ findIndexToken(hashKeyToken: string, rangeKeyToken: string): string | undefined;
579
599
  /**
580
600
  * Query a database entity across shards in a provider-generic fashion.
581
601
  *
@@ -1,6 +1,8 @@
1
1
  import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
2
2
  import { pick } from 'radash';
3
3
  import { addKeys } from './addKeys.js';
4
+ import { encodeGeneratedProperty } from './encodeGeneratedProperty.js';
5
+ import { findIndexToken } from './findIndexToken.js';
4
6
  import { configSchema } from './ParsedConfig.js';
5
7
  import { query } from './query.js';
6
8
  import { removeKeys } from './removeKeys.js';
@@ -45,6 +47,19 @@ class EntityManager {
45
47
  set config(value) {
46
48
  __classPrivateFieldSet(this, _EntityManager_config, configSchema.parse(value), "f");
47
49
  }
50
+ /**
51
+ * Encode a generated property value. Returns a string or undefined if atomicity requirement of sharded properties not met.
52
+ *
53
+ * @param property - {@link Config.generatedProperties | Generated property} key.
54
+ * @param item - Partial {@link ItemMap | `ItemMap`} object.
55
+ *
56
+ * @returns Encoded generated property value.
57
+ *
58
+ * @throws `Error` if `property` is not a {@link Config.generatedProperties | generated property}.
59
+ */
60
+ encodeGeneratedProperty(property, item) {
61
+ return encodeGeneratedProperty(this, property, item);
62
+ }
48
63
  /**
49
64
  * Update generated properties, hash key, and range key on an {@link EntityItem | `EntityItem`} object.
50
65
  *
@@ -89,6 +104,17 @@ class EntityManager {
89
104
  removeKeys(entityToken, item) {
90
105
  return removeKeys(this, entityToken, item);
91
106
  }
107
+ /**
108
+ * Find an index token in a {@link Config | `Config`} object based on the index `hashKey` and `rangeKey`.
109
+ *
110
+ * @param hashKeyToken - Index hash key.
111
+ * @param rangeKeyToken - Index range key.
112
+ *
113
+ * @returns Index token if found.
114
+ */
115
+ findIndexToken(hashKeyToken, rangeKeyToken) {
116
+ return findIndexToken(this, hashKeyToken, rangeKeyToken);
117
+ }
92
118
  /**
93
119
  * Query a database entity across shards in a provider-generic fashion.
94
120
  *
@@ -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)) {
@@ -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 };
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.4"
135
+ "version": "6.13.0"
136
136
  }