Skip to content

Commit 053f24e

Browse files
committed
feat: 增加连接池配置和只读副本支持,更新驱动和数据源架构
1 parent 8e51745 commit 053f24e

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

packages/spec/src/data/datasource.zod.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ export const DatasourceSchema = z.object({
118118
*/
119119
config: z.record(z.any()).describe('Driver specific configuration'),
120120

121+
/**
122+
* Connection Pool Configuration
123+
* Standard connection pooling settings.
124+
*/
125+
pool: z.object({
126+
min: z.number().default(0).describe('Minimum connections'),
127+
max: z.number().default(10).describe('Maximum connections'),
128+
idleTimeoutMillis: z.number().default(30000).describe('Idle timeout'),
129+
connectionTimeoutMillis: z.number().default(3000).describe('Connection establishment timeout'),
130+
}).optional().describe('Connection pool settings'),
131+
132+
/**
133+
* Read Replicas
134+
* Optional list of duplicate configurations for read-only operations.
135+
* Useful for scaling read throughput.
136+
*/
137+
readReplicas: z.array(z.record(z.any())).optional().describe('Read-only replica configurations'),
138+
121139
/**
122140
* Capability Overrides
123141
* Manually override what the driver claims to support.

packages/spec/src/data/driver.zod.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ export const DriverOptionsSchema = z.object({
2121
* Whether to bypass cache and force a fresh read.
2222
*/
2323
skipCache: z.boolean().optional().describe('Bypass cache'),
24+
25+
/**
26+
* Distributed Tracing Context.
27+
* Used for passing OpenTelemetry span context or request IDs for observability.
28+
*/
29+
traceContext: z.record(z.string()).optional().describe('OpenTelemetry context or request ID'),
30+
31+
/**
32+
* Tenant Identifier.
33+
* For multi-tenant databases (row-level security or schema-per-tenant).
34+
*/
35+
tenantId: z.string().optional().describe('Tenant Isolation identifier'),
2436
});
2537

2638
/**
@@ -109,6 +121,17 @@ export const DriverCapabilitiesSchema = z.object({
109121
* If false, arrays will be stored as JSON strings or in separate tables.
110122
*/
111123
arrayFields: z.boolean().describe('Supports array field types'),
124+
125+
/**
126+
* Whether the driver supports vector embeddings and similarity search.
127+
* Required for RAG (Retrieval-Augmented Generation) and AI features.
128+
*/
129+
vectorSearch: z.boolean().default(false).describe('Supports vector embeddings and similarity search'),
130+
131+
/**
132+
* Whether the driver supports geospatial queries.
133+
*/
134+
geoSpatial: z.boolean().default(false).describe('Supports geospatial queries'),
112135
});
113136

114137
/**
@@ -158,6 +181,20 @@ export const DriverInterfaceSchema = z.object({
158181
checkHealth: z.function()
159182
.returns(z.promise(z.boolean()))
160183
.describe('Health check'),
184+
185+
/**
186+
* Get Connection Pool Statistics.
187+
* Useful for monitoring database load.
188+
*/
189+
getPoolStats: z.function()
190+
.returns(z.object({
191+
total: z.number(),
192+
idle: z.number(),
193+
active: z.number(),
194+
waiting: z.number(),
195+
}).optional())
196+
.optional()
197+
.describe('Get connection pool statistics'),
161198

162199
// ============================================================================
163200
// Raw Execution (Escape Hatch)
@@ -211,6 +248,20 @@ export const DriverInterfaceSchema = z.object({
211248
.returns(z.promise(z.array(z.record(z.any()))))
212249
.describe('Find records'),
213250

251+
/**
252+
* Stream records matching the structured query.
253+
* Optimized for large datasets to avoid memory overflow.
254+
*
255+
* @param object - The name of the object.
256+
* @param query - The structured QueryAST.
257+
* @param options - Driver options.
258+
* @returns AsyncIterable/ReadableStream of records.
259+
*/
260+
findStream: z.function()
261+
.args(z.string(), QuerySchema, DriverOptionsSchema.optional())
262+
.returns(z.any()) // Zod cannot easily represent AsyncIterable<T>
263+
.describe('Stream records (AsyncIterable)'),
264+
214265
/**
215266
* Find a single record by query.
216267
* Similar to find(), but returns only the first match or null.
@@ -255,6 +306,20 @@ export const DriverInterfaceSchema = z.object({
255306
.returns(z.promise(z.record(z.any())))
256307
.describe('Update record'),
257308

309+
/**
310+
* Upsert (Update or Insert) a record.
311+
*
312+
* @param object - The object name.
313+
* @param data - The data to upsert.
314+
* @param conflictKeys - Fields to check for conflict (uniqueness).
315+
* @param options - Driver options.
316+
* @returns The created or updated record.
317+
*/
318+
upsert: z.function()
319+
.args(z.string(), z.record(z.any()), z.array(z.string()).optional(), DriverOptionsSchema.optional())
320+
.returns(z.promise(z.record(z.any())))
321+
.describe('Upsert record'),
322+
258323
/**
259324
* Delete a record by ID.
260325
*
@@ -324,9 +389,13 @@ export const DriverInterfaceSchema = z.object({
324389

325390
/**
326391
* Begin a new database transaction.
392+
* @param options - Isolation level and other settings.
327393
* @returns A transaction handle to be passed to subsequent operations via `options.transaction`.
328394
*/
329395
beginTransaction: z.function()
396+
.args(z.object({
397+
isolationLevel: z.enum(['READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ', 'SERIALIZABLE', 'SNAPSHOT']).optional()
398+
}).optional())
330399
.returns(z.promise(z.any()))
331400
.describe('Start transaction'),
332401

@@ -375,6 +444,19 @@ export const DriverInterfaceSchema = z.object({
375444
dropTable: z.function()
376445
.args(z.string(), DriverOptionsSchema.optional())
377446
.returns(z.promise(z.void())),
447+
448+
/**
449+
* Analyze query performance.
450+
* Returns execution plan without executing the query (where possible).
451+
*
452+
* @param object - The object name.
453+
* @param query - The query to explain.
454+
* @returns The execution plan details.
455+
*/
456+
explain: z.function()
457+
.args(z.string(), QuerySchema, DriverOptionsSchema.optional())
458+
.returns(z.promise(z.any()))
459+
.optional(),
378460
});
379461

380462
/**

0 commit comments

Comments
 (0)