@earth-app/collegedb 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +349 -100
- package/dist/durable.d.ts +8 -4
- package/dist/durable.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +9 -8
- package/dist/kvmap.d.ts +68 -5
- package/dist/kvmap.d.ts.map +1 -1
- package/dist/migrations.d.ts +18 -15
- package/dist/migrations.d.ts.map +1 -1
- package/dist/providers.d.ts +207 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/router.d.ts +10 -10
- package/dist/router.d.ts.map +1 -1
- package/dist/types.d.ts +142 -13
- package/dist/types.d.ts.map +1 -1
- package/package.json +39 -14
package/dist/kvmap.d.ts
CHANGED
|
@@ -33,8 +33,7 @@
|
|
|
33
33
|
* @author Gregory Mitchell
|
|
34
34
|
* @since 1.0.0
|
|
35
35
|
*/
|
|
36
|
-
import type {
|
|
37
|
-
import type { CollegeDBConfig, ShardMapping } from './types.js';
|
|
36
|
+
import type { CollegeDBConfig, KVStorage, ShardMapping } from './types.js';
|
|
38
37
|
/**
|
|
39
38
|
* The KVShardMapper class provides a persistent storage layer for mapping
|
|
40
39
|
* primary keys to their assigned D1 database shards. It uses Cloudflare KV
|
|
@@ -67,7 +66,7 @@ import type { CollegeDBConfig, ShardMapping } from './types.js';
|
|
|
67
66
|
*/
|
|
68
67
|
export declare class KVShardMapper {
|
|
69
68
|
/**
|
|
70
|
-
*
|
|
69
|
+
* KV provider for storing mappings
|
|
71
70
|
* @readonly
|
|
72
71
|
*/
|
|
73
72
|
private readonly kv;
|
|
@@ -81,12 +80,57 @@ export declare class KVShardMapper {
|
|
|
81
80
|
* @private
|
|
82
81
|
*/
|
|
83
82
|
private readonly hashCache;
|
|
83
|
+
/**
|
|
84
|
+
* In-memory mapping cache to reduce repeated KV reads.
|
|
85
|
+
* @private
|
|
86
|
+
*/
|
|
87
|
+
private readonly mappingCache;
|
|
88
|
+
/**
|
|
89
|
+
* In-memory known shards cache.
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
private readonly knownShardsCache;
|
|
93
|
+
/**
|
|
94
|
+
* Mapping cache TTL in milliseconds.
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
private readonly mappingCacheTtlMs;
|
|
98
|
+
/**
|
|
99
|
+
* Known shards cache TTL in milliseconds.
|
|
100
|
+
* @private
|
|
101
|
+
*/
|
|
102
|
+
private readonly knownShardsCacheTtlMs;
|
|
84
103
|
/**
|
|
85
104
|
* Creates a new KVShardMapper instance
|
|
86
|
-
* @param kv -
|
|
105
|
+
* @param kv - KV storage provider
|
|
87
106
|
* @param config - Configuration options including hashing preference
|
|
88
107
|
*/
|
|
89
|
-
constructor(kv:
|
|
108
|
+
constructor(kv: KVStorage, config?: Partial<Pick<CollegeDBConfig, 'hashShardMappings' | 'mappingCacheTtlMs' | 'knownShardsCacheTtlMs'>>);
|
|
109
|
+
/**
|
|
110
|
+
* Reads a mapping from the in-memory cache.
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
private getCachedMapping;
|
|
114
|
+
/**
|
|
115
|
+
* Writes a mapping to the in-memory cache.
|
|
116
|
+
* @private
|
|
117
|
+
*/
|
|
118
|
+
private setCachedMapping;
|
|
119
|
+
/**
|
|
120
|
+
* Updates mapping cache entries for the provided logical keys.
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
private cacheMappingForKeys;
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves known shards from cache when available.
|
|
126
|
+
* @private
|
|
127
|
+
*/
|
|
128
|
+
private getCachedKnownShards;
|
|
129
|
+
/**
|
|
130
|
+
* Stores known shards in the cache.
|
|
131
|
+
* @private
|
|
132
|
+
*/
|
|
133
|
+
private setCachedKnownShards;
|
|
90
134
|
/**
|
|
91
135
|
* Hashes a key using SHA-256 if hashing is enabled
|
|
92
136
|
* @param key - The key to hash
|
|
@@ -294,6 +338,25 @@ export declare class KVShardMapper {
|
|
|
294
338
|
* @since 1.0.3
|
|
295
339
|
*/
|
|
296
340
|
addLookupKeys(primaryKey: string, additionalKeys: string[]): Promise<void>;
|
|
341
|
+
/**
|
|
342
|
+
* Sets multiple shard mappings concurrently with a configurable concurrency limit.
|
|
343
|
+
*
|
|
344
|
+
* This helper is used by migration workflows to significantly reduce total
|
|
345
|
+
* mapping time while avoiding unbounded concurrency spikes.
|
|
346
|
+
*
|
|
347
|
+
* @param mappings - The mappings to create
|
|
348
|
+
* @param options - Batch execution options
|
|
349
|
+
* @param options.concurrency - Maximum concurrent set operations (default: 25)
|
|
350
|
+
* @returns Promise that resolves when all mappings are written
|
|
351
|
+
* @since 1.1.0
|
|
352
|
+
*/
|
|
353
|
+
setShardMappingsBatch(mappings: Array<{
|
|
354
|
+
primaryKey: string;
|
|
355
|
+
shard: string;
|
|
356
|
+
additionalKeys?: string[];
|
|
357
|
+
}>, options?: {
|
|
358
|
+
concurrency?: number;
|
|
359
|
+
}): Promise<void>;
|
|
297
360
|
/**
|
|
298
361
|
* Gets all lookup keys associated with a shard mapping. This is useful for
|
|
299
362
|
* understanding what keys resolve to the same shard.
|
package/dist/kvmap.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kvmap.d.ts","sourceRoot":"","sources":["../src/kvmap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;
|
|
1
|
+
{"version":3,"file":"kvmap.d.ts","sourceRoot":"","sources":["../src/kvmap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAwB,YAAY,EAAE,MAAM,YAAY,CAAC;AAgDjG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBA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
|
|
|
30
30
|
* @author Gregory Mitchell
|
|
31
31
|
* @since 1.0.0
|
|
32
32
|
*/
|
|
33
|
-
import type { D1Database } from '@cloudflare/workers-types';
|
|
34
33
|
import type { KVShardMapper } from './kvmap.js';
|
|
35
|
-
import type { CollegeDBConfig, ShardingStrategy } from './types.js';
|
|
34
|
+
import type { CollegeDBConfig, SQLDatabase, ShardingStrategy } from './types.js';
|
|
36
35
|
/**
|
|
37
36
|
* Executes SQL statements to create the default table structure and indexes
|
|
38
37
|
* in the specified D1 database. Supports custom schemas and handles SQL
|
|
@@ -60,7 +59,7 @@ import type { CollegeDBConfig, ShardingStrategy } from './types.js';
|
|
|
60
59
|
* await createSchema(env.DB_PRODUCTS, sql);
|
|
61
60
|
* ```
|
|
62
61
|
*/
|
|
63
|
-
export declare function createSchema(d1:
|
|
62
|
+
export declare function createSchema(d1: SQLDatabase, schema: string): Promise<void>;
|
|
64
63
|
/**
|
|
65
64
|
* Applies the schema to all provided D1 database instances in parallel.
|
|
66
65
|
* This is useful for initializing a complete sharded database system
|
|
@@ -91,7 +90,7 @@ export declare function createSchema(d1: D1Database, schema: string): Promise<vo
|
|
|
91
90
|
* }
|
|
92
91
|
* ```
|
|
93
92
|
*/
|
|
94
|
-
export declare function createSchemaAcrossShards(shards: Record<string,
|
|
93
|
+
export declare function createSchemaAcrossShards(shards: Record<string, SQLDatabase>, schema: string): Promise<void>;
|
|
95
94
|
/**
|
|
96
95
|
* Performs a lightweight check to determine if the expected schema is present
|
|
97
96
|
* in the database.
|
|
@@ -108,7 +107,7 @@ export declare function createSchemaAcrossShards(shards: Record<string, D1Databa
|
|
|
108
107
|
* }
|
|
109
108
|
* ```
|
|
110
109
|
*/
|
|
111
|
-
export declare function schemaExists(d1:
|
|
110
|
+
export declare function schemaExists(d1: SQLDatabase, table: string): Promise<boolean>;
|
|
112
111
|
/**
|
|
113
112
|
* Removes all tables that are part of the default CollegeDB schema from
|
|
114
113
|
* the specified database. This is a destructive operation that cannot be undone.
|
|
@@ -128,7 +127,7 @@ export declare function schemaExists(d1: D1Database, table: string): Promise<boo
|
|
|
128
127
|
* }
|
|
129
128
|
* ```
|
|
130
129
|
*/
|
|
131
|
-
export declare function dropSchema(d1:
|
|
130
|
+
export declare function dropSchema(d1: SQLDatabase, ...tables: string[]): Promise<void>;
|
|
132
131
|
/**
|
|
133
132
|
* Queries the SQLite system catalog to retrieve all user-created tables
|
|
134
133
|
* in the database. This is useful for schema inspection, validation,
|
|
@@ -149,7 +148,7 @@ export declare function dropSchema(d1: D1Database, ...tables: string[]): Promise
|
|
|
149
148
|
* }
|
|
150
149
|
* ```
|
|
151
150
|
*/
|
|
152
|
-
export declare function listTables(d1:
|
|
151
|
+
export declare function listTables(d1: SQLDatabase): Promise<string[]>;
|
|
153
152
|
/**
|
|
154
153
|
* Moves a single record from a source D1 database to a target D1 database.
|
|
155
154
|
* This is typically used during shard rebalancing operations when data needs
|
|
@@ -186,7 +185,7 @@ export declare function listTables(d1: D1Database): Promise<string[]>;
|
|
|
186
185
|
* await migrateRecord(source, target, 'post-456', 'posts');
|
|
187
186
|
* ```
|
|
188
187
|
*/
|
|
189
|
-
export declare function migrateRecord(source:
|
|
188
|
+
export declare function migrateRecord(source: SQLDatabase, target: SQLDatabase, primaryKey: string, tableName: string): Promise<void>;
|
|
190
189
|
/**
|
|
191
190
|
* Scans an existing table to find all primary keys that need to be mapped to shards.
|
|
192
191
|
* This is useful when integrating CollegeDB with an existing database that already
|
|
@@ -207,7 +206,7 @@ export declare function migrateRecord(source: D1Database, target: D1Database, pr
|
|
|
207
206
|
* const orderIds = await discoverExistingPrimaryKeys(env.DB_ORDERS, 'orders', 'order_id');
|
|
208
207
|
* ```
|
|
209
208
|
*/
|
|
210
|
-
export declare function discoverExistingPrimaryKeys(d1:
|
|
209
|
+
export declare function discoverExistingPrimaryKeys(d1: SQLDatabase, tableName: string, primaryKeyColumn?: string): Promise<string[]>;
|
|
211
210
|
/**
|
|
212
211
|
* Discovers existing records with additional columns for multi-key mapping support.
|
|
213
212
|
* Scans a table to find primary keys along with username, email, and name columns
|
|
@@ -229,7 +228,7 @@ export declare function discoverExistingPrimaryKeys(d1: D1Database, tableName: s
|
|
|
229
228
|
* ```
|
|
230
229
|
* @since 1.0.4
|
|
231
230
|
*/
|
|
232
|
-
export declare function discoverExistingRecordsWithColumns(d1:
|
|
231
|
+
export declare function discoverExistingRecordsWithColumns(d1: SQLDatabase, tableName: string, primaryKeyColumn?: string): Promise<Array<{
|
|
233
232
|
[key: string]: any;
|
|
234
233
|
}>>;
|
|
235
234
|
/**
|
|
@@ -255,7 +254,9 @@ export declare function discoverExistingRecordsWithColumns(d1: D1Database, table
|
|
|
255
254
|
* console.log('All existing users mapped to shards');
|
|
256
255
|
* ```
|
|
257
256
|
*/
|
|
258
|
-
export declare function createMappingsForExistingKeys(primaryKeys: string[], shardBindings: string[], strategy: 'hash' | 'round-robin' | 'random', mapper:
|
|
257
|
+
export declare function createMappingsForExistingKeys(primaryKeys: string[], shardBindings: string[], strategy: 'hash' | 'round-robin' | 'random', mapper: KVShardMapper, options?: {
|
|
258
|
+
concurrency?: number;
|
|
259
|
+
}): Promise<void>;
|
|
259
260
|
/**
|
|
260
261
|
* Represents the result of validating a table for sharding.
|
|
261
262
|
* Contains information about the table structure, primary key, record count,
|
|
@@ -289,7 +290,7 @@ export type ValidationResult = {
|
|
|
289
290
|
* }
|
|
290
291
|
* ```
|
|
291
292
|
*/
|
|
292
|
-
export declare function validateTableForSharding(d1:
|
|
293
|
+
export declare function validateTableForSharding(d1: SQLDatabase, tableName: string, primaryKeyColumn: string): Promise<ValidationResult>;
|
|
293
294
|
/**
|
|
294
295
|
* Configuration options for integrating an existing database with CollegeDB.
|
|
295
296
|
* Allows customization of which tables to process, primary key column,
|
|
@@ -302,6 +303,7 @@ export type IntegrationOptions = {
|
|
|
302
303
|
addShardMappingsTable?: boolean;
|
|
303
304
|
dryRun?: boolean;
|
|
304
305
|
migrateOtherColumns?: boolean;
|
|
306
|
+
concurrency?: number;
|
|
305
307
|
};
|
|
306
308
|
/**
|
|
307
309
|
* Represents the result of integrating an existing database with CollegeDB.
|
|
@@ -350,7 +352,7 @@ export type IntegrationResult = {
|
|
|
350
352
|
* // Now users can be looked up by username:john, email:john@example.com, or name:John Doe
|
|
351
353
|
* ```
|
|
352
354
|
*/
|
|
353
|
-
export declare function integrateExistingDatabase(d1:
|
|
355
|
+
export declare function integrateExistingDatabase(d1: SQLDatabase, shardName: string, mapper: KVShardMapper, options?: IntegrationOptions): Promise<IntegrationResult>;
|
|
354
356
|
/**
|
|
355
357
|
* Automatically detects if a database needs migration and performs it
|
|
356
358
|
*
|
|
@@ -384,12 +386,13 @@ export declare function integrateExistingDatabase(d1: D1Database, shardName: str
|
|
|
384
386
|
* }
|
|
385
387
|
* ```
|
|
386
388
|
*/
|
|
387
|
-
export declare function autoDetectAndMigrate(d1:
|
|
389
|
+
export declare function autoDetectAndMigrate(d1: SQLDatabase, shardName: string, config: CollegeDBConfig, options?: {
|
|
388
390
|
primaryKeyColumn?: string;
|
|
389
391
|
tablesToCheck?: string[];
|
|
390
392
|
skipCache?: boolean;
|
|
391
393
|
maxRecordsToCheck?: number;
|
|
392
394
|
migrateOtherColumns?: boolean;
|
|
395
|
+
concurrency?: number;
|
|
393
396
|
}): Promise<{
|
|
394
397
|
migrationNeeded: boolean;
|
|
395
398
|
migrationPerformed: boolean;
|
|
@@ -414,7 +417,7 @@ export declare function autoDetectAndMigrate(d1: D1Database, shardName: string,
|
|
|
414
417
|
* }
|
|
415
418
|
* ```
|
|
416
419
|
*/
|
|
417
|
-
export declare function checkMigrationNeeded(d1:
|
|
420
|
+
export declare function checkMigrationNeeded(d1: SQLDatabase, shardName: string, config: CollegeDBConfig): Promise<boolean>;
|
|
418
421
|
/**
|
|
419
422
|
* Clears the migration status cache
|
|
420
423
|
*
|
package/dist/migrations.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;
|
|
1
|
+
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAuEjF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,YAAY,CAAC,EAAE,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAcjF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQjH;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,YAAY,CAAC,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAOnF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,WAAW,EAAE,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAQpF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,UAAU,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAOnE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,EAAE,SA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MAAM,CAAC;IACxB,MAAM,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC,CA4MD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,oBAAoB,CAAC,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,CA2DxH;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAE1C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAGhE"}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Provider adapters for non-Cloudflare backends.
|
|
3
|
+
*
|
|
4
|
+
* This module defines maintainable adapter factories that allow CollegeDB to run
|
|
5
|
+
* on multiple storage backends while preserving Cloudflare compatibility.
|
|
6
|
+
*
|
|
7
|
+
* Supported KV backends:
|
|
8
|
+
* - Cloudflare KV (native shape)
|
|
9
|
+
* - Redis
|
|
10
|
+
* - Valkey
|
|
11
|
+
*
|
|
12
|
+
* Supported SQL backends:
|
|
13
|
+
* - Cloudflare D1 (native shape)
|
|
14
|
+
* - PostgreSQL compatible clients
|
|
15
|
+
* - MySQL / MariaDB compatible clients
|
|
16
|
+
* - SQLite clients
|
|
17
|
+
* - Hyperdrive-backed PostgreSQL / MySQL clients
|
|
18
|
+
*
|
|
19
|
+
* @author CollegeDB Team
|
|
20
|
+
* @since 1.1.0
|
|
21
|
+
*/
|
|
22
|
+
import type { KVStorage, SQLDatabase } from './types.js';
|
|
23
|
+
/**
|
|
24
|
+
* Minimal Redis/Valkey client contract used by the KV adapter.
|
|
25
|
+
*/
|
|
26
|
+
export interface RedisLikeClient {
|
|
27
|
+
get(key: string): Promise<string | null>;
|
|
28
|
+
set(key: string, value: string): Promise<unknown>;
|
|
29
|
+
del(key: string): Promise<unknown>;
|
|
30
|
+
scan(cursor: string, ...args: any[]): Promise<RedisScanResult>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Redis/Valkey SCAN response in ioredis tuple form.
|
|
34
|
+
*/
|
|
35
|
+
export type RedisScanTupleResult = [string, string[]];
|
|
36
|
+
/**
|
|
37
|
+
* Redis/Valkey SCAN response in node-redis object form.
|
|
38
|
+
*/
|
|
39
|
+
export interface RedisScanObjectResult {
|
|
40
|
+
cursor: string | number;
|
|
41
|
+
keys: string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Accepted SCAN response formats.
|
|
45
|
+
*/
|
|
46
|
+
export type RedisScanResult = RedisScanTupleResult | RedisScanObjectResult;
|
|
47
|
+
/**
|
|
48
|
+
* PostgreSQL result shape used by adapters.
|
|
49
|
+
*/
|
|
50
|
+
export interface PostgresQueryResult<T = Record<string, unknown>> {
|
|
51
|
+
rows: T[];
|
|
52
|
+
rowCount?: number | null;
|
|
53
|
+
command?: string;
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Minimal PostgreSQL client contract used by the SQL adapter.
|
|
58
|
+
*/
|
|
59
|
+
export interface PostgresClientLike {
|
|
60
|
+
query<T = Record<string, unknown>>(sql: string, bindings?: any[]): Promise<PostgresQueryResult<T>>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Optional lifecycle methods used by Hyperdrive helpers.
|
|
64
|
+
*/
|
|
65
|
+
export interface PostgresLifecycleClientLike extends PostgresClientLike {
|
|
66
|
+
connect?: () => Promise<void>;
|
|
67
|
+
end?: () => Promise<void>;
|
|
68
|
+
release?: () => void;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* MySQL/MariaDB OK packet shape.
|
|
72
|
+
*/
|
|
73
|
+
export interface MySQLOkPacket {
|
|
74
|
+
affectedRows?: number;
|
|
75
|
+
insertId?: number;
|
|
76
|
+
warningStatus?: number;
|
|
77
|
+
[key: string]: unknown;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Minimal MySQL/MariaDB client contract used by adapters.
|
|
81
|
+
*/
|
|
82
|
+
export interface MySQLClientLike {
|
|
83
|
+
execute?: (sql: string, bindings?: any[]) => Promise<[unknown, unknown]>;
|
|
84
|
+
query?: (sql: string, bindings?: any[]) => Promise<[unknown, unknown]>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Optional lifecycle methods used by Hyperdrive helpers.
|
|
88
|
+
*/
|
|
89
|
+
export interface MySQLLifecycleClientLike extends MySQLClientLike {
|
|
90
|
+
end?: () => Promise<void>;
|
|
91
|
+
close?: () => Promise<void>;
|
|
92
|
+
destroy?: () => void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Statement contract for SQLite adapters.
|
|
96
|
+
*/
|
|
97
|
+
export interface SQLiteStatementLike {
|
|
98
|
+
run?: (...bindings: any[]) => unknown | Promise<unknown>;
|
|
99
|
+
all?: (...bindings: any[]) => unknown[] | Promise<unknown[]>;
|
|
100
|
+
get?: (...bindings: any[]) => unknown | Promise<unknown>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Minimal SQLite client contract used by adapters.
|
|
104
|
+
*/
|
|
105
|
+
export interface SQLiteClientLike {
|
|
106
|
+
prepare?: (sql: string) => SQLiteStatementLike;
|
|
107
|
+
execute?: (sql: string, bindings?: any[]) => Promise<unknown>;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Hyperdrive binding shape used by helper factories.
|
|
111
|
+
*/
|
|
112
|
+
export interface HyperdriveBindingLike {
|
|
113
|
+
connectionString: string;
|
|
114
|
+
localConnectionString?: string;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Factory for creating PostgreSQL clients from a Hyperdrive connection string.
|
|
118
|
+
*/
|
|
119
|
+
export type HyperdrivePostgresClientFactory = (connectionString: string) => PostgresLifecycleClientLike;
|
|
120
|
+
/**
|
|
121
|
+
* Factory for creating MySQL clients from a Hyperdrive connection string.
|
|
122
|
+
*/
|
|
123
|
+
export type HyperdriveMySQLClientFactory = (connectionString: string) => MySQLLifecycleClientLike;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a Redis-backed KV provider adapter.
|
|
126
|
+
*
|
|
127
|
+
* The adapter supports both node-redis and ioredis scan formats.
|
|
128
|
+
*
|
|
129
|
+
* @param client - Redis/Valkey client
|
|
130
|
+
* @param options - Adapter tuning options
|
|
131
|
+
* @returns KVStorage-compatible adapter
|
|
132
|
+
*/
|
|
133
|
+
export declare function createRedisKVProvider(client: RedisLikeClient, options?: {
|
|
134
|
+
scanCount?: number;
|
|
135
|
+
}): KVStorage;
|
|
136
|
+
/**
|
|
137
|
+
* Creates a Valkey-backed KV provider adapter.
|
|
138
|
+
*
|
|
139
|
+
* Valkey and Redis have compatible command surfaces for this adapter.
|
|
140
|
+
*
|
|
141
|
+
* @param client - Valkey client
|
|
142
|
+
* @param options - Adapter tuning options
|
|
143
|
+
* @returns KVStorage-compatible adapter
|
|
144
|
+
*/
|
|
145
|
+
export declare function createValkeyKVProvider(client: RedisLikeClient, options?: {
|
|
146
|
+
scanCount?: number;
|
|
147
|
+
}): KVStorage;
|
|
148
|
+
/**
|
|
149
|
+
* Creates a PostgreSQL adapter implementing CollegeDB's SQL contract.
|
|
150
|
+
*
|
|
151
|
+
* Supports `pg` Client, Pool, and compatible implementations.
|
|
152
|
+
* SQL using `?` placeholders is automatically rewritten to `$1..$n`.
|
|
153
|
+
*
|
|
154
|
+
* @param client - PostgreSQL client/pool
|
|
155
|
+
* @returns SQLDatabase-compatible adapter
|
|
156
|
+
*/
|
|
157
|
+
export declare function createPostgreSQLProvider(client: PostgresClientLike): SQLDatabase;
|
|
158
|
+
/**
|
|
159
|
+
* Creates a MySQL/MariaDB adapter implementing CollegeDB's SQL contract.
|
|
160
|
+
*
|
|
161
|
+
* Supports mysql2/promise clients, pools, and compatible wrappers.
|
|
162
|
+
*
|
|
163
|
+
* @param client - MySQL/MariaDB client or pool
|
|
164
|
+
* @returns SQLDatabase-compatible adapter
|
|
165
|
+
*/
|
|
166
|
+
export declare function createMySQLProvider(client: MySQLClientLike): SQLDatabase;
|
|
167
|
+
/**
|
|
168
|
+
* Creates a SQLite adapter implementing CollegeDB's SQL contract.
|
|
169
|
+
*
|
|
170
|
+
* Supports both execute-style clients and prepare/run/get/all statement clients.
|
|
171
|
+
*
|
|
172
|
+
* @param client - SQLite client
|
|
173
|
+
* @returns SQLDatabase-compatible adapter
|
|
174
|
+
*/
|
|
175
|
+
export declare function createSQLiteProvider(client: SQLiteClientLike): SQLDatabase;
|
|
176
|
+
/**
|
|
177
|
+
* Creates a PostgreSQL adapter wired to a Hyperdrive binding.
|
|
178
|
+
*
|
|
179
|
+
* The returned provider creates a transient client for each statement execution.
|
|
180
|
+
* Hyperdrive handles connection pooling at the edge, so this pattern remains fast
|
|
181
|
+
* and scalable in Workers.
|
|
182
|
+
*
|
|
183
|
+
* @param hyperdrive - Hyperdrive binding
|
|
184
|
+
* @param clientFactory - Client factory (e.g. `connectionString => new Client({ connectionString })`)
|
|
185
|
+
* @returns SQLDatabase-compatible adapter
|
|
186
|
+
*/
|
|
187
|
+
export declare function createHyperdrivePostgresProvider(hyperdrive: HyperdriveBindingLike, clientFactory: HyperdrivePostgresClientFactory): SQLDatabase;
|
|
188
|
+
/**
|
|
189
|
+
* Creates a MySQL/MariaDB adapter wired to a Hyperdrive binding.
|
|
190
|
+
*
|
|
191
|
+
* The returned provider creates a transient client for each statement execution.
|
|
192
|
+
* Hyperdrive handles connection pooling under the hood.
|
|
193
|
+
*
|
|
194
|
+
* @param hyperdrive - Hyperdrive binding
|
|
195
|
+
* @param clientFactory - Client factory (e.g. `connectionString => mysql.createConnection(connectionString)`)
|
|
196
|
+
* @returns SQLDatabase-compatible adapter
|
|
197
|
+
*/
|
|
198
|
+
export declare function createHyperdriveMySQLProvider(hyperdrive: HyperdriveBindingLike, clientFactory: HyperdriveMySQLClientFactory): SQLDatabase;
|
|
199
|
+
/**
|
|
200
|
+
* Returns `true` when a value looks like an SQL database provider.
|
|
201
|
+
*/
|
|
202
|
+
export declare function isSQLDatabase(value: unknown): value is SQLDatabase;
|
|
203
|
+
/**
|
|
204
|
+
* Returns `true` when a value looks like a KV storage provider.
|
|
205
|
+
*/
|
|
206
|
+
export declare function isKVStorage(value: unknown): value is KVStorage;
|
|
207
|
+
//# sourceMappingURL=providers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,KAAK,EAAgB,SAAS,EAAmD,WAAW,EAAE,MAAM,YAAY,CAAC;AAIxH;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACzC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAClD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC/D;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACrC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,IAAI,EAAE,MAAM,EAAE,CAAC;CACf;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,oBAAoB,GAAG,qBAAqB,CAAC;AAE3E;;GAEG;AACH,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,E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AED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,eAAe,EAAE,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,SAAS,CAE/G;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,kBAAkB,GAAG,WAAW,CAMhF;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,eAAe,GAAG,WAAW,CAMxE;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,gBAAgB,GAAG,WAAW,CAM1E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gCAAgC,CAC/C,UAAU,EAAE,qBAAqB,EACjC,aAAa,EAAE,+BAA+B,GAC5C,WAAW,CAqBb;AAED;;;;;;;;;GASG;AACH,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,qBAAqB,EAAE,aAAa,EAAE,4BAA4B,GAAG,WAAW,CA0BzI;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAMlE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAO9D"}
|
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 {
|
|
43
|
-
import type { CollegeDBConfig, D1Region, ShardStats } from './types.js';
|
|
42
|
+
import type { Request } from '@cloudflare/workers-types';
|
|
43
|
+
import type { CollegeDBConfig, D1Region, PreparedStatement, QueryResult, SQLDatabase, 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
|
|
@@ -198,7 +198,7 @@ export declare function getClosestRegionFromIP(request: Request): D1Region;
|
|
|
198
198
|
* await createSchema(env.DB_NEW_SHARD, userSchema);
|
|
199
199
|
* ```
|
|
200
200
|
*/
|
|
201
|
-
export declare function createSchema(d1:
|
|
201
|
+
export declare function createSchema(d1: SQLDatabase, schema: string): Promise<void>;
|
|
202
202
|
/**
|
|
203
203
|
* Prepares a SQL statement for execution with operation-aware routing.
|
|
204
204
|
*
|
|
@@ -207,7 +207,7 @@ export declare function createSchema(d1: D1Database, schema: string): Promise<vo
|
|
|
207
207
|
* @returns Promise that resolves to a prepared statement
|
|
208
208
|
* @throws {Error} If preparation fails
|
|
209
209
|
*/
|
|
210
|
-
export declare function prepare(key: string, sql: string): Promise<
|
|
210
|
+
export declare function prepare(key: string, sql: string): Promise<PreparedStatement>;
|
|
211
211
|
/**
|
|
212
212
|
* Executes a statement on the appropriate shard based on the primary key.
|
|
213
213
|
* The primary key is used to determine which shard should store the record,
|
|
@@ -270,7 +270,7 @@ export declare function prepare(key: string, sql: string): Promise<D1PreparedSta
|
|
|
270
270
|
* );
|
|
271
271
|
* ```
|
|
272
272
|
*/
|
|
273
|
-
export declare function run<T = Record<string, unknown>>(key: string, sql: string, bindings?: any[]): Promise<
|
|
273
|
+
export declare function run<T = Record<string, unknown>>(key: string, sql: string, bindings?: any[]): Promise<QueryResult<T>>;
|
|
274
274
|
/**
|
|
275
275
|
* Retrieves all records matching the query for a given primary key.
|
|
276
276
|
*
|
|
@@ -302,7 +302,7 @@ export declare function run<T = Record<string, unknown>>(key: string, sql: strin
|
|
|
302
302
|
* console.log(`User has ${postsResult.meta.count} posts`);
|
|
303
303
|
* ```
|
|
304
304
|
*/
|
|
305
|
-
export declare function all<T = Record<string, unknown>>(key: string, sql: string, bindings?: any[]): Promise<
|
|
305
|
+
export declare function all<T = Record<string, unknown>>(key: string, sql: string, bindings?: any[]): Promise<QueryResult<T>>;
|
|
306
306
|
/**
|
|
307
307
|
* Retrieves the first record matching the query for a given primary key.
|
|
308
308
|
*
|
|
@@ -448,7 +448,7 @@ export declare function getShardStats(): Promise<ShardStats[]>;
|
|
|
448
448
|
* console.log(`Inserted user with ID: ${result.lastInsertId}`);
|
|
449
449
|
* ```
|
|
450
450
|
*/
|
|
451
|
-
export declare function runShard<T = Record<string, unknown>>(shardBinding: string, sql: string, bindings?: any[]): Promise<
|
|
451
|
+
export declare function runShard<T = Record<string, unknown>>(shardBinding: string, sql: string, bindings?: any[]): Promise<QueryResult<T>>;
|
|
452
452
|
/**
|
|
453
453
|
* Bypasses the normal routing logic to execute a query directly on a specified
|
|
454
454
|
* shard. This is useful for administrative operations, cross-shard queries,
|
|
@@ -482,7 +482,7 @@ export declare function runShard<T = Record<string, unknown>>(shardBinding: stri
|
|
|
482
482
|
* );
|
|
483
483
|
* ```
|
|
484
484
|
*/
|
|
485
|
-
export declare function allShard<T = Record<string, unknown>>(shardBinding: string, sql: string, bindings?: any[]): Promise<
|
|
485
|
+
export declare function allShard<T = Record<string, unknown>>(shardBinding: string, sql: string, bindings?: any[]): Promise<QueryResult<T>>;
|
|
486
486
|
/**
|
|
487
487
|
* Bypasses the normal routing logic to execute a query directly on a specified
|
|
488
488
|
* shard. This is useful for administrative operations, cross-shard queries,
|
|
@@ -522,7 +522,7 @@ export declare function firstShard<T = Record<string, unknown>>(shardBinding: st
|
|
|
522
522
|
* @returns Promise resolving to an array of results from each shard
|
|
523
523
|
* @since 1.0.4
|
|
524
524
|
*/
|
|
525
|
-
export declare function runAllShards<T = Record<string, unknown>>(sql: string, bindings?: any[], batchSize?: number): Promise<
|
|
525
|
+
export declare function runAllShards<T = Record<string, unknown>>(sql: string, bindings?: any[], batchSize?: number): Promise<QueryResult<T>[]>;
|
|
526
526
|
/**
|
|
527
527
|
* Executes a query on all shards and returns all matching records from each shard.
|
|
528
528
|
*
|
|
@@ -535,7 +535,7 @@ export declare function runAllShards<T = Record<string, unknown>>(sql: string, b
|
|
|
535
535
|
* @returns Promise resolving to an array of results from each shard
|
|
536
536
|
* @since 1.0.4
|
|
537
537
|
*/
|
|
538
|
-
export declare function allAllShards<T = Record<string, unknown>>(sql: string, bindings?: any[], batchSize?: number): Promise<
|
|
538
|
+
export declare function allAllShards<T = Record<string, unknown>>(sql: string, bindings?: any[], batchSize?: number): Promise<QueryResult<T>[]>;
|
|
539
539
|
/**
|
|
540
540
|
* Executes a query on all shards and returns the first matching record from each shard.
|
|
541
541
|
*
|
package/dist/router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,KAAK,EACX,eAAe,EACf,QAAQ,EAER,iBAAiB,EACjB,WAAW,EACX,WAAW,EAEX,UAAU,EAEV,MAAM,YAAY,CAAC;AA6CpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,eAAe,QA0BjD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,eAAe,iBAuB5D;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,CAAC,cAG5E;AA8DD;;;;;GAKG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAIlC;AAkGD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAA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