-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver-sql.zod.ts
More file actions
218 lines (206 loc) · 6.68 KB
/
driver-sql.zod.ts
File metadata and controls
218 lines (206 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { DriverConfigSchema } from './driver.zod';
/**
* SQL Dialect Enumeration
* Supported SQL database dialects
*/
export const SQLDialectSchema = z.enum([
'postgresql',
'mysql',
'sqlite',
'mssql',
'oracle',
'mariadb',
]);
export type SQLDialect = z.infer<typeof SQLDialectSchema>;
/**
* Data Type Mapping Schema
* Maps ObjectStack field types to SQL-specific data types
*
* @example PostgreSQL data type mapping
* {
* text: 'VARCHAR(255)',
* number: 'NUMERIC',
* boolean: 'BOOLEAN',
* date: 'DATE',
* datetime: 'TIMESTAMP',
* json: 'JSONB',
* uuid: 'UUID',
* binary: 'BYTEA'
* }
*/
export const DataTypeMappingSchema = z.object({
text: z.string().describe('SQL type for text fields (e.g., VARCHAR, TEXT)'),
number: z.string().describe('SQL type for number fields (e.g., NUMERIC, DECIMAL, INT)'),
boolean: z.string().describe('SQL type for boolean fields (e.g., BOOLEAN, BIT)'),
date: z.string().describe('SQL type for date fields (e.g., DATE)'),
datetime: z.string().describe('SQL type for datetime fields (e.g., TIMESTAMP, DATETIME)'),
json: z.string().optional().describe('SQL type for JSON fields (e.g., JSON, JSONB)'),
uuid: z.string().optional().describe('SQL type for UUID fields (e.g., UUID, CHAR(36))'),
binary: z.string().optional().describe('SQL type for binary fields (e.g., BLOB, BYTEA)'),
});
export type DataTypeMapping = z.infer<typeof DataTypeMappingSchema>;
/**
* SSL Configuration Schema
* SSL/TLS connection configuration for secure database connections
*
* @example PostgreSQL SSL configuration
* {
* rejectUnauthorized: true,
* ca: '/path/to/ca-cert.pem',
* cert: '/path/to/client-cert.pem',
* key: '/path/to/client-key.pem'
* }
*/
export const SSLConfigSchema = z.object({
rejectUnauthorized: z.boolean().default(true).describe('Reject connections with invalid certificates'),
ca: z.string().optional().describe('CA certificate file path or content'),
cert: z.string().optional().describe('Client certificate file path or content'),
key: z.string().optional().describe('Client private key file path or content'),
}).refine((data) => {
// If cert is provided, key must also be provided, and vice versa
const hasCert = data.cert !== undefined;
const hasKey = data.key !== undefined;
return hasCert === hasKey;
}, {
message: 'Client certificate (cert) and private key (key) must be provided together',
});
export type SSLConfig = z.infer<typeof SSLConfigSchema>;
/**
* SQL Driver Configuration Schema
* Extended driver configuration specific to SQL databases
*
* @example PostgreSQL driver configuration
* {
* name: 'primary-db',
* type: 'sql',
* dialect: 'postgresql',
* connectionString: 'postgresql://user:pass@localhost:5432/mydb',
* dataTypeMapping: {
* text: 'VARCHAR(255)',
* number: 'NUMERIC',
* boolean: 'BOOLEAN',
* date: 'DATE',
* datetime: 'TIMESTAMP',
* json: 'JSONB',
* uuid: 'UUID',
* binary: 'BYTEA'
* },
* ssl: true,
* sslConfig: {
* rejectUnauthorized: true,
* ca: '/etc/ssl/certs/ca.pem'
* },
* poolConfig: {
* min: 2,
* max: 10,
* idleTimeoutMillis: 30000,
* connectionTimeoutMillis: 5000
* },
* capabilities: {
* create: true,
* read: true,
* update: true,
* delete: true,
* bulkCreate: true,
* bulkUpdate: true,
* bulkDelete: true,
* transactions: true,
* savepoints: true,
* isolationLevels: ['read-committed', 'repeatable-read', 'serializable'],
* queryFilters: true,
* queryAggregations: true,
* querySorting: true,
* queryPagination: true,
* queryWindowFunctions: true,
* querySubqueries: true,
* queryCTE: true,
* joins: true,
* fullTextSearch: true,
* jsonQuery: true,
* geospatialQuery: false,
* streaming: true,
* jsonFields: true,
* arrayFields: true,
* vectorSearch: true,
* schemaSync: true,
* migrations: true,
* indexes: true,
* connectionPooling: true,
* preparedStatements: true,
* queryCache: false
* }
* }
*/
export const SQLDriverConfigSchema = DriverConfigSchema.extend({
type: z.literal('sql').describe('Driver type must be "sql"'),
dialect: SQLDialectSchema.describe('SQL database dialect'),
dataTypeMapping: DataTypeMappingSchema.describe('SQL data type mapping configuration'),
ssl: z.boolean().default(false).describe('Enable SSL/TLS connection'),
sslConfig: SSLConfigSchema.optional().describe('SSL/TLS configuration (required when ssl is true)'),
}).refine((data) => {
// If ssl is enabled, sslConfig must be provided
if (data.ssl && !data.sslConfig) {
return false;
}
return true;
}, {
message: 'sslConfig is required when ssl is true',
});
export type SQLDriverConfig = z.infer<typeof SQLDriverConfigSchema>;
// ==========================================================================
// SQLite-Specific Constants
// ==========================================================================
/**
* Default data type mappings for SQLite/libSQL dialect.
*
* SQLite uses a simplified type system with type affinity:
* - TEXT: strings, dates, UUIDs
* - REAL: floating-point numbers
* - INTEGER: integers, booleans
* - BLOB: binary data
*
* @see https://www.sqlite.org/datatype3.html
*/
export const SQLiteDataTypeMappingDefaults: DataTypeMapping = {
text: 'TEXT',
number: 'REAL',
boolean: 'INTEGER',
date: 'TEXT',
datetime: 'TEXT',
json: 'TEXT',
uuid: 'TEXT',
binary: 'BLOB',
};
/**
* SQLite ALTER TABLE Limitations.
*
* SQLite has limited ALTER TABLE support compared to other SQL databases.
* This constant documents the known limitations that affect migration planning.
* The schema diff service must use the "table rebuild" strategy for operations
* that SQLite cannot perform natively.
*
* @see https://www.sqlite.org/lang_altertable.html
*/
export const SQLiteAlterTableLimitations = {
/** SQLite supports ADD COLUMN */
supportsAddColumn: true,
/** SQLite supports RENAME COLUMN (3.25.0+) */
supportsRenameColumn: true,
/** SQLite supports DROP COLUMN (3.35.0+) */
supportsDropColumn: true,
/** SQLite does NOT support MODIFY/ALTER COLUMN type */
supportsModifyColumn: false,
/** SQLite does NOT support adding constraints to existing columns */
supportsAddConstraint: false,
/**
* When an unsupported alteration is needed, the migration planner
* must use the 12-step table rebuild strategy:
* 1. CREATE new table with desired schema
* 2. Copy data from old table
* 3. DROP old table
* 4. RENAME new table to old name
*/
rebuildStrategy: 'create_copy_drop_rename',
} as const;