@karmaniverous/entity-manager 5.0.9 → 5.0.10
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/.github/FUNDING.yml +3 -3
- package/dist/cjs/BaseEntityClient/BaseEntityClient.js +28 -0
- package/dist/cjs/BaseQueryBuilder/BaseQueryBuilder.js +62 -0
- package/dist/cjs/EntityManager/EntityManager.js +131 -0
- package/dist/cjs/EntityManager/ParsedConfig.js +385 -0
- package/dist/cjs/EntityManager/addKeys.js +60 -0
- package/dist/cjs/EntityManager/createEntityManager.js +46 -0
- package/dist/cjs/EntityManager/decodeElement.js +46 -0
- package/dist/cjs/EntityManager/decodeGeneratedProperty.js +53 -0
- package/dist/cjs/EntityManager/dehydrateIndexItem.js +68 -0
- package/dist/cjs/EntityManager/dehydratePageKeyMap.js +93 -0
- package/dist/cjs/EntityManager/encodeElement.js +40 -0
- package/dist/cjs/EntityManager/encodeGeneratedProperty.js +55 -0
- package/dist/cjs/EntityManager/findIndexToken.js +10 -0
- package/dist/cjs/EntityManager/getHashKeySpace.js +89 -0
- package/dist/cjs/EntityManager/getIndexComponents.js +23 -0
- package/dist/cjs/EntityManager/getPrimaryKey.js +76 -0
- package/dist/cjs/EntityManager/getShardBump.js +24 -0
- package/dist/cjs/EntityManager/query.js +130 -0
- package/dist/cjs/EntityManager/rehydrateIndexItem.js +65 -0
- package/dist/cjs/EntityManager/rehydratePageKeyMap.js +103 -0
- package/dist/cjs/EntityManager/removeKeys.js +51 -0
- package/dist/cjs/EntityManager/unwrapIndex.js +59 -0
- package/dist/cjs/EntityManager/updateItemHashKey.js +79 -0
- package/dist/cjs/EntityManager/updateItemRangeKey.js +65 -0
- package/dist/cjs/EntityManager/validateEntityToken.js +17 -0
- package/dist/cjs/EntityManager/validateGeneratedProperty.js +23 -0
- package/dist/cjs/EntityManager/validateIndexToken.js +16 -0
- package/dist/cjs/EntityManager/validateTranscodedProperty.js +16 -0
- package/dist/cjs/index.js +15 -0
- package/dist/default/lib/EntityManager/EntityManager.js +46 -69
- package/dist/default/lib/EntityManager/PrivateEntityManager.js +44 -51
- package/dist/index.d.ts +1163 -0
- package/dist/mjs/BaseEntityClient/BaseEntityClient.js +26 -0
- package/dist/mjs/BaseQueryBuilder/BaseQueryBuilder.js +60 -0
- package/dist/mjs/EntityManager/EntityManager.js +129 -0
- package/dist/mjs/EntityManager/ParsedConfig.js +383 -0
- package/dist/mjs/EntityManager/addKeys.js +58 -0
- package/dist/mjs/EntityManager/createEntityManager.js +44 -0
- package/dist/mjs/EntityManager/decodeElement.js +44 -0
- package/dist/mjs/EntityManager/decodeGeneratedProperty.js +51 -0
- package/dist/mjs/EntityManager/dehydrateIndexItem.js +66 -0
- package/dist/mjs/EntityManager/dehydratePageKeyMap.js +91 -0
- package/dist/mjs/EntityManager/encodeElement.js +38 -0
- package/dist/mjs/EntityManager/encodeGeneratedProperty.js +53 -0
- package/dist/mjs/EntityManager/findIndexToken.js +8 -0
- package/dist/mjs/EntityManager/getHashKeySpace.js +87 -0
- package/dist/mjs/EntityManager/getIndexComponents.js +21 -0
- package/dist/mjs/EntityManager/getPrimaryKey.js +74 -0
- package/dist/mjs/EntityManager/getShardBump.js +22 -0
- package/dist/mjs/EntityManager/query.js +128 -0
- package/dist/mjs/EntityManager/rehydrateIndexItem.js +63 -0
- package/dist/mjs/EntityManager/rehydratePageKeyMap.js +101 -0
- package/dist/mjs/EntityManager/removeKeys.js +49 -0
- package/dist/mjs/EntityManager/unwrapIndex.js +57 -0
- package/dist/mjs/EntityManager/updateItemHashKey.js +77 -0
- package/dist/mjs/EntityManager/updateItemRangeKey.js +63 -0
- package/dist/mjs/EntityManager/validateEntityToken.js +15 -0
- package/dist/mjs/EntityManager/validateGeneratedProperty.js +21 -0
- package/dist/mjs/EntityManager/validateIndexToken.js +14 -0
- package/dist/mjs/EntityManager/validateTranscodedProperty.js +14 -0
- package/dist/mjs/index.js +5 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +99 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1648 -0
- package/docs/classes/index.BaseEntityClient.html +124 -0
- package/docs/classes/index.BaseQueryBuilder.html +229 -0
- package/docs/classes/index.EntityManager.html +510 -0
- package/docs/documents/CHANGELOG.html +1432 -0
- package/docs/documents/guides_stan-assistant-guide.html +431 -0
- package/docs/functions/index.createEntityManager.html +60 -0
- package/docs/hierarchy.html +31 -0
- package/docs/index.html +212 -0
- package/docs/interfaces/index.BaseConfigMap.html +113 -0
- package/docs/interfaces/index.BaseEntityClientOptions.html +82 -0
- package/docs/interfaces/index.BaseQueryBuilderOptions.html +92 -0
- package/docs/interfaces/index.CapturedConfigMapFrom.html +126 -0
- package/docs/interfaces/index.ConfigInput.html +189 -0
- package/docs/interfaces/index.ParsedConfig.html +144 -0
- package/docs/interfaces/index.ParsedEntityConfig.html +91 -0
- package/docs/interfaces/index.ParsedGeneratedPropertiesConfig.html +67 -0
- package/docs/interfaces/index.ParsedIndexConfig.html +79 -0
- package/docs/interfaces/index.ParsedTranscoder.html +71 -0
- package/docs/interfaces/index.QueryOptions.html +180 -0
- package/docs/interfaces/index.QueryResult.html +91 -0
- package/docs/interfaces/index.ShardBump.html +79 -0
- package/docs/interfaces/index.ShardQueryResult.html +93 -0
- package/docs/modules/index.html +47 -0
- package/docs/modules.html +38 -0
- package/docs/sitemap.xml +263 -0
- package/docs/types/index.BaseKeyTokens.html +39 -0
- package/docs/types/index.Config.html +75 -0
- package/docs/types/index.ConfigMap.html +41 -0
- package/docs/types/index.ConfigOfClient.html +45 -0
- package/docs/types/index.EntitiesFromSchema.html +39 -0
- package/docs/types/index.EntityClientItemByToken.html +45 -0
- package/docs/types/index.EntityClientRecordByToken.html +43 -0
- package/docs/types/index.EntityItem.html +41 -0
- package/docs/types/index.EntityItemPartial.html +44 -0
- package/docs/types/index.EntityKey.html +40 -0
- package/docs/types/index.EntityOfToken.html +39 -0
- package/docs/types/index.EntityRecord.html +39 -0
- package/docs/types/index.EntityRecordPartial.html +40 -0
- package/docs/types/index.EntityToken.html +40 -0
- package/docs/types/index.FallbackIndexTokenSet.html +40 -0
- package/docs/types/index.HasIndexFor.html +43 -0
- package/docs/types/index.HashKeyFrom.html +38 -0
- package/docs/types/index.IndexComponentTokens.html +45 -0
- package/docs/types/index.IndexHashKeyOf.html +42 -0
- package/docs/types/index.IndexRangeKeyOf.html +41 -0
- package/docs/types/index.IndexTokensFrom.html +41 -0
- package/docs/types/index.IndexTokensOf.html +40 -0
- package/docs/types/index.KeysFrom.html +38 -0
- package/docs/types/index.PageKey.html +40 -0
- package/docs/types/index.PageKeyByIndex.html +46 -0
- package/docs/types/index.PresentIndexTokenSet.html +45 -0
- package/docs/types/index.Projected.html +39 -0
- package/docs/types/index.QueryBuilderQueryOptions.html +45 -0
- package/docs/types/index.QueryOptionsByCC.html +49 -0
- package/docs/types/index.QueryOptionsByCF.html +47 -0
- package/docs/types/index.RangeKeyFrom.html +38 -0
- package/docs/types/index.ShardQueryFunction.html +63 -0
- package/docs/types/index.ShardQueryMap.html +56 -0
- package/docs/types/index.ShardQueryMapByCC.html +49 -0
- package/docs/types/index.ShardQueryMapByCF.html +47 -0
- package/docs/types/index.ShardedKeysFrom.html +38 -0
- package/docs/types/index.StorageItem.html +42 -0
- package/docs/types/index.StorageRecord.html +40 -0
- package/docs/types/index.TranscodedPropertiesFrom.html +38 -0
- package/docs/types/index.UnshardedKeysFrom.html +38 -0
- package/docs/types/index.ValidateConfigMap.html +40 -0
- package/lib/EntityManager/PrivateEntityManager.js +4 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1163 @@
|
|
|
1
|
+
import { EntityMap, TranscodeRegistry, ConditionalProperty, Exactify, PropertiesOfType, TranscodableProperties, FlattenEntityMap, Transcodes, MutuallyExclusive, NotNever, DefaultTranscodeRegistry, SortOrder } from '@karmaniverous/entity-tools';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
import { z as z$1, ZodType } from 'zod';
|
|
4
|
+
import { BatchProcessOptions } from '@karmaniverous/batch-process';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default type parameter for {@link ConfigMap | `ConfigMap`}.
|
|
8
|
+
*
|
|
9
|
+
* @category EntityManager
|
|
10
|
+
*/
|
|
11
|
+
interface BaseConfigMap {
|
|
12
|
+
/** Entity map type (entity token -\> entity shape). */
|
|
13
|
+
EntityMap: EntityMap;
|
|
14
|
+
/** Global hash-key property name used by the storage layer. */
|
|
15
|
+
HashKey: string;
|
|
16
|
+
/** Global range-key property name used by the storage layer. */
|
|
17
|
+
RangeKey: string;
|
|
18
|
+
/** Union of sharded generated key tokens (string-valued generated properties). */
|
|
19
|
+
ShardedKeys: string;
|
|
20
|
+
/** Union of unsharded generated key tokens (string-valued generated properties). */
|
|
21
|
+
UnshardedKeys: string;
|
|
22
|
+
/** Union of transcoded property tokens (domain properties mapped in `propertyTranscodes`). */
|
|
23
|
+
TranscodedProperties: string;
|
|
24
|
+
/** Transcode registry mapping transcode names to value types. */
|
|
25
|
+
TranscodeRegistry: TranscodeRegistry;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Defines a single time period in an entity sharding strategy.
|
|
30
|
+
*
|
|
31
|
+
* @category EntityManager
|
|
32
|
+
* @protected
|
|
33
|
+
*/
|
|
34
|
+
interface ShardBump {
|
|
35
|
+
/**
|
|
36
|
+
* The timestamp marking the beginning of the time period. Must be a non-negative integer.
|
|
37
|
+
*
|
|
38
|
+
* This value must be unique across all {@link ShardBump | `ShardBumps`} for the entity.
|
|
39
|
+
*/
|
|
40
|
+
timestamp: number;
|
|
41
|
+
/**
|
|
42
|
+
* 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.
|
|
43
|
+
*
|
|
44
|
+
* This value must be an integer between `1` and `5` inclusive.
|
|
45
|
+
*/
|
|
46
|
+
charBits: number;
|
|
47
|
+
/**
|
|
48
|
+
* The number of characters used to represent the bump's shard key.
|
|
49
|
+
*
|
|
50
|
+
* 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! *
|
|
51
|
+
* A ShardBump with `chars` of `2` and `charBits` of `3` would yield a two-character shard key with a space of 16 shards.
|
|
52
|
+
*/
|
|
53
|
+
chars: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Configuration object for an {@link EntityManager | `EntityManager`} instance.
|
|
58
|
+
*
|
|
59
|
+
* @typeParam C - {@link ConfigMap | `ConfigMap`} that defines the configuration's {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
60
|
+
*
|
|
61
|
+
* @category EntityManager
|
|
62
|
+
*/
|
|
63
|
+
type Config<C extends BaseConfigMap = BaseConfigMap> = ConditionalProperty<'entities', keyof Exactify<C['EntityMap']>, {
|
|
64
|
+
[E in keyof Exactify<C['EntityMap']>]: {
|
|
65
|
+
/** Default max number of items returned by {@link EntityManager.query | `query`} for this entity (across all shards). */
|
|
66
|
+
defaultLimit?: number;
|
|
67
|
+
/** Default per-shard page size used by {@link EntityManager.query | `query`} for this entity. */
|
|
68
|
+
defaultPageSize?: number;
|
|
69
|
+
/** Shard bump schedule for this entity (time-based sharding scale-up). */
|
|
70
|
+
shardBumps?: ShardBump[];
|
|
71
|
+
/** Property token whose value selects the active shard bump (must be a transcoded numeric property). */
|
|
72
|
+
timestampProperty: Extract<Extract<C['TranscodedProperties'], PropertiesOfType<C['EntityMap'][E], number>>, TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>>;
|
|
73
|
+
/** Property token used as the logical unique identifier for this entity (must be a transcoded scalar). */
|
|
74
|
+
uniqueProperty: Extract<Extract<C['TranscodedProperties'], keyof C['EntityMap'][E]>, TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>>;
|
|
75
|
+
};
|
|
76
|
+
}> & ConditionalProperty<'generatedProperties', C['ShardedKeys'] | C['UnshardedKeys'], ConditionalProperty<'sharded', C['ShardedKeys'], Record<C['ShardedKeys'], (C['TranscodedProperties'] & TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>)[]>> & ConditionalProperty<'unsharded', C['UnshardedKeys'], Record<C['UnshardedKeys'], (C['TranscodedProperties'] & TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>)[]>>> & ConditionalProperty<'propertyTranscodes', C['TranscodedProperties'] & TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>, {
|
|
77
|
+
[P in C['TranscodedProperties'] & TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>]: PropertiesOfType<C['TranscodeRegistry'], FlattenEntityMap<C['EntityMap']>[P]>;
|
|
78
|
+
}> & ConditionalProperty<'transcodes', keyof C['TranscodeRegistry'], Transcodes<C['TranscodeRegistry']>> & {
|
|
79
|
+
/** Delimiter between generated key elements (default `|`). Must not collide with other delimiters. */
|
|
80
|
+
generatedKeyDelimiter?: string;
|
|
81
|
+
/** Delimiter between a generated property key and its encoded value (default `#`). Must not collide with other delimiters. */
|
|
82
|
+
generatedValueDelimiter?: string;
|
|
83
|
+
/** Global hash key property name. */
|
|
84
|
+
hashKey: C['HashKey'];
|
|
85
|
+
/**
|
|
86
|
+
* Index token map. Keys are index names; values define the index hash/range key tokens and optional projections.
|
|
87
|
+
*
|
|
88
|
+
* @remarks
|
|
89
|
+
* This is provider-agnostic metadata used for page-key narrowing and (de)hydration; provider adapters map this to platform-specific indexes.
|
|
90
|
+
*/
|
|
91
|
+
indexes?: Record<string, {
|
|
92
|
+
/** Index hash key token (global hash key or a sharded generated key token). */
|
|
93
|
+
hashKey: C['HashKey'] | C['ShardedKeys'];
|
|
94
|
+
/** Index range key token (global range key, an unsharded generated key token, or a transcoded scalar property token). */
|
|
95
|
+
rangeKey: C['RangeKey'] | C['UnshardedKeys'] | (C['TranscodedProperties'] & TranscodableProperties<C['EntityMap'], C['TranscodeRegistry']>);
|
|
96
|
+
/** Optional list of projected attribute names (validated to exclude key tokens). */
|
|
97
|
+
projections?: string[];
|
|
98
|
+
}>;
|
|
99
|
+
/** Global range key property name. */
|
|
100
|
+
rangeKey: C['RangeKey'];
|
|
101
|
+
/** Delimiter between entity token and shard suffix in the global hash key value (default `!`). */
|
|
102
|
+
shardKeyDelimiter?: string;
|
|
103
|
+
/** Maximum number of shard queries to execute concurrently during {@link EntityManager.query | `query`}. */
|
|
104
|
+
throttle?: number;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Validates a type derived from {@link BaseConfigMap | `BaseConfigMap`} to ensure HashKey and RangeKey are both defined and that all sets of special keys are mutually exclusive.
|
|
109
|
+
*
|
|
110
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
111
|
+
*
|
|
112
|
+
* @category EntityManager
|
|
113
|
+
* @protected
|
|
114
|
+
*/
|
|
115
|
+
type ValidateConfigMap<CC extends BaseConfigMap> = MutuallyExclusive<[
|
|
116
|
+
CC['HashKey'],
|
|
117
|
+
CC['RangeKey'],
|
|
118
|
+
CC['ShardedKeys'],
|
|
119
|
+
CC['UnshardedKeys'],
|
|
120
|
+
keyof FlattenEntityMap<CC['EntityMap']>
|
|
121
|
+
]> extends true ? NotNever<CC, ['HashKey' | 'RangeKey']> extends true ? CC : Exclude<NotNever<CC, ['HashKey' | 'RangeKey']>, true> : Exclude<MutuallyExclusive<[
|
|
122
|
+
CC['HashKey'],
|
|
123
|
+
CC['RangeKey'],
|
|
124
|
+
CC['ShardedKeys'],
|
|
125
|
+
CC['UnshardedKeys'],
|
|
126
|
+
keyof FlattenEntityMap<CC['EntityMap']>
|
|
127
|
+
]>, true>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Generates & validates the map defining defines an {@link EntityManager | `EntityManager`} configuration's {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}.
|
|
131
|
+
*
|
|
132
|
+
* Unspecified properties will default to those defined in {@link BaseConfigMap | `BaseConfigMap`}.
|
|
133
|
+
*
|
|
134
|
+
* @typeParam M - {@link BaseConfigMap | `BaseConfigMap`} extension. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
135
|
+
*
|
|
136
|
+
* @category EntityManager
|
|
137
|
+
*/
|
|
138
|
+
type ConfigMap<M extends Partial<BaseConfigMap> = Partial<BaseConfigMap>> = ValidateConfigMap<{
|
|
139
|
+
/** Entity map type (entity token -\> entity shape). */
|
|
140
|
+
EntityMap: 'EntityMap' extends keyof M ? NonNullable<M['EntityMap']> : Record<string, never>;
|
|
141
|
+
/** Global hash key property name (defaults to `"hashKey"`). */
|
|
142
|
+
HashKey: 'HashKey' extends keyof M ? NonNullable<M['HashKey']> : 'hashKey';
|
|
143
|
+
/** Global range key property name (defaults to `"rangeKey"`). */
|
|
144
|
+
RangeKey: 'RangeKey' extends keyof M ? NonNullable<M['RangeKey']> : 'rangeKey';
|
|
145
|
+
/** Union of sharded generated key tokens (defaults to `never`). */
|
|
146
|
+
ShardedKeys: 'ShardedKeys' extends keyof M ? NonNullable<M['ShardedKeys']> : never;
|
|
147
|
+
/** Union of unsharded generated key tokens (defaults to `never`). */
|
|
148
|
+
UnshardedKeys: 'UnshardedKeys' extends keyof M ? NonNullable<M['UnshardedKeys']> : never;
|
|
149
|
+
/** Union of transcoded property tokens (defaults to `never`). */
|
|
150
|
+
TranscodedProperties: 'TranscodedProperties' extends keyof M ? NonNullable<M['TranscodedProperties']> : never;
|
|
151
|
+
/** Transcode registry type (defaults to {@link DefaultTranscodeRegistry | `DefaultTranscodeRegistry`}). */
|
|
152
|
+
TranscodeRegistry: 'TranscodeRegistry' extends keyof M ? NonNullable<M['TranscodeRegistry']> : DefaultTranscodeRegistry;
|
|
153
|
+
}>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Database-facing record key type from a {@link BaseConfigMap | `ConfigMap`} with required hash & range keys.
|
|
157
|
+
*
|
|
158
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
159
|
+
*
|
|
160
|
+
* @category EntityClient
|
|
161
|
+
* @protected
|
|
162
|
+
*/
|
|
163
|
+
type EntityKey<CC extends BaseConfigMap> = Record<CC['HashKey'] | CC['RangeKey'], string>;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Extracts entity tokens from a {@link ConfigMap | `ConfigMap`}.
|
|
167
|
+
*
|
|
168
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
169
|
+
*
|
|
170
|
+
* @category EntityManager
|
|
171
|
+
* @protected
|
|
172
|
+
*/
|
|
173
|
+
type EntityToken<CC extends BaseConfigMap> = Extract<keyof Exactify<CC['EntityMap']>, string>;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Storage-facing partial item type from a {@link BaseConfigMap | `ConfigMap`}.
|
|
177
|
+
*
|
|
178
|
+
* Token-agnostic shape used by encoding/decoding, key updates, and
|
|
179
|
+
* (de)hydration services.
|
|
180
|
+
*
|
|
181
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
182
|
+
*
|
|
183
|
+
* @category EntityManager
|
|
184
|
+
*/
|
|
185
|
+
type StorageItem<CC extends BaseConfigMap> = Partial<FlattenEntityMap<CC['EntityMap']> & Record<CC['HashKey'] | CC['RangeKey'] | CC['ShardedKeys'] | CC['UnshardedKeys'], string>> & Record<string, unknown>;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A partial {@link StorageItem | `StorageItem`} restricted to keys defined in `C`.
|
|
189
|
+
*
|
|
190
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
191
|
+
*
|
|
192
|
+
* @category QueryBuilder
|
|
193
|
+
* @protected
|
|
194
|
+
*/
|
|
195
|
+
type PageKey<CC extends BaseConfigMap> = Pick<StorageItem<CC>, CC['HashKey'] | CC['RangeKey'] | CC['ShardedKeys'] | CC['UnshardedKeys'] | CC['TranscodedProperties']>;
|
|
196
|
+
/**
|
|
197
|
+
* Internal helpers to safely derive index component tokens for an index IT.
|
|
198
|
+
*
|
|
199
|
+
* These helpers avoid direct generic indexing into `CF['indexes'][IT]` which can
|
|
200
|
+
* trigger TS2536. They guard presence and key membership before extracting
|
|
201
|
+
* literal types when available.
|
|
202
|
+
*/
|
|
203
|
+
type IndexHashKeyOf<CF, IT extends string> = CF extends {
|
|
204
|
+
/** Optional values-first index map used for literal narrowing. */
|
|
205
|
+
indexes?: infer I;
|
|
206
|
+
} ? I extends Record<string, unknown> ? IT extends keyof I ? I[IT] extends {
|
|
207
|
+
/** Index hash key token. */
|
|
208
|
+
hashKey: infer HK;
|
|
209
|
+
} ? HK & string : never : never : never : never;
|
|
210
|
+
/**
|
|
211
|
+
* Derive the index range-key token for a specific index token.
|
|
212
|
+
*
|
|
213
|
+
* When CF carries an `indexes` object and IT is a member key, this extracts the
|
|
214
|
+
* concrete `rangeKey` token type.
|
|
215
|
+
*/
|
|
216
|
+
type IndexRangeKeyOf<CF, IT extends string> = CF extends {
|
|
217
|
+
/** Optional values-first index map used for literal narrowing. */
|
|
218
|
+
indexes?: infer I;
|
|
219
|
+
} ? I extends Record<string, unknown> ? IT extends keyof I ? I[IT] extends {
|
|
220
|
+
/** Index range key token. */
|
|
221
|
+
rangeKey: infer RK;
|
|
222
|
+
} ? RK & string : never : never : never : never;
|
|
223
|
+
/**
|
|
224
|
+
* Derive the union of index token names from a values-first config literal.
|
|
225
|
+
*
|
|
226
|
+
* When CF carries an `indexes` object with preserved literal keys (prefer `as const`),
|
|
227
|
+
* this helper captures the index token union. Falls back to `string` if absent.
|
|
228
|
+
*/
|
|
229
|
+
type IndexTokensOf<CF> = CF extends {
|
|
230
|
+
/** Optional values-first index map used for index-token narrowing. */
|
|
231
|
+
indexes?: infer I;
|
|
232
|
+
} ? I extends Record<string, unknown> ? Extract<keyof I, string> : string : string;
|
|
233
|
+
/**
|
|
234
|
+
* Test whether a values-first config literal CF carries a concrete index IT.
|
|
235
|
+
*
|
|
236
|
+
* @remarks
|
|
237
|
+
* Used to decide whether index-aware narrowing is available.
|
|
238
|
+
*/
|
|
239
|
+
type HasIndexFor<CF, IT extends string> = CF extends {
|
|
240
|
+
/** Optional values-first index map used for literal narrowing. */
|
|
241
|
+
indexes?: infer I;
|
|
242
|
+
} ? I extends Record<string, unknown> ? IT extends keyof I ? true : false : false : false;
|
|
243
|
+
/**
|
|
244
|
+
* Base index component tokens shared by all indexes
|
|
245
|
+
* (the global hashKey and rangeKey defined in the Config).
|
|
246
|
+
*
|
|
247
|
+
* @category QueryBuilder
|
|
248
|
+
*/
|
|
249
|
+
type BaseKeyTokens<CC extends BaseConfigMap> = CC['HashKey'] | CC['RangeKey'];
|
|
250
|
+
/**
|
|
251
|
+
* Key set for index component tokens when CF/IT identify a concrete index.
|
|
252
|
+
* - Always includes base key tokens (global hash/range).
|
|
253
|
+
* - Conditionally includes index hashKey/rangeKey when they do not collapse
|
|
254
|
+
* to the base key union.
|
|
255
|
+
*
|
|
256
|
+
* @category QueryBuilder
|
|
257
|
+
*/
|
|
258
|
+
type PresentIndexTokenSet<CC extends BaseConfigMap, CF, IT extends string> = Record<BaseKeyTokens<CC>, true> & {
|
|
259
|
+
[K in IndexHashKeyOf<CF, IT> as K extends BaseKeyTokens<CC> ? never : K]: true;
|
|
260
|
+
} & {
|
|
261
|
+
[K in IndexRangeKeyOf<CF, IT> as K extends BaseKeyTokens<CC> ? never : K]: true;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Key set for index component tokens when CF does not carry an `indexes` map
|
|
265
|
+
* or IT is unknown. Includes global keys, generated keys, and transcodable
|
|
266
|
+
* properties.
|
|
267
|
+
*
|
|
268
|
+
* @category QueryBuilder
|
|
269
|
+
*/
|
|
270
|
+
type FallbackIndexTokenSet<CC extends BaseConfigMap> = Record<CC['HashKey'] | CC['RangeKey'] | CC['ShardedKeys'] | CC['UnshardedKeys'] | CC['TranscodedProperties'], true>;
|
|
271
|
+
/**
|
|
272
|
+
* Derive the union of token names that may appear in a page key for a specific index.
|
|
273
|
+
*
|
|
274
|
+
* @remarks
|
|
275
|
+
* If CF carries a concrete `indexes` map and IT is a member key, this narrows to the
|
|
276
|
+
* exact component-token set for that index. Otherwise, it falls back to the broad key set.
|
|
277
|
+
*/
|
|
278
|
+
type IndexComponentTokens<CC extends BaseConfigMap, CF, IT extends string> = HasIndexFor<CF, IT> extends true ? keyof PresentIndexTokenSet<CC, CF, IT> : keyof FallbackIndexTokenSet<CC>;
|
|
279
|
+
/**
|
|
280
|
+
* Page key typed for a specific index token.
|
|
281
|
+
*
|
|
282
|
+
* - With CF (values-first config literal) present and carrying `indexes`, the
|
|
283
|
+
* shape narrows to exactly the component tokens of IT.
|
|
284
|
+
* - Without CF, falls back to the broad PageKey<CC> shape.
|
|
285
|
+
*/
|
|
286
|
+
type PageKeyByIndex<CC extends BaseConfigMap, ET extends EntityToken<CC>, IT extends string = string, CF = unknown> = Pick<StorageItem<CC>, IndexComponentTokens<CC, CF, IT>>;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Runtime configuration schema for {@link EntityManager | `EntityManager`}.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* This is authoritative at runtime and used internally by the {@link EntityManager | `EntityManager`} constructor.
|
|
293
|
+
* It is also used by tests. It is not intended as a user-facing TypeDoc artifact.
|
|
294
|
+
*
|
|
295
|
+
* @hidden
|
|
296
|
+
*/
|
|
297
|
+
declare const configSchema: z$1.ZodObject<{
|
|
298
|
+
entities: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodObject<{
|
|
299
|
+
defaultLimit: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
|
|
300
|
+
defaultPageSize: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
|
|
301
|
+
shardBumps: z$1.ZodPipe<z$1.ZodDefault<z$1.ZodOptional<z$1.ZodArray<z$1.ZodObject<{
|
|
302
|
+
timestamp: z$1.ZodNumber;
|
|
303
|
+
charBits: z$1.ZodNumber;
|
|
304
|
+
chars: z$1.ZodNumber;
|
|
305
|
+
}, z$1.core.$strict>>>>, z$1.ZodTransform<{
|
|
306
|
+
timestamp: number;
|
|
307
|
+
charBits: number;
|
|
308
|
+
chars: number;
|
|
309
|
+
}[], {
|
|
310
|
+
timestamp: number;
|
|
311
|
+
charBits: number;
|
|
312
|
+
chars: number;
|
|
313
|
+
}[]>>;
|
|
314
|
+
timestampProperty: z$1.ZodString;
|
|
315
|
+
uniqueProperty: z$1.ZodString;
|
|
316
|
+
}, z$1.core.$strict>>>>;
|
|
317
|
+
generatedProperties: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodObject<{
|
|
318
|
+
sharded: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodArray<z$1.ZodString>>>>;
|
|
319
|
+
unsharded: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodArray<z$1.ZodString>>>>;
|
|
320
|
+
}, z$1.core.$strip>>>;
|
|
321
|
+
hashKey: z$1.ZodString;
|
|
322
|
+
indexes: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodObject<{
|
|
323
|
+
hashKey: z$1.ZodString;
|
|
324
|
+
rangeKey: z$1.ZodString;
|
|
325
|
+
projections: z$1.ZodOptional<z$1.ZodArray<z$1.ZodString>>;
|
|
326
|
+
}, z$1.core.$strip>>>>;
|
|
327
|
+
generatedKeyDelimiter: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodString>>;
|
|
328
|
+
generatedValueDelimiter: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodString>>;
|
|
329
|
+
propertyTranscodes: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodString>>>;
|
|
330
|
+
rangeKey: z$1.ZodString;
|
|
331
|
+
shardKeyDelimiter: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodString>>;
|
|
332
|
+
throttle: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodNumber>>;
|
|
333
|
+
transcodes: z$1.ZodDefault<z$1.ZodOptional<z$1.ZodRecord<z$1.ZodString, z$1.ZodObject<{
|
|
334
|
+
encode: z$1.ZodCustom<unknown, unknown>;
|
|
335
|
+
decode: z$1.ZodCustom<unknown, unknown>;
|
|
336
|
+
}, z$1.core.$strict>>>>;
|
|
337
|
+
}, z$1.core.$strict>;
|
|
338
|
+
/**
|
|
339
|
+
* Parsed transcoder entry.
|
|
340
|
+
*
|
|
341
|
+
* @remarks
|
|
342
|
+
* This reflects the runtime contract enforced by Zod for entries in `transcodes`.
|
|
343
|
+
*/
|
|
344
|
+
interface ParsedTranscoder {
|
|
345
|
+
/** Encode a value to a lexicographically sortable string. */
|
|
346
|
+
encode: unknown;
|
|
347
|
+
/** Decode a previously encoded string back to the value type. */
|
|
348
|
+
decode: unknown;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Parsed index definition (provider-agnostic).
|
|
352
|
+
*
|
|
353
|
+
* @remarks
|
|
354
|
+
* Provider adapters map these tokens to provider-specific index queries.
|
|
355
|
+
*/
|
|
356
|
+
interface ParsedIndexConfig {
|
|
357
|
+
/** Index hash key token (global hash key or a sharded generated key). */
|
|
358
|
+
hashKey: string;
|
|
359
|
+
/** Index range key token (global range key, an unsharded generated key, or a transcoded scalar). */
|
|
360
|
+
rangeKey: string;
|
|
361
|
+
/** Optional list of projected attribute names (validated to exclude key tokens). */
|
|
362
|
+
projections?: string[] | undefined;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Parsed generated properties configuration.
|
|
366
|
+
*/
|
|
367
|
+
interface ParsedGeneratedPropertiesConfig {
|
|
368
|
+
/** Sharded generated property tokens (hash-side); atomic encoding semantics. */
|
|
369
|
+
sharded: Record<string, string[]>;
|
|
370
|
+
/** Unsharded generated property tokens (range-side); non-atomic encoding semantics. */
|
|
371
|
+
unsharded: Record<string, string[]>;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Parsed per-entity configuration.
|
|
375
|
+
*/
|
|
376
|
+
interface ParsedEntityConfig {
|
|
377
|
+
/** Default max items returned by EntityManager.query for this entity (across all shards). */
|
|
378
|
+
defaultLimit: number;
|
|
379
|
+
/** Default per-shard page size used by EntityManager.query for this entity. */
|
|
380
|
+
defaultPageSize: number;
|
|
381
|
+
/** Shard bump schedule for this entity (time-based sharding scale-up). */
|
|
382
|
+
shardBumps: ShardBump[];
|
|
383
|
+
/** Property token whose value selects the shard bump (typically a timestamp). */
|
|
384
|
+
timestampProperty: string;
|
|
385
|
+
/** Property token used to dedupe and build the global range key. */
|
|
386
|
+
uniqueProperty: string;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Simplified runtime configuration shape after parsing/validation.
|
|
390
|
+
*
|
|
391
|
+
* @remarks
|
|
392
|
+
* This is the type exposed by {@link EntityManager.config | `EntityManager.config`}.
|
|
393
|
+
* It mirrors the validated Zod schema output.
|
|
394
|
+
*
|
|
395
|
+
* @category EntityManager
|
|
396
|
+
*/
|
|
397
|
+
interface ParsedConfig {
|
|
398
|
+
/** Entity definitions keyed by entity token. */
|
|
399
|
+
entities: Record<string, ParsedEntityConfig>;
|
|
400
|
+
/** Generated property token maps. */
|
|
401
|
+
generatedProperties: ParsedGeneratedPropertiesConfig;
|
|
402
|
+
/** Global hash key property name. */
|
|
403
|
+
hashKey: string;
|
|
404
|
+
/** Provider-agnostic index definitions keyed by index token. */
|
|
405
|
+
indexes: Record<string, ParsedIndexConfig>;
|
|
406
|
+
/** Delimiter between generated key elements (default `|`). */
|
|
407
|
+
generatedKeyDelimiter: string;
|
|
408
|
+
/** Delimiter between generated element name and value (default `#`). */
|
|
409
|
+
generatedValueDelimiter: string;
|
|
410
|
+
/** Map of transcoded property token -\> transcode name. */
|
|
411
|
+
propertyTranscodes: Record<string, string>;
|
|
412
|
+
/** Global range key property name. */
|
|
413
|
+
rangeKey: string;
|
|
414
|
+
/** Delimiter between entity token and shard suffix in hash key values (default `!`). */
|
|
415
|
+
shardKeyDelimiter: string;
|
|
416
|
+
/** Default max concurrency for shard queries during EntityManager.query. */
|
|
417
|
+
throttle: number;
|
|
418
|
+
/** Transcoder registry used for encoding/decoding values. */
|
|
419
|
+
transcodes: Record<string, ParsedTranscoder>;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** EntityOfToken — resolves the concrete entity shape for a specific entity token. */
|
|
423
|
+
type EntityOfToken<CC extends BaseConfigMap, ET extends EntityToken<CC>> = Exactify<CC['EntityMap']>[ET];
|
|
424
|
+
/**
|
|
425
|
+
* EntityItem — domain-facing item narrowed to a specific entity token, plus
|
|
426
|
+
* optional key/token properties. Required fields per captured entitiesSchema
|
|
427
|
+
* (when present); no string index signature.
|
|
428
|
+
*/
|
|
429
|
+
type EntityItem<CC extends BaseConfigMap, ET extends EntityToken<CC>> = EntityOfToken<CC, ET> & Partial<Record<CC['HashKey'] | CC['RangeKey'] | CC['ShardedKeys'] | CC['UnshardedKeys'], string>>;
|
|
430
|
+
/**
|
|
431
|
+
* Normalize literals: string | readonly string[] -\> union of strings.
|
|
432
|
+
*/
|
|
433
|
+
type KeysFrom<K> = K extends readonly (infer E)[] ? Extract<E, string> : K extends string ? K : never;
|
|
434
|
+
/**
|
|
435
|
+
* Project item shape by keys; if K is never/unknown, fall back to T.
|
|
436
|
+
*/
|
|
437
|
+
type Projected<T, K> = [KeysFrom<K>] extends [never] ? T : T extends object ? Pick<T, Extract<KeysFrom<K>, keyof Exactify<T>>> : T;
|
|
438
|
+
/** EntityRecord — DB-facing record (keys required), narrowed to a specific entity token. */
|
|
439
|
+
type EntityRecord<CC extends BaseConfigMap, ET extends EntityToken<CC>> = Partial<EntityItem<CC, ET>> & EntityKey<CC>;
|
|
440
|
+
/** EntityItemPartial — projected/seed domain shape by token.
|
|
441
|
+
* - If K provided: required projected keys (`Projected<EntityItem<CC, ET>, K>`).
|
|
442
|
+
* - If K omitted: permissive seed (`Partial<EntityItem<CC, ET>>`).
|
|
443
|
+
*/
|
|
444
|
+
type EntityItemPartial<CC extends BaseConfigMap, ET extends EntityToken<CC>, K = unknown> = [KeysFrom<K>] extends [never] ? Partial<EntityItem<CC, ET>> : Projected<EntityItem<CC, ET>, K>;
|
|
445
|
+
/** EntityRecordPartial — projected DB record shape by token. */
|
|
446
|
+
type EntityRecordPartial<CC extends BaseConfigMap, ET extends EntityToken<CC>, K = unknown> = Projected<EntityRecord<CC, ET>, K>;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* A result returned by a {@link ShardQueryFunction | `ShardQueryFunction`} querying an individual shard.
|
|
450
|
+
*
|
|
451
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`}.
|
|
452
|
+
* @typeParam ET - Entity token narrowing the item type.
|
|
453
|
+
* @typeParam IT - Index token (for page key typing).
|
|
454
|
+
* @typeParam CF - Optional values-first config literal type for narrowing.
|
|
455
|
+
* @typeParam K - Optional projection keys; narrows item shape when provided.
|
|
456
|
+
*
|
|
457
|
+
* @category EntityManager
|
|
458
|
+
* @protected
|
|
459
|
+
*/
|
|
460
|
+
interface ShardQueryResult<CC extends BaseConfigMap, ET extends EntityToken<CC>, IT extends string, CF = unknown, K = unknown> {
|
|
461
|
+
/** The number of records returned. */
|
|
462
|
+
count: number;
|
|
463
|
+
/** The returned records. */
|
|
464
|
+
items: EntityItemPartial<CC, ET, K>[];
|
|
465
|
+
/** The page key for the next query on this shard. */
|
|
466
|
+
pageKey?: PageKeyByIndex<CC, ET, IT, CF>;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* A query function that returns a single page of results from an individual shard.
|
|
471
|
+
*
|
|
472
|
+
* This function will typically be composed dynamically to express a specific query index & logic. The arguments to this function will be provided by the {@link EntityManager.query | `EntityManager.query`} method, which assembles many returned pages queried across multiple shards into a single query result.
|
|
473
|
+
*
|
|
474
|
+
* @param hashKey - The hash key value of the shard being queried.
|
|
475
|
+
* @param pageKey - The typed page key for the index being queried.
|
|
476
|
+
* @param pageSize - The maximum number of items to return from this query.
|
|
477
|
+
*
|
|
478
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`}.
|
|
479
|
+
* @typeParam ET - Entity token narrowing the item/record types.
|
|
480
|
+
* @typeParam IT - Index token (inferred from shardQueryMap keys).
|
|
481
|
+
* @typeParam CF - Optional values-first config literal type for narrowing.
|
|
482
|
+
* @typeParam K - Optional projection keys; narrows item shape when provided.
|
|
483
|
+
*
|
|
484
|
+
* @category EntityManager
|
|
485
|
+
* @protected
|
|
486
|
+
*/
|
|
487
|
+
type ShardQueryFunction<CC extends BaseConfigMap, ET extends EntityToken<CC>, IT extends string, CF = unknown, K = unknown> = CF extends {
|
|
488
|
+
/** Optional values-first index map used for index-token narrowing. */
|
|
489
|
+
indexes?: infer I;
|
|
490
|
+
} ? I extends Record<string, unknown> ? IT extends Extract<keyof I, string> ? (hashKey: string, pageKey?: PageKeyByIndex<CC, ET, IT, CF>, pageSize?: number) => Promise<ShardQueryResult<CC, ET, IT, CF, K>> : never : (hashKey: string, pageKey?: PageKeyByIndex<CC, ET, IT, CF>, pageSize?: number) => Promise<ShardQueryResult<CC, ET, IT, CF, K>> : (hashKey: string, pageKey?: PageKeyByIndex<CC, ET, IT, CF>, pageSize?: number) => Promise<ShardQueryResult<CC, ET, IT, CF, K>>;
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Relates a specific index token to a {@link ShardQueryFunction | `ShardQueryFunction`} to be performed on that index.
|
|
494
|
+
*
|
|
495
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`}.
|
|
496
|
+
* @typeParam ET - Entity token narrowing the function item types.
|
|
497
|
+
* @typeParam ITS - Index token subset (inferred from object keys).
|
|
498
|
+
* @typeParam CF - Optional values-first config literal type for narrowing. When
|
|
499
|
+
* provided and it carries an `indexes` object with preserved
|
|
500
|
+
* literal keys (prefer `as const` at call sites), the map keys
|
|
501
|
+
* are constrained to that set. Excess keys are rejected by
|
|
502
|
+
* excess property checks on object literals.
|
|
503
|
+
* @typeParam K - Optional projection keys; narrows item shape when provided.
|
|
504
|
+
*
|
|
505
|
+
* @category EntityManager
|
|
506
|
+
* @protected
|
|
507
|
+
*/
|
|
508
|
+
type ShardQueryMap<CC extends BaseConfigMap, ET extends EntityToken<CC>, ITS extends string, CF = unknown, K = unknown> = CF extends {
|
|
509
|
+
/** Optional values-first index map used for index-token narrowing. */
|
|
510
|
+
indexes?: infer I;
|
|
511
|
+
} ? I extends Record<string, unknown> ? Record<Extract<ITS, Extract<keyof I, string>>, ShardQueryFunction<CC, ET, Extract<ITS, Extract<keyof I, string>>, CF, K>> : Record<ITS, ShardQueryFunction<CC, ET, ITS, CF, K>> : Record<ITS, ShardQueryFunction<CC, ET, ITS, CF, K>>;
|
|
512
|
+
/**
|
|
513
|
+
* Convenience alias for ShardQueryMap that derives ITS (index token subset)
|
|
514
|
+
* from a values-first captured config CC (e.g., your config literal type).
|
|
515
|
+
*
|
|
516
|
+
* - If CC has `indexes`, ITS becomes the union of its keys.
|
|
517
|
+
* - Otherwise, ITS defaults to `string`.
|
|
518
|
+
*
|
|
519
|
+
* It also passes CC through the CF channel so per-index page-key narrowing
|
|
520
|
+
* applies consistently.
|
|
521
|
+
*
|
|
522
|
+
* This is optional DX sugar; it does not change runtime behavior.
|
|
523
|
+
*/
|
|
524
|
+
type ShardQueryMapByCC<CC extends BaseConfigMap, ET extends EntityToken<CC>, CCLit = unknown, K = unknown> = ShardQueryMap<CC, ET, IndexTokensFrom<CCLit>, CCLit, K>;
|
|
525
|
+
/**
|
|
526
|
+
* Convenience alias for ShardQueryMap that derives ITS (index token subset)
|
|
527
|
+
* directly from a values-first config literal CF when it carries `indexes`.
|
|
528
|
+
*
|
|
529
|
+
* - If CF has `indexes`, ITS becomes the union of its keys.
|
|
530
|
+
* - Otherwise, ITS defaults to `string`.
|
|
531
|
+
*
|
|
532
|
+
* This is optional DX sugar; it does not change runtime behavior.
|
|
533
|
+
*/
|
|
534
|
+
type ShardQueryMapByCF<CC extends BaseConfigMap, ET extends EntityToken<CC>, CF = unknown, K = unknown> = ShardQueryMap<CC, ET, IndexTokensOf<CF>, CF, K>;
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Options passed to the {@link EntityManager.query | `EntityManager.query`} method.
|
|
538
|
+
*
|
|
539
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
540
|
+
* @typeParam ET - Entity token narrowing the item types.
|
|
541
|
+
* @typeParam ITS - Index token subset (inferred from shardQueryMap keys).
|
|
542
|
+
* @typeParam CF - Optional values-first config literal type used for index-aware narrowing.
|
|
543
|
+
* @typeParam K - Optional projection keys; narrows item/sort shapes when provided.
|
|
544
|
+
*
|
|
545
|
+
* @category EntityManager
|
|
546
|
+
* @protected
|
|
547
|
+
*/
|
|
548
|
+
interface QueryOptions<CC extends BaseConfigMap, ET extends EntityToken<CC> = EntityToken<CC>, ITS extends string = string, CF = unknown, K = unknown> {
|
|
549
|
+
/** Identifies the entity to be queried. Key of {@link Config | `Config`} `entities`. */
|
|
550
|
+
entityToken: ET;
|
|
551
|
+
/**
|
|
552
|
+
* Partial item object sufficiently populated to generate index hash keys.
|
|
553
|
+
*/
|
|
554
|
+
item: EntityItemPartial<CC, ET>;
|
|
555
|
+
/**
|
|
556
|
+
* The target maximum number of records to be returned by the query across
|
|
557
|
+
* all shards.
|
|
558
|
+
*
|
|
559
|
+
* The actual number of records returned will be a product of {@link QueryOptions.pageSize | `pageSize`} and the
|
|
560
|
+
* number of shards queried, unless limited by available records in a given
|
|
561
|
+
* shard.
|
|
562
|
+
*/
|
|
563
|
+
limit?: number | undefined;
|
|
564
|
+
/**
|
|
565
|
+
* {@link QueryResult.pageKeyMap | `pageKeyMap`} returned by the previous iteration of this query.
|
|
566
|
+
*/
|
|
567
|
+
pageKeyMap?: string | undefined;
|
|
568
|
+
/**
|
|
569
|
+
* The maximum number of records to be returned by each individual query to a
|
|
570
|
+
* single shard (i.e. {@link ShardQueryFunction | `ShardQueryFunction`} execution).
|
|
571
|
+
*
|
|
572
|
+
* Note that, within a given {@link EntityManager.query | `query`} method execution, these queries will be
|
|
573
|
+
* repeated until either available data is exhausted or the {@link QueryOptions.limit | `limit`} value is
|
|
574
|
+
* reached.
|
|
575
|
+
*/
|
|
576
|
+
pageSize?: number | undefined;
|
|
577
|
+
/**
|
|
578
|
+
* Each key in this object is a valid entity index token. Each value is a valid
|
|
579
|
+
* {@link ShardQueryFunction | 'ShardQueryFunction'} that specifies the query of a single page of data on a
|
|
580
|
+
* single shard for the mapped index.
|
|
581
|
+
*
|
|
582
|
+
* This allows simultaneous queries on multiple sort keys to share a single
|
|
583
|
+
* page key, e.g. to match the same string against `firstName` and `lastName`
|
|
584
|
+
* properties without performing a table scan for either.
|
|
585
|
+
*/
|
|
586
|
+
shardQueryMap: ShardQueryMap<CC, ET, ITS, CF, K>;
|
|
587
|
+
/**
|
|
588
|
+
* A {@link SortOrder | `SortOrder`} object specifying the sort order of the result set. Defaults to `[]`. Aligned with the projected item shape when K is provided.
|
|
589
|
+
*/
|
|
590
|
+
sortOrder?: SortOrder<EntityItemPartial<CC, ET, K>> | undefined;
|
|
591
|
+
/**
|
|
592
|
+
* Lower limit to query shard space.
|
|
593
|
+
*
|
|
594
|
+
* Only valid if the query is constrained along the dimension used by the
|
|
595
|
+
* {@link Config | `EntityManager.config.entities.<entityToken>.sharding.timestamptokens.timestamp`}
|
|
596
|
+
* function to generate `shardKey`.
|
|
597
|
+
*
|
|
598
|
+
* @defaultValue `0`
|
|
599
|
+
*/
|
|
600
|
+
timestampFrom?: number | undefined;
|
|
601
|
+
/**
|
|
602
|
+
* Upper limit to query shard space.
|
|
603
|
+
*
|
|
604
|
+
* Only valid if the query is constrained along the dimension used by the
|
|
605
|
+
* {@link Config | `EntityManager.config.entities.<entityToken>.sharding.timestamptokens.timestamp`}
|
|
606
|
+
* function to generate `shardKey`.
|
|
607
|
+
*
|
|
608
|
+
* @defaultValue `Date.now()`
|
|
609
|
+
*/
|
|
610
|
+
timestampTo?: number | undefined;
|
|
611
|
+
/**
|
|
612
|
+
* The maximum number of shards to query in parallel. Overrides options `throttle`.
|
|
613
|
+
*
|
|
614
|
+
* @defaultValue `options.throttle`
|
|
615
|
+
*/
|
|
616
|
+
throttle?: number | undefined;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Convenience alias for QueryOptions that derives ITS (index token subset)
|
|
620
|
+
* directly from a values-first config literal CF when it carries `indexes`.
|
|
621
|
+
*
|
|
622
|
+
* - If CF has `indexes`, ITS becomes the union of its keys.
|
|
623
|
+
* - Otherwise, ITS defaults to `string`.
|
|
624
|
+
*
|
|
625
|
+
* This is optional DX sugar; it does not change runtime behavior.
|
|
626
|
+
*/
|
|
627
|
+
type QueryOptionsByCF<CC extends BaseConfigMap, ET extends EntityToken<CC> = EntityToken<CC>, CF = unknown, K = unknown> = QueryOptions<CC, ET, IndexTokensOf<CF>, CF, K>;
|
|
628
|
+
/**
|
|
629
|
+
* Convenience alias for QueryOptions that derives ITS (index token subset)
|
|
630
|
+
* from a values-first captured config CC (e.g., your config literal type).
|
|
631
|
+
*
|
|
632
|
+
* - If CC has `indexes`, ITS becomes the union of its keys.
|
|
633
|
+
* - Otherwise, ITS defaults to `string`.
|
|
634
|
+
*
|
|
635
|
+
* It also passes CC through the CF channel so page-key narrowing and other
|
|
636
|
+
* CF-aware typing applies consistently.
|
|
637
|
+
*
|
|
638
|
+
* This is optional DX sugar; it does not change runtime behavior.
|
|
639
|
+
*/
|
|
640
|
+
type QueryOptionsByCC<CCMap extends BaseConfigMap, ET extends EntityToken<CCMap> = EntityToken<CCMap>, CC = unknown, K = unknown> = QueryOptions<CCMap, ET, IndexTokensFrom<CC>, CC, K>;
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* A result returned by a query across multiple shards, where each shard may receive multiple page queries via a dynamically-generated {@link ShardQueryFunction | `ShardQueryFunction`}.
|
|
644
|
+
*
|
|
645
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`}.
|
|
646
|
+
* @typeParam ET - Entity token narrowing the result item type.
|
|
647
|
+
* @typeParam ITS - Index token subset (carried for symmetry; not represented in the shape).
|
|
648
|
+
* @typeParam K - Optional projection keys; narrows item shape when provided.
|
|
649
|
+
*
|
|
650
|
+
* @category EntityManager
|
|
651
|
+
* @protected
|
|
652
|
+
*/
|
|
653
|
+
interface QueryResult<CC extends BaseConfigMap, ET extends EntityToken<CC>, ITS extends string, K = unknown> {
|
|
654
|
+
/** Total number of records returned across all shards. */
|
|
655
|
+
count: number;
|
|
656
|
+
/** The returned records. */
|
|
657
|
+
items: EntityItemPartial<CC, ET, K>[];
|
|
658
|
+
/**
|
|
659
|
+
* A compressed, two-layer map of page keys, used to query the next page of
|
|
660
|
+
* data for a given sort key on each shard of a given hash key.
|
|
661
|
+
*/
|
|
662
|
+
pageKeyMap: string;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* The EntityManager class applies a configuration-driven sharded data model &
|
|
667
|
+
* query strategy to NoSql data.
|
|
668
|
+
*
|
|
669
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines the configuration's {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
670
|
+
* @typeParam CF - Values-first config literal type captured at construction
|
|
671
|
+
* time (phantom generic; type-only). This is used by downstream
|
|
672
|
+
* adapters to infer index-token unions (ITS) and per-index page
|
|
673
|
+
* key shapes.
|
|
674
|
+
*
|
|
675
|
+
* @remarks
|
|
676
|
+
* While the {@link EntityManager.query | `query`} method is `public`, normally it should not be called directly. The `query` method is used by a platform-specific {@link BaseQueryBuilder.query | `QueryBuilder.query`} method to provide a fluent query API.
|
|
677
|
+
*
|
|
678
|
+
* @category EntityManager
|
|
679
|
+
*/
|
|
680
|
+
declare class EntityManager<CC extends BaseConfigMap, CF = unknown> {
|
|
681
|
+
#private;
|
|
682
|
+
/** Logger object (defaults to `console`, must support `debug` & `error` methods). */
|
|
683
|
+
readonly logger: Pick<Console, 'debug' | 'error'>;
|
|
684
|
+
/**
|
|
685
|
+
* Create an EntityManager instance.
|
|
686
|
+
*
|
|
687
|
+
* @param config - EntityManager {@link Config | `Config`} object.
|
|
688
|
+
* @param logger - Logger object (defaults to `console`, must support `debug` & `error` methods).
|
|
689
|
+
*/
|
|
690
|
+
constructor(config: Config<CC>, logger?: Pick<Console, 'debug' | 'error'>);
|
|
691
|
+
/**
|
|
692
|
+
* Get the current EntityManager {@link Config | `Config`} object.
|
|
693
|
+
*
|
|
694
|
+
* @returns Current {@link Config | `Config`} object.
|
|
695
|
+
*/
|
|
696
|
+
get config(): ParsedConfig;
|
|
697
|
+
/**
|
|
698
|
+
* Set the current EntityManager {@link Config | `Config`} object.
|
|
699
|
+
*
|
|
700
|
+
* @param value - {@link Config | `Config`} object.
|
|
701
|
+
*/
|
|
702
|
+
set config(value: ParsedConfig);
|
|
703
|
+
/**
|
|
704
|
+
* Encode a generated property value. Returns a string or undefined if atomicity requirement of sharded properties not met.
|
|
705
|
+
*
|
|
706
|
+
* @param property - {@link Config | Config} `generatedProperties` key.
|
|
707
|
+
* @param item - {@link StorageItem | `StorageItem`} object.
|
|
708
|
+
*
|
|
709
|
+
* @returns Encoded generated property value.
|
|
710
|
+
*
|
|
711
|
+
* @throws `Error` if `property` is not a {@link Config | Config} `generatedProperties` key.
|
|
712
|
+
*/
|
|
713
|
+
encodeGeneratedProperty(property: CC['ShardedKeys'] | CC['UnshardedKeys'], item: StorageItem<CC>): string | undefined;
|
|
714
|
+
/**
|
|
715
|
+
* Update generated properties, hash key, and range key on an {@link EntityItem | `EntityItem`} object.
|
|
716
|
+
*
|
|
717
|
+
* @param entityToken - Entity token (narrows types by token).
|
|
718
|
+
* @param item - Single item to update with generated keys/properties.
|
|
719
|
+
* @param overwrite - Overwrite existing keys/properties (default `false`).
|
|
720
|
+
*
|
|
721
|
+
* @returns {@link EntityRecord | `EntityRecord`} object with updated properties.
|
|
722
|
+
*
|
|
723
|
+
* @throws `Error` if `entityToken` is invalid.
|
|
724
|
+
*
|
|
725
|
+
* @overload
|
|
726
|
+
*/
|
|
727
|
+
addKeys<ET extends EntityToken<CC>>(entityToken: ET, item: EntityItemPartial<CC, ET>, overwrite?: boolean): EntityRecordPartial<CC, ET>;
|
|
728
|
+
/**
|
|
729
|
+
* Update generated properties, hash key, and range key on an array of {@link EntityItem | `EntityItem`} objects.
|
|
730
|
+
*
|
|
731
|
+
* @param entityToken - Entity token (narrows types by token).
|
|
732
|
+
* @param item - Items to update with generated keys/properties.
|
|
733
|
+
* @param overwrite - Overwrite existing keys/properties (default `false`).
|
|
734
|
+
*
|
|
735
|
+
* @returns Array of {@link EntityRecord | `EntityRecord`} objects with updated properties.
|
|
736
|
+
*
|
|
737
|
+
* @throws `Error` if `entityToken` is invalid.
|
|
738
|
+
*
|
|
739
|
+
* @overload
|
|
740
|
+
*/
|
|
741
|
+
addKeys<ET extends EntityToken<CC>>(entityToken: ET, item: EntityItemPartial<CC, ET>[], overwrite?: boolean): EntityRecordPartial<CC, ET>[];
|
|
742
|
+
/**
|
|
743
|
+
* Convert one or more {@link EntityItem | `EntityItem`} objects into an array of {@link EntityKey | `EntityKey`} values.
|
|
744
|
+
*
|
|
745
|
+
* @param entityToken - Entity token (narrows types by token).
|
|
746
|
+
* @param item - Single item to derive primary keys for.
|
|
747
|
+
* @param overwrite - Overwrite existing keys on the item before deriving (default `false`).
|
|
748
|
+
*
|
|
749
|
+
* @returns Array of {@link EntityKey | `EntityKey`} values derived from `item`.
|
|
750
|
+
*
|
|
751
|
+
* @throws `Error` if `entityToken` is invalid.
|
|
752
|
+
*
|
|
753
|
+
* @overload
|
|
754
|
+
*/
|
|
755
|
+
getPrimaryKey<ET extends EntityToken<CC>>(entityToken: ET, item: EntityItemPartial<CC, ET>, overwrite?: boolean): EntityKey<CC>[];
|
|
756
|
+
/**
|
|
757
|
+
* Convert an array of {@link EntityItem | `EntityItem`} objects into {@link EntityKey | `EntityKey`} values.
|
|
758
|
+
*
|
|
759
|
+
* @param entityToken - Entity token (narrows types by token).
|
|
760
|
+
* @param items - Array of items to derive primary keys for.
|
|
761
|
+
* @param overwrite - Overwrite existing keys on each item before deriving (default `false`).
|
|
762
|
+
*
|
|
763
|
+
* @returns Array of {@link EntityKey | `EntityKey`} values derived from all `items`.
|
|
764
|
+
*
|
|
765
|
+
* @throws `Error` if `entityToken` is invalid.
|
|
766
|
+
*
|
|
767
|
+
* @overload
|
|
768
|
+
*/
|
|
769
|
+
getPrimaryKey<ET extends EntityToken<CC>>(entityToken: ET, items: EntityItemPartial<CC, ET>[], overwrite?: boolean): EntityKey<CC>[];
|
|
770
|
+
/**
|
|
771
|
+
* Strips generated properties, hash key, and range key from an {@link EntityRecord | `EntityRecord`} object.
|
|
772
|
+
*
|
|
773
|
+
* @param entityToken - {@link Config | `Config`} `entities` key.
|
|
774
|
+
* @param item - {@link EntityRecord | `EntityRecord`} object.
|
|
775
|
+
*
|
|
776
|
+
* @returns {@link EntityItem | `EntityItem`} with generated properties, hash key & range key removed.
|
|
777
|
+
*
|
|
778
|
+
* @throws `Error` if `entityToken` is invalid.
|
|
779
|
+
*
|
|
780
|
+
* Overloads:
|
|
781
|
+
*/
|
|
782
|
+
removeKeys<ET extends EntityToken<CC>>(entityToken: ET, item: EntityRecord<CC, ET>): EntityItem<CC, ET>;
|
|
783
|
+
removeKeys<ET extends EntityToken<CC>, K = unknown>(entityToken: ET, item: EntityRecordPartial<CC, ET, K>): EntityItemPartial<CC, ET, K>;
|
|
784
|
+
removeKeys<ET extends EntityToken<CC>>(entityToken: ET, items: EntityRecord<CC, ET>[]): EntityItem<CC, ET>[];
|
|
785
|
+
removeKeys<ET extends EntityToken<CC>, K = unknown>(entityToken: ET, items: EntityRecordPartial<CC, ET, K>[]): EntityItemPartial<CC, ET, K>[];
|
|
786
|
+
/**
|
|
787
|
+
* Find an index token based on the configured hash and range key tokens.
|
|
788
|
+
*
|
|
789
|
+
* @param hashKeyToken - Index hash key token (global hashKey or a sharded generated key).
|
|
790
|
+
* @param rangeKeyToken - Index range key token (global rangeKey, unsharded generated key, or a transcodable scalar).
|
|
791
|
+
* @param suppressError - When false (default), throws if no match; when true, returns undefined instead.
|
|
792
|
+
*
|
|
793
|
+
* @returns A configured index token (narrowed to the CF.indexes key union) or undefined when allowed.
|
|
794
|
+
*
|
|
795
|
+
* @throws `Error` if no match is found and `suppressError` is not `true`.
|
|
796
|
+
*/
|
|
797
|
+
findIndexToken(hashKeyToken: CC['HashKey'] | CC['ShardedKeys'], rangeKeyToken: CC['RangeKey'] | CC['UnshardedKeys'] | CC['TranscodedProperties'], suppressError?: false): IndexTokensOf<CF>;
|
|
798
|
+
findIndexToken(hashKeyToken: CC['HashKey'] | CC['ShardedKeys'], rangeKeyToken: CC['RangeKey'] | CC['UnshardedKeys'] | CC['TranscodedProperties'], suppressError: true): IndexTokensOf<CF> | undefined;
|
|
799
|
+
/**
|
|
800
|
+
* Query a database entity across shards in a provider-generic fashion.
|
|
801
|
+
*
|
|
802
|
+
* @remarks
|
|
803
|
+
* The provided `shardQueryMap` performs the actual query of individual data pages on individual index/shard combinations.
|
|
804
|
+
*
|
|
805
|
+
* Individual shard query results will be combined, deduped by {@link Config | `Config`} `uniqueProperty` value, and sorted by {@link QueryOptions.sortOrder | `sortOrder`}.
|
|
806
|
+
*
|
|
807
|
+
* In queries on sharded data, expect the leading and trailing edges of returned data pages to interleave somewhat with preceding & following pages.
|
|
808
|
+
*
|
|
809
|
+
* Unsharded query results should sort & page as expected.
|
|
810
|
+
*
|
|
811
|
+
* **Normally this method should not be called directly!** It is used by a platform-specific {@link BaseQueryBuilder.query | `QueryBuilder.query`} method to provide a fluent query API.
|
|
812
|
+
*
|
|
813
|
+
* @param options - {@link QueryOptions | `QueryOptions`} object.
|
|
814
|
+
*
|
|
815
|
+
* @returns {@link QueryResult} object.
|
|
816
|
+
*
|
|
817
|
+
* @throws Error if `options` {@link QueryOptions.pageKeyMap | `pageKeyMap`} `pageKeyMap` keys do not match {@link QueryOptions.shardQueryMap | `shardQueryMap`} keys.
|
|
818
|
+
*
|
|
819
|
+
* @protected
|
|
820
|
+
*/
|
|
821
|
+
query<ET extends EntityToken<CC>, ITS extends string, CF = unknown, K = unknown>(options: QueryOptions<CC, ET, ITS, CF, K>): Promise<QueryResult<CC, ET, ITS, K>>;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Values-first config input used to capture literal tokens from the provided
|
|
826
|
+
* configuration value. This does not enforce full Config shape at compile
|
|
827
|
+
* time; runtime validation still occurs via Zod in the EntityManager.
|
|
828
|
+
*
|
|
829
|
+
* Keep this intentionally permissive to maximize inference from `as const`.
|
|
830
|
+
*/
|
|
831
|
+
interface ConfigInput {
|
|
832
|
+
/** Global hash key property name (e.g., `"pk"`). */
|
|
833
|
+
hashKey: string;
|
|
834
|
+
/** Global range key property name (e.g., `"sk"`). */
|
|
835
|
+
rangeKey: string;
|
|
836
|
+
/**
|
|
837
|
+
* Optional generated property token maps.
|
|
838
|
+
*
|
|
839
|
+
* @remarks
|
|
840
|
+
* - `sharded` keys are hash-side generated property tokens and are encoded atomically.
|
|
841
|
+
* - `unsharded` keys are range-side generated property tokens.
|
|
842
|
+
*/
|
|
843
|
+
generatedProperties?: {
|
|
844
|
+
/** Sharded generated property tokens (hash-side). */
|
|
845
|
+
sharded?: Record<string, readonly string[]>;
|
|
846
|
+
/** Unsharded generated property tokens (range-side). */
|
|
847
|
+
unsharded?: Record<string, readonly string[]>;
|
|
848
|
+
};
|
|
849
|
+
/**
|
|
850
|
+
* Optional map of transcodable property token -\> transcode name.
|
|
851
|
+
*
|
|
852
|
+
* @remarks
|
|
853
|
+
* Only properties present here are treated as “transcoded properties”.
|
|
854
|
+
*/
|
|
855
|
+
propertyTranscodes?: Record<string, string>;
|
|
856
|
+
/**
|
|
857
|
+
* Optional index token map used for typing and paging-key narrowing.
|
|
858
|
+
*
|
|
859
|
+
* @remarks
|
|
860
|
+
* This is provider-agnostic metadata (not a provider-specific index definition).
|
|
861
|
+
*/
|
|
862
|
+
indexes?: Record<string, {
|
|
863
|
+
/** Index hash key token (global hash key or sharded generated key). */
|
|
864
|
+
hashKey: string;
|
|
865
|
+
/** Index range key token (global range key, unsharded generated key, or transcoded scalar). */
|
|
866
|
+
rangeKey: string;
|
|
867
|
+
/** Optional list of projected attribute names (validated at runtime to exclude key tokens). */
|
|
868
|
+
projections?: string[];
|
|
869
|
+
}>;
|
|
870
|
+
/**
|
|
871
|
+
* Optional per-entity configuration (runtime semantics).
|
|
872
|
+
*
|
|
873
|
+
* @remarks
|
|
874
|
+
* This is intentionally permissive in `ConfigInput`; runtime validation occurs
|
|
875
|
+
* in the {@link EntityManager | `EntityManager`} constructor via Zod.
|
|
876
|
+
*/
|
|
877
|
+
entities?: Record<string, unknown>;
|
|
878
|
+
/**
|
|
879
|
+
* Optional Zod schemas for per-entity domain shapes (non-generated fields only).
|
|
880
|
+
*
|
|
881
|
+
* @remarks
|
|
882
|
+
* Schemas MUST declare only base (non-generated) properties. Do not include:
|
|
883
|
+
* - global keys (hashKey/rangeKey), or
|
|
884
|
+
* - generated property tokens (sharded/unsharded keys).
|
|
885
|
+
*/
|
|
886
|
+
entitiesSchema?: Record<string, ZodType>;
|
|
887
|
+
/** Optional delimiter between generated key elements (default `|`). */
|
|
888
|
+
generatedKeyDelimiter?: string;
|
|
889
|
+
/** Optional delimiter between a generated element name and its value (default `#`). */
|
|
890
|
+
generatedValueDelimiter?: string;
|
|
891
|
+
/** Optional delimiter between entity token and shard suffix in hash key values (default `!`). */
|
|
892
|
+
shardKeyDelimiter?: string;
|
|
893
|
+
/** Optional transcode registry/value (validated at runtime). */
|
|
894
|
+
transcodes?: unknown;
|
|
895
|
+
/** Optional maximum concurrency for shard queries. */
|
|
896
|
+
throttle?: number;
|
|
897
|
+
}
|
|
898
|
+
/** Extract the hash key token string literal from a values-first config input type. */
|
|
899
|
+
type HashKeyFrom<CC> = CC extends {
|
|
900
|
+
/** Hash key token property name. */
|
|
901
|
+
hashKey: infer H;
|
|
902
|
+
} ? H & string : 'hashKey';
|
|
903
|
+
/** Extract the range key token string literal from a values-first config input type. */
|
|
904
|
+
type RangeKeyFrom<CC> = CC extends {
|
|
905
|
+
/** Range key token property name. */
|
|
906
|
+
rangeKey: infer R;
|
|
907
|
+
} ? R & string : 'rangeKey';
|
|
908
|
+
/** Extract the union of sharded generated key tokens from a values-first config input type. */
|
|
909
|
+
type ShardedKeysFrom<CC> = CC extends {
|
|
910
|
+
/** Optional generated properties object containing sharded/unsharded maps. */
|
|
911
|
+
generatedProperties?: infer GP;
|
|
912
|
+
} ? GP extends {
|
|
913
|
+
/** Sharded generated property token map. */
|
|
914
|
+
sharded?: infer S;
|
|
915
|
+
} ? keyof S & string : never : never;
|
|
916
|
+
/** Extract the union of unsharded generated key tokens from a values-first config input type. */
|
|
917
|
+
type UnshardedKeysFrom<CC> = CC extends {
|
|
918
|
+
/** Optional generated properties object containing sharded/unsharded maps. */
|
|
919
|
+
generatedProperties?: infer GP;
|
|
920
|
+
} ? GP extends {
|
|
921
|
+
/** Unsharded generated property token map. */
|
|
922
|
+
unsharded?: infer U;
|
|
923
|
+
} ? keyof U & string : never : never;
|
|
924
|
+
/** Extract the union of transcoded property tokens from a values-first config input type. */
|
|
925
|
+
type TranscodedPropertiesFrom<CC> = CC extends {
|
|
926
|
+
/** Optional map of property token -\> transcode name. */
|
|
927
|
+
propertyTranscodes?: infer PT;
|
|
928
|
+
} ? keyof PT & string : never;
|
|
929
|
+
/**
|
|
930
|
+
* Derive an EntityMap from CC.entitiesSchema when provided (values-first, no generics).
|
|
931
|
+
*
|
|
932
|
+
* Fallback to broad EntityMap if schemas are absent.
|
|
933
|
+
*/
|
|
934
|
+
type EntitiesFromSchema<CC> = CC extends {
|
|
935
|
+
/** Optional per-entity Zod schema map used only for type inference. */
|
|
936
|
+
entitiesSchema?: infer S;
|
|
937
|
+
} ? S extends Record<string, ZodType> ? {
|
|
938
|
+
[K in Extract<keyof S, string>]: z.infer<S[K]>;
|
|
939
|
+
} & EntityMap : EntityMap : EntityMap;
|
|
940
|
+
/**
|
|
941
|
+
* Derive the union of index token names from a values-first config input.
|
|
942
|
+
*
|
|
943
|
+
* When the provided config literal carries an `indexes` object with preserved
|
|
944
|
+
* literal keys (prefer `as const` at call sites), this helper captures the
|
|
945
|
+
* index token union. Falls back to `string` if absent.
|
|
946
|
+
*/
|
|
947
|
+
type IndexTokensFrom<CC> = CC extends {
|
|
948
|
+
/** Optional index token map used for index-token inference. */
|
|
949
|
+
indexes?: infer I;
|
|
950
|
+
} ? keyof I & string : string;
|
|
951
|
+
/**
|
|
952
|
+
* Captures a BaseConfigMap-compatible type from a literal ConfigInput value
|
|
953
|
+
* and an EntityMap (defaults to MinimalEntityMapFrom<CC>).
|
|
954
|
+
*/
|
|
955
|
+
interface CapturedConfigMapFrom<CC, EM extends EntityMap> extends BaseConfigMap {
|
|
956
|
+
/** Entity map type (from schemas when provided; otherwise broad). */
|
|
957
|
+
EntityMap: EM;
|
|
958
|
+
/** Hash key token captured from the config literal. */
|
|
959
|
+
HashKey: HashKeyFrom<CC>;
|
|
960
|
+
/** Range key token captured from the config literal. */
|
|
961
|
+
RangeKey: RangeKeyFrom<CC>;
|
|
962
|
+
/** Sharded generated key token union captured from the config literal. */
|
|
963
|
+
ShardedKeys: ShardedKeysFrom<CC>;
|
|
964
|
+
/** Unsharded generated key token union captured from the config literal. */
|
|
965
|
+
UnshardedKeys: UnshardedKeysFrom<CC>;
|
|
966
|
+
/** Transcoded property token union captured from the config literal. */
|
|
967
|
+
TranscodedProperties: TranscodedPropertiesFrom<CC>;
|
|
968
|
+
/** Transcode registry type (default registry; runtime validation still applies). */
|
|
969
|
+
TranscodeRegistry: DefaultTranscodeRegistry;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Values-first factory that captures literal tokens and index names directly
|
|
973
|
+
* from the provided config value. Runtime config parsing/validation is
|
|
974
|
+
* unchanged (performed in the EntityManager constructor).
|
|
975
|
+
*
|
|
976
|
+
* @typeParam CC - Captured config input (values-first). Prefer `as const` and
|
|
977
|
+
* `satisfies` at call sites to preserve literal keys.
|
|
978
|
+
* @typeParam EM - EntityMap for the manager. Defaults to a minimal derived map
|
|
979
|
+
* from `CC.entitiesSchema` when present; otherwise falls back to EntityMap.
|
|
980
|
+
*
|
|
981
|
+
* @returns An {@link EntityManager | `EntityManager`} instance whose type
|
|
982
|
+
* captures CF from the single values-first config literal ({@link ConfigInput | `ConfigInput`})
|
|
983
|
+
* as the second generic parameter (phantom; type-only).
|
|
984
|
+
*/
|
|
985
|
+
declare function createEntityManager<const CC extends ConfigInput, EM extends EntityMap = EntitiesFromSchema<CC>>(config: CC, logger?: Pick<Console, 'debug' | 'error'>): EntityManager<CapturedConfigMapFrom<CC, EM>, CC>;
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* Storage-facing record type with required keys.
|
|
989
|
+
*
|
|
990
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
991
|
+
*
|
|
992
|
+
* @category EntityManager
|
|
993
|
+
*/
|
|
994
|
+
type StorageRecord<CC extends BaseConfigMap> = StorageItem<CC> & EntityKey<CC>;
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Base EntityClient options.
|
|
998
|
+
*
|
|
999
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
1000
|
+
*
|
|
1001
|
+
* @category EntityClient
|
|
1002
|
+
*/
|
|
1003
|
+
interface BaseEntityClientOptions<CC extends BaseConfigMap, CF = unknown> {
|
|
1004
|
+
/** Default batch process options. */
|
|
1005
|
+
batchProcessOptions?: Omit<BatchProcessOptions<unknown, unknown>, 'batchHandler' | 'unprocessedItemExtractor'>;
|
|
1006
|
+
/** {@link EntityManager | `EntityManager`} instance. */
|
|
1007
|
+
entityManager: EntityManager<CC, CF>;
|
|
1008
|
+
/** Injected logger object. Must support `debug` and `error` methods. Default: `console` */
|
|
1009
|
+
logger?: Pick<Console, 'debug' | 'error'> | undefined;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* Base EntityClient class. Integrates {@link EntityManager | `EntityManager`} with injected logging & enhanced batch processing.
|
|
1014
|
+
*
|
|
1015
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s
|
|
1016
|
+
* {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}.
|
|
1017
|
+
* If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
1018
|
+
* @typeParam CF - Values-first config literal type captured by the manager (phantom; type-only). Propagated so
|
|
1019
|
+
* client-facing calls that return `IndexTokensOf<CF>` retain the narrowed union.
|
|
1020
|
+
*
|
|
1021
|
+
* @category EntityClient
|
|
1022
|
+
*/
|
|
1023
|
+
declare abstract class BaseEntityClient<CC extends BaseConfigMap, CF = unknown> {
|
|
1024
|
+
/** Default batch process options. */
|
|
1025
|
+
readonly batchProcessOptions: NonNullable<BaseEntityClientOptions<CC, CF>['batchProcessOptions']>;
|
|
1026
|
+
/** {@link EntityManager | `EntityManager`} instance. */
|
|
1027
|
+
readonly entityManager: EntityManager<CC, CF>;
|
|
1028
|
+
/** Injected logger object. Must support `debug` and `error` methods. Default: `console` */
|
|
1029
|
+
readonly logger: NonNullable<BaseEntityClientOptions<CC, CF>['logger']>;
|
|
1030
|
+
/**
|
|
1031
|
+
* Base EntityClient constructor.
|
|
1032
|
+
*
|
|
1033
|
+
* @param options - {@link BaseEntityClientOptions | `BaseEntityClientOptions`} object.
|
|
1034
|
+
*/
|
|
1035
|
+
constructor(options: BaseEntityClientOptions<CC, CF>);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* Extract the captured config map type from a {@link BaseEntityClient | `BaseEntityClient`} instance type.
|
|
1040
|
+
*
|
|
1041
|
+
* @typeParam EC - A {@link BaseEntityClient | `BaseEntityClient`} instance type.
|
|
1042
|
+
*
|
|
1043
|
+
* @remarks
|
|
1044
|
+
* This is a pure type-level helper used to derive token-aware types from a client
|
|
1045
|
+
* instance type without requiring callers to restate the config map type.
|
|
1046
|
+
*/
|
|
1047
|
+
type ConfigOfClient<EC> = EC extends BaseEntityClient<infer CC> ? CC : never;
|
|
1048
|
+
/**
|
|
1049
|
+
* Map a client instance type + entity token to the storage-facing record type.
|
|
1050
|
+
*
|
|
1051
|
+
* @typeParam EC - A {@link BaseEntityClient | `BaseEntityClient`} instance type.
|
|
1052
|
+
* @typeParam ET - An {@link EntityToken | `EntityToken`} for that client’s config.
|
|
1053
|
+
*/
|
|
1054
|
+
type EntityClientRecordByToken<EC, ET extends EntityToken<ConfigOfClient<EC>>> = EntityRecord<ConfigOfClient<EC>, ET>;
|
|
1055
|
+
/**
|
|
1056
|
+
* Map a client instance type + entity token to the domain-facing item type.
|
|
1057
|
+
*
|
|
1058
|
+
* This is the “item-facing” shape (global keys and generated property tokens are
|
|
1059
|
+
* not required and are typically stripped via {@link EntityManager.removeKeys | `removeKeys`}).
|
|
1060
|
+
*
|
|
1061
|
+
* @typeParam EC - A {@link BaseEntityClient | `BaseEntityClient`} instance type.
|
|
1062
|
+
* @typeParam ET - An {@link EntityToken | `EntityToken`} for that client’s config.
|
|
1063
|
+
*/
|
|
1064
|
+
type EntityClientItemByToken<EC, ET extends EntityToken<ConfigOfClient<EC>>> = EntityItem<ConfigOfClient<EC>, ET>;
|
|
1065
|
+
|
|
1066
|
+
/**
|
|
1067
|
+
* Constructor options for {@link BaseQueryBuilder | `BaseQueryBuilder`}.
|
|
1068
|
+
*
|
|
1069
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
1070
|
+
* @typeParam EntityClient - {@link BaseEntityClient | `BaseEntityClient`} derived class instance.
|
|
1071
|
+
*
|
|
1072
|
+
* @category QueryBuilder
|
|
1073
|
+
*/
|
|
1074
|
+
interface BaseQueryBuilderOptions<CC extends BaseConfigMap, EntityClient extends BaseEntityClient<CC>> {
|
|
1075
|
+
/** {@link BaseEntityClient | `EntityClient`} instance. */
|
|
1076
|
+
entityClient: EntityClient;
|
|
1077
|
+
/** Entity token. */
|
|
1078
|
+
entityToken: EntityToken<CC>;
|
|
1079
|
+
/** Hash key token. */
|
|
1080
|
+
hashKeyToken: CC['HashKey'] | CC['ShardedKeys'];
|
|
1081
|
+
/** Dehydrated page key map. */
|
|
1082
|
+
pageKeyMap?: string | undefined;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* Options for {@link BaseQueryBuilder.query | `query`} method on all derived classes.
|
|
1087
|
+
*
|
|
1088
|
+
* Same as {@link QueryOptions | `QueryOptions`} for {@link EntityManager.query | `EntityManager.query`}, excluding `entityToken`, `pageKeyMap`, and `shardQueryMap`.
|
|
1089
|
+
*
|
|
1090
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
1091
|
+
* @typeParam ET - Entity token narrowing the item types for options.item.
|
|
1092
|
+
*
|
|
1093
|
+
* @category QueryBuilder
|
|
1094
|
+
*/
|
|
1095
|
+
type QueryBuilderQueryOptions<CC extends BaseConfigMap, ET extends EntityToken<CC>, CF = unknown> = Omit<QueryOptions<CC, ET, string, CF>, 'entityToken' | 'pageKeyMap' | 'shardQueryMap'>;
|
|
1096
|
+
|
|
1097
|
+
/**
|
|
1098
|
+
* Abstract base class supporting a fluent API for building a {@link ShardQueryMap | `ShardQueryMap`} using a database client.
|
|
1099
|
+
*
|
|
1100
|
+
* @typeParam CC - {@link ConfigMap | `ConfigMap`} that defines an {@link Config | `EntityManager configuration`}'s {@link EntityMap | `EntityMap`}, key properties, and {@link TranscodeRegistry | `TranscodeRegistry`}. If omitted, defaults to {@link BaseConfigMap | `BaseConfigMap`}.
|
|
1101
|
+
* @typeParam EntityClient - {@link BaseEntityClient | `BaseEntityClient`} derived class instance.
|
|
1102
|
+
* @typeParam IndexParams - Database platform-specific, index-specific query parameters.
|
|
1103
|
+
* @typeParam CF - Optional values-first config literal type for page key narrowing.
|
|
1104
|
+
* @typeParam K - Optional projection keys; narrows item shape when provided.
|
|
1105
|
+
*
|
|
1106
|
+
* @category QueryBuilder
|
|
1107
|
+
*/
|
|
1108
|
+
declare abstract class BaseQueryBuilder<CC extends BaseConfigMap, EntityClient extends BaseEntityClient<CC>, IndexParams, ET extends EntityToken<CC> = EntityToken<CC>, ITS extends string = string, CF = unknown, K = unknown> {
|
|
1109
|
+
/** {@link BaseEntityClient | `EntityClient`} instance. */
|
|
1110
|
+
readonly entityClient: EntityClient;
|
|
1111
|
+
/** Entity token. */
|
|
1112
|
+
readonly entityToken: EntityToken<CC>;
|
|
1113
|
+
/** Hash key token. */
|
|
1114
|
+
readonly hashKeyToken: CC['HashKey'] | CC['ShardedKeys'];
|
|
1115
|
+
/** Dehydrated page key map. */
|
|
1116
|
+
readonly pageKeyMap?: string | undefined;
|
|
1117
|
+
/**
|
|
1118
|
+
* Maps `indexToken` values to database platform-specific query parameters.
|
|
1119
|
+
*
|
|
1120
|
+
* @protected
|
|
1121
|
+
*/
|
|
1122
|
+
readonly indexParamsMap: Record<ITS, IndexParams>;
|
|
1123
|
+
/** BaseQueryBuilder constructor. */
|
|
1124
|
+
constructor(options: BaseQueryBuilderOptions<CC, EntityClient>);
|
|
1125
|
+
/**
|
|
1126
|
+
* Build a shard query function for a specific index token.
|
|
1127
|
+
*
|
|
1128
|
+
* @param indexToken - Index token identifying which index to query.
|
|
1129
|
+
*
|
|
1130
|
+
* @returns A {@link ShardQueryFunction | `ShardQueryFunction`} that queries a single shard for this index.
|
|
1131
|
+
*
|
|
1132
|
+
* @remarks
|
|
1133
|
+
* Implementations are provider-specific (e.g., DynamoDB). The returned function must:
|
|
1134
|
+
* - Query exactly one shard (partition) per invocation using the provided hash key value.
|
|
1135
|
+
* - Respect `pageKey` and `pageSize` for pagination.
|
|
1136
|
+
* - Return `pageKey: undefined` when that shard is exhausted.
|
|
1137
|
+
*
|
|
1138
|
+
* Entity Manager will orchestrate calling these functions across shards and indexes.
|
|
1139
|
+
*
|
|
1140
|
+
* @protected
|
|
1141
|
+
*/
|
|
1142
|
+
protected abstract getShardQueryFunction(indexToken: ITS): ShardQueryFunction<CC, ET, ITS, CF, K>;
|
|
1143
|
+
/**
|
|
1144
|
+
* Builds a {@link ShardQueryMap | `ShardQueryMap`} object.
|
|
1145
|
+
*
|
|
1146
|
+
* @returns - The {@link ShardQueryMap | `ShardQueryMap`} object.
|
|
1147
|
+
*/
|
|
1148
|
+
build(): ShardQueryMap<CC, ET, ITS, CF, K>;
|
|
1149
|
+
/**
|
|
1150
|
+
* Execute the built query across shards and indexes via {@link EntityManager.query | `EntityManager.query`}.
|
|
1151
|
+
*
|
|
1152
|
+
* @param options - Query options excluding `entityToken`, `pageKeyMap`, and `shardQueryMap`, which are supplied by the builder.
|
|
1153
|
+
*
|
|
1154
|
+
* @returns The merged, de-duplicated, sorted query result, including a compact `pageKeyMap` token for the next page.
|
|
1155
|
+
*
|
|
1156
|
+
* @remarks
|
|
1157
|
+
* This delegates orchestration to Entity Manager; provider-specific behavior lives in {@link getShardQueryFunction | `getShardQueryFunction`}.
|
|
1158
|
+
*/
|
|
1159
|
+
query(options: QueryBuilderQueryOptions<CC, ET, CF>): Promise<QueryResult<CC, ET, ITS, K>>;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
export { BaseEntityClient, BaseQueryBuilder, EntityManager, configSchema, createEntityManager };
|
|
1163
|
+
export type { BaseConfigMap, BaseEntityClientOptions, BaseKeyTokens, BaseQueryBuilderOptions, CapturedConfigMapFrom, Config, ConfigInput, ConfigMap, ConfigOfClient, EntitiesFromSchema, EntityClientItemByToken, EntityClientRecordByToken, EntityItem, EntityItemPartial, EntityKey, EntityOfToken, EntityRecord, EntityRecordPartial, EntityToken, FallbackIndexTokenSet, HasIndexFor, HashKeyFrom, IndexComponentTokens, IndexHashKeyOf, IndexRangeKeyOf, IndexTokensFrom, IndexTokensOf, KeysFrom, PageKey, PageKeyByIndex, ParsedConfig, ParsedEntityConfig, ParsedGeneratedPropertiesConfig, ParsedIndexConfig, ParsedTranscoder, PresentIndexTokenSet, Projected, QueryBuilderQueryOptions, QueryOptions, QueryOptionsByCC, QueryOptionsByCF, QueryResult, RangeKeyFrom, ShardBump, ShardQueryFunction, ShardQueryMap, ShardQueryMapByCC, ShardQueryMapByCF, ShardQueryResult, ShardedKeysFrom, StorageItem, StorageRecord, TranscodedPropertiesFrom, UnshardedKeysFrom, ValidateConfigMap };
|