|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { IntrospectedSchema, IntrospectedTable, IntrospectedColumn, IntrospectedForeignKey } from '@objectql/types'; |
10 | | -import type { DriverInterface } from '@objectstack/spec'; |
| 9 | +import { Driver, IntrospectedSchema, IntrospectedTable, IntrospectedColumn, IntrospectedForeignKey } from '@objectql/types'; |
11 | 10 | import knex, { Knex } from 'knex'; |
12 | 11 |
|
13 | | -export class SqlDriver implements DriverInterface { |
14 | | - readonly name = 'sql'; |
15 | | - readonly version = '3.0.1'; |
16 | | - readonly supports = { |
17 | | - transactions: true, |
18 | | - joins: true, |
19 | | - fullTextSearch: false, |
20 | | - jsonFields: true, |
21 | | - arrayFields: false |
22 | | - }; |
23 | | - |
| 12 | +/** |
| 13 | + * SQL Driver for ObjectQL |
| 14 | + * Implements Driver interface from @objectql/types |
| 15 | + */ |
| 16 | +export class SqlDriver implements Driver { |
24 | 17 | private knex: Knex; |
25 | 18 | private config: any; |
26 | 19 | private jsonFields: Record<string, string[]> = {}; |
@@ -230,41 +223,16 @@ export class SqlDriver implements DriverInterface { |
230 | 223 | return 0; |
231 | 224 | } |
232 | 225 |
|
233 | | - // Connection Management |
234 | | - async connect(): Promise<void> { |
235 | | - // Knex initializes connection pool automatically |
236 | | - // We can test the connection here |
237 | | - await this.knex.raw('SELECT 1'); |
238 | | - } |
239 | | - |
240 | | - async checkHealth(): Promise<boolean> { |
241 | | - try { |
242 | | - await this.knex.raw('SELECT 1'); |
243 | | - return true; |
244 | | - } catch { |
245 | | - return false; |
246 | | - } |
247 | | - } |
248 | | - |
249 | | - async execute(command: any, parameters?: any[], options?: any): Promise<any> { |
250 | | - // For SQL driver, execute raw SQL |
251 | | - if (typeof command === 'string') { |
252 | | - return await this.knex.raw(command, parameters); |
253 | | - } |
254 | | - // For object commands, could be knex query builder |
255 | | - throw new Error('Execute with non-string commands not supported in SQL driver'); |
256 | | - } |
257 | | - |
258 | 226 | // Transaction Support |
259 | 227 | async beginTransaction(): Promise<any> { |
260 | 228 | return await this.knex.transaction(); |
261 | 229 | } |
262 | 230 |
|
263 | | - async commit(trx: Knex.Transaction): Promise<void> { |
| 231 | + async commitTransaction(trx: Knex.Transaction): Promise<void> { |
264 | 232 | await trx.commit(); |
265 | 233 | } |
266 | 234 |
|
267 | | - async rollback(trx: Knex.Transaction): Promise<void> { |
| 235 | + async rollbackTransaction(trx: Knex.Transaction): Promise<void> { |
268 | 236 | await trx.rollback(); |
269 | 237 | } |
270 | 238 |
|
@@ -314,44 +282,22 @@ export class SqlDriver implements DriverInterface { |
314 | 282 | } |
315 | 283 |
|
316 | 284 | // Bulk Operations |
317 | | - async bulkCreate(objectName: string, data: any[], options?: any): Promise<any> { |
| 285 | + async createMany(objectName: string, data: any[], options?: any): Promise<any> { |
318 | 286 | const builder = this.getBuilder(objectName, options); |
319 | 287 | return await builder.insert(data).returning('*'); |
320 | 288 | } |
321 | 289 |
|
322 | | - async bulkUpdate(objectName: string, filters: any, data: any, options?: any): Promise<any> { |
| 290 | + async updateMany(objectName: string, filters: any, data: any, options?: any): Promise<any> { |
323 | 291 | const builder = this.getBuilder(objectName, options); |
324 | 292 | if(filters) this.applyFilters(builder, filters); |
325 | 293 | return await builder.update(data); |
326 | 294 | } |
327 | 295 |
|
328 | | - async bulkDelete(objectName: string, filters: any, options?: any): Promise<any> { |
| 296 | + async deleteMany(objectName: string, filters: any, options?: any): Promise<any> { |
329 | 297 | const builder = this.getBuilder(objectName, options); |
330 | 298 | if(filters) this.applyFilters(builder, filters); |
331 | 299 | return await builder.delete(); |
332 | 300 | } |
333 | | - |
334 | | - // Aliases for backward compatibility |
335 | | - async createMany(objectName: string, data: any[], options?: any): Promise<any> { |
336 | | - return this.bulkCreate(objectName, data, options); |
337 | | - } |
338 | | - |
339 | | - async updateMany(objectName: string, filters: any, data: any, options?: any): Promise<any> { |
340 | | - return this.bulkUpdate(objectName, filters, data, options); |
341 | | - } |
342 | | - |
343 | | - async deleteMany(objectName: string, filters: any, options?: any): Promise<any> { |
344 | | - return this.bulkDelete(objectName, filters, options); |
345 | | - } |
346 | | - |
347 | | - // Transaction aliases for backward compatibility |
348 | | - async commitTransaction(trx: Knex.Transaction): Promise<void> { |
349 | | - return this.commit(trx); |
350 | | - } |
351 | | - |
352 | | - async rollbackTransaction(trx: Knex.Transaction): Promise<void> { |
353 | | - return this.rollback(trx); |
354 | | - } |
355 | 301 |
|
356 | 302 | async init(objects: any[]): Promise<void> { |
357 | 303 | await this.ensureDatabaseExists(); |
@@ -565,21 +511,6 @@ export class SqlDriver implements DriverInterface { |
565 | 511 | } |
566 | 512 |
|
567 | 513 | /** |
568 | | - * Synchronize schema - alias for init for DriverInterface compatibility |
569 | | - */ |
570 | | - async syncSchema(objects: any[]): Promise<void> { |
571 | | - return this.init(objects); |
572 | | - } |
573 | | - |
574 | | - /** |
575 | | - * Drop a table from the database |
576 | | - */ |
577 | | - async dropTable(tableName: string): Promise<void> { |
578 | | - const exists = await this.knex.schema.hasTable(tableName); |
579 | | - if (exists) { |
580 | | - await this.knex.schema.dropTable(tableName); |
581 | | - } |
582 | | - } |
583 | 514 |
|
584 | 515 | /** |
585 | 516 | * Introspect the database schema to discover existing tables, columns, and relationships. |
|
0 commit comments