@karmaniverous/entity-manager 6.1.0-2 → 6.1.0-4

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.mts DELETED
@@ -1,910 +0,0 @@
1
- import { Entity, Exactify, TranscodeMap, PropertiesOfType, TranscodableProperties, Transcodes, DefaultTranscodeMap, SortOrder } from '@karmaniverous/entity-tools';
2
- export { DefaultTranscodeMap, Entity, PropertiesOfType, SortOrder, TranscodableProperties, TranscodeMap, Transcodes, defaultTranscodes } from '@karmaniverous/entity-tools';
3
- import { z } from 'zod';
4
-
5
- /**
6
- * Transforms a function such that it only executes when `condition` is truthy.
7
- *
8
- * @param fn - The function to conditionally execute.
9
- * @param condition - The condition to check before executing `fn`.
10
- *
11
- * @typeParam F - The type of the function to conditionally execute.
12
- *
13
- * @returns The conditionalized function with the same signature as `fn`.
14
- *
15
- */
16
- declare function conditionalize<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, condition?: unknown): (...args: Parameters<F>) => ReturnType<F> | undefined;
17
-
18
- /**
19
- * The base EntityMap type. All EntityMaps should extend this type.
20
- *
21
- * @category Entities
22
- */
23
- type EntityMap = Record<string, Entity>;
24
- /**
25
- * Tests a string literal type to determine whether it is a key of any {@link Entity | `Entity`} in an {@link EntityMap | `EntityMap`} or is a member of a union of reserved keys.
26
- *
27
- * @typeParam K - The string literal type to test.
28
- * @typeParam M - The {@link EntityMap | `EntityMap`}.
29
- * @typeParam R - The reserved set of string literal types.
30
- *
31
- * @returns `K` if `K` is exclusive or `never` otherwise.
32
- *
33
- * @category Config
34
- * @protected
35
- */
36
- type ExclusiveKey<K extends string, M extends EntityMap, R extends string = never> = keyof {
37
- [E in keyof Exactify<M> as K extends keyof Exactify<M[E]> | R ? K : never]: never;
38
- } extends never ? K : never;
39
- /**
40
- * Returns the `generated` property of a Config entity.
41
- *
42
- * @typeParam EntityToken - The {@link Entity | `Entity`} token.
43
- * @typeParam M - The {@link EntityMap | `EntityMap`}.
44
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying transcodable property types.
45
- *
46
- * @remarks
47
- * All Entity properties of type `never` must be represented, and no extra properties are allowed.
48
- *
49
- * @category Config
50
- * @protected
51
- */
52
- type ConfigEntityGenerated<EntityToken extends keyof Exactify<M>, M extends EntityMap, T extends TranscodeMap> = ([PropertiesOfType<M[EntityToken], never>] extends [never] ? never : Record<PropertiesOfType<M[EntityToken], never>, {
53
- atomic?: boolean;
54
- elements: TranscodableProperties<M[EntityToken], T>[];
55
- sharded?: boolean;
56
- }>) | ([PropertiesOfType<M[EntityToken], never>] extends [never] ? Record<string, never> : never);
57
- /**
58
- * Defines a single time period in an {@link Entity | `Entity`} sharding strategy.
59
- *
60
- * @category Config
61
- * @protected
62
- */
63
- interface ShardBump {
64
- /**
65
- * The timestamp marking the beginning of the time period. Must be a non-negative integer.
66
- *
67
- * This value must be unique across all {@link ShardBump | `ShardBumps`} for the {@link Entity | `Entity`}.
68
- */
69
- timestamp: number;
70
- /**
71
- * The number of bits per character in the bump's shard space. For example, `0` yields a single shard per character, and a value of `2` would yield 4 shards per character.
72
- *
73
- * This value must be an integer between `1` and `5` inclusive.
74
- */
75
- charBits: number;
76
- /**
77
- * The number of characters used to represent the bump's shard key.
78
- *
79
- * This value must be an integer between `0` and `40` inclusive. Note that more than a few characters will result in an impossibly large shard space! *
80
- * A ShardBump with `chars` of `2` and `charBits` of `3` would yield a two-character shard key with a space of 16 shards.
81
- */
82
- chars: number;
83
- }
84
- /**
85
- * Returns a Config entity type.
86
- *
87
- * @typeParam EntityToken - The {@link Entity | `Entity`} token.
88
- * @typeParam M - The {@link EntityMap | `EntityMap`}.
89
- * @typeParam HashKey - The property used across the configuration to store an {@link Entity | `Entity`}'s sharded hash key. Should be configured as the table hash key. Must not conflict with any {@link Entity | `Entity`} property.
90
- * @typeParam RangeKey - The property used across the configuration to store an {@link Entity | `Entity`}'s range key. Should be configured as the table range key. Must not conflict with any {@link Entity | `Entity`} property.
91
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying transcodable property types. Only {@link Entity | `Entity`} properties of these types can be components of an {@link ConfigEntity.indexes | index} or a {@link ConfigEntityGenerated | generated property}.
92
-
93
- * @remarks
94
- * `generated` is optional if `E` has no properties of type `never`.
95
- *
96
- * @category Config
97
- * @protected
98
- */
99
- type ConfigEntity<EntityToken extends keyof Exactify<M>, M extends EntityMap, HashKey extends string, RangeKey extends string, T extends TranscodeMap> = {
100
- /**
101
- * The default maximum number of records to return from a query.
102
- *
103
- * @defaultValue `10`
104
- *
105
- * @remarks
106
- * Can be overridden at {@link QueryOptions.limit | `QueryOptions.limit`}.
107
- *
108
- * In cross-shard queries, the actual number of records returned is heavily influenced by {@link QueryOptions.pageSize | query pageSize} and the number of shards queried. Actual results may significantly exceed this limit.
109
- */
110
- defaultLimit?: number;
111
- /**
112
- * The default maximum number of records to return per data page on an individual shard query.
113
- *
114
- * @defaultValue `10`
115
- *
116
- * @remarks
117
- * Shard queries will be repeated internally until either the shard is exhausted or the number of records returned exceeds the {@link QueryOptions.limit | query limit}.
118
- *
119
- * Can be overridden at {@link QueryOptions.pageSize | `QueryOptions.pageSize`}.
120
- */
121
- defaultPageSize?: number;
122
- /**
123
- * This object assigns transcodes to {@link Entity | `Entity`} generated property & ungenerated index elements.
124
- *
125
- * These transcodes are used to encode and decode generated property values & pageKeys.
126
- *
127
- * The keys of this object must be transcodable properties of the {@link Entity | `Entity`}.
128
- *
129
- * The values of this object must be one of the keys of the {@link Config | `Config`} `T` type parameter (the config's {@link TranscodeMap | `TranscodeMap`}).
130
- *
131
- * The types of the related {@link Entity | `Entity`} and {@link TranscodeMap | `TranscodeMap`} properties should match.
132
- *
133
- * If any entity generated property element or ungenerated index element is not included here, the {@link Config | `Config`} object will fail to parse.
134
- *
135
- * @example
136
- * ```
137
- * // Default transcodable types.
138
- * interface DefaultTranscodeMap extends TranscodeMap {
139
- * string: string;
140
- * number: number;
141
- * boolean: boolean;
142
- * bigint: bigint;
143
- * }
144
- *
145
- * interface MyEntityMap extends EntityMap {
146
- * user: {
147
- * created: number;
148
- * data?: Json; // Not a Stringifiable type
149
- * userId: string;
150
- * };
151
- * }
152
- *
153
- * // T type param defaults to DefaultTranscodeMap.
154
- * const config: Config<MyEntityMap> = {
155
- * entities: {
156
- * user: {
157
- * ...,
158
- * types: { // All Stringafiable properties required!
159
- * created: 'number',
160
- * userId: 'string',
161
- * // 'data' not allowed: not a Stringifiable type
162
- * }
163
- * }
164
- * },
165
- * ...
166
- * };
167
- * ```
168
- */
169
- elementTranscodes?: ([TranscodableProperties<M[EntityToken], T>] extends [never] ? never : {
170
- [P in TranscodableProperties<M[EntityToken], T>]?: PropertiesOfType<T, M[EntityToken][P]>;
171
- }) | ([TranscodableProperties<M[EntityToken], T>] extends [never] ? Record<string, never> : never);
172
- /**
173
- * Indexes defined for the {@link Entity | `Entity`}. Should reflect the underlying database table indexes.
174
- *
175
- * Each key is the name of an index, and each value is a non-empty array of {@link Entity | `Entity`} property names that define the index.
176
- *
177
- * Related property types must be align with the {@link Config | `Config`} `T` type parameter. Note tha all {@link ConfigEntityGenerated | generated property} types are transcodable by definition.
178
- */
179
- indexes?: Record<string, (TranscodableProperties<M[EntityToken], T> | PropertiesOfType<M[EntityToken], never> | HashKey | RangeKey)[]>;
180
- /**
181
- * An array of {@link ShardBump | `ShardBump`} objects representing the {@link Entity | `Entity`}'s sharding strategy.
182
- *
183
- * If omitted, or if configured without a zero-{@link ShardBump.timestamp | `timestamp`} {@link ShardBump | `ShardBump`}, this array will be initialized with the following {@link ShardBump | `ShardBump`} as its first member:
184
- *
185
- * ```
186
- * { timestamp: 0, charBits: 0, chars: 1 }
187
- * ```
188
- *
189
- * Members must be unique by {@link ShardBump.timestamp | `timestamp`}.
190
- *
191
- * {@link ShardBump.chars | `chars`} must increase monotonically with {@link ShardBump.timestamp | `timestamp`}
192
- *
193
- * Array will be sorted in ascending order by {@link ShardBump.timestamp | `timestamp`} on initialization.
194
- *
195
- * Future {@link ShardBump | `ShardBumps`} can be changed as required, but past {@link ShardBump | `ShardBumps`} should not be modified or data integrity will be compromised!
196
- */
197
- shardBumps?: ShardBump[];
198
- /**
199
- * Identifies the {@link Entity | `Entity`} property used as the timestamp for shard key calculations.
200
- *
201
- * This property must be of type `number`. Its value should not change over the life of the record. A `created` timestamp is ideal.
202
- *
203
- * Once in production, this configuration property should not be changed or data integrity will be compromised!
204
- */
205
- timestampProperty: PropertiesOfType<M[EntityToken], number>;
206
- /**
207
- * Identifies the {@link Entity | `Entity`} used as the basis for both shard key calculations and the table's {@link ConfigKeys.rangeKey | range key}.
208
- *
209
- * This property must be of type `string` or `number`. Its value should be a unique record identifier and should not change over the life of the record.
210
- *
211
- * Once in production, this configuration property should not be changed or data integrity will be compromised!
212
- */
213
- uniqueProperty: PropertiesOfType<M[EntityToken], number | string>;
214
- } & ([PropertiesOfType<M[EntityToken], never>] extends [never] ? {
215
- generated?: ConfigEntityGenerated<EntityToken, M, T>;
216
- } : {
217
- /**
218
- * {@link Entity | `Entity`} properties whose values will be generated by EntityManager.
219
- *
220
- * These properties should be indicated by a `never` type in the {@link Config | `Config`} `EntityMap` type parameter.
221
- *
222
- * All such properties must be accounted for in the `generated` object, and no additional properties are permitted..
223
- */
224
- generated: ConfigEntityGenerated<EntityToken, M, T>;
225
- });
226
- /**
227
- * Returns the `entities` property of the {@link Config | `Config`} tyoe.
228
- *
229
- * @typeParam M - The {@link EntityMap | `EntityMap`} type that identitfies the {@link Entity | `Entity`} & related property types to be managed by EntityManager.
230
- * @typeParam HashKey - The property used across the configuration to store an {@link Entity | `Entity`}'s sharded hash key. Should be configured as the table hash key. Must not conflict with any {@link Entity | `Entity`} property.
231
- * @typeParam RangeKey - The property used across the configuration to store an {@link Entity | `Entity`}'s range key. Should be configured as the table range key. Must not conflict with any {@link Entity | `Entity`} property.
232
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying transcodable property types. Only {@link Entity | `Entity`} properties of these types can be components of an {@link ConfigEntity.indexes | index} or a {@link ConfigEntityGenerated | generated property}.
233
- *
234
- * @remarks
235
- * All properties of `M` must be represented, and no extra properties are allowed.
236
- *
237
- * @category Config
238
- * @protected
239
- */
240
- type ConfigEntities<M extends EntityMap, HashKey extends string, RangeKey extends string, T extends TranscodeMap> = ([keyof Exactify<M>] extends [never] ? never : {
241
- [E in keyof Exactify<M>]: ConfigEntity<E, M, HashKey, RangeKey, T>;
242
- }) | Record<string, never>;
243
- /**
244
- * Returns variably-optional properties of the {@link Config | `Config`} type as optional.
245
-
246
- * @typeParam M - The {@link EntityMap | `EntityMap`} type that identitfies the {@link Entity | `Entity`} & related property types to be managed by EntityManager.
247
- * @typeParam HashKey - The property used across the configuration to store an {@link Entity | `Entity`}'s sharded hash key. Should be configured as the table hash key. Must not conflict with any {@link Entity | `Entity`} property.
248
- * @typeParam RangeKey - The property used across the configuration to store an {@link Entity | `Entity`}'s range key. Should be configured as the table range key. Must not conflict with any {@link Entity | `Entity`} property.
249
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying transcodable property types. Only {@link Entity | `Entity`} properties of these types can be components of an {@link ConfigEntity.indexes | index} or a {@link ConfigEntityGenerated | generated property}.
250
- *
251
- * @category Config
252
- * @protected
253
- */
254
- interface ConfigKeys<M extends EntityMap, HashKey extends string, RangeKey extends string, T extends TranscodeMap> {
255
- /**
256
- * Defines options for each {@link Entity | `Entity`} in the {@link Config | `Config`} `EntityMap` type parameter.
257
- *
258
- * The properties of this object must exactly match the keys of the {@link Config | `Config`} `EntityMap` type parameter.
259
- */
260
- entities?: ConfigEntities<M, HashKey, RangeKey, T>;
261
- /**
262
- * The property used across the configuration to store an {@link Entity | `Entity`}'s sharded hash key. Should be configured as the table hash key.
263
- *
264
- * This value must exactly match the {@link Config | `Config`} `HashKey` type parameter, and must not conflict with any {@link Entity | `Entity`} property.
265
- *
266
- * @defaultValue `'hashKey'`
267
- *
268
- * @category Config
269
- * @protected
270
- */
271
- hashKey?: ExclusiveKey<HashKey, M, RangeKey>;
272
- /**
273
- * The property used across the configuration to store an {@link Entity | `Entity`}'s range key. Should be configured as the table range key.
274
- *
275
- * This value must exactly match the {@link Config | `Config`} `RangeKey` type parameter, and must not conflict with any {@link Entity | `Entity`} property.
276
- *
277
- * @defaultValue `'rangeKey'`
278
- */
279
- rangeKey?: ExclusiveKey<RangeKey, M, HashKey>;
280
- }
281
- /**
282
- * @category Config
283
- * @protected
284
- */
285
- type ConfigTranscodes<T extends TranscodeMap> = ([keyof Exactify<T>] extends [never] ? never : Transcodes<T>) | ([keyof Exactify<T>] extends [never] ? Record<string, never> : never);
286
- /**
287
- * EntityManager Config type.
288
- *
289
- * @typeParam M - The {@link EntityMap | `EntityMap`} type that identitfies the {@link Entity | `Entity`} & related property types to be managed by EntityManager.
290
- * @typeParam HashKey - The property used across the configuration to store an {@link Entity | `Entity`}'s sharded hash key. Should be configured as the table hash key. Must not conflict with any {@link Entity | `Entity`} property. Defaults to `'hashKey'`.
291
- * @typeParam RangeKey - The property used across the configuration to store an {@link Entity | `Entity`}'s range key. Should be configured as the table range key. Must not conflict with any {@link Entity | `Entity`} property. Defaults to `'rangeKey'`.
292
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying transcodable property types. Only {@link Entity | `Entity`} properties of these types can be components of an {@link ConfigEntity.indexes | index} or a {@link ConfigEntityGenerated | generated property}. Defaults to {@link DefaultTranscodeMap | `DefaultTranscodeMap`}.
293
- *
294
- * @remarks
295
- * `entities` is optional if `M` is empty.
296
- *
297
- * @category Config
298
- */
299
- type Config<M extends EntityMap = Record<string, never>, HashKey extends string = 'hashKey', RangeKey extends string = 'rangeKey', T extends TranscodeMap = DefaultTranscodeMap> = ([keyof Exactify<M>] extends [never] ? ConfigKeys<M, HashKey, RangeKey, T> : Required<ConfigKeys<M, HashKey, RangeKey, T>>) & {
300
- /**
301
- * Defines the delimiter used to separate key-value pairs in a generated property value.
302
- *
303
- * Must consist of one or more non-word characters, and must not intersect with {@link Config.generatedValueDelimiter | `generatedValueDelimiter`} or {@link Config.shardKeyDelimiter | `shardKeyDelimiter`}.
304
- *
305
- * @defaultValue `'|'`
306
- */
307
- generatedKeyDelimiter?: string;
308
- /**
309
- * Defines the delimiter used to separate keys & values in a generated property value.
310
- *
311
- * Must consist of one or more non-word characters, and must not intersect with {@link Config.generatedKeyDelimiter | `generatedKeyDelimiter`} or {@link Config.shardKeyDelimiter | `shardKeyDelimiter`}.
312
- *
313
- * @defaultValue `'#'`
314
- */
315
- generatedValueDelimiter?: string;
316
- /**
317
- * Defines the delimiter used to construct an Entity's hashKey value from its Entity key and shard key.
318
- *
319
- * Must consist of one or more non-word characters, and must not intersect with {@link Config.generatedKeyDelimiter | `generatedKeyDelimiter`} or {@link Config.generatedValueDelimiter | `generatedValueDelimiter`}.
320
- *
321
- * @defaultValue `'!'`
322
- */
323
- shardKeyDelimiter?: string;
324
- /**
325
- * The default maximum number of shards to query in parallel. Can be overridden at {@link QueryOptions.throttle | `QueryOptions.throttle`}.
326
- *
327
- * @defaultValue `10`
328
- */
329
- throttle?: number;
330
- } & ([keyof Exactify<T>] extends [never] ? {
331
- transcodes?: ConfigTranscodes<T>;
332
- } : DefaultTranscodeMap extends T ? {
333
- transcodes?: ConfigTranscodes<T>;
334
- } : {
335
- transcodes: ConfigTranscodes<T>;
336
- });
337
- /**
338
- * Flattens the top layer of logic in a type.
339
- *
340
- * @category Utility
341
- * @protected
342
- */
343
- type Unwrap<T> = {
344
- [P in keyof T]: T[P];
345
- };
346
- /**
347
- * Extracts a map of {@link Entity | `Entity`} item types decorated with {@link ConfigKeys.hashKey | hashKey}, {@link ConfigKeys.rangeKey | rangeKey}, and {@link ConfigEntityGenerated | generated properties}.
348
- *
349
- * @typeParam M - The {@link EntityMap | `EntityMap`} type that identitfies the {@link Entity | `Entity`} & related property types to be managed by EntityManager.
350
- * @typeParam HashKey - The property used across the configuration to store an {@link Entity | `Entity`}'s sharded hash key. Should be configured as the table hash key. Must not conflict with any {@link Entity | `Entity`} property. Defaults to `'hashKey'`.
351
- * @typeParam RangeKey - The property used across the configuration to store an {@link Entity | `Entity`}'s range key. Should be configured as the table range key. Must not conflict with any {@link Entity | `Entity`} property. Defaults to `'rangeKey'`.
352
- *
353
- * @category Entities
354
- */
355
- type ItemMap<M extends EntityMap, HashKey extends string = 'hashKey', RangeKey extends string = 'rangeKey'> = {
356
- [EntityToken in keyof Exactify<M>]: Unwrap<{
357
- [P in keyof Exactify<M[EntityToken]>]: [
358
- NonNullable<M[EntityToken][P]>
359
- ] extends [never] ? string : M[EntityToken][P];
360
- } & {
361
- [P in HashKey | RangeKey]?: string;
362
- }>;
363
- };
364
-
365
- declare const configSchema: z.ZodEffects<z.ZodObject<{
366
- entities: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodObject<{
367
- defaultLimit: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
368
- defaultPageSize: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
369
- generated: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodOptional<z.ZodObject<{
370
- atomic: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
371
- elements: z.ZodEffects<z.ZodArray<z.ZodString, "atleastone">, [string, ...string[]], [string, ...string[]]>;
372
- sharded: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
373
- }, "strip", z.ZodTypeAny, {
374
- atomic: boolean;
375
- elements: [string, ...string[]];
376
- sharded: boolean;
377
- }, {
378
- elements: [string, ...string[]];
379
- atomic?: boolean | undefined;
380
- sharded?: boolean | undefined;
381
- }>>>>>;
382
- elementTranscodes: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
383
- indexes: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodEffects<z.ZodArray<z.ZodString, "atleastone">, [string, ...string[]], [string, ...string[]]>>>>;
384
- shardBumps: z.ZodEffects<z.ZodEffects<z.ZodEffects<z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodObject<{
385
- timestamp: z.ZodNumber;
386
- charBits: z.ZodNumber;
387
- chars: z.ZodNumber;
388
- }, "strict", z.ZodTypeAny, {
389
- timestamp: number;
390
- charBits: number;
391
- chars: number;
392
- }, {
393
- timestamp: number;
394
- charBits: number;
395
- chars: number;
396
- }>, "many">>>, {
397
- timestamp: number;
398
- charBits: number;
399
- chars: number;
400
- }[], {
401
- timestamp: number;
402
- charBits: number;
403
- chars: number;
404
- }[] | undefined>, {
405
- timestamp: number;
406
- charBits: number;
407
- chars: number;
408
- }[], {
409
- timestamp: number;
410
- charBits: number;
411
- chars: number;
412
- }[] | undefined>, {
413
- timestamp: number;
414
- charBits: number;
415
- chars: number;
416
- }[], {
417
- timestamp: number;
418
- charBits: number;
419
- chars: number;
420
- }[] | undefined>;
421
- timestampProperty: z.ZodString;
422
- uniqueProperty: z.ZodString;
423
- }, "strict", z.ZodTypeAny, {
424
- defaultLimit: number;
425
- defaultPageSize: number;
426
- elementTranscodes: Record<string, string>;
427
- indexes: Record<string, [string, ...string[]]>;
428
- shardBumps: {
429
- timestamp: number;
430
- charBits: number;
431
- chars: number;
432
- }[];
433
- timestampProperty: string;
434
- uniqueProperty: string;
435
- generated: Record<string, {
436
- atomic: boolean;
437
- elements: [string, ...string[]];
438
- sharded: boolean;
439
- } | undefined>;
440
- }, {
441
- timestampProperty: string;
442
- uniqueProperty: string;
443
- defaultLimit?: number | undefined;
444
- defaultPageSize?: number | undefined;
445
- elementTranscodes?: Record<string, string> | undefined;
446
- indexes?: Record<string, [string, ...string[]]> | undefined;
447
- shardBumps?: {
448
- timestamp: number;
449
- charBits: number;
450
- chars: number;
451
- }[] | undefined;
452
- generated?: Record<string, {
453
- elements: [string, ...string[]];
454
- atomic?: boolean | undefined;
455
- sharded?: boolean | undefined;
456
- } | undefined> | undefined;
457
- }>, {
458
- defaultLimit: number;
459
- defaultPageSize: number;
460
- elementTranscodes: Record<string, string>;
461
- indexes: Record<string, [string, ...string[]]>;
462
- shardBumps: {
463
- timestamp: number;
464
- charBits: number;
465
- chars: number;
466
- }[];
467
- timestampProperty: string;
468
- uniqueProperty: string;
469
- generated: Record<string, {
470
- atomic: boolean;
471
- elements: [string, ...string[]];
472
- sharded: boolean;
473
- } | undefined>;
474
- }, {
475
- timestampProperty: string;
476
- uniqueProperty: string;
477
- defaultLimit?: number | undefined;
478
- defaultPageSize?: number | undefined;
479
- elementTranscodes?: Record<string, string> | undefined;
480
- indexes?: Record<string, [string, ...string[]]> | undefined;
481
- shardBumps?: {
482
- timestamp: number;
483
- charBits: number;
484
- chars: number;
485
- }[] | undefined;
486
- generated?: Record<string, {
487
- elements: [string, ...string[]];
488
- atomic?: boolean | undefined;
489
- sharded?: boolean | undefined;
490
- } | undefined> | undefined;
491
- }>>>>;
492
- generatedKeyDelimiter: z.ZodDefault<z.ZodOptional<z.ZodString>>;
493
- generatedValueDelimiter: z.ZodDefault<z.ZodOptional<z.ZodString>>;
494
- shardKeyDelimiter: z.ZodDefault<z.ZodOptional<z.ZodString>>;
495
- hashKey: z.ZodDefault<z.ZodOptional<z.ZodString>>;
496
- rangeKey: z.ZodDefault<z.ZodOptional<z.ZodString>>;
497
- throttle: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
498
- transcodes: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
499
- encode: z.ZodFunction<z.ZodTuple<[z.ZodAny], z.ZodUnknown>, z.ZodString>;
500
- decode: z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodAny>;
501
- }, "strict", z.ZodTypeAny, {
502
- encode: (args_0: any, ...args: unknown[]) => string;
503
- decode: (args_0: string, ...args: unknown[]) => any;
504
- }, {
505
- encode: (args_0: any, ...args: unknown[]) => string;
506
- decode: (args_0: string, ...args: unknown[]) => any;
507
- }>>>>;
508
- }, "strict", z.ZodTypeAny, {
509
- hashKey: string;
510
- rangeKey: string;
511
- entities: Record<string, {
512
- defaultLimit: number;
513
- defaultPageSize: number;
514
- elementTranscodes: Record<string, string>;
515
- indexes: Record<string, [string, ...string[]]>;
516
- shardBumps: {
517
- timestamp: number;
518
- charBits: number;
519
- chars: number;
520
- }[];
521
- timestampProperty: string;
522
- uniqueProperty: string;
523
- generated: Record<string, {
524
- atomic: boolean;
525
- elements: [string, ...string[]];
526
- sharded: boolean;
527
- } | undefined>;
528
- }>;
529
- generatedKeyDelimiter: string;
530
- generatedValueDelimiter: string;
531
- shardKeyDelimiter: string;
532
- throttle: number;
533
- transcodes: Record<string, {
534
- encode: (args_0: any, ...args: unknown[]) => string;
535
- decode: (args_0: string, ...args: unknown[]) => any;
536
- }>;
537
- }, {
538
- hashKey?: string | undefined;
539
- rangeKey?: string | undefined;
540
- entities?: Record<string, {
541
- timestampProperty: string;
542
- uniqueProperty: string;
543
- defaultLimit?: number | undefined;
544
- defaultPageSize?: number | undefined;
545
- elementTranscodes?: Record<string, string> | undefined;
546
- indexes?: Record<string, [string, ...string[]]> | undefined;
547
- shardBumps?: {
548
- timestamp: number;
549
- charBits: number;
550
- chars: number;
551
- }[] | undefined;
552
- generated?: Record<string, {
553
- elements: [string, ...string[]];
554
- atomic?: boolean | undefined;
555
- sharded?: boolean | undefined;
556
- } | undefined> | undefined;
557
- }> | undefined;
558
- generatedKeyDelimiter?: string | undefined;
559
- generatedValueDelimiter?: string | undefined;
560
- shardKeyDelimiter?: string | undefined;
561
- throttle?: number | undefined;
562
- transcodes?: Record<string, {
563
- encode: (args_0: any, ...args: unknown[]) => string;
564
- decode: (args_0: string, ...args: unknown[]) => any;
565
- }> | undefined;
566
- }>, {
567
- hashKey: string;
568
- rangeKey: string;
569
- entities: Record<string, {
570
- defaultLimit: number;
571
- defaultPageSize: number;
572
- elementTranscodes: Record<string, string>;
573
- indexes: Record<string, [string, ...string[]]>;
574
- shardBumps: {
575
- timestamp: number;
576
- charBits: number;
577
- chars: number;
578
- }[];
579
- timestampProperty: string;
580
- uniqueProperty: string;
581
- generated: Record<string, {
582
- atomic: boolean;
583
- elements: [string, ...string[]];
584
- sharded: boolean;
585
- } | undefined>;
586
- }>;
587
- generatedKeyDelimiter: string;
588
- generatedValueDelimiter: string;
589
- shardKeyDelimiter: string;
590
- throttle: number;
591
- transcodes: Record<string, {
592
- encode: (args_0: any, ...args: unknown[]) => string;
593
- decode: (args_0: string, ...args: unknown[]) => any;
594
- }>;
595
- }, {
596
- hashKey?: string | undefined;
597
- rangeKey?: string | undefined;
598
- entities?: Record<string, {
599
- timestampProperty: string;
600
- uniqueProperty: string;
601
- defaultLimit?: number | undefined;
602
- defaultPageSize?: number | undefined;
603
- elementTranscodes?: Record<string, string> | undefined;
604
- indexes?: Record<string, [string, ...string[]]> | undefined;
605
- shardBumps?: {
606
- timestamp: number;
607
- charBits: number;
608
- chars: number;
609
- }[] | undefined;
610
- generated?: Record<string, {
611
- elements: [string, ...string[]];
612
- atomic?: boolean | undefined;
613
- sharded?: boolean | undefined;
614
- } | undefined> | undefined;
615
- }> | undefined;
616
- generatedKeyDelimiter?: string | undefined;
617
- generatedValueDelimiter?: string | undefined;
618
- shardKeyDelimiter?: string | undefined;
619
- throttle?: number | undefined;
620
- transcodes?: Record<string, {
621
- encode: (args_0: any, ...args: unknown[]) => string;
622
- decode: (args_0: string, ...args: unknown[]) => any;
623
- }> | undefined;
624
- }>;
625
- /**
626
- * Foo
627
- *
628
- * @category Config
629
- */
630
- type ParsedConfig = z.infer<typeof configSchema>;
631
-
632
- /**
633
- * A result returned by a {@link ShardQueryFunction | `ShardQueryFunction`} querying an individual shard.
634
- *
635
- * @typeParam Item - The {@link ItemMap | `ItemMap`} type being queried.
636
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying property types that can be indexed.
637
-
638
- * @category Query
639
- */
640
- interface ShardQueryResult<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string = 'hashKey', RangeKey extends string = 'rangeKey', T extends TranscodeMap = DefaultTranscodeMap> {
641
- /** The number of records returned. */
642
- count: number;
643
- /** The returned records. */
644
- items: Item[];
645
- /** The page key for the next query on this shard. */
646
- pageKey?: Partial<Pick<Item, PropertiesOfType<Item, T[keyof T]>>>;
647
- }
648
-
649
- /**
650
- * A query function that returns a single page of results from an individual
651
- * shard. This function will typically be composed dynamically to express a
652
- * specific query index & logic. The arguments to this function will be
653
- * provided by the {@link EntityManager.query | `EntityManager.query`} method, which assembles many returned
654
- * pages queried across multiple shards into a single query result.
655
- *
656
- * @typeParam Item - The {@link ItemMap | `ItemMap`} type being queried.
657
- * @typeParam T - The {@link TranscodeMap | `TranscodeMap`} identifying property types that can be indexed. Defaults to {@link DefaultTranscodeMap | `DefaultTranscodeMap`}.
658
-
659
- * @param hashKey - The {@link ConfigKeys.hashKey | `this.config.hashKey`} property value of the shard being queried.
660
- * @param pageKey - The page key returned by the previous query on this shard.
661
- * @param pageSize - The maximum number of items to return from this query.
662
- *
663
- * @category Query
664
- */
665
- type ShardQueryFunction<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string = 'hashKey', RangeKey extends string = 'rangeKey', T extends TranscodeMap = DefaultTranscodeMap> = (hashKey: string, pageKey?: Partial<Pick<Item, PropertiesOfType<Item, T[keyof T]>>>, pageSize?: number) => Promise<ShardQueryResult<Item, EntityToken, M, HashKey, RangeKey, T>>;
666
-
667
- /**
668
- * Options passed to the {@link EntityManager.query | `EntityManager.query`} method.
669
- *
670
- * @category Query
671
- */
672
- interface QueryOptions<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> {
673
- /** Identifies the entity to be queried. Key of {@link Config | `EntityManager.config.entities`}. */
674
- entityToken: EntityToken;
675
- /**
676
- * Identifies the entity key across which the query will be sharded. Key of
677
- * {@link Config | `EntityManager.config.entities.<entityToken>.keys`}.
678
- */
679
- hashKey: string;
680
- /**
681
- * A partial {@link ItemMap | `ItemMap`} object containing at least the properties specified in
682
- * {@link Config | `EntityManager.config.entities.<entityToken>.keys.<keyToken>.elements`}, except for the properties specified in {@link Config | `EntityManager.config.tokens`}.
683
- *
684
- * This data will be used to generate query keys across all shards.
685
- */
686
- item?: Item;
687
- /**
688
- * The target maximum number of records to be returned by the query across
689
- * all shards.
690
- *
691
- * The actual number of records returned will be a product of {@link QueryOptions.pageSize | `pageSize`} and the
692
- * number of shards queried, unless limited by available records in a given
693
- * shard.
694
- */
695
- limit?: number;
696
- /**
697
- * {@link QueryResult.pageKeyMap | `pageKeyMap`} returned by the previous iteration of this query.
698
- */
699
- pageKeyMap?: string;
700
- /**
701
- * The maximum number of records to be returned by each individual query to a
702
- * single shard (i.e. {@link ShardQueryFunction | `ShardQueryFunction`} execution).
703
- *
704
- * Note that, within a given {@link EntityManager.query | `query`} method execution, these queries will be
705
- * repeated until either available data is exhausted or the {@link QueryOptions.limit | `limit`} value is
706
- * reached.
707
- */
708
- pageSize?: number;
709
- /**
710
- * Each key in this object is a valid entity index token. Each value is a valid
711
- * {@link ShardQueryFunction | 'ShardQueryFunction'} that specifies the query of a single page of data on a
712
- * single shard for the mapped index.
713
- *
714
- * This allows simultaneous queries on multiple sort keys to share a single
715
- * page key, e.g. to match the same string against `firstName` and `lastName`
716
- * properties without performing a table scan for either.
717
- */
718
- queryMap: Record<string, ShardQueryFunction<Item, EntityToken, M, HashKey, RangeKey, T>>;
719
- /**
720
- * A {@link SortOrder | `SortOrder`} object specifying the sort order of the result set. Defaults to `[]`.
721
- */
722
- sortOrder?: SortOrder<Item>;
723
- /**
724
- * Lower limit to query shard space.
725
- *
726
- * Only valid if the query is constrained along the dimension used by the
727
- * {@link Config | `EntityManager.config.entities.<entityToken>.sharding.timestamptokens.timestamp`}
728
- * function to generate `shardKey`.
729
- *
730
- * @defaultValue `0`
731
- */
732
- timestampFrom?: number;
733
- /**
734
- * Upper limit to query shard space.
735
- *
736
- * Only valid if the query is constrained along the dimension used by the
737
- * {@link Config | `EntityManager.config.entities.<entityToken>.sharding.timestamptokens.timestamp`}
738
- * function to generate `shardKey`.
739
- *
740
- * @defaultValue `Date.now()`
741
- */
742
- timestampTo?: number;
743
- /**
744
- * The maximum number of shards to query in parallel. Overrides options `throttle`.
745
- *
746
- * @defaultValue `options.throttle`
747
- */
748
- throttle?: number;
749
- }
750
-
751
- /**
752
- * A result returned by a query across multiple shards, where each shard may
753
- * receive multiple page queries via a dynamically-generated {@link ShardQueryFunction | `ShardQueryFunction`}.
754
- *
755
- * @category Query
756
- */
757
- interface QueryResult<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string, M extends EntityMap, HashKey extends string, RangeKey extends string> {
758
- /** Total number of records returned across all shards. */
759
- count: number;
760
- /** The returned records. */
761
- items: Item[];
762
- /**
763
- * A compressed, two-layer map of page keys, used to query the next page of
764
- * data for a given sort key on each shard of a given hash key.
765
- */
766
- pageKeyMap: string;
767
- }
768
-
769
- /**
770
- * The EntityManager class applies a configuration-driven sharded data model &
771
- * query strategy to NoSql data.
772
- *
773
- * @category Entity Manager
774
- */
775
- declare class EntityManager<M extends EntityMap, HashKey extends string, RangeKey extends string, T extends TranscodeMap> {
776
- #private;
777
- /**
778
- * Create an EntityManager instance.
779
- *
780
- * @param config - EntityManager {@link Config | `Config`} object.
781
- */
782
- constructor(config: Config<M, HashKey, RangeKey, T>);
783
- /**
784
- * Get the current EntityManager {@link Config | `Config`} object.
785
- *
786
- * @returns Current {@link Config | `Config`} object.
787
- */
788
- get config(): ParsedConfig;
789
- /**
790
- * Set the current EntityManager {@link Config | `Config`} object.
791
- *
792
- * @param value - {@link Config | `Config`} object.
793
- */
794
- set config(value: ParsedConfig);
795
- /**
796
- * Update generated properties, hash key, and range key on an {@link ItemMap | `ItemMap`} object. Mutates `item`.
797
- *
798
- * @param item - {@link ItemMap | `ItemMap`} object.
799
- * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
800
- * @param overwrite - Overwrite existing properties (default `false`).
801
- *
802
- * @returns Mutated `item` with updated properties.
803
- *
804
- * @throws `Error` if `entityToken` is invalid.
805
- */
806
- addKeys<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(item: Item, entityToken: EntityToken, overwrite?: boolean): Item;
807
- /**
808
- * Strips generated properties, hash key, and range key from an {@link ItemMap | `ItemMap`} object. Mutates `item`.
809
- *
810
- * @param item - {@link ItemMap | `ItemMap`} object.
811
- * @param entityToken - {@link ConfigKeys.entities | `this.config.entities`} key.
812
- *
813
- * @returns Mutated `item` without generated properties, hash key or range key.
814
- *
815
- * @throws `Error` if `entityToken` is invalid.
816
- */
817
- removeKeys<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(item: Item, entityToken: EntityToken): Item;
818
- /**
819
- * Query a database entity across shards in a provider-generic fashion.
820
- *
821
- * @remarks
822
- * 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.
823
- *
824
- * Individual shard query results will be combined, deduped by {@link ConfigEntity.uniqueProperty} property value, and sorted by {@link QueryOptions.sortOrder | `sortOrder`}.
825
- *
826
- * In queries on sharded data, expect the leading and trailing edges of returned data pages to interleave somewhat with preceding & following pages.
827
- *
828
- * Unsharded query results should sort & page as expected.
829
- *
830
- * @param options - {@link QueryOptions | `QueryOptions`} object.
831
- *
832
- * @returns {@link QueryResult} object.
833
- *
834
- * @throws Error if {@link QueryOptions.pageKeyMap | `pageKeyMap`} keys do not match {@link QueryOptions.queryMap | `queryMap`} keys.
835
- */
836
- query<Item extends ItemMap<M, HashKey, RangeKey>[EntityToken], EntityToken extends keyof Exactify<M> & string>(options: QueryOptions<Item, EntityToken, M, HashKey, RangeKey, T>): Promise<QueryResult<Item, EntityToken, M, HashKey, RangeKey>>;
837
- }
838
-
839
- /**
840
- * Options for EntityManager client methods that support batch operations.
841
- */
842
- interface EntityManagerClientBatchOptions {
843
- /** Batch size. */
844
- batchSize?: number;
845
- /** Delay increment in ms for retry operations. Doubles on each retry. */
846
- delayIncrement?: number;
847
- /** Max retries for retry operations. */
848
- maxRetries?: number;
849
- /** Throttle for parallel operations. */
850
- throttle?: number;
851
- }
852
-
853
- /**
854
- * Generic logger endpoint type.
855
- *
856
- * @category Logger
857
- */
858
- type LoggerEndpoint = (...args: unknown[]) => void;
859
- /**
860
- * Logger interface.
861
- *
862
- * @category Logger
863
- */
864
- interface Logger {
865
- debug: LoggerEndpoint;
866
- error: LoggerEndpoint;
867
- }
868
- /**
869
- * Logger options.
870
- *
871
- * @category Logger
872
- */
873
- interface LoggerOptions {
874
- /** Logger to use for internal logging. Must support the `debug` & `error` methods. Defaults to `console`. */
875
- logger?: Logger;
876
- /** Enables internal logging when `true`. */
877
- logInternals?: boolean;
878
- }
879
-
880
- /**
881
- * EntityManagerClient base class options.
882
- *
883
- * @category Client
884
- */
885
- type EntityManagerClientOptions = EntityManagerClientBatchOptions & LoggerOptions;
886
- /**
887
- * EntityManagerClient base class.
888
- *
889
- * @category Client
890
- */
891
- declare abstract class EntityManagerClient<O extends EntityManagerClientOptions> {
892
- #private;
893
- constructor({ batchSize, delayIncrement, maxRetries, throttle, logger, logInternals, ...childOptions }: O);
894
- /**
895
- * Returns the options used to create the EntityManagerClient instance.
896
- */
897
- get options(): Required<O>;
898
- }
899
-
900
- /**
901
- * Returns an object type with specific properties rendered required and non-nullable.
902
- *
903
- * @typeParam T - The object type to modify.
904
- * @typeParam K - Union of keys of `T` to render required and non-nullable.
905
- */
906
- type WithRequiredAndNonNullable<T, K extends keyof T> = T & {
907
- [P in K]-?: NonNullable<T[P]>;
908
- };
909
-
910
- export { type Config, type ConfigEntities, type ConfigEntity, type ConfigEntityGenerated, type ConfigKeys, type ConfigTranscodes, EntityManager, EntityManagerClient, type EntityManagerClientBatchOptions, type EntityManagerClientOptions, type EntityMap, type ExclusiveKey, type ItemMap, type Logger, type LoggerEndpoint, type LoggerOptions, type ParsedConfig, type QueryOptions, type QueryResult, type ShardBump, type ShardQueryFunction, type ShardQueryResult, type Unwrap, type WithRequiredAndNonNullable, conditionalize };