Skip to content

Commit dd0e2b4

Browse files
Copilothotlong
andcommitted
WIP: Update types to use DriverInterface from @objectstack/spec
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2b0327e commit dd0e2b4

6 files changed

Lines changed: 94 additions & 39 deletions

File tree

packages/drivers/sql/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@objectql/types": "workspace:*",
26+
"@objectstack/spec": "^0.1.2",
2627
"knex": "^3.1.0"
2728
},
2829
"devDependencies": {

packages/drivers/sql/src/index.ts

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
import { Driver, IntrospectedSchema, IntrospectedTable, IntrospectedColumn, IntrospectedForeignKey } from '@objectql/types';
9+
import { IntrospectedSchema, IntrospectedTable, IntrospectedColumn, IntrospectedForeignKey } from '@objectql/types';
10+
import type { DriverInterface } from '@objectstack/spec';
1011
import knex, { Knex } from 'knex';
1112

12-
export class SqlDriver implements Driver {
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+
1324
private knex: Knex;
1425
private config: any;
1526
private jsonFields: Record<string, string[]> = {};
@@ -219,16 +230,41 @@ export class SqlDriver implements Driver {
219230
return 0;
220231
}
221232

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+
222258
// Transaction Support
223259
async beginTransaction(): Promise<any> {
224260
return await this.knex.transaction();
225261
}
226262

227-
async commitTransaction(trx: Knex.Transaction): Promise<void> {
263+
async commit(trx: Knex.Transaction): Promise<void> {
228264
await trx.commit();
229265
}
230266

231-
async rollbackTransaction(trx: Knex.Transaction): Promise<void> {
267+
async rollback(trx: Knex.Transaction): Promise<void> {
232268
await trx.rollback();
233269
}
234270

@@ -277,23 +313,45 @@ export class SqlDriver implements Driver {
277313
}
278314
}
279315

280-
// Bulk
281-
async createMany(objectName: string, data: any[], options?: any): Promise<any> {
316+
// Bulk Operations
317+
async bulkCreate(objectName: string, data: any[], options?: any): Promise<any> {
282318
const builder = this.getBuilder(objectName, options);
283319
return await builder.insert(data).returning('*');
284320
}
285321

286-
async updateMany(objectName: string, filters: any, data: any, options?: any): Promise<any> {
322+
async bulkUpdate(objectName: string, filters: any, data: any, options?: any): Promise<any> {
287323
const builder = this.getBuilder(objectName, options);
288324
if(filters) this.applyFilters(builder, filters);
289325
return await builder.update(data);
290326
}
291327

292-
async deleteMany(objectName: string, filters: any, options?: any): Promise<any> {
328+
async bulkDelete(objectName: string, filters: any, options?: any): Promise<any> {
293329
const builder = this.getBuilder(objectName, options);
294330
if(filters) this.applyFilters(builder, filters);
295331
return await builder.delete();
296332
}
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+
}
297355

298356
async init(objects: any[]): Promise<void> {
299357
await this.ensureDatabaseExists();
@@ -506,6 +564,23 @@ export class SqlDriver implements Driver {
506564
return data;
507565
}
508566

567+
/**
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+
509584
/**
510585
* Introspect the database schema to discover existing tables, columns, and relationships.
511586
*/

packages/foundation/core/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ export class ObjectQL implements IObjectQL {
159159

160160
try {
161161
const result = await callback(trxCtx);
162-
if (driver.commitTransaction) await driver.commitTransaction(trx);
162+
if (driver.commit) await driver.commit(trx);
163163
return result;
164164
} catch (error) {
165-
if (driver.rollbackTransaction) await driver.rollbackTransaction(trx);
165+
if (driver.rollback) await driver.rollback(trx);
166166
throw error;
167167
}
168168
},

packages/foundation/core/src/repository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
import { ObjectQLContext, IObjectQL, ObjectConfig, Driver, UnifiedQuery, ActionContext, HookAPI, RetrievalHookContext, MutationHookContext, UpdateHookContext, ValidationContext, ValidationError, ValidationRuleResult, FormulaContext } from '@objectql/types';
9+
import { ObjectQLContext, IObjectQL, ObjectConfig, UnifiedQuery, ActionContext, HookAPI, RetrievalHookContext, MutationHookContext, UpdateHookContext, ValidationContext, ValidationError, ValidationRuleResult, FormulaContext } from '@objectql/types';
10+
import type { DriverInterface } from '@objectstack/spec';
1011
import { Validator } from './validator';
1112
import { FormulaEngine } from './formula-engine';
1213

@@ -23,7 +24,7 @@ export class ObjectRepository {
2324
this.formulaEngine = new FormulaEngine();
2425
}
2526

26-
private getDriver(): Driver {
27+
private getDriver(): DriverInterface {
2728
const obj = this.getSchema();
2829
const datasourceName = obj.datasource || 'default';
2930
return this.app.datasource(datasourceName);

packages/foundation/types/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
*/
88

99
import { ObjectConfig } from "./object";
10-
import { Driver } from "./driver";
1110
import { MetadataRegistry } from "./registry";
1211
import { HookName, HookHandler, HookContext } from "./hook";
1312
import { ActionHandler, ActionContext } from "./action";
1413
import { LoaderPlugin } from "./loader";
14+
import type { DriverInterface } from "@objectstack/spec";
1515

1616
export interface IObjectQL {
1717
getObject(name: string): ObjectConfig | undefined;
1818
getConfigs(): Record<string, ObjectConfig>;
19-
datasource(name: string): Driver;
19+
datasource(name: string): DriverInterface;
2020
init(): Promise<void>;
2121
close?(): Promise<void>;
2222
removePackage(name: string): void;

pnpm-lock.yaml

Lines changed: 3 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)