@karmaniverous/entity-manager 6.10.2 → 6.11.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.
@@ -13,10 +13,10 @@ class BaseEntityClient {
13
13
  *
14
14
  * @param options - {@link BaseEntityClientOptions | `BaseEntityClientOptions`} object.
15
15
  */
16
- constructor(entityManager, options = {}) {
17
- this.entityManager = entityManager;
18
- const { batchProcessOptions = {}, logger = console } = options;
16
+ constructor(options) {
17
+ const { batchProcessOptions = {}, entityManager, logger = console, } = options;
19
18
  this.batchProcessOptions = batchProcessOptions;
19
+ this.entityManager = entityManager;
20
20
  this.logger = logger;
21
21
  }
22
22
  }
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var tslib = require('tslib');
4
+ var radash = require('radash');
4
5
  var addKeys = require('./addKeys.js');
5
6
  var ParsedConfig = require('./ParsedConfig.js');
6
7
  var query = require('./query.js');
@@ -60,6 +61,23 @@ class EntityManager {
60
61
  addKeys(entityToken, item, overwrite = false) {
61
62
  return addKeys.addKeys(this, entityToken, item, overwrite);
62
63
  }
64
+ /**
65
+ * Convert an {@link EntityItem | `EntityItem`} into an {@link EntityKey | `EntityKey`}.
66
+ *
67
+ * @param entityToken - {@link Config | `Config`} `entities` key.
68
+ * @param item - {@link EntityItem | `EntityItem`} object.
69
+ * @param overwrite - Overwrite existing properties (default `false`).
70
+ *
71
+ * @returns {@link EntityKey | `EntityKey`} extracted from shallow clone of `item` with updated properties.
72
+ *
73
+ * @throws `Error` if `entityToken` is invalid.
74
+ */
75
+ getPrimaryKey(entityToken, item, overwrite = false) {
76
+ const { hashKey, rangeKey } = this.config;
77
+ return radash.pick(!overwrite && item[hashKey] && item[rangeKey]
78
+ ? item
79
+ : addKeys.addKeys(this, entityToken, item, overwrite), [this.config.hashKey, this.config.rangeKey]);
80
+ }
63
81
  /**
64
82
  * Strips generated properties, hash key, and range key from an {@link EntityItem | `EntityItem`} object.
65
83
  *
package/dist/index.d.ts CHANGED
@@ -17,20 +17,6 @@ interface BaseConfigMap {
17
17
  TranscodeMap: TranscodeMap;
18
18
  }
19
19
 
20
- /**
21
- * Base EntityClient options.
22
- *
23
- * @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`}.
24
- *
25
- * @category EntityClient
26
- */
27
- interface BaseEntityClientOptions {
28
- /** Default batch process options. */
29
- batchProcessOptions?: Omit<BatchProcessOptions<unknown, unknown>, 'batchHandler' | 'unprocessedItemExtractor'>;
30
- /** Injected logger object. Must support `debug` and `error` methods. Default: `console` */
31
- logger?: Pick<Console, 'debug' | 'error'>;
32
- }
33
-
34
20
  /**
35
21
  * Defines a single time period in an entity sharding strategy.
36
22
  *
@@ -100,6 +86,16 @@ type Config<C extends BaseConfigMap = BaseConfigMap> = ConditionalProperty<'enti
100
86
  */
101
87
  type EntityItem<C extends BaseConfigMap> = Partial<FlattenEntityMap<C['EntityMap']> & Record<C['HashKey'] | C['RangeKey'] | C['ShardedKeys'] | C['UnshardedKeys'], string>> & Record<string, unknown>;
102
88
 
89
+ /**
90
+ * Database-facing record key type from a {@link BaseConfigMap | `ConfigMap`} with required hash & range keys.
91
+ *
92
+ * @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`}.
93
+ *
94
+ * @category EntityClient
95
+ * @protected
96
+ */
97
+ type EntityKey<C extends BaseConfigMap> = Record<C['HashKey'] | C['RangeKey'], string>;
98
+
103
99
  /**
104
100
  * Extracts entity tokens from a {@link ConfigMap | `ConfigMap`}.
105
101
  *
@@ -217,7 +213,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
217
213
  }, "strict", z.ZodTypeAny, {
218
214
  hashKey: string;
219
215
  rangeKey: string;
220
- throttle: number;
221
216
  entities: Record<string, {
222
217
  defaultLimit: number;
223
218
  defaultPageSize: number;
@@ -242,6 +237,7 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
242
237
  generatedValueDelimiter: string;
243
238
  propertyTranscodes: Record<string, string>;
244
239
  shardKeyDelimiter: string;
240
+ throttle: number;
245
241
  transcodes: Record<string, {
246
242
  encode: (args_0: any, ...args: unknown[]) => string;
247
243
  decode: (args_0: string, ...args: unknown[]) => any;
@@ -249,7 +245,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
249
245
  }, {
250
246
  hashKey: string;
251
247
  rangeKey: string;
252
- throttle?: number | undefined;
253
248
  entities?: Record<string, {
254
249
  timestampProperty: string;
255
250
  uniqueProperty: string;
@@ -274,6 +269,7 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
274
269
  generatedValueDelimiter?: string | undefined;
275
270
  propertyTranscodes?: Record<string, string> | undefined;
276
271
  shardKeyDelimiter?: string | undefined;
272
+ throttle?: number | undefined;
277
273
  transcodes?: Record<string, {
278
274
  encode: (args_0: any, ...args: unknown[]) => string;
279
275
  decode: (args_0: string, ...args: unknown[]) => any;
@@ -281,7 +277,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
281
277
  }>, {
282
278
  hashKey: string;
283
279
  rangeKey: string;
284
- throttle: number;
285
280
  entities: Record<string, {
286
281
  defaultLimit: number;
287
282
  defaultPageSize: number;
@@ -306,6 +301,7 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
306
301
  generatedValueDelimiter: string;
307
302
  propertyTranscodes: Record<string, string>;
308
303
  shardKeyDelimiter: string;
304
+ throttle: number;
309
305
  transcodes: Record<string, {
310
306
  encode: (args_0: any, ...args: unknown[]) => string;
311
307
  decode: (args_0: string, ...args: unknown[]) => any;
@@ -313,7 +309,6 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
313
309
  }, {
314
310
  hashKey: string;
315
311
  rangeKey: string;
316
- throttle?: number | undefined;
317
312
  entities?: Record<string, {
318
313
  timestampProperty: string;
319
314
  uniqueProperty: string;
@@ -338,6 +333,7 @@ declare const configSchema: z.ZodEffects<z.ZodObject<{
338
333
  generatedValueDelimiter?: string | undefined;
339
334
  propertyTranscodes?: Record<string, string> | undefined;
340
335
  shardKeyDelimiter?: string | undefined;
336
+ throttle?: number | undefined;
341
337
  transcodes?: Record<string, {
342
338
  encode: (args_0: any, ...args: unknown[]) => string;
343
339
  decode: (args_0: string, ...args: unknown[]) => any;
@@ -547,6 +543,18 @@ declare class EntityManager<C extends BaseConfigMap> {
547
543
  * @throws `Error` if `entityToken` is invalid.
548
544
  */
549
545
  addKeys(entityToken: EntityToken<C>, item: EntityItem<C>, overwrite?: boolean): EntityItem<C>;
546
+ /**
547
+ * Convert an {@link EntityItem | `EntityItem`} into an {@link EntityKey | `EntityKey`}.
548
+ *
549
+ * @param entityToken - {@link Config | `Config`} `entities` key.
550
+ * @param item - {@link EntityItem | `EntityItem`} object.
551
+ * @param overwrite - Overwrite existing properties (default `false`).
552
+ *
553
+ * @returns {@link EntityKey | `EntityKey`} extracted from shallow clone of `item` with updated properties.
554
+ *
555
+ * @throws `Error` if `entityToken` is invalid.
556
+ */
557
+ getPrimaryKey(entityToken: EntityToken<C>, item: EntityItem<C>, overwrite?: boolean): EntityKey<C>;
550
558
  /**
551
559
  * Strips generated properties, hash key, and range key from an {@link EntityItem | `EntityItem`} object.
552
560
  *
@@ -583,6 +591,22 @@ declare class EntityManager<C extends BaseConfigMap> {
583
591
  query(options: QueryOptions<C>): Promise<QueryResult<C>>;
584
592
  }
585
593
 
594
+ /**
595
+ * Base EntityClient options.
596
+ *
597
+ * @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`}.
598
+ *
599
+ * @category EntityClient
600
+ */
601
+ interface BaseEntityClientOptions<C extends BaseConfigMap> {
602
+ /** Default batch process options. */
603
+ batchProcessOptions?: Omit<BatchProcessOptions<unknown, unknown>, 'batchHandler' | 'unprocessedItemExtractor'>;
604
+ /** {@link EntityManager | `EntityManager`} instance. */
605
+ entityManager: EntityManager<C>;
606
+ /** Injected logger object. Must support `debug` and `error` methods. Default: `console` */
607
+ logger?: Pick<Console, 'debug' | 'error'>;
608
+ }
609
+
586
610
  /**
587
611
  * Base EntityClient class. Integrates {@link EntityManager | `EntityManager`} with injected logging & enhanced batch processing.
588
612
  *
@@ -591,15 +615,18 @@ declare class EntityManager<C extends BaseConfigMap> {
591
615
  * @category EntityClient
592
616
  */
593
617
  declare abstract class BaseEntityClient<C extends BaseConfigMap> {
618
+ /** Default batch process options. */
619
+ readonly batchProcessOptions: NonNullable<BaseEntityClientOptions<C>['batchProcessOptions']>;
620
+ /** {@link EntityManager | `EntityManager`} instance. */
594
621
  readonly entityManager: EntityManager<C>;
595
- readonly batchProcessOptions: NonNullable<BaseEntityClientOptions['batchProcessOptions']>;
596
- readonly logger: NonNullable<BaseEntityClientOptions['logger']>;
622
+ /** Injected logger object. Must support `debug` and `error` methods. Default: `console` */
623
+ readonly logger: NonNullable<BaseEntityClientOptions<C>['logger']>;
597
624
  /**
598
625
  * DynamoDB EntityClient constructor.
599
626
  *
600
627
  * @param options - {@link BaseEntityClientOptions | `BaseEntityClientOptions`} object.
601
628
  */
602
- constructor(entityManager: EntityManager<C>, options?: BaseEntityClientOptions);
629
+ constructor(options: BaseEntityClientOptions<C>);
603
630
  }
604
631
 
605
632
  /**
@@ -709,16 +736,6 @@ type ConfigMap<M extends Partial<BaseConfigMap> = Partial<BaseConfigMap>> = Vali
709
736
  TranscodeMap: 'TranscodeMap' extends keyof M ? NonNullable<M['TranscodeMap']> : DefaultTranscodeMap;
710
737
  }>;
711
738
 
712
- /**
713
- * Database-facing record key type from a {@link BaseConfigMap | `ConfigMap`} with required hash & range keys.
714
- *
715
- * @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`}.
716
- *
717
- * @category EntityClient
718
- * @protected
719
- */
720
- type EntityKey<C extends BaseConfigMap> = Record<C['HashKey'] | C['RangeKey'], string>;
721
-
722
739
  /**
723
740
  * Database-facing record type from a {@link BaseConfigMap | `ConfigMap`} with required hash & range keys.
724
741
  *
@@ -11,10 +11,10 @@ class BaseEntityClient {
11
11
  *
12
12
  * @param options - {@link BaseEntityClientOptions | `BaseEntityClientOptions`} object.
13
13
  */
14
- constructor(entityManager, options = {}) {
15
- this.entityManager = entityManager;
16
- const { batchProcessOptions = {}, logger = console } = options;
14
+ constructor(options) {
15
+ const { batchProcessOptions = {}, entityManager, logger = console, } = options;
17
16
  this.batchProcessOptions = batchProcessOptions;
17
+ this.entityManager = entityManager;
18
18
  this.logger = logger;
19
19
  }
20
20
  }
@@ -1,4 +1,5 @@
1
1
  import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
2
+ import { pick } from 'radash';
2
3
  import { addKeys } from './addKeys.js';
3
4
  import { configSchema } from './ParsedConfig.js';
4
5
  import { query } from './query.js';
@@ -58,6 +59,23 @@ class EntityManager {
58
59
  addKeys(entityToken, item, overwrite = false) {
59
60
  return addKeys(this, entityToken, item, overwrite);
60
61
  }
62
+ /**
63
+ * Convert an {@link EntityItem | `EntityItem`} into an {@link EntityKey | `EntityKey`}.
64
+ *
65
+ * @param entityToken - {@link Config | `Config`} `entities` key.
66
+ * @param item - {@link EntityItem | `EntityItem`} object.
67
+ * @param overwrite - Overwrite existing properties (default `false`).
68
+ *
69
+ * @returns {@link EntityKey | `EntityKey`} extracted from shallow clone of `item` with updated properties.
70
+ *
71
+ * @throws `Error` if `entityToken` is invalid.
72
+ */
73
+ getPrimaryKey(entityToken, item, overwrite = false) {
74
+ const { hashKey, rangeKey } = this.config;
75
+ return pick(!overwrite && item[hashKey] && item[rangeKey]
76
+ ? item
77
+ : addKeys(this, entityToken, item, overwrite), [this.config.hashKey, this.config.rangeKey]);
78
+ }
61
79
  /**
62
80
  * Strips generated properties, hash key, and range key from an {@link EntityItem | `EntityItem`} object.
63
81
  *
package/package.json CHANGED
@@ -132,5 +132,5 @@
132
132
  },
133
133
  "type": "module",
134
134
  "types": "dist/index.d.ts",
135
- "version": "6.10.2"
135
+ "version": "6.11.0"
136
136
  }