drizzle-orm 0.38.4-e36471e → 0.39.0-0cc153d

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.
Files changed (71) hide show
  1. package/alias.cjs.map +1 -1
  2. package/alias.d.cts +1 -1
  3. package/alias.d.ts +1 -1
  4. package/alias.js.map +1 -1
  5. package/d1/session.cjs +2 -2
  6. package/d1/session.cjs.map +1 -1
  7. package/d1/session.js +2 -2
  8. package/d1/session.js.map +1 -1
  9. package/mysql-core/db.cjs +3 -0
  10. package/mysql-core/db.cjs.map +1 -1
  11. package/mysql-core/db.js +3 -0
  12. package/mysql-core/db.js.map +1 -1
  13. package/mysql-core/dialect.cjs +22 -19
  14. package/mysql-core/dialect.cjs.map +1 -1
  15. package/mysql-core/dialect.d.cts +5 -2
  16. package/mysql-core/dialect.d.ts +5 -2
  17. package/mysql-core/dialect.js +25 -21
  18. package/mysql-core/dialect.js.map +1 -1
  19. package/package.json +49 -49
  20. package/pg-core/db.cjs +3 -0
  21. package/pg-core/db.cjs.map +1 -1
  22. package/pg-core/db.js +3 -0
  23. package/pg-core/db.js.map +1 -1
  24. package/pg-core/dialect.cjs +19 -7
  25. package/pg-core/dialect.cjs.map +1 -1
  26. package/pg-core/dialect.d.cts +5 -3
  27. package/pg-core/dialect.d.ts +5 -3
  28. package/pg-core/dialect.js +22 -9
  29. package/pg-core/dialect.js.map +1 -1
  30. package/relations.cjs +69 -71
  31. package/relations.cjs.map +1 -1
  32. package/relations.d.cts +79 -69
  33. package/relations.d.ts +79 -69
  34. package/relations.js +69 -73
  35. package/relations.js.map +1 -1
  36. package/sql/expressions/conditions.cjs.map +1 -1
  37. package/sql/expressions/conditions.d.cts +4 -4
  38. package/sql/expressions/conditions.d.ts +4 -4
  39. package/sql/expressions/conditions.js.map +1 -1
  40. package/sql/sql.cjs +22 -1
  41. package/sql/sql.cjs.map +1 -1
  42. package/sql/sql.js +23 -2
  43. package/sql/sql.js.map +1 -1
  44. package/sql-js/session.cjs +32 -69
  45. package/sql-js/session.cjs.map +1 -1
  46. package/sql-js/session.d.cts +3 -7
  47. package/sql-js/session.d.ts +3 -7
  48. package/sql-js/session.js +32 -69
  49. package/sql-js/session.js.map +1 -1
  50. package/sqlite-core/db.cjs +3 -0
  51. package/sqlite-core/db.cjs.map +1 -1
  52. package/sqlite-core/db.js +3 -0
  53. package/sqlite-core/db.js.map +1 -1
  54. package/sqlite-core/dialect.cjs +21 -18
  55. package/sqlite-core/dialect.cjs.map +1 -1
  56. package/sqlite-core/dialect.d.cts +5 -2
  57. package/sqlite-core/dialect.d.ts +5 -2
  58. package/sqlite-core/dialect.js +24 -20
  59. package/sqlite-core/dialect.js.map +1 -1
  60. package/sqlite-core/query-builders/query.cjs +3 -3
  61. package/sqlite-core/query-builders/query.cjs.map +1 -1
  62. package/sqlite-core/query-builders/query.js +3 -3
  63. package/sqlite-core/query-builders/query.js.map +1 -1
  64. package/table.cjs.map +1 -1
  65. package/table.d.cts +2 -2
  66. package/table.d.ts +2 -2
  67. package/table.js.map +1 -1
  68. package/version.cjs +1 -1
  69. package/version.d.cts +1 -1
  70. package/version.d.ts +1 -1
  71. package/version.js +1 -1
package/relations.cjs CHANGED
@@ -29,7 +29,9 @@ __export(relations_exports, {
29
29
  RelationsHelperStatic: () => RelationsHelperStatic,
30
30
  createRelationsHelper: () => createRelationsHelper,
31
31
  defineRelations: () => defineRelations,
32
+ fieldSelectionToSQL: () => fieldSelectionToSQL,
32
33
  getOrderByOperators: () => getOrderByOperators,
34
+ getTableAsAliasSQL: () => getTableAsAliasSQL,
33
35
  mapRelationalRow: () => mapRelationalRow,
34
36
  operators: () => operators,
35
37
  orderByOperators: () => orderByOperators,
@@ -43,41 +45,27 @@ var import_table = require("./table.cjs");
43
45
  var import_table2 = require("./table.cjs");
44
46
  var import_column = require("./column.cjs");
45
47
  var import_entity = require("./entity.cjs");
46
- var import_primary_keys = require("./pg-core/primary-keys.cjs");
47
48
  var import_expressions = require("./sql/expressions/index.cjs");
48
49
  var import_sql = require("./sql/sql.cjs");
49
- var import_utils = require("./utils.cjs");
50
50
  class Relations {
51
51
  constructor(schema, tables, config) {
52
52
  this.schema = schema;
53
53
  this.tables = tables;
54
54
  this.config = config;
55
55
  for (const [tsName, table] of Object.entries(tables)) {
56
- if (!(0, import_entity.is)(table, import_table.Table))
56
+ const isTable = (0, import_entity.is)(table, import_table.Table);
57
+ const isView = (0, import_entity.is)(table, import_sql.View);
58
+ if (!(isTable || isView))
57
59
  continue;
58
60
  this.tableNamesMap[(0, import_table.getTableUniqueName)(table)] = tsName;
59
- const tableConfig = this.tablesConfig[tsName] = {
61
+ this.tablesConfig[tsName] = {
60
62
  table,
61
63
  tsName,
62
64
  dbName: table[import_table.Table.Symbol.Name],
63
65
  schema: table[import_table.Table.Symbol.Schema],
64
66
  columns: table[import_table.Table.Symbol.Columns],
65
- relations: config[tsName] || {},
66
- primaryKey: []
67
+ relations: config[tsName] || {}
67
68
  };
68
- for (const column of Object.values(table[import_table.Table.Symbol.Columns])) {
69
- if (column.primary) {
70
- tableConfig.primaryKey.push(column);
71
- }
72
- }
73
- const extraConfig = table[import_table.Table.Symbol.ExtraConfigBuilder]?.(table);
74
- if (extraConfig) {
75
- for (const configEntry of Object.values(extraConfig)) {
76
- if ((0, import_entity.is)(configEntry, import_primary_keys.PrimaryKeyBuilder)) {
77
- tableConfig.primaryKey.push(...configEntry.columns);
78
- }
79
- }
80
- }
81
69
  }
82
70
  for (const tableConfig of Object.values(this.tablesConfig)) {
83
71
  for (const [relationFieldName, relation] of Object.entries(tableConfig.relations)) {
@@ -116,14 +104,14 @@ class Relations {
116
104
  );
117
105
  }
118
106
  for (const column of relation.through.source) {
119
- if (column.table !== relation.throughTable) {
107
+ if (tables[column._.tableName] !== relation.throughTable) {
120
108
  throw new Error(
121
109
  `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
122
110
  );
123
111
  }
124
112
  }
125
113
  for (const column of relation.through.target) {
126
- if (column.table !== relation.throughTable) {
114
+ if (tables[column._.tableName] !== relation.throughTable) {
127
115
  throw new Error(
128
116
  `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
129
117
  );
@@ -222,65 +210,59 @@ class Relation {
222
210
  class One extends Relation {
223
211
  static [import_entity.entityKind] = "OneV2";
224
212
  optional;
225
- constructor(targetTable, config) {
213
+ constructor(tables, targetTable, config) {
226
214
  super(targetTable);
227
215
  this.alias = config?.alias;
228
216
  this.where = config?.where;
229
217
  if (config?.from) {
230
218
  this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
231
- this.throughTable ??= it._.through?._.column.table;
219
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
232
220
  return it._.column;
233
221
  });
234
222
  }
235
223
  if (config?.to) {
236
224
  this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
237
- this.throughTable ??= it._.through?._.column.table;
225
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
238
226
  return it._.column;
239
227
  });
240
228
  }
241
229
  if (this.throughTable) {
242
230
  this.through = Array.isArray(config?.from) ? {
243
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
244
- source: config.from.map((e) => e._.through?._.column),
245
- target: (config.to ?? []).map((e) => e._.column)
231
+ source: config.from.map((c) => c._.through),
232
+ target: (config.to ?? []).map((c) => c._.through)
246
233
  } : {
247
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
248
- source: [config?.from?._.through?._.column],
249
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
250
- target: [config?.to?._.through?._.column]
234
+ source: (config?.from ? [config.from] : []).map((c) => c._.through),
235
+ target: (config?.to ? [config.to] : []).map((c) => c._.through)
251
236
  };
252
237
  }
253
238
  this.optional = config?.optional ?? true;
254
239
  }
255
240
  }
256
241
  class Many extends Relation {
257
- constructor(targetTable, config) {
242
+ constructor(tables, targetTable, config) {
258
243
  super(targetTable);
259
244
  this.config = config;
260
245
  this.alias = config?.alias;
261
246
  this.where = config?.where;
262
247
  if (config?.from) {
263
248
  this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
264
- this.throughTable ??= it._.through?._.column.table;
249
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
265
250
  return it._.column;
266
251
  });
267
252
  }
268
253
  if (config?.to) {
269
254
  this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
270
- this.throughTable ??= it._.through?._.column.table;
255
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
271
256
  return it._.column;
272
257
  });
273
258
  }
274
259
  if (this.throughTable) {
275
260
  this.through = Array.isArray(config?.from) ? {
276
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
277
- source: config.from.map((e) => e._.through?._.column),
278
- target: (config.to ?? []).map((e) => e._.column)
261
+ source: config.from.map((c) => c._.through),
262
+ target: (config.to ?? []).map((c) => c._.through)
279
263
  } : {
280
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
281
- source: [config?.from?._.through?._.column],
282
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
283
- target: [config?.to?._.through?._.column]
264
+ source: (config?.from ? [config.from] : []).map((c) => c._.through),
265
+ target: (config?.to ? [config.to] : []).map((c) => c._.through)
284
266
  };
285
267
  }
286
268
  }
@@ -301,8 +283,7 @@ class Count extends AggregatedField {
301
283
  if (!this.query) {
302
284
  if (!this.table)
303
285
  throw new Error("Table must be set before building aggregate field");
304
- const table = this.table;
305
- this.query = import_sql.sql`select count(*) as ${import_sql.sql.identifier("r")} from ${table[import_table.IsAlias] ? import_sql.sql`${import_sql.sql`${import_sql.sql.identifier(table[import_table.Schema] ?? "")}.`.if(table[import_table.Schema])}${import_sql.sql.identifier(table[import_table.OriginalName])} as ${table}` : table}`.mapWith(Number);
286
+ this.query = import_sql.sql`select count(*) as ${import_sql.sql.identifier("r")} from ${getTableAsAliasSQL(this.table)}`.mapWith(Number);
306
287
  }
307
288
  return this.query;
308
289
  }
@@ -341,8 +322,7 @@ function getOrderByOperators() {
341
322
  }
342
323
  function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false, path) {
343
324
  for (const selectionItem of buildQueryResultSelection) {
344
- const field = selectionItem.field;
345
- if ((0, import_entity.is)(field, import_table.Table)) {
325
+ if (selectionItem.selection) {
346
326
  const currentPath = `${path ? `${path}.` : ""}${selectionItem.key}`;
347
327
  if (row[selectionItem.key] === null)
348
328
  continue;
@@ -369,6 +349,7 @@ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (valu
369
349
  );
370
350
  continue;
371
351
  }
352
+ const field = selectionItem.field;
372
353
  const value = mapColumnValue(row[selectionItem.key]);
373
354
  if (value === null)
374
355
  continue;
@@ -389,9 +370,9 @@ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (valu
389
370
  class RelationsBuilderTable {
390
371
  static [import_entity.entityKind] = "RelationsBuilderTable";
391
372
  _;
392
- constructor(table) {
373
+ constructor(table, key) {
393
374
  this._ = {
394
- name: (0, import_table2.getTableName)(table),
375
+ name: key,
395
376
  table
396
377
  };
397
378
  }
@@ -402,16 +383,22 @@ class RelationsBuilderTable {
402
383
  class RelationsBuilderColumn {
403
384
  static [import_entity.entityKind] = "RelationsBuilderColumn";
404
385
  _;
405
- constructor(column, through) {
386
+ constructor(column, tableName, key, through) {
406
387
  this._ = {
407
- tableName: (0, import_table2.getTableName)(column.table),
388
+ tableName,
408
389
  data: void 0,
409
390
  column,
410
- through
391
+ through,
392
+ key
411
393
  };
412
394
  }
413
395
  through(column) {
414
- return new RelationsBuilderColumn(this._.column, column);
396
+ return new RelationsBuilderColumn(
397
+ this._.column,
398
+ this._.tableName,
399
+ this._.key,
400
+ column
401
+ );
415
402
  }
416
403
  getSQL() {
417
404
  return this._.column.getSQL();
@@ -428,10 +415,10 @@ class RelationsHelperStatic {
428
415
  const many = {};
429
416
  for (const [tableName, table] of Object.entries(tables)) {
430
417
  one[tableName] = (config) => {
431
- return new One(table, config);
418
+ return new One(tables, table, config);
432
419
  };
433
420
  many[tableName] = (config) => {
434
- return new Many(table, config);
421
+ return new Many(tables, table, config);
435
422
  };
436
423
  }
437
424
  this.one = one;
@@ -447,21 +434,21 @@ class RelationsHelperStatic {
447
434
  }
448
435
  function createRelationsHelper(schema) {
449
436
  const schemaTables = Object.fromEntries(
450
- Object.entries(schema).filter((e) => (0, import_entity.is)(e[1], import_table.Table))
437
+ Object.entries(schema).filter((e) => (0, import_entity.is)(e[1], import_table.Table) || (0, import_entity.is)(e[1], import_sql.View))
451
438
  );
452
439
  const helperStatic = new RelationsHelperStatic(schemaTables);
453
- const tables = Object.entries(schema).reduce((acc, [key, value]) => {
454
- if ((0, import_entity.is)(value, import_table.Table)) {
455
- const rTable = new RelationsBuilderTable(value);
456
- const columns = Object.entries((0, import_utils.getTableColumns)(value)).reduce(
457
- (acc2, [key2, column]) => {
458
- const rbColumn = new RelationsBuilderColumn(column);
459
- acc2[key2] = rbColumn;
440
+ const tables = Object.entries(schema).reduce((acc, [tKey, value]) => {
441
+ if ((0, import_entity.is)(value, import_table.Table) || (0, import_entity.is)(value, import_sql.View)) {
442
+ const rTable = new RelationsBuilderTable(value, tKey);
443
+ const columns = Object.entries(value[import_table2.Columns]).reduce(
444
+ (acc2, [cKey, column]) => {
445
+ const rbColumn = new RelationsBuilderColumn(column, tKey, cKey);
446
+ acc2[cKey] = rbColumn;
460
447
  return acc2;
461
448
  },
462
449
  {}
463
450
  );
464
- acc[key] = Object.assign(rTable, columns);
451
+ acc[tKey] = Object.assign(rTable, columns);
465
452
  }
466
453
  return acc;
467
454
  }, {});
@@ -474,6 +461,10 @@ function defineRelations(schema, relations) {
474
461
  relations(createRelationsHelper(schema))
475
462
  );
476
463
  }
464
+ function fieldSelectionToSQL(table, target) {
465
+ const field = table[import_table2.Columns][target];
466
+ return field ? (0, import_entity.is)(field, import_column.Column) ? field : (0, import_entity.is)(field, import_sql.SQL.Aliased) ? import_sql.sql`${table}.${import_sql.sql.identifier(field.fieldAlias)}` : import_sql.sql`${table}.${import_sql.sql.identifier(target)}` : import_sql.sql`${table}.${import_sql.sql.identifier(target)}`;
467
+ }
477
468
  function relationsFieldFilterToSQL(column, filter) {
478
469
  if (typeof filter !== "object")
479
470
  return (0, import_expressions.eq)(column, filter);
@@ -554,9 +545,7 @@ function relationsFilterToSQL(table, filter) {
554
545
  continue;
555
546
  parts.push(
556
547
  (0, import_expressions.or)(
557
- ...value.map(
558
- (subFilter) => relationsFilterToSQL(table, subFilter)
559
- )
548
+ ...value.map((subFilter) => relationsFilterToSQL(table, subFilter))
560
549
  )
561
550
  );
562
551
  continue;
@@ -571,8 +560,9 @@ function relationsFilterToSQL(table, filter) {
571
560
  continue;
572
561
  }
573
562
  default: {
563
+ const column = fieldSelectionToSQL(table, target);
574
564
  const colFilter = relationsFieldFilterToSQL(
575
- table[target],
565
+ column,
576
566
  value
577
567
  );
578
568
  if (colFilter)
@@ -591,7 +581,10 @@ function relationsOrderToSQL(table, orders) {
591
581
  const entries = Object.entries(orders).filter(([_, value]) => value);
592
582
  if (!entries.length)
593
583
  return void 0;
594
- return import_sql.sql.join(entries.map(([column, value]) => (value === "asc" ? import_expressions.asc : import_expressions.desc)(table[import_table2.Columns][column])), import_sql.sql`, `);
584
+ return import_sql.sql.join(
585
+ entries.map(([target, value]) => (value === "asc" ? import_expressions.asc : import_expressions.desc)(fieldSelectionToSQL(table, target))),
586
+ import_sql.sql`, `
587
+ );
595
588
  }
596
589
  function relationExtrasToSQL(table, extras) {
597
590
  const subqueries = [];
@@ -620,19 +613,19 @@ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
620
613
  const t = relation.through.source[i];
621
614
  return (0, import_expressions.eq)(
622
615
  import_sql.sql`${sourceTable}.${import_sql.sql.identifier(s.name)}`,
623
- import_sql.sql`${throughTable}.${import_sql.sql.identifier(t.name)}`
616
+ import_sql.sql`${throughTable}.${import_sql.sql.identifier((0, import_entity.is)(t._.column, import_column.Column) ? t._.column.name : t._.key)}`
624
617
  );
625
618
  });
626
619
  const innerColumnWhere = relation.targetColumns.map((s, i) => {
627
620
  const t = relation.through.target[i];
628
621
  return (0, import_expressions.eq)(
629
- import_sql.sql`${throughTable}.${import_sql.sql.identifier(t.name)}`,
622
+ import_sql.sql`${throughTable}.${import_sql.sql.identifier((0, import_entity.is)(t._.column, import_column.Column) ? t._.column.name : t._.key)}`,
630
623
  import_sql.sql`${targetTable}.${import_sql.sql.identifier(s.name)}`
631
624
  );
632
625
  });
633
626
  return {
634
627
  filter: (0, import_expressions.and)(
635
- relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
628
+ relation.where ? relationsFilterToSQL(relation.isReversed ? targetTable : sourceTable, relation.where) : void 0
636
629
  ),
637
630
  joinCondition: (0, import_expressions.and)(
638
631
  ...outerColumnWhere,
@@ -649,10 +642,13 @@ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
649
642
  });
650
643
  const fullWhere = (0, import_expressions.and)(
651
644
  ...columnWhere,
652
- relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
645
+ relation.where ? relationsFilterToSQL(relation.isReversed ? targetTable : sourceTable, relation.where) : void 0
653
646
  );
654
647
  return { filter: fullWhere };
655
648
  }
649
+ function getTableAsAliasSQL(table) {
650
+ return import_sql.sql`${table[import_table.IsAlias] ? import_sql.sql`${import_sql.sql`${import_sql.sql.identifier(table[import_table.Schema] ?? "")}.`.if(table[import_table.Schema])}${import_sql.sql.identifier(table[import_table.OriginalName])} as ${table}` : table}`;
651
+ }
656
652
  // Annotate the CommonJS export names for ESM import in node:
657
653
  0 && (module.exports = {
658
654
  AggregatedField,
@@ -666,7 +662,9 @@ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
666
662
  RelationsHelperStatic,
667
663
  createRelationsHelper,
668
664
  defineRelations,
665
+ fieldSelectionToSQL,
669
666
  getOrderByOperators,
667
+ getTableAsAliasSQL,
670
668
  mapRelationalRow,
671
669
  operators,
672
670
  orderByOperators,