@objectstack/driver-sql 4.0.3 → 4.0.5

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/src/sql-driver.ts DELETED
@@ -1,1373 +0,0 @@
1
- // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2
-
3
- /**
4
- * SQL Driver for ObjectStack
5
- *
6
- * Implements the standard IDataDriver from @objectstack/spec via Knex.js.
7
- * Supports PostgreSQL, MySQL, SQLite, and other SQL databases.
8
- */
9
-
10
- import type { QueryAST, DriverOptions } from '@objectstack/spec/data';
11
- import type { IDataDriver } from '@objectstack/spec/contracts';
12
- import knex, { Knex } from 'knex';
13
- import { nanoid } from 'nanoid';
14
-
15
- /**
16
- * Default ID length for auto-generated IDs.
17
- */
18
- const DEFAULT_ID_LENGTH = 16;
19
-
20
- // ── Introspection Types ──────────────────────────────────────────────────────
21
-
22
- export interface IntrospectedColumn {
23
- name: string;
24
- type: string;
25
- nullable: boolean;
26
- defaultValue?: unknown;
27
- isPrimary?: boolean;
28
- isUnique?: boolean;
29
- maxLength?: number;
30
- }
31
-
32
- export interface IntrospectedForeignKey {
33
- columnName: string;
34
- referencedTable: string;
35
- referencedColumn: string;
36
- constraintName?: string;
37
- }
38
-
39
- export interface IntrospectedTable {
40
- name: string;
41
- columns: IntrospectedColumn[];
42
- foreignKeys: IntrospectedForeignKey[];
43
- primaryKeys: string[];
44
- }
45
-
46
- export interface IntrospectedSchema {
47
- tables: Record<string, IntrospectedTable>;
48
- }
49
-
50
- // ── Configuration Types ──────────────────────────────────────────────────────
51
-
52
- /**
53
- * SqlDriver configuration — passed directly to Knex.
54
- * See https://knexjs.org/guide/#configuration-options
55
- */
56
- export type SqlDriverConfig = Knex.Config;
57
-
58
- // ── SQL Driver ───────────────────────────────────────────────────────────────
59
-
60
- /**
61
- * SQL Driver for ObjectStack.
62
- *
63
- * Implements the IDataDriver contract via Knex.js for optimal SQL
64
- * generation against PostgreSQL, MySQL, SQLite and other SQL databases.
65
- */
66
- export class SqlDriver implements IDataDriver {
67
- // IDataDriver metadata
68
- public readonly name: string = 'com.objectstack.driver.sql';
69
- public readonly version: string = '1.0.0';
70
- public readonly supports = {
71
- // Basic CRUD Operations
72
- create: true,
73
- read: true,
74
- update: true,
75
- delete: true,
76
-
77
- // Bulk Operations
78
- bulkCreate: true,
79
- bulkUpdate: true,
80
- bulkDelete: true,
81
-
82
- // Transaction & Connection Management
83
- transactions: true,
84
- savepoints: false,
85
-
86
- // Query Operations
87
- queryFilters: true,
88
- queryAggregations: true,
89
- querySorting: true,
90
- queryPagination: true,
91
- queryWindowFunctions: true,
92
- querySubqueries: true,
93
- queryCTE: false,
94
- joins: true,
95
-
96
- // Advanced Features
97
- fullTextSearch: false,
98
- jsonQuery: false,
99
- geospatialQuery: false,
100
- streaming: false,
101
- jsonFields: true,
102
- arrayFields: true,
103
- vectorSearch: false,
104
-
105
- // Schema Management
106
- schemaSync: true,
107
- batchSchemaSync: false,
108
- migrations: false,
109
- indexes: false,
110
-
111
- // Performance & Optimization
112
- connectionPooling: true,
113
- preparedStatements: true,
114
- queryCache: false,
115
- };
116
-
117
- protected knex: Knex;
118
- protected config: Knex.Config;
119
- protected jsonFields: Record<string, string[]> = {};
120
- protected booleanFields: Record<string, string[]> = {};
121
- protected tablesWithTimestamps: Set<string> = new Set();
122
-
123
- /** Whether the underlying database is a SQLite variant (sqlite3 or better-sqlite3). */
124
- protected get isSqlite(): boolean {
125
- const c = (this.config as any).client;
126
- return c === 'sqlite3' || c === 'better-sqlite3';
127
- }
128
-
129
- /** Whether the underlying database is PostgreSQL. */
130
- protected get isPostgres(): boolean {
131
- const c = (this.config as any).client;
132
- return c === 'pg' || c === 'postgresql';
133
- }
134
-
135
- /** Whether the underlying database is MySQL. */
136
- protected get isMysql(): boolean {
137
- const c = (this.config as any).client;
138
- return c === 'mysql' || c === 'mysql2';
139
- }
140
-
141
- constructor(config: SqlDriverConfig) {
142
- this.config = config;
143
- this.knex = knex(config);
144
- }
145
-
146
- // ===================================
147
- // Lifecycle
148
- // ===================================
149
-
150
- async connect(): Promise<void> {
151
- return Promise.resolve();
152
- }
153
-
154
- async checkHealth(): Promise<boolean> {
155
- try {
156
- await this.knex.raw('SELECT 1');
157
- return true;
158
- } catch {
159
- return false;
160
- }
161
- }
162
-
163
- async disconnect(): Promise<void> {
164
- await this.knex.destroy();
165
- }
166
-
167
- // ===================================
168
- // CRUD — DriverInterface core
169
- // ===================================
170
-
171
- async find(object: string, query: QueryAST, options?: DriverOptions): Promise<any[]> {
172
- const builder = this.getBuilder(object, options);
173
-
174
- // SELECT
175
- if (query.fields) {
176
- builder.select((query.fields as string[]).map((f: string) => this.mapSortField(f)));
177
- } else {
178
- builder.select('*');
179
- }
180
-
181
- // WHERE
182
- if (query.where) {
183
- this.applyFilters(builder, query.where);
184
- }
185
-
186
- // ORDER BY
187
- if (query.orderBy && Array.isArray(query.orderBy)) {
188
- for (const item of query.orderBy) {
189
- if (item.field) {
190
- builder.orderBy(this.mapSortField(item.field), item.order || 'asc');
191
- }
192
- }
193
- }
194
-
195
- // PAGINATION
196
- if (query.offset !== undefined) builder.offset(query.offset);
197
- if (query.limit !== undefined) builder.limit(query.limit);
198
-
199
- let results: any[];
200
- try {
201
- results = await builder;
202
- } catch (error: any) {
203
- if (
204
- error.message &&
205
- (error.message.includes('no such column') ||
206
- (error.message.includes('column') && error.message.includes('does not exist')))
207
- ) {
208
- return [];
209
- }
210
- throw error;
211
- }
212
-
213
- if (!Array.isArray(results)) {
214
- return [];
215
- }
216
-
217
- if (this.isSqlite) {
218
- for (const row of results) {
219
- this.formatOutput(object, row);
220
- }
221
- }
222
- return results;
223
- }
224
-
225
- async findOne(object: string, query: QueryAST, options?: DriverOptions): Promise<any> {
226
- // When called with a string/number id fall back gracefully
227
- if (typeof query === 'string' || typeof query === 'number') {
228
- const res = await this.getBuilder(object, options).where('id', query).first();
229
- return this.formatOutput(object, res) || null;
230
- }
231
-
232
- if (query && typeof query === 'object') {
233
- const results = await this.find(object, { ...query, limit: 1 }, options);
234
- return results[0] || null;
235
- }
236
-
237
- return null;
238
- }
239
-
240
- /**
241
- * Stream records matching a structured query.
242
- * NOTE: Current implementation fetches all results then yields them.
243
- * TODO: Use Knex .stream() for true cursor-based streaming on large datasets.
244
- */
245
- async *findStream(object: string, query: QueryAST, options?: DriverOptions): AsyncGenerator<Record<string, any>> {
246
- const results = await this.find(object, query, options);
247
- for (const row of results) {
248
- yield row;
249
- }
250
- }
251
-
252
- async create(object: string, data: Record<string, any>, options?: DriverOptions): Promise<any> {
253
- const { _id, ...rest } = data;
254
- const toInsert = { ...rest };
255
-
256
- if (_id !== undefined && toInsert.id === undefined) {
257
- toInsert.id = _id;
258
- } else if (toInsert.id === undefined) {
259
- toInsert.id = nanoid(DEFAULT_ID_LENGTH);
260
- }
261
-
262
- const builder = this.getBuilder(object, options);
263
- const formatted = this.formatInput(object, toInsert);
264
-
265
- const result = await builder.insert(formatted).returning('*');
266
- return this.formatOutput(object, result[0]);
267
- }
268
-
269
- async update(object: string, id: string | number, data: Record<string, any>, options?: DriverOptions): Promise<any> {
270
- const builder = this.getBuilder(object, options);
271
- const formatted = this.formatInput(object, data);
272
-
273
- if (this.tablesWithTimestamps.has(object)) {
274
- if (this.isSqlite) {
275
- const now = new Date();
276
- formatted.updated_at = now.toISOString().replace('T', ' ').replace('Z', '');
277
- } else {
278
- formatted.updated_at = this.knex.fn.now();
279
- }
280
- }
281
-
282
- await builder.where('id', id).update(formatted);
283
-
284
- const updated = await this.getBuilder(object, options).where('id', id).first();
285
- return this.formatOutput(object, updated) || null;
286
- }
287
-
288
- async upsert(object: string, data: Record<string, any>, conflictKeys?: string[], options?: DriverOptions): Promise<Record<string, any>> {
289
- const { _id, ...rest } = data;
290
- const toUpsert = { ...rest };
291
-
292
- if (_id !== undefined && toUpsert.id === undefined) {
293
- toUpsert.id = _id;
294
- } else if (toUpsert.id === undefined) {
295
- toUpsert.id = nanoid(DEFAULT_ID_LENGTH);
296
- }
297
-
298
- const formatted = this.formatInput(object, toUpsert);
299
- const mergeKeys = conflictKeys && conflictKeys.length > 0 ? conflictKeys : ['id'];
300
-
301
- const builder = this.getBuilder(object, options);
302
- await builder.insert(formatted).onConflict(mergeKeys).merge();
303
-
304
- const result = await this.getBuilder(object, options).where('id', toUpsert.id).first();
305
- return this.formatOutput(object, result) || toUpsert;
306
- }
307
-
308
- async delete(object: string, id: string | number, options?: DriverOptions): Promise<boolean> {
309
- const builder = this.getBuilder(object, options);
310
- const count = await builder.where('id', id).delete();
311
- return count > 0;
312
- }
313
-
314
- // ===================================
315
- // Bulk & Batch Operations
316
- // ===================================
317
-
318
- async bulkCreate(object: string, data: any[], options?: DriverOptions): Promise<any> {
319
- const builder = this.getBuilder(object, options);
320
- return await builder.insert(data).returning('*');
321
- }
322
-
323
- /**
324
- * Batch-update multiple records by ID.
325
- * NOTE: Current implementation performs sequential updates for correctness.
326
- * TODO: Optimize with SQL CASE statements or batched transactions for performance.
327
- */
328
- async bulkUpdate(object: string, updates: Array<{ id: string | number; data: Record<string, any> }>, options?: DriverOptions): Promise<Record<string, any>[]> {
329
- const results: Record<string, any>[] = [];
330
- for (const { id, data } of updates) {
331
- const updated = await this.update(object, id, data, options);
332
- if (updated) results.push(updated);
333
- }
334
- return results;
335
- }
336
-
337
- async bulkDelete(object: string, ids: Array<string | number>, options?: DriverOptions): Promise<void> {
338
- const builder = this.getBuilder(object, options);
339
- await builder.whereIn('id', ids).delete();
340
- }
341
-
342
- async updateMany(object: string, query: QueryAST, data: any, options?: DriverOptions): Promise<number> {
343
- const builder = this.getBuilder(object, options);
344
- if (query.where) this.applyFilters(builder, query.where);
345
- const count = await builder.update(data);
346
- return count || 0;
347
- }
348
-
349
- async deleteMany(object: string, query: QueryAST, options?: DriverOptions): Promise<number> {
350
- const builder = this.getBuilder(object, options);
351
- if (query.where) this.applyFilters(builder, query.where);
352
- const count = await builder.delete();
353
- return count || 0;
354
- }
355
-
356
- async count(object: string, query?: QueryAST, options?: DriverOptions): Promise<number> {
357
- const builder = this.getBuilder(object, options);
358
-
359
- if (query?.where) {
360
- this.applyFilters(builder, query.where);
361
- }
362
-
363
- const result = await builder.count<{ count: number }[]>('* as count');
364
- if (result && result.length > 0) {
365
- const row: any = result[0];
366
- return Number(row.count ?? row['count(*)'] ?? 0);
367
- }
368
- return 0;
369
- }
370
-
371
- // ===================================
372
- // Raw Execution
373
- // ===================================
374
-
375
- async execute(command: any, params?: any[], options?: DriverOptions): Promise<any> {
376
- if (typeof command !== 'string') {
377
- return command;
378
- }
379
-
380
- const builder =
381
- options?.transaction
382
- ? this.knex.raw(command, params || []).transacting(options.transaction as Knex.Transaction)
383
- : this.knex.raw(command, params || []);
384
-
385
- return await builder;
386
- }
387
-
388
- // ===================================
389
- // Transactions
390
- // ===================================
391
-
392
- async beginTransaction(): Promise<Knex.Transaction> {
393
- return await this.knex.transaction();
394
- }
395
-
396
- /** IDataDriver standard */
397
- async commit(transaction: unknown): Promise<void> {
398
- await (transaction as Knex.Transaction).commit();
399
- }
400
-
401
- /** IDataDriver standard */
402
- async rollback(transaction: unknown): Promise<void> {
403
- await (transaction as Knex.Transaction).rollback();
404
- }
405
-
406
- /** @deprecated Use commit() instead */
407
- async commitTransaction(trx: Knex.Transaction): Promise<void> {
408
- await this.commit(trx);
409
- }
410
-
411
- /** @deprecated Use rollback() instead */
412
- async rollbackTransaction(trx: Knex.Transaction): Promise<void> {
413
- await this.rollback(trx);
414
- }
415
-
416
- // ===================================
417
- // Aggregation
418
- // ===================================
419
-
420
- async aggregate(object: string, query: any, options?: DriverOptions): Promise<any> {
421
- const builder = this.getBuilder(object, options);
422
-
423
- if (query.where) {
424
- this.applyFilters(builder, query.where);
425
- }
426
-
427
- if (query.groupBy) {
428
- builder.groupBy(query.groupBy);
429
- for (const field of query.groupBy) {
430
- builder.select(field);
431
- }
432
- }
433
-
434
- const aggregates = query.aggregations || query.aggregate;
435
- if (aggregates) {
436
- for (const agg of aggregates) {
437
- const funcName = agg.function || agg.func;
438
- const rawFunc = this.mapAggregateFunc(funcName);
439
- if (agg.alias) {
440
- builder.select(this.knex.raw(`${rawFunc}(??) as ??`, [agg.field, agg.alias]));
441
- } else {
442
- builder.select(this.knex.raw(`${rawFunc}(??)`, [agg.field]));
443
- }
444
- }
445
- }
446
-
447
- return await builder;
448
- }
449
-
450
- // ===================================
451
- // Distinct
452
- // ===================================
453
-
454
- async distinct(object: string, field: string, filters?: any, options?: DriverOptions): Promise<any[]> {
455
- const builder = this.getBuilder(object, options);
456
-
457
- if (filters) {
458
- this.applyFilters(builder, filters);
459
- }
460
-
461
- builder.distinct(field);
462
- const results = await builder;
463
- return results.map((row: any) => row[field]);
464
- }
465
-
466
- // ===================================
467
- // Window Functions
468
- // ===================================
469
-
470
- async findWithWindowFunctions(object: string, query: any, options?: DriverOptions): Promise<any[]> {
471
- const builder = this.getBuilder(object, options);
472
-
473
- builder.select('*');
474
-
475
- if (query.where) {
476
- this.applyFilters(builder, query.where);
477
- }
478
-
479
- if (query.windowFunctions && Array.isArray(query.windowFunctions)) {
480
- for (const wf of query.windowFunctions) {
481
- const windowFunc = this.buildWindowFunction(wf);
482
- builder.select(this.knex.raw(`${windowFunc} as ??`, [wf.alias]));
483
- }
484
- }
485
-
486
- if (query.orderBy && Array.isArray(query.orderBy)) {
487
- for (const sort of query.orderBy) {
488
- builder.orderBy(this.mapSortField(sort.field), sort.order || 'asc');
489
- }
490
- }
491
-
492
- if (query.limit) builder.limit(query.limit);
493
- if (query.offset) builder.offset(query.offset);
494
-
495
- return await builder;
496
- }
497
-
498
- // ===================================
499
- // Query Plan Analysis
500
- // ===================================
501
-
502
- /** IDataDriver standard: analyze query performance */
503
- async explain(object: string, query: any, options?: DriverOptions): Promise<any> {
504
- return this.analyzeQuery(object, query, options);
505
- }
506
-
507
- async analyzeQuery(object: string, query: any, options?: DriverOptions): Promise<any> {
508
- const builder = this.getBuilder(object, options);
509
-
510
- if (query.fields) {
511
- builder.select(query.fields);
512
- } else {
513
- builder.select('*');
514
- }
515
-
516
- if (query.where) {
517
- this.applyFilters(builder, query.where);
518
- }
519
-
520
- if (query.orderBy && Array.isArray(query.orderBy)) {
521
- for (const sort of query.orderBy) {
522
- builder.orderBy(this.mapSortField(sort.field), sort.order || 'asc');
523
- }
524
- }
525
-
526
- if (query.limit) builder.limit(query.limit);
527
- if (query.offset) builder.offset(query.offset);
528
-
529
- const sql = builder.toSQL();
530
- const client = (this.config as any).client;
531
- let explainResults: any;
532
-
533
- try {
534
- if (this.isPostgres) {
535
- explainResults = await this.knex.raw(`EXPLAIN (FORMAT JSON, ANALYZE) ${sql.sql}`, sql.bindings);
536
- } else if (this.isMysql) {
537
- explainResults = await this.knex.raw(`EXPLAIN FORMAT=JSON ${sql.sql}`, sql.bindings);
538
- } else if (this.isSqlite) {
539
- explainResults = await this.knex.raw(`EXPLAIN QUERY PLAN ${sql.sql}`, sql.bindings);
540
- } else {
541
- return {
542
- sql: sql.sql,
543
- bindings: sql.bindings,
544
- client,
545
- note: 'EXPLAIN not supported for this database client',
546
- };
547
- }
548
-
549
- return { sql: sql.sql, bindings: sql.bindings, client, plan: explainResults };
550
- } catch (error: any) {
551
- return {
552
- sql: sql.sql,
553
- bindings: sql.bindings,
554
- client,
555
- error: error.message,
556
- note: 'Failed to execute EXPLAIN.',
557
- };
558
- }
559
- }
560
-
561
- // ===================================
562
- // Schema Sync (syncSchema / init)
563
- // ===================================
564
-
565
- async syncSchema(object: string, schema: unknown, _options?: DriverOptions): Promise<void> {
566
- const objectDef = schema as { name: string; fields?: Record<string, any> };
567
- // Use the physical table name (`object`) for DDL operations. The caller
568
- // (e.g. syncRegisteredSchemas) passes the resolved tableName (e.g. 'sys_user')
569
- // while objectDef.name may contain the FQN (e.g. 'sys__user').
570
- await this.initObjects([{ ...objectDef, name: object }]);
571
- }
572
-
573
- async dropTable(object: string, _options?: DriverOptions): Promise<void> {
574
- await this.knex.schema.dropTableIfExists(object);
575
- }
576
-
577
- /**
578
- * Batch-initialise tables from an array of object definitions.
579
- */
580
- async initObjects(objects: Array<{ name: string; tableName?: string; fields?: Record<string, any> }>): Promise<void> {
581
- await this.ensureDatabaseExists();
582
-
583
- for (const obj of objects) {
584
- // Prefer explicit tableName (physical name) over obj.name (which may be a FQN).
585
- const tableName = obj.tableName || obj.name;
586
-
587
- const jsonCols: string[] = [];
588
- const booleanCols: string[] = [];
589
- if (obj.fields) {
590
- for (const [name, field] of Object.entries<any>(obj.fields)) {
591
- const type = field.type || 'string';
592
- if (this.isJsonField(type, field)) {
593
- jsonCols.push(name);
594
- }
595
- if (type === 'boolean') {
596
- booleanCols.push(name);
597
- }
598
- }
599
- }
600
- this.jsonFields[tableName] = jsonCols;
601
- this.booleanFields[tableName] = booleanCols;
602
-
603
- let exists = await this.knex.schema.hasTable(tableName);
604
-
605
- if (exists) {
606
- const columnInfo = await this.knex(tableName).columnInfo();
607
- const existingColumns = Object.keys(columnInfo);
608
-
609
- if (existingColumns.includes('_id') && !existingColumns.includes('id')) {
610
- await this.knex.schema.dropTable(tableName);
611
- exists = false;
612
- }
613
- }
614
-
615
- // Columns created unconditionally by initObjects — skip them when
616
- // iterating obj.fields to avoid duplicate-column errors (e.g. SQLite
617
- // rejects CREATE TABLE with two columns of the same name).
618
- const builtinColumns = new Set(['id', 'created_at', 'updated_at']);
619
-
620
- if (!exists) {
621
- await this.knex.schema.createTable(tableName, (table) => {
622
- table.string('id').primary();
623
- table.timestamp('created_at').defaultTo(this.knex.fn.now());
624
- table.timestamp('updated_at').defaultTo(this.knex.fn.now());
625
- if (obj.fields) {
626
- for (const [name, field] of Object.entries(obj.fields)) {
627
- if (builtinColumns.has(name)) continue;
628
- this.createColumn(table, name, field);
629
- }
630
- }
631
- });
632
- this.tablesWithTimestamps.add(tableName);
633
- } else {
634
- const columnInfo = await this.knex(tableName).columnInfo();
635
- const existingColumns = Object.keys(columnInfo);
636
-
637
- if (existingColumns.includes('updated_at')) {
638
- this.tablesWithTimestamps.add(tableName);
639
- }
640
-
641
- await this.knex.schema.alterTable(tableName, (table) => {
642
- if (obj.fields) {
643
- for (const [name, field] of Object.entries(obj.fields)) {
644
- if (!existingColumns.includes(name)) {
645
- this.createColumn(table, name, field);
646
- }
647
- }
648
- }
649
- });
650
- }
651
- }
652
- }
653
-
654
- // ===================================
655
- // Schema Introspection
656
- // ===================================
657
-
658
- async introspectSchema(): Promise<IntrospectedSchema> {
659
- const tables: Record<string, IntrospectedTable> = {};
660
- let tableNames: string[] = [];
661
-
662
- if (this.isPostgres) {
663
- const result = await this.knex.raw(`
664
- SELECT table_name
665
- FROM information_schema.tables
666
- WHERE table_schema = 'public'
667
- AND table_type = 'BASE TABLE'
668
- `);
669
- tableNames = result.rows.map((row: any) => row.table_name);
670
- } else if (this.isMysql) {
671
- const result = await this.knex.raw(`
672
- SELECT table_name
673
- FROM information_schema.tables
674
- WHERE table_schema = DATABASE()
675
- AND table_type = 'BASE TABLE'
676
- `);
677
- tableNames = result[0].map((row: any) => row.TABLE_NAME);
678
- } else if (this.isSqlite) {
679
- const result = await this.knex.raw(`
680
- SELECT name as table_name
681
- FROM sqlite_master
682
- WHERE type='table'
683
- AND name NOT LIKE 'sqlite_%'
684
- `);
685
- tableNames = result.map((row: any) => row.table_name);
686
- }
687
-
688
- for (const tableName of tableNames) {
689
- const columns = await this.introspectColumns(tableName);
690
- const foreignKeys = await this.introspectForeignKeys(tableName);
691
- const primaryKeys = await this.introspectPrimaryKeys(tableName);
692
- const uniqueConstraints = await this.introspectUniqueConstraints(tableName);
693
-
694
- for (const col of columns) {
695
- if (primaryKeys.includes(col.name)) col.isPrimary = true;
696
- if (uniqueConstraints.includes(col.name)) col.isUnique = true;
697
- }
698
-
699
- tables[tableName] = { name: tableName, columns, foreignKeys, primaryKeys };
700
- }
701
-
702
- return { tables };
703
- }
704
-
705
- // ===================================
706
- // Internal helpers
707
- // ===================================
708
-
709
- /** Expose the underlying Knex instance for advanced usage. */
710
- getKnex(): Knex {
711
- return this.knex;
712
- }
713
-
714
- protected getBuilder(object: string, options?: DriverOptions) {
715
- let builder = this.knex(object);
716
- if (options?.transaction) {
717
- builder = builder.transacting(options.transaction as Knex.Transaction);
718
- }
719
- return builder;
720
- }
721
-
722
- // ── Filter helpers ──────────────────────────────────────────────────────────
723
-
724
- protected applyFilters(builder: Knex.QueryBuilder, filters: any) {
725
- if (!filters) return;
726
-
727
- if (!Array.isArray(filters) && typeof filters === 'object') {
728
- const hasMongoOperators = Object.keys(filters).some(
729
- (k) =>
730
- k.startsWith('$') ||
731
- (typeof filters[k] === 'object' &&
732
- filters[k] !== null &&
733
- Object.keys(filters[k]).some((op) => op.startsWith('$'))),
734
- );
735
-
736
- if (hasMongoOperators) {
737
- this.applyFilterCondition(builder, filters);
738
- return;
739
- }
740
-
741
- for (const [key, value] of Object.entries(filters)) {
742
- if (['limit', 'offset', 'fields', 'orderBy'].includes(key)) continue;
743
- builder.where(key, value as any);
744
- }
745
- return;
746
- }
747
-
748
- if (!Array.isArray(filters) || filters.length === 0) return;
749
-
750
- let nextJoin: 'and' | 'or' = 'and';
751
-
752
- for (const item of filters) {
753
- if (typeof item === 'string') {
754
- if (item.toLowerCase() === 'or') nextJoin = 'or';
755
- else if (item.toLowerCase() === 'and') nextJoin = 'and';
756
- continue;
757
- }
758
-
759
- if (Array.isArray(item)) {
760
- const [fieldRaw, op, value] = item;
761
- const isCriterion = typeof fieldRaw === 'string' && typeof op === 'string';
762
-
763
- if (isCriterion) {
764
- const field = this.mapSortField(fieldRaw);
765
- const apply = (b: any) => {
766
- const method = nextJoin === 'or' ? 'orWhere' : 'where';
767
- const methodIn = nextJoin === 'or' ? 'orWhereIn' : 'whereIn';
768
- const methodNotIn = nextJoin === 'or' ? 'orWhereNotIn' : 'whereNotIn';
769
-
770
- if (op === 'contains') {
771
- b[method](field, 'like', `%${value}%`);
772
- return;
773
- }
774
-
775
- switch (op) {
776
- case '=':
777
- b[method](field, value);
778
- break;
779
- case '!=':
780
- b[method](field, '<>', value);
781
- break;
782
- case 'in':
783
- b[methodIn](field, value);
784
- break;
785
- case 'nin':
786
- b[methodNotIn](field, value);
787
- break;
788
- default:
789
- b[method](field, op, value);
790
- }
791
- };
792
- apply(builder);
793
- } else {
794
- const method = nextJoin === 'or' ? 'orWhere' : 'where';
795
- (builder as any)[method]((qb: any) => {
796
- this.applyFilters(qb, item);
797
- });
798
- }
799
-
800
- nextJoin = 'and';
801
- }
802
- }
803
- }
804
-
805
- protected applyFilterCondition(builder: Knex.QueryBuilder, condition: any, logicalOp: 'and' | 'or' = 'and') {
806
- if (!condition || typeof condition !== 'object') return;
807
-
808
- for (const [key, value] of Object.entries(condition)) {
809
- if (key === '$and' && Array.isArray(value)) {
810
- builder.where((qb) => {
811
- for (const sub of value) {
812
- qb.where((subQb) => {
813
- this.applyFilterCondition(subQb, sub, 'and');
814
- });
815
- }
816
- });
817
- } else if (key === '$or' && Array.isArray(value)) {
818
- const method = logicalOp === 'or' ? 'orWhere' : 'where';
819
- (builder as any)[method]((qb: any) => {
820
- for (const sub of value) {
821
- qb.orWhere((subQb: any) => {
822
- this.applyFilterCondition(subQb, sub, 'or');
823
- });
824
- }
825
- });
826
- } else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
827
- const field = this.mapSortField(key);
828
- for (const [op, opValue] of Object.entries(value as Record<string, any>)) {
829
- const method = logicalOp === 'or' ? 'orWhere' : 'where';
830
- switch (op) {
831
- case '$eq':
832
- (builder as any)[method](field, opValue);
833
- break;
834
- case '$ne':
835
- (builder as any)[method](field, '<>', opValue);
836
- break;
837
- case '$gt':
838
- (builder as any)[method](field, '>', opValue);
839
- break;
840
- case '$gte':
841
- (builder as any)[method](field, '>=', opValue);
842
- break;
843
- case '$lt':
844
- (builder as any)[method](field, '<', opValue);
845
- break;
846
- case '$lte':
847
- (builder as any)[method](field, '<=', opValue);
848
- break;
849
- case '$in': {
850
- const mIn = logicalOp === 'or' ? 'orWhereIn' : 'whereIn';
851
- (builder as any)[mIn](field, opValue as any[]);
852
- break;
853
- }
854
- case '$nin': {
855
- const mNotIn = logicalOp === 'or' ? 'orWhereNotIn' : 'whereNotIn';
856
- (builder as any)[mNotIn](field, opValue as any[]);
857
- break;
858
- }
859
- case '$contains':
860
- (builder as any)[method](field, 'like', `%${opValue}%`);
861
- break;
862
- default:
863
- (builder as any)[method](field, opValue);
864
- }
865
- }
866
- } else {
867
- const field = this.mapSortField(key);
868
- const method = logicalOp === 'or' ? 'orWhere' : 'where';
869
- (builder as any)[method](field, value as any);
870
- }
871
- }
872
- }
873
-
874
- // ── Field mapping ───────────────────────────────────────────────────────────
875
-
876
- protected mapSortField(field: string): string {
877
- if (field === 'createdAt') return 'created_at';
878
- if (field === 'updatedAt') return 'updated_at';
879
- return field;
880
- }
881
-
882
- protected mapAggregateFunc(func: string): string {
883
- switch (func) {
884
- case 'count':
885
- return 'count';
886
- case 'sum':
887
- return 'sum';
888
- case 'avg':
889
- return 'avg';
890
- case 'min':
891
- return 'min';
892
- case 'max':
893
- return 'max';
894
- default:
895
- throw new Error(`Unsupported aggregate function: ${func}`);
896
- }
897
- }
898
-
899
- // ── Window function builder ─────────────────────────────────────────────────
900
-
901
- protected buildWindowFunction(spec: any): string {
902
- const func = spec.function.toUpperCase();
903
- let sql = `${func}()`;
904
-
905
- const overParts: string[] = [];
906
-
907
- if (spec.partitionBy && Array.isArray(spec.partitionBy) && spec.partitionBy.length > 0) {
908
- const partitionFields = spec.partitionBy.map((f: string) => this.mapSortField(f)).join(', ');
909
- overParts.push(`PARTITION BY ${partitionFields}`);
910
- }
911
-
912
- if (spec.orderBy && Array.isArray(spec.orderBy) && spec.orderBy.length > 0) {
913
- const orderFields = spec.orderBy
914
- .map((s: any) => {
915
- const field = this.mapSortField(s.field);
916
- const order = (s.order || 'asc').toUpperCase();
917
- return `${field} ${order}`;
918
- })
919
- .join(', ');
920
- overParts.push(`ORDER BY ${orderFields}`);
921
- }
922
-
923
- sql += overParts.length > 0 ? ` OVER (${overParts.join(' ')})` : ` OVER ()`;
924
- return sql;
925
- }
926
-
927
- // ── Column creation helper ──────────────────────────────────────────────────
928
-
929
- protected createColumn(table: Knex.CreateTableBuilder, name: string, field: any) {
930
- if (field.multiple) {
931
- table.json(name);
932
- return;
933
- }
934
-
935
- const type = field.type || 'string';
936
- let col: any;
937
- switch (type) {
938
- case 'string':
939
- case 'email':
940
- case 'url':
941
- case 'phone':
942
- case 'password':
943
- col = table.string(name);
944
- break;
945
- case 'text':
946
- case 'textarea':
947
- case 'html':
948
- case 'markdown':
949
- col = table.text(name);
950
- break;
951
- case 'integer':
952
- case 'int':
953
- col = table.integer(name);
954
- break;
955
- case 'float':
956
- case 'number':
957
- case 'currency':
958
- case 'percent':
959
- col = table.float(name);
960
- break;
961
- case 'boolean':
962
- col = table.boolean(name);
963
- break;
964
- case 'date':
965
- col = table.date(name);
966
- break;
967
- case 'datetime':
968
- col = table.timestamp(name);
969
- break;
970
- case 'time':
971
- col = table.time(name);
972
- break;
973
- case 'json':
974
- case 'object':
975
- case 'array':
976
- case 'image':
977
- case 'file':
978
- case 'avatar':
979
- case 'location':
980
- col = table.json(name);
981
- break;
982
- case 'lookup':
983
- col = table.string(name);
984
- if (field.reference_to) {
985
- table.foreign(name).references('id').inTable(field.reference_to);
986
- }
987
- break;
988
- case 'summary':
989
- col = table.float(name);
990
- break;
991
- case 'auto_number':
992
- col = table.string(name);
993
- break;
994
- case 'formula':
995
- return; // Virtual — no column
996
- default:
997
- col = table.string(name);
998
- }
999
-
1000
- if (col) {
1001
- if (field.unique) col.unique();
1002
- if (field.required) col.notNullable();
1003
- }
1004
- }
1005
-
1006
- // ── Database helpers ────────────────────────────────────────────────────────
1007
-
1008
- protected async ensureDatabaseExists() {
1009
- // SQLite auto-creates database files — no need to check
1010
- if (this.isSqlite) return;
1011
-
1012
- // Only PostgreSQL and MySQL support programmatic database creation
1013
- if (!this.isPostgres && !this.isMysql) return;
1014
-
1015
- try {
1016
- await this.knex.raw('SELECT 1');
1017
- } catch (e: any) {
1018
- // PostgreSQL: '3D000' = database does not exist
1019
- // MySQL: 'ER_BAD_DB_ERROR' (errno 1049) = unknown database
1020
- if (
1021
- e.code === '3D000' ||
1022
- e.code === 'ER_BAD_DB_ERROR' ||
1023
- e.errno === 1049
1024
- ) {
1025
- await this.createDatabase();
1026
- } else {
1027
- throw e;
1028
- }
1029
- }
1030
- }
1031
-
1032
- protected async createDatabase() {
1033
- const config = this.config as any;
1034
- const connection = config.connection;
1035
- let dbName = '';
1036
- const adminConfig = { ...config };
1037
-
1038
- if (this.isPostgres) {
1039
- // PostgreSQL: connect to the 'postgres' maintenance database
1040
- if (typeof connection === 'string') {
1041
- const url = new URL(connection);
1042
- dbName = url.pathname.slice(1);
1043
- url.pathname = '/postgres';
1044
- adminConfig.connection = url.toString();
1045
- } else {
1046
- dbName = connection.database;
1047
- adminConfig.connection = { ...connection, database: 'postgres' };
1048
- }
1049
- } else if (this.isMysql) {
1050
- // MySQL: connect without specifying a database
1051
- if (typeof connection === 'string') {
1052
- const url = new URL(connection);
1053
- dbName = url.pathname.slice(1);
1054
- url.pathname = '/';
1055
- adminConfig.connection = url.toString();
1056
- } else {
1057
- dbName = connection.database;
1058
- const { database: _db, ...rest } = connection;
1059
- adminConfig.connection = rest;
1060
- }
1061
- } else {
1062
- return; // Unsupported dialect for auto-creation
1063
- }
1064
-
1065
- const adminKnex = knex(adminConfig);
1066
- try {
1067
- if (this.isPostgres) {
1068
- await adminKnex.raw(`CREATE DATABASE "${dbName}"`);
1069
- } else if (this.isMysql) {
1070
- await adminKnex.raw(`CREATE DATABASE IF NOT EXISTS \`${dbName}\``);
1071
- }
1072
- } finally {
1073
- await adminKnex.destroy();
1074
- }
1075
- }
1076
-
1077
- protected isJsonField(type: string, field: any): boolean {
1078
- return ['json', 'object', 'array', 'image', 'file', 'avatar', 'location'].includes(type) || field.multiple;
1079
- }
1080
-
1081
- // ── SQLite serialisation ────────────────────────────────────────────────────
1082
-
1083
- protected formatInput(object: string, data: any): any {
1084
- if (!this.isSqlite) return data;
1085
-
1086
- const fields = this.jsonFields[object];
1087
- if (!fields || fields.length === 0) return data;
1088
-
1089
- const copy = { ...data };
1090
- for (const field of fields) {
1091
- if (copy[field] !== undefined && typeof copy[field] === 'object' && copy[field] !== null) {
1092
- copy[field] = JSON.stringify(copy[field]);
1093
- }
1094
- }
1095
- return copy;
1096
- }
1097
-
1098
- protected formatOutput(object: string, data: any): any {
1099
- if (!data) return data;
1100
-
1101
- if (this.isSqlite) {
1102
- const jsonFields = this.jsonFields[object];
1103
- if (jsonFields && jsonFields.length > 0) {
1104
- for (const field of jsonFields) {
1105
- if (data[field] !== undefined && typeof data[field] === 'string') {
1106
- try {
1107
- data[field] = JSON.parse(data[field]);
1108
- } catch {
1109
- // keep as string
1110
- }
1111
- }
1112
- }
1113
- }
1114
-
1115
- const booleanFields = this.booleanFields[object];
1116
- if (booleanFields && booleanFields.length > 0) {
1117
- for (const field of booleanFields) {
1118
- if (data[field] !== undefined && data[field] !== null) {
1119
- data[field] = Boolean(data[field]);
1120
- }
1121
- }
1122
- }
1123
- }
1124
-
1125
- return data;
1126
- }
1127
-
1128
- // ── Introspection internals ─────────────────────────────────────────────────
1129
-
1130
- protected async introspectColumns(tableName: string): Promise<IntrospectedColumn[]> {
1131
- const columnInfo = await this.knex(tableName).columnInfo();
1132
- const columns: IntrospectedColumn[] = [];
1133
-
1134
- for (const [colName, info] of Object.entries<any>(columnInfo)) {
1135
- let type = 'string';
1136
- let maxLength: number | undefined;
1137
-
1138
- if (this.isSqlite) {
1139
- type = info.type?.toLowerCase() || 'string';
1140
- } else {
1141
- type = info.type || 'string';
1142
- }
1143
-
1144
- if (info.maxLength) {
1145
- maxLength = info.maxLength;
1146
- }
1147
-
1148
- columns.push({
1149
- name: colName,
1150
- type,
1151
- nullable: info.nullable !== false,
1152
- defaultValue: info.defaultValue,
1153
- isPrimary: false,
1154
- isUnique: false,
1155
- maxLength,
1156
- });
1157
- }
1158
-
1159
- return columns;
1160
- }
1161
-
1162
- protected async introspectForeignKeys(tableName: string): Promise<IntrospectedForeignKey[]> {
1163
- const foreignKeys: IntrospectedForeignKey[] = [];
1164
-
1165
- try {
1166
- if (this.isPostgres) {
1167
- const result = await this.knex.raw(
1168
- `
1169
- SELECT
1170
- kcu.column_name,
1171
- ccu.table_name AS referenced_table,
1172
- ccu.column_name AS referenced_column,
1173
- tc.constraint_name
1174
- FROM information_schema.table_constraints AS tc
1175
- JOIN information_schema.key_column_usage AS kcu
1176
- ON tc.constraint_name = kcu.constraint_name
1177
- AND tc.table_schema = kcu.table_schema
1178
- JOIN information_schema.constraint_column_usage AS ccu
1179
- ON ccu.constraint_name = tc.constraint_name
1180
- AND ccu.table_schema = tc.table_schema
1181
- WHERE tc.constraint_type = 'FOREIGN KEY'
1182
- AND tc.table_name = ?
1183
- `,
1184
- [tableName],
1185
- );
1186
-
1187
- for (const row of result.rows) {
1188
- foreignKeys.push({
1189
- columnName: row.column_name,
1190
- referencedTable: row.referenced_table,
1191
- referencedColumn: row.referenced_column,
1192
- constraintName: row.constraint_name,
1193
- });
1194
- }
1195
- } else if (this.isMysql) {
1196
- const result = await this.knex.raw(
1197
- `
1198
- SELECT
1199
- COLUMN_NAME as column_name,
1200
- REFERENCED_TABLE_NAME as referenced_table,
1201
- REFERENCED_COLUMN_NAME as referenced_column,
1202
- CONSTRAINT_NAME as constraint_name
1203
- FROM information_schema.KEY_COLUMN_USAGE
1204
- WHERE TABLE_SCHEMA = DATABASE()
1205
- AND TABLE_NAME = ?
1206
- AND REFERENCED_TABLE_NAME IS NOT NULL
1207
- `,
1208
- [tableName],
1209
- );
1210
-
1211
- for (const row of result[0]) {
1212
- foreignKeys.push({
1213
- columnName: row.column_name,
1214
- referencedTable: row.referenced_table,
1215
- referencedColumn: row.referenced_column,
1216
- constraintName: row.constraint_name,
1217
- });
1218
- }
1219
- } else if (this.isSqlite) {
1220
- const tableExistsResult = await this.knex.raw(
1221
- "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?",
1222
- [tableName],
1223
- );
1224
-
1225
- if (!Array.isArray(tableExistsResult) || tableExistsResult.length === 0) {
1226
- return foreignKeys;
1227
- }
1228
-
1229
- const safeTableName = tableName.replace(/[^a-zA-Z0-9_]/g, '');
1230
- const result = await this.knex.raw(`PRAGMA foreign_key_list(${safeTableName})`);
1231
-
1232
- for (const row of result) {
1233
- foreignKeys.push({
1234
- columnName: row.from,
1235
- referencedTable: row.table,
1236
- referencedColumn: row.to,
1237
- constraintName: `fk_${tableName}_${row.from}`,
1238
- });
1239
- }
1240
- }
1241
- } catch {
1242
- // silently ignore introspection errors
1243
- }
1244
-
1245
- return foreignKeys;
1246
- }
1247
-
1248
- protected async introspectPrimaryKeys(tableName: string): Promise<string[]> {
1249
- const primaryKeys: string[] = [];
1250
-
1251
- try {
1252
- if (this.isPostgres) {
1253
- const result = await this.knex.raw(
1254
- `
1255
- SELECT a.attname as column_name
1256
- FROM pg_index i
1257
- JOIN pg_attribute a ON a.attrelid = i.indrelid
1258
- AND a.attnum = ANY(i.indkey)
1259
- WHERE i.indrelid = ?::regclass
1260
- AND i.indisprimary
1261
- `,
1262
- [tableName],
1263
- );
1264
-
1265
- for (const row of result.rows) {
1266
- primaryKeys.push(row.column_name);
1267
- }
1268
- } else if (this.isMysql) {
1269
- const result = await this.knex.raw(
1270
- `
1271
- SELECT COLUMN_NAME as column_name
1272
- FROM information_schema.KEY_COLUMN_USAGE
1273
- WHERE TABLE_SCHEMA = DATABASE()
1274
- AND TABLE_NAME = ?
1275
- AND CONSTRAINT_NAME = 'PRIMARY'
1276
- `,
1277
- [tableName],
1278
- );
1279
-
1280
- for (const row of result[0]) {
1281
- primaryKeys.push(row.column_name);
1282
- }
1283
- } else if (this.isSqlite) {
1284
- const safeTableName = tableName.replace(/[^a-zA-Z0-9_]/g, '');
1285
-
1286
- const tablesResult = await this.knex.raw("SELECT name FROM sqlite_master WHERE type = 'table'");
1287
- const tableNames = Array.isArray(tablesResult) ? tablesResult.map((row: any) => row.name) : [];
1288
-
1289
- if (!tableNames.includes(safeTableName)) {
1290
- return primaryKeys;
1291
- }
1292
-
1293
- const result = await this.knex.raw(`PRAGMA table_info(${safeTableName})`);
1294
-
1295
- for (const row of result) {
1296
- if (row.pk === 1) {
1297
- primaryKeys.push(row.name);
1298
- }
1299
- }
1300
- }
1301
- } catch {
1302
- // silently ignore
1303
- }
1304
-
1305
- return primaryKeys;
1306
- }
1307
-
1308
- protected async introspectUniqueConstraints(tableName: string): Promise<string[]> {
1309
- const uniqueColumns: string[] = [];
1310
-
1311
- try {
1312
- if (this.isPostgres) {
1313
- const result = await this.knex.raw(
1314
- `
1315
- SELECT c.column_name
1316
- FROM information_schema.table_constraints tc
1317
- JOIN information_schema.constraint_column_usage AS ccu
1318
- ON tc.constraint_schema = ccu.constraint_schema
1319
- AND tc.constraint_name = ccu.constraint_name
1320
- WHERE tc.constraint_type = 'UNIQUE'
1321
- AND tc.table_name = ?
1322
- `,
1323
- [tableName],
1324
- );
1325
-
1326
- for (const row of result.rows) {
1327
- uniqueColumns.push(row.column_name);
1328
- }
1329
- } else if (this.isMysql) {
1330
- const result = await this.knex.raw(
1331
- `
1332
- SELECT COLUMN_NAME
1333
- FROM information_schema.TABLE_CONSTRAINTS tc
1334
- JOIN information_schema.KEY_COLUMN_USAGE kcu
1335
- USING (CONSTRAINT_NAME, TABLE_SCHEMA, TABLE_NAME)
1336
- WHERE CONSTRAINT_TYPE = 'UNIQUE'
1337
- AND TABLE_SCHEMA = DATABASE()
1338
- AND TABLE_NAME = ?
1339
- `,
1340
- [tableName],
1341
- );
1342
-
1343
- for (const row of result[0]) {
1344
- uniqueColumns.push(row.COLUMN_NAME);
1345
- }
1346
- } else if (this.isSqlite) {
1347
- const safeTableName = tableName.replace(/[^a-zA-Z0-9_]/g, '');
1348
-
1349
- const tablesResult = await this.knex.raw("SELECT name FROM sqlite_master WHERE type = 'table'");
1350
- const tableNames = Array.isArray(tablesResult) ? tablesResult.map((row: any) => row.name) : [];
1351
-
1352
- if (!tableNames.includes(safeTableName)) {
1353
- return uniqueColumns;
1354
- }
1355
-
1356
- const indexes = await this.knex.raw(`PRAGMA index_list(${safeTableName})`);
1357
-
1358
- for (const idx of indexes) {
1359
- if (idx.unique === 1) {
1360
- const info = await this.knex.raw(`PRAGMA index_info(${idx.name})`);
1361
- if (info.length === 1) {
1362
- uniqueColumns.push(info[0].name);
1363
- }
1364
- }
1365
- }
1366
- }
1367
- } catch {
1368
- // silently ignore
1369
- }
1370
-
1371
- return uniqueColumns;
1372
- }
1373
- }