@earth-app/collegedb 1.0.1 → 1.0.3

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/kvmap.d.ts CHANGED
@@ -8,19 +8,21 @@
8
8
  *
9
9
  * Key features:
10
10
  * - Primary key to shard mapping storage and retrieval
11
+ * - Multiple lookup keys for the same shard mapping (e.g., username, email, id)
12
+ * - SHA-256 hashing of keys for security and privacy (enabled by default)
11
13
  * - Shard discovery and management
12
14
  * - Key counting and statistics for load balancing
13
15
  * - Batch operations for efficient KV usage
14
16
  *
15
17
  * @example
16
18
  * ```typescript
17
- * const mapper = new KVShardMapper(env.KV);
19
+ * const mapper = new KVShardMapper(env.KV, { hashShardMappings: true });
18
20
  *
19
- * // Set a mapping
20
- * await mapper.setShardMapping('user-123', 'db-east');
21
+ * // Set a mapping with multiple lookup keys
22
+ * await mapper.setShardMapping('user-123', 'db-east', ['username:john', 'email:john@example.com', 'id:123']);
21
23
  *
22
- * // Get a mapping
23
- * const mapping = await mapper.getShardMapping('user-123');
24
+ * // Get a mapping by any of the keys
25
+ * const mapping = await mapper.getShardMapping('email:john@example.com');
24
26
  * console.log(mapping?.shard); // 'db-east'
25
27
  *
26
28
  * // Get statistics
@@ -32,7 +34,7 @@
32
34
  * @since 1.0.0
33
35
  */
34
36
  import type { KVNamespace } from '@cloudflare/workers-types';
35
- import type { ShardMapping } from './types.js';
37
+ import type { CollegeDBConfig, ShardMapping } from './types.js';
36
38
  /**
37
39
  * The KVShardMapper class provides a persistent storage layer for mapping
38
40
  * primary keys to their assigned D1 database shards. It uses Cloudflare KV
@@ -40,49 +42,68 @@ import type { ShardMapping } from './types.js';
40
42
  *
41
43
  * Features:
42
44
  * - CRUD operations for shard mappings
45
+ * - Multiple lookup keys for the same shard (username, email, id, etc.)
46
+ * - SHA-256 hashing of keys for security and privacy
43
47
  * - Atomic updates with timestamp tracking
44
48
  * - Efficient bulk operations and statistics
45
49
  * - Prefix-based key organization for performance
46
50
  *
47
51
  * @example
48
52
  * ```typescript
49
- * const mapper = new KVShardMapper(env.KV);
53
+ * const mapper = new KVShardMapper(env.KV, { hashShardMappings: true });
50
54
  *
51
- * // Create a new mapping
52
- * await mapper.setShardMapping('order-456', 'db-central');
55
+ * // Create a new mapping with multiple lookup keys
56
+ * await mapper.setShardMapping('primary-key-123', 'db-central', ['username:john', 'email:john@example.com']);
53
57
  *
54
58
  * // Update an existing mapping
55
- * await mapper.updateShardMapping('order-456', 'db-west');
59
+ * await mapper.updateShardMapping('username:john', 'db-west');
56
60
  *
57
- * // Query mapping
58
- * const mapping = await mapper.getShardMapping('order-456');
61
+ * // Query mapping by any key
62
+ * const mapping = await mapper.getShardMapping('email:john@example.com');
59
63
  * if (mapping) {
60
- * console.log(`Order 456 is on ${mapping.shard}`);
64
+ * console.log(`User is on ${mapping.shard}`);
61
65
  * }
62
66
  * ```
63
67
  */
64
68
  export declare class KVShardMapper {
65
- private kv;
66
69
  /**
67
70
  * Cloudflare KV namespace for storing mappings
68
71
  * @readonly
69
72
  */
70
- constructor(kv: KVNamespace);
73
+ private readonly kv;
74
+ /**
75
+ * Whether to hash mapping keys with SHA-256
76
+ * @readonly
77
+ */
78
+ private readonly hashKeys;
79
+ /**
80
+ * Creates a new KVShardMapper instance
81
+ * @param kv - Cloudflare KV namespace
82
+ * @param config - Configuration options including hashing preference
83
+ */
84
+ constructor(kv: KVNamespace, config?: Partial<Pick<CollegeDBConfig, 'hashShardMappings'>>);
85
+ /**
86
+ * Hashes a key using SHA-256 if hashing is enabled
87
+ * @private
88
+ * @param key - The key to hash
89
+ * @returns The hashed key or original key if hashing is disabled
90
+ */
91
+ private hashKey;
71
92
  /**
72
93
  * Retrieves the shard assignment for a given primary key from KV storage.
73
94
  * Returns null if no mapping exists, indicating the key has not been
74
- * assigned to any shard yet.
75
- * @param primaryKey - The primary key to look up
95
+ * assigned to any shard yet. Supports both single-key and multi-key lookups.
96
+ * @param primaryKey - The primary key to look up (will be hashed if hashing is enabled)
76
97
  * @returns Promise resolving to the shard mapping or null if not found
77
98
  * @throws {Error} If KV read operation fails
78
99
  * @example
79
100
  * ```typescript
80
- * const mapping = await mapper.getShardMapping('user-789');
101
+ * const mapping = await mapper.getShardMapping('email:user@example.com');
81
102
  * if (mapping) {
82
- * console.log(`User 789 is on shard: ${mapping.shard}`);
103
+ * console.log(`User is on shard: ${mapping.shard}`);
83
104
  * console.log(`Created: ${new Date(mapping.createdAt)}`);
84
105
  * } else {
85
- * console.log('User 789 not yet assigned to any shard');
106
+ * console.log('User not yet assigned to any shard');
86
107
  * }
87
108
  * ```
88
109
  */
@@ -96,21 +117,23 @@ export declare class KVShardMapper {
96
117
  * Use updateShardMapping() if you want to preserve creation timestamp.
97
118
  * @param primaryKey - The primary key to map
98
119
  * @param shard - The shard binding name to assign
120
+ * @param additionalKeys - Optional array of additional lookup keys for the same mapping
99
121
  * @returns Promise that resolves when the mapping is stored
100
122
  * @throws {Error} If KV write operation fails
101
123
  * @example
102
124
  * ```typescript
103
- * // Assign a new user to the west coast shard
104
- * await mapper.setShardMapping('user-california-123', 'db-west');
125
+ * // Assign a new user to the west coast shard with multiple lookup keys
126
+ * await mapper.setShardMapping('user-123', 'db-west', ['username:john', 'email:john@example.com']);
105
127
  * ```
106
128
  */
107
- setShardMapping(primaryKey: string, shard: string): Promise<void>;
129
+ setShardMapping(primaryKey: string, shard: string, additionalKeys?: string[]): Promise<void>;
108
130
  /**
109
131
  * Changes the shard assignment for a primary key that already has a mapping.
110
132
  * Preserves the original creation timestamp while updating the modified
111
133
  * timestamp. Throws an error if no existing mapping is found.
112
134
  *
113
135
  * This is typically used during shard rebalancing or data migration operations.
136
+ * Works with both single-key and multi-key mappings.
114
137
  * @param primaryKey - The primary key to update
115
138
  * @param newShard - The new shard binding name to assign
116
139
  * @returns Promise that resolves when the mapping is updated
@@ -119,7 +142,7 @@ export declare class KVShardMapper {
119
142
  * ```typescript
120
143
  * try {
121
144
  * // Move user to a different shard for rebalancing
122
- * await mapper.updateShardMapping('user-456', 'db-central');
145
+ * await mapper.updateShardMapping('email:user@example.com', 'db-central');
123
146
  * console.log('User successfully moved to central shard');
124
147
  * } catch (error) {
125
148
  * console.error('Failed to update mapping:', error.message);
@@ -130,7 +153,7 @@ export declare class KVShardMapper {
130
153
  /**
131
154
  * Completely removes the shard assignment for a primary key from KV storage.
132
155
  * This is typically used when data is being permanently deleted or when
133
- * cleaning up orphaned mappings.
156
+ * cleaning up orphaned mappings. Handles both single-key and multi-key mappings.
134
157
  *
135
158
  * **WARNING**: After deletion, the primary key will be treated as new
136
159
  * and may be assigned to a different shard on next access.
@@ -141,7 +164,7 @@ export declare class KVShardMapper {
141
164
  * @example
142
165
  * ```typescript
143
166
  * // Remove mapping for deleted user
144
- * await mapper.deleteShardMapping('user-deleted-789');
167
+ * await mapper.deleteShardMapping('email:deleted@example.com');
145
168
  * console.log('Mapping removed for deleted user');
146
169
  * ```
147
170
  */
@@ -251,5 +274,36 @@ export declare class KVShardMapper {
251
274
  * ```
252
275
  */
253
276
  clearAllMappings(): Promise<void>;
277
+ /**
278
+ * Adds additional lookup keys to an existing shard mapping. This allows you to
279
+ * query the same shard mapping using multiple identifiers (e.g., username, email, id).
280
+ *
281
+ * @param primaryKey - An existing key in the mapping
282
+ * @param additionalKeys - New keys to add for lookup
283
+ * @returns Promise that resolves when the additional keys are added
284
+ * @throws {Error} If no existing mapping found or KV operations fail
285
+ * @example
286
+ * ```typescript
287
+ * // Add email lookup to an existing user mapping
288
+ * await mapper.addLookupKeys('user-123', ['email:user@example.com']);
289
+ * ```
290
+ * @since 1.0.3
291
+ */
292
+ addLookupKeys(primaryKey: string, additionalKeys: string[]): Promise<void>;
293
+ /**
294
+ * Gets all lookup keys associated with a shard mapping. This is useful for
295
+ * understanding what keys resolve to the same shard.
296
+ *
297
+ * @param primaryKey - Any key in the mapping
298
+ * @returns Promise resolving to array of all keys in the mapping
299
+ * @throws {Error} If no existing mapping found
300
+ * @example
301
+ * ```typescript
302
+ * const allKeys = await mapper.getAllLookupKeys('email:user@example.com');
303
+ * console.log(allKeys); // ['user-123', 'username:john', 'email:user@example.com']
304
+ * ```
305
+ * @since 1.0.3
306
+ */
307
+ getAllLookupKeys(primaryKey: string): Promise<string[]>;
254
308
  }
255
309
  //# sourceMappingURL=kvmap.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"kvmap.d.ts","sourceRoot":"","sources":["../src/kvmap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAyB/C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,aAAa;IAKb,OAAO,CAAC,EAAE;IAJtB;;;OAGG;gBACiB,EAAE,EAAE,WAAW;IAEnC;;;;;;;;;;;;;;;;;OAiBG;IACG,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAMvE;;;;;;;;;;;;;;;;OAgBG;IACG,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWvE;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB7E;;;;;;;;;;;;;;;;;OAiBG;IACG,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK3D;;;;;;;;;;;;;OAaG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAKzC;;;;;;;;;;;;OAYG;IACG,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAKrD;;;;;;;;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;;;;;;;;;;;;;;;;;OAqBG;IACG,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA8C7E;;;;;;;;;;;;;;;;;OAiBG;IACG,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA0B3D;;;;;;;;;;;;;OAaG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAKzC;;;;;;;;;;;;OAYG;IACG,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAKrD;;;;;;;;;;;;;OAaG;IACG,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUjD;;;;;;;;;;;;;;OAcG;IACG,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA+BvD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAyB1D;;;;;;;;;;;;;;;;;;;OAmBG;IACG,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAYvC;;;;;;;;;;;;;;OAcG;IACG,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAkDhF;;;;;;;;;;;;;OAaG;IACG,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAkB7D"}
@@ -389,4 +389,18 @@ export declare function checkMigrationNeeded(d1: D1Database, shardName: string,
389
389
  * ```
390
390
  */
391
391
  export declare function clearMigrationCache(): void;
392
+ /**
393
+ * Clears a specific migration cache entry
394
+ *
395
+ * Resets the cache for a specific shard, forcing re-check on next
396
+ * migration detection call.
397
+ *
398
+ * @param shardName - The shard name to clear from cache
399
+ * @example
400
+ * ```typescript
401
+ * // Force re-check of specific shard
402
+ * clearShardMigrationCache('db-auto');
403
+ * ```
404
+ */
405
+ export declare function clearShardMigrationCache(shardName: string): void;
392
406
  //# sourceMappingURL=migrations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAQpE;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAchF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQhH;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAOlF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAQnF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAOlE;AAED;;;;;;;;;;;;;;;;;;;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AE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,yBAAyB,CAC9C,EAAE,EAAE,UAAU,EACd,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,aAAa,EACrB,OAAO,GAAE,kBAAuB,GAC9B,OAAO,CAAC,iBAAiB,CAAC,CA8E5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,oBAAoB,CACzC,EAAE,EAAE,UAAU,EACd,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,eAAe,EACvB,OAAO,GAAE;IACR,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CACtB,GACJ,OAAO,CAAC;IACV,eAAe,EAAE,OAAO,CAAC;IACzB,kBAAkB,EAAE,OAAO,CAAC;IAC5B,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC,CA6ID;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,oBAAoB,CAAC,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,CAuDvH;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAGhE"}
package/dist/router.d.ts CHANGED
@@ -39,8 +39,8 @@
39
39
  * @author CollegeDB Team
40
40
  * @since 1.0.0
41
41
  */
42
- import type { D1Database, D1PreparedStatement, D1Result } from '@cloudflare/workers-types';
43
- import type { CollegeDBConfig, ShardStats } from './types.js';
42
+ import type { D1Database, D1PreparedStatement, D1Result, Request } from '@cloudflare/workers-types';
43
+ import type { CollegeDBConfig, D1Region, ShardStats } from './types.js';
44
44
  /**
45
45
  * Sets up the global configuration for the CollegeDB system. This must be called
46
46
  * before any other operations can be performed. The configuration includes KV
@@ -144,6 +144,41 @@ export declare function initializeAsync(config: CollegeDBConfig): Promise<void>;
144
144
  * ```
145
145
  */
146
146
  export declare function collegedb<T>(config: CollegeDBConfig, callback: () => T): Promise<T>;
147
+ /**
148
+ * Resets the global configuration (for testing purposes only)
149
+ *
150
+ * @private
151
+ * @internal
152
+ */
153
+ export declare function resetConfig(): void;
154
+ /**
155
+ * Determines the closest D1 region based on an IP address.
156
+ * Uses IP geolocation to estimate the user's location and find the nearest D1 region.
157
+ *
158
+ * This function uses Cloudflare's CF object which provides geolocation data
159
+ * in Cloudflare Workers environment. Falls back to 'wnam' if geolocation fails.
160
+ *
161
+ * @param request - The incoming Request object (contains CF geolocation data in Cloudflare Workers)
162
+ * @returns The closest D1Region based on IP geolocation
163
+ * @example
164
+ * ```typescript
165
+ * // In a Cloudflare Worker
166
+ * export default {
167
+ * async fetch(request: Request, env: Env) {
168
+ * const userRegion = getClosestRegionFromIP(request);
169
+ *
170
+ * initialize({
171
+ * kv: env.KV,
172
+ * strategy: 'location',
173
+ * targetRegion: userRegion, // Automatically optimized for user location
174
+ * shardLocations: { ... },
175
+ * shards: { ... }
176
+ * });
177
+ * }
178
+ * };
179
+ * ```
180
+ */
181
+ export declare function getClosestRegionFromIP(request: Request): D1Region;
147
182
  /**
148
183
  * Creates the database schema in the specified D1 database
149
184
  *
@@ -165,7 +200,7 @@ export declare function collegedb<T>(config: CollegeDBConfig, callback: () => T)
165
200
  */
166
201
  export declare function createSchema(d1: D1Database, schema: string): Promise<void>;
167
202
  /**
168
- * Prepares a SQL statement for execution.
203
+ * Prepares a SQL statement for execution with operation-aware routing.
169
204
  *
170
205
  * @param key - The primary key to route the query
171
206
  * @param sql - The SQL statement to prep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Database, DurableObjectNamespace, KVNamespace } from '@cloudflare/workers-types';
29
+ /**
30
+ * Available Cloudflare D1 regions for geographic optimization
31
+ */
32
+ export type D1Region = 'wnam' | 'enam' | 'weur' | 'eeur' | 'apac' | 'oc' | 'me' | 'af';
33
+ /**
34
+ * Shard location configuration for geographic optimization
35
+ */
36
+ export interface ShardLocation {
37
+ /** The D1 region where this shard is located */
38
+ region: D1Region;
39
+ /** Optional priority weight for this shard (higher = preferred) */
40
+ priority?: number;
41
+ }
29
42
  /**
30
43
  * Sharding strategy options for CollegeDB
31
44
  * - `round-robin`: Distributes keys evenly across available shards.
32
45
  * - `random`: Selects a random shard for each key.
33
46
  * - `hash`: Uses a hash function to determine the shard based on the primary key.
47
+ * - `location`: Selects shards based on geographic proximity to reduce latency.
48
+ */
49
+ export type ShardingStrategy = 'round-robin' | 'random' | 'hash' | 'location';
50
+ /**
51
+ * Mixed sharding strategy configuration for different operation types
52
+ * @since 1.0.2
53
+ */
54
+ export interface MixedShardingStrategy {
55
+ /** Strategy for read operations (SELECT) */
56
+ read: ShardingStrategy;
57
+ /** Strategy for write operations (INSERT, UPDATE, DELETE) */
58
+ write: ShardingStrategy;
59
+ }
60
+ /**
61
+ * Database operation types for strategy selection
62
+ * @since 1.0.2
34
63
  */
35
- export type ShardingStrategy = 'round-robin' | 'random' | 'hash';
64
+ export type OperationType = 'read' | 'write';
36
65
  /**
37
66
  * Environment bindings for the Cloudflare Worker
38
67
  */
@@ -54,8 +83,25 @@ export interface CollegeDBConfig {
54
83
  coordinator?: DurableObjectNamespace;
55
84
  /** Available D1 database bindings */
56
85
  shards: Record<string, D1Database>;
57
- /** Default shard allocation strategy */
58
- strategy?: ShardingStrategy;
86
+ /** Default shard allocation strategy (can be single strategy or mixed strategy object) */
87
+ strategy?: ShardingStrategy | MixedShardingStrategy;
88
+ /** Target region for location-based sharding */
89
+ targetRegion?: D1Region;
90
+ /** Geographic locations of each shard (required for location strategy) */
91
+ shardLocations?: Record<string, ShardLocation>;
92
+ /**
93
+ * Disable automatic migration detection and background migration (useful for testing)
94
+ * @since 1.0.2
95
+ */
96
+ disableAutoMigration?: boolean;
97
+ /**
98
+ * Whether to hash shard mapping keys with SHA-256 for security and privacy.
99
+ * When enabled, primary keys are hashed before storing in KV, protecting
100
+ * sensitive data like emails from being visible in KV keys.
101
+ * @default true
102
+ * @since 1.0.3
103
+ */
104
+ hashShardMappings?: boolean;
59
105
  }
60
106
  /**
61
107
  * Shard statistics for monitoring and load balancing
@@ -85,6 +131,22 @@ export interface ShardMapping {
85
131
  createdAt: number;
86
132
  /** Timestamp when mapping was last updated */
87
133
  updatedAt: number;
134
+ /** Original unhashed primary key (only stored when hashing is disabled) */
135
+ originalKey?: string;
136
+ }
137
+ /**
138
+ * Multi-key shard mapping for lookup by various unique identifiers
139
+ * @since 1.0.3
140
+ */
141
+ export interface MultiKeyShardMapping {
142
+ /** D1 binding name */
143
+ shard: string;
144
+ /** Timestamp when mapping was created */
145
+ createdAt: number;
146
+ /** Timestamp when mapping was last updated */
147
+ updatedAt: number;
148
+ /** All keys that resolve to this shard mapping (for reverse lookups) */
149
+ keys: string[];
88
150
  }
89
151
  /**
90
152
  * Durable Object state for shard coordination
@@ -99,9 +161,15 @@ export interface ShardCoordinatorState {
99
161
  * `round-robin` - distributes keys evenly across shards
100
162
  * `random` - selects a random shard for each key
101
163
  * `hash` - uses a hash function to determine shard based on primary key (default)
164
+ * `location` - selects shards based on geographic proximity to reduce latency
165
+ * Can also be a mixed strategy object with separate read/write strategies
102
166
  */
103
- strategy: ShardingStrategy;
167
+ strategy: ShardingStrategy | MixedShardingStrategy;
104
168
  /** Round-robin counter for allocation */
105
169
  roundRobinIndex: number;
170
+ /** Target region for location-based allocation */
171
+ targetRegion?: D1Region;
172
+ /** Geographic locations of each shard */
173
+ shardLocations?: Record<string, ShardLocation>;
106
174
  }
107
175
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["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;IAClB,wEAAwE;IACxE,IAAI,EAAE,MAAM,EAAE,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACrC,gCAAgC;IAChC,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,gCAAgC;IAChC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACvC;;;;;;;OAOG;IACH,QAAQ,EAAE,gBAAgB,GAAG,qBAAqB,CAAC;IACnD,yCAAyC;IACzC,eAAe,EAAE,MAAM,CAAC;IACxB,kDAAkD;IAClD,YAAY,CAAC,EAAE,QAAQ,CAAC;IACxB,yCAAyC;IACzC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;CAC/C"}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "version": "1.0.1",
7
+ "version": "1.0.3",
8
8
  "files": [
9
9
  "dist/**/*",
10
10
  "README.md",
@@ -37,7 +37,7 @@
37
37
  "jsdoc-babel": "^0.5.0",
38
38
  "lint-staged": "^16.1.2",
39
39
  "prettier": "^3.6.2",
40
- "prettier-plugin-organize-imports": "^4.2.0",
40
+ "prettier-plugin-organize-imports": "4.1.0",
41
41
  "typedoc": "^0.28.8",
42
42
  "vitest": "^3.2.4"
43
43
  },