turbine-orm 0.10.0 → 0.11.0

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.
@@ -10,9 +10,10 @@
10
10
  * Schema-driven: all column names, types, and relations come from introspected
11
11
  * metadata — nothing is hardcoded.
12
12
  */
13
+ import { postgresDialect } from '../dialect.js';
13
14
  import { CircularRelationError, NotFoundError, RelationError, TimeoutError, ValidationError, wrapPgError, } from '../errors.js';
14
15
  import { camelToSnake, snakeToCamel } from '../schema.js';
15
- import { buildCorrelation, escapeLike, escSingleQuote, LRUCache, OPERATOR_KEYS, quoteIdent, sqlToPreparedName, } from './utils.js';
16
+ import { escapeLike, LRUCache, OPERATOR_KEYS, sqlToPreparedName } from './utils.js';
16
17
  // ---------------------------------------------------------------------------
17
18
  // Internal detection helpers — used by QueryInterface
18
19
  // ---------------------------------------------------------------------------
@@ -109,6 +110,7 @@ export class QueryInterface {
109
110
  warnOnUnlimited;
110
111
  preparedStatementsEnabled;
111
112
  sqlCacheEnabled;
113
+ dialect;
112
114
  /**
113
115
  * Tracks tables that have already triggered an unlimited-query warning so
114
116
  * the user is not spammed once per row. Per-instance state — each
@@ -143,6 +145,7 @@ export class QueryInterface {
143
145
  this.warnOnUnlimited = options?.warnOnUnlimited !== false;
144
146
  this.preparedStatementsEnabled = options?.preparedStatements ?? true;
145
147
  this.sqlCacheEnabled = options?.sqlCache !== false;
148
+ this.dialect = options?.dialect ?? postgresDialect;
146
149
  // Pre-compute column type lookup maps (TASK-26)
147
150
  this.columnPgTypeMap = new Map();
148
151
  this.columnArrayTypeMap = new Map();
@@ -151,6 +154,14 @@ export class QueryInterface {
151
154
  this.columnArrayTypeMap.set(col.name, col.pgArrayType);
152
155
  }
153
156
  }
157
+ /** Quote an identifier through the active SQL dialect. */
158
+ q(name) {
159
+ return this.dialect.quoteIdentifier(name);
160
+ }
161
+ /** Return the active dialect's placeholder for a 1-indexed parameter position. */
162
+ p(index) {
163
+ return this.dialect.paramPlaceholder(index);
164
+ }
154
165
  /**
155
166
  * Return cache hit/miss statistics for this QueryInterface instance.
156
167
  * Useful for monitoring and benchmarking.
@@ -288,11 +299,11 @@ export class QueryInterface {
288
299
  // Simple path: plain equality, no operators/null/OR
289
300
  if (!args.with && isSimpleWhere) {
290
301
  const entry = this.acquireSql(ck, () => {
291
- const qt = quoteIdent(this.table);
302
+ const qt = this.q(this.table);
292
303
  const tempParams = whereKeys.map((k) => whereObj[k]);
293
- const whereClauses = whereKeys.map((k, i) => `${this.toSqlColumn(k)} = $${i + 1}`);
304
+ const whereClauses = whereKeys.map((k, i) => `${this.toSqlColumn(k)} = ${this.p(i + 1)}`);
294
305
  const whereSql = whereClauses.length > 0 ? ` WHERE ${whereClauses.join(' AND ')}` : '';
295
- const selectExpr = columnsList ? columnsList.map((c) => `${qt}.${quoteIdent(c)}`).join(', ') : `${qt}.*`;
306
+ const selectExpr = columnsList ? columnsList.map((c) => `${qt}.${this.q(c)}`).join(', ') : `${qt}.*`;
296
307
  void tempParams; // params are positional, SQL is value-invariant
297
308
  return `SELECT ${selectExpr} FROM ${qt}${whereSql} LIMIT 1`;
298
309
  });
@@ -317,8 +328,8 @@ export class QueryInterface {
317
328
  const freshParams = [];
318
329
  const clause = this.buildWhereClause(whereObj, freshParams);
319
330
  const whereSql = clause ? ` WHERE ${clause}` : '';
320
- const qt = quoteIdent(this.table);
321
- const selectExpr = columnsList ? columnsList.map((c) => `${qt}.${quoteIdent(c)}`).join(', ') : `${qt}.*`;
331
+ const qt = this.q(this.table);
332
+ const selectExpr = columnsList ? columnsList.map((c) => `${qt}.${this.q(c)}`).join(', ') : `${qt}.*`;
322
333
  return `SELECT ${selectExpr} FROM ${qt}${whereSql} LIMIT 1`;
323
334
  });
324
335
  // Collect params
@@ -344,7 +355,7 @@ export class QueryInterface {
344
355
  const clause = this.buildWhereClause(whereObj, freshParams);
345
356
  const whereSql = clause ? ` WHERE ${clause}` : '';
346
357
  const selectClause = this.buildSelectWithRelations(this.table, args.with, freshParams, columnsList);
347
- return `SELECT ${selectClause} FROM ${quoteIdent(this.table)}${whereSql} LIMIT 1`;
358
+ return `SELECT ${selectClause} FROM ${this.q(this.table)}${whereSql} LIMIT 1`;
348
359
  });
349
360
  // Collect params in exact build order: where first, then with-clause relations
350
361
  this.collectWhereParams(whereObj, params);
@@ -455,7 +466,7 @@ export class QueryInterface {
455
466
  return { sql: clause ? ` WHERE ${clause}` : '' };
456
467
  })()
457
468
  : { sql: '' };
458
- const qt = quoteIdent(this.table);
469
+ const qt = this.q(this.table);
459
470
  let distinctPrefix = '';
460
471
  if (args?.distinct && args.distinct.length > 0) {
461
472
  const distinctCols = args.distinct.map((k) => this.toSqlColumn(k));
@@ -466,7 +477,7 @@ export class QueryInterface {
466
477
  selectClause = this.buildSelectWithRelations(this.table, args.with, freshParams, columnsList);
467
478
  }
468
479
  else if (columnsList) {
469
- selectClause = columnsList.map((c) => `${qt}.${quoteIdent(c)}`).join(', ');
480
+ selectClause = columnsList.map((c) => `${qt}.${this.q(c)}`).join(', ');
470
481
  }
471
482
  else {
472
483
  selectClause = `${qt}.*`;
@@ -480,7 +491,7 @@ export class QueryInterface {
480
491
  const dir = args.orderBy?.[k] ?? 'asc';
481
492
  const op = dir === 'desc' ? '<' : '>';
482
493
  freshParams.push(v);
483
- return `${qt}.${col} ${op} $${freshParams.length}`;
494
+ return `${qt}.${col} ${op} ${this.p(freshParams.length)}`;
484
495
  });
485
496
  if (freshWhereSql) {
486
497
  sql += ` AND ${cursorConditions.join(' AND ')}`;
@@ -495,11 +506,11 @@ export class QueryInterface {
495
506
  }
496
507
  if (effectiveLimit !== undefined) {
497
508
  freshParams.push(Number(effectiveLimit));
498
- sql += ` LIMIT $${freshParams.length}`;
509
+ sql += ` LIMIT ${this.p(freshParams.length)}`;
499
510
  }
500
511
  if (args?.offset !== undefined) {
501
512
  freshParams.push(Number(args.offset));
502
- sql += ` OFFSET $${freshParams.length}`;
513
+ sql += ` OFFSET ${this.p(freshParams.length)}`;
503
514
  }
504
515
  return sql;
505
516
  });
@@ -587,7 +598,7 @@ export class QueryInterface {
587
598
  // Acquire a dedicated connection — cursors require a single connection in a transaction
588
599
  const client = await this.pool.connect();
589
600
  const cursorName = `turbine_cursor_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
590
- const quotedCursor = quoteIdent(cursorName);
601
+ const quotedCursor = this.q(cursorName);
591
602
  try {
592
603
  await client.query('BEGIN');
593
604
  await client.query(`DECLARE ${quotedCursor} NO SCROLL CURSOR FOR ${deferred.sql}`, deferred.params);
@@ -721,8 +732,8 @@ export class QueryInterface {
721
732
  const entries = Object.entries(args.data).filter(([, v]) => v !== undefined);
722
733
  const columns = entries.map(([k]) => this.toSqlColumn(k));
723
734
  const params = entries.map(([, v]) => v);
724
- const placeholders = entries.map((_, i) => `$${i + 1}`);
725
- const sql = `INSERT INTO ${quoteIdent(this.table)} (${columns.join(', ')}) VALUES (${placeholders.join(', ')}) RETURNING *`;
735
+ const placeholders = entries.map((_, i) => `${this.p(i + 1)}`);
736
+ const sql = `INSERT INTO ${this.q(this.table)} (${columns.join(', ')}) VALUES (${placeholders.join(', ')}) RETURNING *`;
726
737
  return {
727
738
  sql,
728
739
  params,
@@ -751,7 +762,7 @@ export class QueryInterface {
751
762
  });
752
763
  }
753
764
  buildCreateMany(args) {
754
- const qt = quoteIdent(this.table);
765
+ const qt = this.q(this.table);
755
766
  if (args.data.length === 0) {
756
767
  return {
757
768
  sql: `SELECT * FROM ${qt} WHERE false`,
@@ -772,8 +783,8 @@ export class QueryInterface {
772
783
  }
773
784
  // Use actual Postgres types for array casts
774
785
  const typeCasts = columns.map((col) => this.getColumnArrayType(col));
775
- const unnestArgs = columnArrays.map((_, i) => `$${i + 1}::${typeCasts[i]}`);
776
- const quotedColumns = columns.map((c) => quoteIdent(c));
786
+ const unnestArgs = columnArrays.map((_, i) => `${this.p(i + 1)}::${typeCasts[i]}`);
787
+ const quotedColumns = columns.map((c) => this.q(c));
777
788
  let sql = `INSERT INTO ${qt} (${quotedColumns.join(', ')}) SELECT * FROM UNNEST(${unnestArgs.join(', ')})`;
778
789
  // skipDuplicates: add ON CONFLICT DO NOTHING
779
790
  if (args.skipDuplicates) {
@@ -811,7 +822,7 @@ export class QueryInterface {
811
822
  const whereClause = this.buildWhereClause(whereObj, freshParams);
812
823
  const whereSql = whereClause ? ` WHERE ${whereClause}` : '';
813
824
  this.assertMutationHasPredicate('update', whereSql, args.allowFullTableScan);
814
- return `UPDATE ${quoteIdent(this.table)} SET ${setClauses.join(', ')}${whereSql} RETURNING *`;
825
+ return `UPDATE ${this.q(this.table)} SET ${setClauses.join(', ')}${whereSql} RETURNING *`;
815
826
  });
816
827
  // On cache hit, validate predicate
817
828
  if (whereFp === '') {
@@ -860,7 +871,7 @@ export class QueryInterface {
860
871
  const clause = this.buildWhereClause(whereObj, freshParams);
861
872
  const whereSql = clause ? ` WHERE ${clause}` : '';
862
873
  this.assertMutationHasPredicate('delete', whereSql, args.allowFullTableScan);
863
- return `DELETE FROM ${quoteIdent(this.table)}${whereSql} RETURNING *`;
874
+ return `DELETE FROM ${this.q(this.table)}${whereSql} RETURNING *`;
864
875
  });
865
876
  // On cache hit, still validate the predicate
866
877
  if (whereFp === '') {
@@ -900,7 +911,7 @@ export class QueryInterface {
900
911
  const createEntries = Object.entries(args.create).filter(([, v]) => v !== undefined);
901
912
  const columns = createEntries.map(([k]) => this.toSqlColumn(k));
902
913
  const createParams = createEntries.map(([, v]) => v);
903
- const placeholders = createEntries.map((_, i) => `$${i + 1}`);
914
+ const placeholders = createEntries.map((_, i) => `${this.p(i + 1)}`);
904
915
  // The conflict target comes from `where` keys — must be unique/PK columns
905
916
  const conflictKeys = Object.keys(args.where).filter((k) => args.where[k] !== undefined);
906
917
  const conflictColumns = conflictKeys.map((k) => this.toSqlColumn(k));
@@ -908,13 +919,13 @@ export class QueryInterface {
908
919
  const updateEntries = Object.entries(args.update).filter(([, v]) => v !== undefined);
909
920
  let paramIdx = createParams.length + 1;
910
921
  const setClauses = updateEntries.map(([k]) => {
911
- const clause = `${this.toSqlColumn(k)} = $${paramIdx}`;
922
+ const clause = `${this.toSqlColumn(k)} = ${this.p(paramIdx)}`;
912
923
  paramIdx++;
913
924
  return clause;
914
925
  });
915
926
  const updateParams = updateEntries.map(([, v]) => v);
916
927
  const params = [...createParams, ...updateParams];
917
- const sql = `INSERT INTO ${quoteIdent(this.table)} (${columns.join(', ')}) VALUES (${placeholders.join(', ')})` +
928
+ const sql = `INSERT INTO ${this.q(this.table)} (${columns.join(', ')}) VALUES (${placeholders.join(', ')})` +
918
929
  ` ON CONFLICT (${conflictColumns.join(', ')}) DO UPDATE SET ${setClauses.join(', ')}` +
919
930
  ` RETURNING *`;
920
931
  return {
@@ -959,7 +970,7 @@ export class QueryInterface {
959
970
  const whereClause = this.buildWhereClause(whereObj, freshParams);
960
971
  const whereSql = whereClause ? ` WHERE ${whereClause}` : '';
961
972
  this.assertMutationHasPredicate('updateMany', whereSql, args.allowFullTableScan);
962
- return `UPDATE ${quoteIdent(this.table)} SET ${setClauses.join(', ')}${whereSql}`;
973
+ return `UPDATE ${this.q(this.table)} SET ${setClauses.join(', ')}${whereSql}`;
963
974
  });
964
975
  if (whereFp === '') {
965
976
  this.assertMutationHasPredicate('updateMany', '', args.allowFullTableScan);
@@ -994,7 +1005,7 @@ export class QueryInterface {
994
1005
  const clause = this.buildWhereClause(whereObj, freshParams);
995
1006
  const whereSql = clause ? ` WHERE ${clause}` : '';
996
1007
  this.assertMutationHasPredicate('deleteMany', whereSql, args.allowFullTableScan);
997
- return `DELETE FROM ${quoteIdent(this.table)}${whereSql}`;
1008
+ return `DELETE FROM ${this.q(this.table)}${whereSql}`;
998
1009
  });
999
1010
  if (whereFp === '') {
1000
1011
  this.assertMutationHasPredicate('deleteMany', '', args.allowFullTableScan);
@@ -1027,7 +1038,7 @@ export class QueryInterface {
1027
1038
  const freshParams = [];
1028
1039
  const clause = args?.where ? this.buildWhereClause(whereObj, freshParams) : null;
1029
1040
  const whereSql = clause ? ` WHERE ${clause}` : '';
1030
- return `SELECT COUNT(*)::int AS count FROM ${quoteIdent(this.table)}${whereSql}`;
1041
+ return `SELECT COUNT(*)::int AS count FROM ${this.q(this.table)}${whereSql}`;
1031
1042
  });
1032
1043
  if (args?.where) {
1033
1044
  this.collectWhereParams(whereObj, params);
@@ -1060,7 +1071,7 @@ export class QueryInterface {
1060
1071
  }
1061
1072
  }
1062
1073
  const groupColsRaw = args.by.map((k) => this.toColumn(k));
1063
- const groupCols = groupColsRaw.map((c) => quoteIdent(c));
1074
+ const groupCols = groupColsRaw.map((c) => this.q(c));
1064
1075
  const { sql: whereSql, params } = args.where ? this.buildWhere(args.where) : { sql: '', params: [] };
1065
1076
  // Build SELECT expressions: group-by columns + aggregate functions
1066
1077
  const selectExprs = [...groupCols];
@@ -1074,7 +1085,7 @@ export class QueryInterface {
1074
1085
  for (const [field, enabled] of Object.entries(args._sum)) {
1075
1086
  if (enabled) {
1076
1087
  const col = this.toColumn(field);
1077
- selectExprs.push(`SUM(${quoteIdent(col)}) AS ${quoteIdent(`_sum_${col}`)}`);
1088
+ selectExprs.push(`SUM(${this.q(col)}) AS ${this.q(`_sum_${col}`)}`);
1078
1089
  }
1079
1090
  }
1080
1091
  }
@@ -1083,7 +1094,7 @@ export class QueryInterface {
1083
1094
  for (const [field, enabled] of Object.entries(args._avg)) {
1084
1095
  if (enabled) {
1085
1096
  const col = this.toColumn(field);
1086
- selectExprs.push(`AVG(${quoteIdent(col)})::float AS ${quoteIdent(`_avg_${col}`)}`);
1097
+ selectExprs.push(`AVG(${this.q(col)})::float AS ${this.q(`_avg_${col}`)}`);
1087
1098
  }
1088
1099
  }
1089
1100
  }
@@ -1092,7 +1103,7 @@ export class QueryInterface {
1092
1103
  for (const [field, enabled] of Object.entries(args._min)) {
1093
1104
  if (enabled) {
1094
1105
  const col = this.toColumn(field);
1095
- selectExprs.push(`MIN(${quoteIdent(col)}) AS ${quoteIdent(`_min_${col}`)}`);
1106
+ selectExprs.push(`MIN(${this.q(col)}) AS ${this.q(`_min_${col}`)}`);
1096
1107
  }
1097
1108
  }
1098
1109
  }
@@ -1101,11 +1112,11 @@ export class QueryInterface {
1101
1112
  for (const [field, enabled] of Object.entries(args._max)) {
1102
1113
  if (enabled) {
1103
1114
  const col = this.toColumn(field);
1104
- selectExprs.push(`MAX(${quoteIdent(col)}) AS ${quoteIdent(`_max_${col}`)}`);
1115
+ selectExprs.push(`MAX(${this.q(col)}) AS ${this.q(`_max_${col}`)}`);
1105
1116
  }
1106
1117
  }
1107
1118
  }
1108
- let sql = `SELECT ${selectExprs.join(', ')} FROM ${quoteIdent(this.table)}${whereSql} GROUP BY ${groupCols.join(', ')}`;
1119
+ let sql = `SELECT ${selectExprs.join(', ')} FROM ${this.q(this.table)}${whereSql} GROUP BY ${groupCols.join(', ')}`;
1109
1120
  // ORDER BY
1110
1121
  if (args.orderBy) {
1111
1122
  sql += ` ORDER BY ${this.buildOrderBy(args.orderBy)}`;
@@ -1213,7 +1224,7 @@ export class QueryInterface {
1213
1224
  for (const [field, enabled] of Object.entries(args._count)) {
1214
1225
  if (enabled) {
1215
1226
  const col = this.toColumn(field);
1216
- selectExprs.push(`COUNT(${quoteIdent(col)})::int AS ${quoteIdent(`_count_${col}`)}`);
1227
+ selectExprs.push(`COUNT(${this.q(col)})::int AS ${this.q(`_count_${col}`)}`);
1217
1228
  }
1218
1229
  }
1219
1230
  }
@@ -1222,7 +1233,7 @@ export class QueryInterface {
1222
1233
  for (const [field, enabled] of Object.entries(args._sum)) {
1223
1234
  if (enabled) {
1224
1235
  const col = this.toColumn(field);
1225
- selectExprs.push(`SUM(${quoteIdent(col)}) AS ${quoteIdent(`_sum_${col}`)}`);
1236
+ selectExprs.push(`SUM(${this.q(col)}) AS ${this.q(`_sum_${col}`)}`);
1226
1237
  }
1227
1238
  }
1228
1239
  }
@@ -1231,7 +1242,7 @@ export class QueryInterface {
1231
1242
  for (const [field, enabled] of Object.entries(args._avg)) {
1232
1243
  if (enabled) {
1233
1244
  const col = this.toColumn(field);
1234
- selectExprs.push(`AVG(${quoteIdent(col)})::float AS ${quoteIdent(`_avg_${col}`)}`);
1245
+ selectExprs.push(`AVG(${this.q(col)})::float AS ${this.q(`_avg_${col}`)}`);
1235
1246
  }
1236
1247
  }
1237
1248
  }
@@ -1240,7 +1251,7 @@ export class QueryInterface {
1240
1251
  for (const [field, enabled] of Object.entries(args._min)) {
1241
1252
  if (enabled) {
1242
1253
  const col = this.toColumn(field);
1243
- selectExprs.push(`MIN(${quoteIdent(col)}) AS ${quoteIdent(`_min_${col}`)}`);
1254
+ selectExprs.push(`MIN(${this.q(col)}) AS ${this.q(`_min_${col}`)}`);
1244
1255
  }
1245
1256
  }
1246
1257
  }
@@ -1249,14 +1260,14 @@ export class QueryInterface {
1249
1260
  for (const [field, enabled] of Object.entries(args._max)) {
1250
1261
  if (enabled) {
1251
1262
  const col = this.toColumn(field);
1252
- selectExprs.push(`MAX(${quoteIdent(col)}) AS ${quoteIdent(`_max_${col}`)}`);
1263
+ selectExprs.push(`MAX(${this.q(col)}) AS ${this.q(`_max_${col}`)}`);
1253
1264
  }
1254
1265
  }
1255
1266
  }
1256
1267
  if (selectExprs.length === 0) {
1257
1268
  selectExprs.push('COUNT(*)::int AS _count');
1258
1269
  }
1259
- const sql = `SELECT ${selectExprs.join(', ')} FROM ${quoteIdent(this.table)}${whereSql}`;
1270
+ const sql = `SELECT ${selectExprs.join(', ')} FROM ${this.q(this.table)}${whereSql}`;
1260
1271
  return {
1261
1272
  sql,
1262
1273
  params,
@@ -1373,7 +1384,7 @@ export class QueryInterface {
1373
1384
  }
1374
1385
  /** Convert camelCase field name to a double-quoted SQL identifier */
1375
1386
  toSqlColumn(field) {
1376
- return quoteIdent(this.toColumn(field));
1387
+ return this.q(this.toColumn(field));
1377
1388
  }
1378
1389
  /**
1379
1390
  * Build a single SET clause entry for update/updateMany.
@@ -1406,7 +1417,7 @@ export class QueryInterface {
1406
1417
  const opValue = v[op];
1407
1418
  if (op === 'set') {
1408
1419
  params.push(opValue);
1409
- return `${col} = $${params.length}`;
1420
+ return `${col} = ${this.p(params.length)}`;
1410
1421
  }
1411
1422
  // Arithmetic operators: must be finite numbers
1412
1423
  if (typeof opValue !== 'number' || !Number.isFinite(opValue)) {
@@ -1414,19 +1425,19 @@ export class QueryInterface {
1414
1425
  }
1415
1426
  if (op === 'increment') {
1416
1427
  params.push(opValue);
1417
- return `${col} = ${col} + $${params.length}`;
1428
+ return `${col} = ${col} + ${this.p(params.length)}`;
1418
1429
  }
1419
1430
  if (op === 'decrement') {
1420
1431
  params.push(opValue);
1421
- return `${col} = ${col} - $${params.length}`;
1432
+ return `${col} = ${col} - ${this.p(params.length)}`;
1422
1433
  }
1423
1434
  if (op === 'multiply') {
1424
1435
  params.push(opValue);
1425
- return `${col} = ${col} * $${params.length}`;
1436
+ return `${col} = ${col} * ${this.p(params.length)}`;
1426
1437
  }
1427
1438
  if (op === 'divide') {
1428
1439
  params.push(opValue);
1429
- return `${col} = ${col} / $${params.length}`;
1440
+ return `${col} = ${col} / ${this.p(params.length)}`;
1430
1441
  }
1431
1442
  }
1432
1443
  // Fall through: multi-key objects or non-operator single-key objects
@@ -1434,7 +1445,7 @@ export class QueryInterface {
1434
1445
  }
1435
1446
  // Plain value (including null, Date, Buffer, arrays, JSON objects)
1436
1447
  params.push(value);
1437
- return `${col} = $${params.length}`;
1448
+ return `${col} = ${this.p(params.length)}`;
1438
1449
  }
1439
1450
  // =========================================================================
1440
1451
  // Fingerprinting — value-invariant shape keys for SQL cache lookup
@@ -1957,7 +1968,7 @@ export class QueryInterface {
1957
1968
  }
1958
1969
  }
1959
1970
  const rawColumn = this.toColumn(key);
1960
- const column = quoteIdent(rawColumn);
1971
+ const column = this.q(rawColumn);
1961
1972
  // Handle null → IS NULL
1962
1973
  if (value === null) {
1963
1974
  andClauses.push(`${column} IS NULL`);
@@ -2007,7 +2018,7 @@ export class QueryInterface {
2007
2018
  }
2008
2019
  // Plain equality
2009
2020
  params.push(value);
2010
- andClauses.push(`${column} = $${params.length}`);
2021
+ andClauses.push(`${column} = ${this.p(params.length)}`);
2011
2022
  }
2012
2023
  if (andClauses.length === 0)
2013
2024
  return null;
@@ -2022,18 +2033,18 @@ export class QueryInterface {
2022
2033
  const targetMeta = this.schema.tables[targetTable];
2023
2034
  if (!targetMeta)
2024
2035
  return null;
2025
- const qt = quoteIdent(targetTable);
2026
- const qSelf = quoteIdent(this.table);
2036
+ const qt = this.q(targetTable);
2037
+ const qSelf = this.q(this.table);
2027
2038
  const clauses = [];
2028
2039
  // Correlation: link child table to parent table (supports composite FKs)
2029
2040
  let correlation;
2030
2041
  if (relDef.type === 'hasMany' || relDef.type === 'hasOne') {
2031
2042
  // parent.pk = child.fk
2032
- correlation = buildCorrelation(qt, relDef.foreignKey, qSelf, relDef.referenceKey);
2043
+ correlation = this.dialect.buildCorrelation(qt, relDef.foreignKey, qSelf, relDef.referenceKey);
2033
2044
  }
2034
2045
  else {
2035
2046
  // belongsTo: parent.fk = child.pk
2036
- correlation = buildCorrelation(qt, relDef.referenceKey, qSelf, relDef.foreignKey);
2047
+ correlation = this.dialect.buildCorrelation(qt, relDef.referenceKey, qSelf, relDef.foreignKey);
2037
2048
  }
2038
2049
  // "some": EXISTS (SELECT 1 FROM target WHERE correlation AND filter)
2039
2050
  if (filterObj.some !== undefined) {
@@ -2070,7 +2081,7 @@ export class QueryInterface {
2070
2081
  const meta = this.schema.tables[targetTable];
2071
2082
  if (!meta)
2072
2083
  return null;
2073
- const qt = quoteIdent(targetTable);
2084
+ const qt = this.q(targetTable);
2074
2085
  const conditions = [];
2075
2086
  for (const [field, value] of Object.entries(subWhere)) {
2076
2087
  if (value === undefined)
@@ -2080,7 +2091,7 @@ export class QueryInterface {
2080
2091
  throw new ValidationError(`[turbine] Unknown field "${field}" in relation filter for table "${targetTable}". ` +
2081
2092
  `Known fields: ${Object.keys(meta.columnMap).join(', ') || '(none)'}.`);
2082
2093
  }
2083
- const qCol = `${qt}.${quoteIdent(col)}`;
2094
+ const qCol = `${qt}.${this.q(col)}`;
2084
2095
  if (value === null) {
2085
2096
  conditions.push(`${qCol} IS NULL`);
2086
2097
  continue;
@@ -2091,7 +2102,7 @@ export class QueryInterface {
2091
2102
  continue;
2092
2103
  }
2093
2104
  params.push(value);
2094
- conditions.push(`${qCol} = $${params.length}`);
2105
+ conditions.push(`${qCol} = ${this.p(params.length)}`);
2095
2106
  }
2096
2107
  return conditions.length > 0 ? conditions.join(' AND ') : null;
2097
2108
  }
@@ -2103,19 +2114,19 @@ export class QueryInterface {
2103
2114
  const clauses = [];
2104
2115
  if (op.gt !== undefined) {
2105
2116
  params.push(op.gt);
2106
- clauses.push(`${column} > $${params.length}`);
2117
+ clauses.push(`${column} > ${this.p(params.length)}`);
2107
2118
  }
2108
2119
  if (op.gte !== undefined) {
2109
2120
  params.push(op.gte);
2110
- clauses.push(`${column} >= $${params.length}`);
2121
+ clauses.push(`${column} >= ${this.p(params.length)}`);
2111
2122
  }
2112
2123
  if (op.lt !== undefined) {
2113
2124
  params.push(op.lt);
2114
- clauses.push(`${column} < $${params.length}`);
2125
+ clauses.push(`${column} < ${this.p(params.length)}`);
2115
2126
  }
2116
2127
  if (op.lte !== undefined) {
2117
2128
  params.push(op.lte);
2118
- clauses.push(`${column} <= $${params.length}`);
2129
+ clauses.push(`${column} <= ${this.p(params.length)}`);
2119
2130
  }
2120
2131
  if (op.not !== undefined) {
2121
2132
  if (op.not === null) {
@@ -2123,30 +2134,29 @@ export class QueryInterface {
2123
2134
  }
2124
2135
  else {
2125
2136
  params.push(op.not);
2126
- clauses.push(`${column} != $${params.length}`);
2137
+ clauses.push(`${column} != ${this.p(params.length)}`);
2127
2138
  }
2128
2139
  }
2129
2140
  if (op.in !== undefined) {
2130
2141
  params.push(op.in);
2131
- clauses.push(`${column} = ANY($${params.length})`);
2142
+ clauses.push(`${column} = ANY(${this.p(params.length)})`);
2132
2143
  }
2133
2144
  if (op.notIn !== undefined) {
2134
2145
  params.push(op.notIn);
2135
- clauses.push(`${column} != ALL($${params.length})`);
2146
+ clauses.push(`${column} != ALL(${this.p(params.length)})`);
2136
2147
  }
2137
- // Use ILIKE for case-insensitive mode, LIKE otherwise
2138
- const likeOp = op.mode === 'insensitive' ? 'ILIKE' : 'LIKE';
2148
+ const buildLikeClause = (paramRef) => op.mode === 'insensitive' ? this.dialect.buildInsensitiveLike(column, paramRef) : `${column} LIKE ${paramRef}`;
2139
2149
  if (op.contains !== undefined) {
2140
2150
  params.push(`%${escapeLike(op.contains)}%`);
2141
- clauses.push(`${column} ${likeOp} $${params.length} ESCAPE '\\'`);
2151
+ clauses.push(`${buildLikeClause(this.p(params.length))} ESCAPE '\\'`);
2142
2152
  }
2143
2153
  if (op.startsWith !== undefined) {
2144
2154
  params.push(`${escapeLike(op.startsWith)}%`);
2145
- clauses.push(`${column} ${likeOp} $${params.length} ESCAPE '\\'`);
2155
+ clauses.push(`${buildLikeClause(this.p(params.length))} ESCAPE '\\'`);
2146
2156
  }
2147
2157
  if (op.endsWith !== undefined) {
2148
2158
  params.push(`%${escapeLike(op.endsWith)}`);
2149
- clauses.push(`${column} ${likeOp} $${params.length} ESCAPE '\\'`);
2159
+ clauses.push(`${buildLikeClause(this.p(params.length))} ESCAPE '\\'`);
2150
2160
  }
2151
2161
  return clauses;
2152
2162
  }
@@ -2306,8 +2316,8 @@ export class QueryInterface {
2306
2316
  if (!meta)
2307
2317
  throw new ValidationError(`[turbine] Unknown table "${table}"`);
2308
2318
  const cols = columnsList ?? meta.allColumns;
2309
- const qtbl = quoteIdent(table);
2310
- const baseCols = cols.map((col) => `${qtbl}.${quoteIdent(col)}`).join(', ');
2319
+ const qtbl = this.q(table);
2320
+ const baseCols = cols.map((col) => `${qtbl}.${this.q(col)}`).join(', ');
2311
2321
  const relationSelects = [];
2312
2322
  const aliasCounter = { n: 0 };
2313
2323
  for (const [relName, relSpec] of Object.entries(withClause)) {
@@ -2318,7 +2328,7 @@ export class QueryInterface {
2318
2328
  }
2319
2329
  // The main table is not aliased, so pass table name as parentRef
2320
2330
  const subquery = this.buildRelationSubquery(relDef, relSpec, params, table, aliasCounter, depth, path);
2321
- relationSelects.push(`(${subquery}) AS ${quoteIdent(relName)}`);
2331
+ relationSelects.push(`(${subquery}) AS ${this.q(relName)}`);
2322
2332
  }
2323
2333
  return [baseCols, ...relationSelects].join(', ');
2324
2334
  }
@@ -2380,7 +2390,7 @@ export class QueryInterface {
2380
2390
  * 8. **Parameter threading:** All user-supplied values (where filters, limit) are
2381
2391
  * pushed to the shared `params` array with `$N` placeholders. No string
2382
2392
  * interpolation of user data ever occurs -- all identifiers go through
2383
- * `quoteIdent()` and all values are parameterized.
2393
+ * `this.q()` and all values are parameterized.
2384
2394
  *
2385
2395
  * ### Example output (hasMany with nested relation)
2386
2396
  * ```sql
@@ -2444,8 +2454,11 @@ export class QueryInterface {
2444
2454
  .map(([k]) => targetMeta.columnMap[k] ?? camelToSnake(k)));
2445
2455
  targetColumns = targetMeta.allColumns.filter((col) => !omittedFields.has(col));
2446
2456
  }
2447
- // Build json_build_object pairs for resolved columns
2448
- const jsonPairs = targetColumns.map((col) => `'${escSingleQuote(targetMeta.reverseColumnMap[col] ?? snakeToCamel(col))}', ${alias}.${quoteIdent(col)}`);
2457
+ // Build JSON object pairs for resolved columns
2458
+ const jsonPairs = targetColumns.map((col) => [
2459
+ targetMeta.reverseColumnMap[col] ?? snakeToCamel(col),
2460
+ `${alias}.${this.q(col)}`,
2461
+ ]);
2449
2462
  // Determine if this hasMany will take the wrapped subquery path (LIMIT or ORDER BY).
2450
2463
  // When wrapping, nested relations are built in the wrapped path referencing innerAlias,
2451
2464
  // so we must NOT build them here (they would push orphaned params).
@@ -2461,14 +2474,14 @@ export class QueryInterface {
2461
2474
  // Recursively build nested subquery, passing THIS alias as the parent reference
2462
2475
  const nestedSubquery = this.buildRelationSubquery(nestedRelDef, nestedSpec, params, alias, aliasCounter, currentDepth + 1, [...currentPath, relDef.name]);
2463
2476
  // Use '[]'::json for hasMany (empty array), NULL for belongsTo/hasOne (no object)
2464
- const fallback = nestedRelDef.type === 'hasMany' ? "'[]'::json" : 'NULL';
2465
- jsonPairs.push(`'${escSingleQuote(nestedRelName)}', COALESCE((${nestedSubquery}), ${fallback})`);
2477
+ const fallback = nestedRelDef.type === 'hasMany' ? this.dialect.emptyJsonArrayLiteral : this.dialect.nullJsonLiteral;
2478
+ jsonPairs.push([nestedRelName, `COALESCE((${nestedSubquery}), ${fallback})`]);
2466
2479
  }
2467
2480
  }
2468
- const jsonObj = `json_build_object(${jsonPairs.join(', ')})`;
2481
+ const jsonObj = this.dialect.buildJsonObject(jsonPairs);
2469
2482
  // Quote parent ref — can be a table name or auto-generated alias
2470
- const qParent = quoteIdent(parentRef);
2471
- const qTarget = quoteIdent(targetTable);
2483
+ const qParent = this.q(parentRef);
2484
+ const qTarget = this.q(targetTable);
2472
2485
  // Build ORDER BY for json_agg
2473
2486
  let orderClause = '';
2474
2487
  if (spec !== true && spec.orderBy) {
@@ -2479,7 +2492,7 @@ export class QueryInterface {
2479
2492
  throw new ValidationError(`[turbine] Unknown column "${k}" in orderBy for table "${targetTable}"`);
2480
2493
  }
2481
2494
  const safeDir = dir.toLowerCase() === 'desc' ? 'DESC' : 'ASC';
2482
- return `${alias}.${quoteIdent(col)} ${safeDir}`;
2495
+ return `${alias}.${this.q(col)} ${safeDir}`;
2483
2496
  })
2484
2497
  .join(', ');
2485
2498
  orderClause = ` ORDER BY ${orders}`;
@@ -2490,10 +2503,10 @@ export class QueryInterface {
2490
2503
  // Supports composite foreign keys (string[]) via buildCorrelation.
2491
2504
  let whereClause;
2492
2505
  if (relDef.type === 'belongsTo' || relDef.type === 'hasOne') {
2493
- whereClause = buildCorrelation(alias, relDef.referenceKey, qParent, relDef.foreignKey);
2506
+ whereClause = this.dialect.buildCorrelation(alias, relDef.referenceKey, qParent, relDef.foreignKey);
2494
2507
  }
2495
2508
  else {
2496
- whereClause = buildCorrelation(alias, relDef.foreignKey, qParent, relDef.referenceKey);
2509
+ whereClause = this.dialect.buildCorrelation(alias, relDef.foreignKey, qParent, relDef.referenceKey);
2497
2510
  }
2498
2511
  // Additional filters — properly parameterized
2499
2512
  if (spec !== true && spec.where) {
@@ -2503,14 +2516,14 @@ export class QueryInterface {
2503
2516
  throw new ValidationError(`[turbine] Unknown column "${k}" in where for table "${targetTable}"`);
2504
2517
  }
2505
2518
  params.push(v);
2506
- whereClause += ` AND ${alias}.${quoteIdent(col)} = $${params.length}`;
2519
+ whereClause += ` AND ${alias}.${this.q(col)} = ${this.p(params.length)}`;
2507
2520
  }
2508
2521
  }
2509
2522
  // LIMIT
2510
2523
  let limitClause = '';
2511
2524
  if (spec !== true && spec.limit) {
2512
2525
  params.push(Number(spec.limit));
2513
- limitClause = ` LIMIT $${params.length}`;
2526
+ limitClause = ` LIMIT ${this.p(params.length)}`;
2514
2527
  }
2515
2528
  if (relDef.type === 'hasMany') {
2516
2529
  // When LIMIT or ORDER BY is used, wrap in a subquery so LIMIT applies to rows
@@ -2519,9 +2532,12 @@ export class QueryInterface {
2519
2532
  const innerAlias = `${alias}i`;
2520
2533
  // Rewrite: SELECT json_agg(json_build_object(...)) FROM (SELECT * FROM table WHERE ... ORDER BY ... LIMIT N) AS alias
2521
2534
  // Inner SELECT always needs all columns for WHERE/ORDER to work; json_build_object filters later
2522
- const innerSql = `SELECT ${targetMeta.allColumns.map((c) => `${alias}.${quoteIdent(c)}`).join(', ')} FROM ${qTarget} ${alias} WHERE ${whereClause}${orderClause}${limitClause}`;
2535
+ const innerSql = `SELECT ${targetMeta.allColumns.map((c) => `${alias}.${this.q(c)}`).join(', ')} FROM ${qTarget} ${alias} WHERE ${whereClause}${orderClause}${limitClause}`;
2523
2536
  // For the json_build_object, reference the inner alias — only include resolved columns
2524
- const innerJsonPairs = targetColumns.map((col) => `'${escSingleQuote(targetMeta.reverseColumnMap[col] ?? snakeToCamel(col))}', ${innerAlias}.${quoteIdent(col)}`);
2537
+ const innerJsonPairs = targetColumns.map((col) => [
2538
+ targetMeta.reverseColumnMap[col] ?? snakeToCamel(col),
2539
+ `${innerAlias}.${this.q(col)}`,
2540
+ ]);
2525
2541
  // Build nested relation subqueries referencing innerAlias
2526
2542
  if (spec !== true && spec.with) {
2527
2543
  for (const [nestedRelName, nestedSpec] of Object.entries(spec.with)) {
@@ -2531,14 +2547,14 @@ export class QueryInterface {
2531
2547
  `Available: ${Object.keys(targetMeta.relations).join(', ')}`);
2532
2548
  }
2533
2549
  const nestedSub = this.buildRelationSubquery(nestedRelDef, nestedSpec, params, innerAlias, aliasCounter, currentDepth + 1, [...currentPath, relDef.name]);
2534
- const fallback = nestedRelDef.type === 'hasMany' ? "'[]'::json" : 'NULL';
2535
- innerJsonPairs.push(`'${escSingleQuote(nestedRelName)}', COALESCE((${nestedSub}), ${fallback})`);
2550
+ const fallback = nestedRelDef.type === 'hasMany' ? this.dialect.emptyJsonArrayLiteral : this.dialect.nullJsonLiteral;
2551
+ innerJsonPairs.push([nestedRelName, `COALESCE((${nestedSub}), ${fallback})`]);
2536
2552
  }
2537
2553
  }
2538
- const innerJsonObj = `json_build_object(${innerJsonPairs.join(', ')})`;
2539
- return `SELECT COALESCE(json_agg(${innerJsonObj}), '[]'::json) FROM (${innerSql}) ${innerAlias}`;
2554
+ const innerJsonObj = this.dialect.buildJsonObject(innerJsonPairs);
2555
+ return `SELECT ${this.dialect.buildJsonArrayAgg(innerJsonObj)} FROM (${innerSql}) ${innerAlias}`;
2540
2556
  }
2541
- return `SELECT COALESCE(json_agg(${jsonObj}${orderClause}), '[]'::json) FROM ${qTarget} ${alias} WHERE ${whereClause}`;
2557
+ return `SELECT ${this.dialect.buildJsonArrayAgg(jsonObj, orderClause.trim() || undefined)} FROM ${qTarget} ${alias} WHERE ${whereClause}`;
2542
2558
  }
2543
2559
  // belongsTo / hasOne — return single object
2544
2560
  return `SELECT ${jsonObj} FROM ${qTarget} ${alias} WHERE ${whereClause} LIMIT 1`;
@@ -2585,22 +2601,22 @@ export class QueryInterface {
2585
2601
  params.push(filter.path);
2586
2602
  const pathParam = params.length;
2587
2603
  params.push(String(filter.equals));
2588
- clauses.push(`${column} #>> $${pathParam}::text[] = $${params.length}`);
2604
+ clauses.push(`${this.dialect.buildJsonPathExtract(column, this.p(pathParam))} = ${this.p(params.length)}`);
2589
2605
  }
2590
2606
  else if (filter.equals !== undefined) {
2591
2607
  // Containment equality: column @> $N::jsonb
2592
2608
  params.push(JSON.stringify(filter.equals));
2593
- clauses.push(`${column} @> $${params.length}::jsonb`);
2609
+ clauses.push(this.dialect.buildJsonContains(column, this.p(params.length)));
2594
2610
  }
2595
2611
  if (filter.contains !== undefined) {
2596
2612
  // Containment: column @> $N::jsonb
2597
2613
  params.push(JSON.stringify(filter.contains));
2598
- clauses.push(`${column} @> $${params.length}::jsonb`);
2614
+ clauses.push(this.dialect.buildJsonContains(column, this.p(params.length)));
2599
2615
  }
2600
2616
  if (filter.hasKey !== undefined) {
2601
2617
  // Key existence: column ? $N
2602
2618
  params.push(filter.hasKey);
2603
- clauses.push(`${column} ? $${params.length}`);
2619
+ clauses.push(`${column} ? ${this.p(params.length)}`);
2604
2620
  }
2605
2621
  return clauses;
2606
2622
  }
@@ -2614,17 +2630,17 @@ export class QueryInterface {
2614
2630
  if (filter.has !== undefined) {
2615
2631
  // value = ANY(column)
2616
2632
  params.push(filter.has);
2617
- clauses.push(`$${params.length} = ANY(${column})`);
2633
+ clauses.push(`${this.p(params.length)} = ANY(${column})`);
2618
2634
  }
2619
2635
  if (filter.hasEvery !== undefined) {
2620
2636
  // column @> ARRAY[...]::type[]
2621
2637
  params.push(filter.hasEvery);
2622
- clauses.push(`${column} @> $${params.length}::${elementType}[]`);
2638
+ clauses.push(`${column} @> ${this.p(params.length)}::${elementType}[]`);
2623
2639
  }
2624
2640
  if (filter.hasSome !== undefined) {
2625
2641
  // column && ARRAY[...]::type[]
2626
2642
  params.push(filter.hasSome);
2627
- clauses.push(`${column} && $${params.length}::${elementType}[]`);
2643
+ clauses.push(`${column} && ${this.p(params.length)}::${elementType}[]`);
2628
2644
  }
2629
2645
  if (filter.isEmpty === true) {
2630
2646
  // array_length(column, 1) IS NULL