drizzle-kit 0.30.1-38fedf0 → 0.30.1-5734f71

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 (4) hide show
  1. package/api.js +181 -50
  2. package/api.mjs +181 -50
  3. package/bin.cjs +1 -1
  4. package/package.json +1 -1
package/api.js CHANGED
@@ -24842,15 +24842,8 @@ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (valu
24842
24842
  const field = selectionItem.field;
24843
24843
  if (is(field, Table2)) {
24844
24844
  const currentPath = `${path2 ? `${path2}.` : ""}${selectionItem.key}`;
24845
- if (row[selectionItem.key] === null) {
24846
- if (!selectionItem.isOptional) {
24847
- throw new DrizzleError({
24848
- message: `Unexpected null in relational query result on field "${currentPath}".
24849
- Did you forget to mark relation as optional?`
24850
- });
24851
- }
24845
+ if (row[selectionItem.key] === null)
24852
24846
  continue;
24853
- }
24854
24847
  if (parseJson)
24855
24848
  row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
24856
24849
  if (selectionItem.isArray) {
@@ -25060,18 +25053,49 @@ function relationExtrasToSQL(table5, extras) {
25060
25053
  selection
25061
25054
  };
25062
25055
  }
25063
- function relationToSQL(relation, sourceTable, targetTable) {
25056
+ function getTableSql(table5) {
25057
+ return sql`${sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(!table5[IsAlias] && table5[Schema])}`}${table5}`;
25058
+ }
25059
+ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
25060
+ if (relation.through) {
25061
+ const outerColumnWhere = relation.sourceColumns.map((s, i) => {
25062
+ const t = relation.through.source[i];
25063
+ return eq(
25064
+ sql`${getTableSql(sourceTable)}.${sql.identifier(s.name)}`,
25065
+ sql`${getTableSql(throughTable)}.${sql.identifier(t.name)}`
25066
+ );
25067
+ });
25068
+ const innerColumnWhere = relation.targetColumns.map((s, i) => {
25069
+ const t = relation.through.target[i];
25070
+ return eq(
25071
+ sql`${getTableSql(throughTable)}.${sql.identifier(t.name)}`,
25072
+ sql`${getTableSql(targetTable)}.${sql.identifier(s.name)}`
25073
+ );
25074
+ });
25075
+ return {
25076
+ filter: and(
25077
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25078
+ ),
25079
+ joinCondition: and(
25080
+ ...outerColumnWhere,
25081
+ ...innerColumnWhere
25082
+ )
25083
+ };
25084
+ }
25064
25085
  const columnWhere = relation.sourceColumns.map((s, i) => {
25065
25086
  const t = relation.targetColumns[i];
25066
25087
  return eq(
25067
- sql`${sql`${sql`${sql.identifier(sourceTable[Schema] ?? "")}.`.if(sourceTable[Schema] && !sourceTable[IsAlias])}`}${sourceTable}.${sql.identifier(s.name)}`,
25068
- sql`${sql`${sql`${sql.identifier(targetTable[Schema] ?? "")}.`.if(targetTable[Schema] && !targetTable[IsAlias])}`}${targetTable}.${sql.identifier(t.name)}`
25088
+ sql`${getTableSql(sourceTable)}.${sql.identifier(s.name)}`,
25089
+ sql`${getTableSql(targetTable)}.${sql.identifier(t.name)}`
25069
25090
  );
25070
25091
  });
25071
- const targetWhere = relation.where ? and(...columnWhere, relationsFilterToSQL(sourceTable, relation.where)) : and(...columnWhere);
25072
- return targetWhere;
25092
+ const fullWhere = and(
25093
+ ...columnWhere,
25094
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25095
+ );
25096
+ return { filter: fullWhere };
25073
25097
  }
25074
- var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, RelationsBuilderColumn, _a126, RelationsHelperStatic;
25098
+ var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, _RelationsBuilderColumn, RelationsBuilderColumn, _a126, RelationsHelperStatic;
25075
25099
  var init_relations = __esm({
25076
25100
  "../drizzle-orm/dist/relations.js"() {
25077
25101
  "use strict";
@@ -25079,7 +25103,6 @@ var init_relations = __esm({
25079
25103
  init_table();
25080
25104
  init_column();
25081
25105
  init_entity();
25082
- init_errors();
25083
25106
  init_primary_keys();
25084
25107
  init_expressions();
25085
25108
  init_sql();
@@ -25147,9 +25170,30 @@ var init_relations = __esm({
25147
25170
  if (relation.sourceColumns && relation.targetColumns) {
25148
25171
  if (relation.sourceColumns.length !== relation.targetColumns.length) {
25149
25172
  throw new Error(
25150
- `${relationPrintName}: "from" and "to" arrays must have the same length`
25173
+ `${relationPrintName}: "from" and "to" fields must have the same length`
25151
25174
  );
25152
25175
  }
25176
+ if (relation.through) {
25177
+ if (relation.through.source.length !== relation.through.target.length || relation.through.source.length !== relation.sourceColumns.length || relation.through.target.length !== relation.targetColumns.length) {
25178
+ throw new Error(
25179
+ `${relationPrintName}: ".through(column)" must be used either on all columns in "from" and "to" or not defined on any of them`
25180
+ );
25181
+ }
25182
+ for (const column5 of relation.through.source) {
25183
+ if (column5.table !== relation.throughTable) {
25184
+ throw new Error(
25185
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25186
+ );
25187
+ }
25188
+ }
25189
+ for (const column5 of relation.through.target) {
25190
+ if (column5.table !== relation.throughTable) {
25191
+ throw new Error(
25192
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25193
+ );
25194
+ }
25195
+ }
25196
+ }
25153
25197
  continue;
25154
25198
  }
25155
25199
  if (relation.sourceColumns || relation.targetColumns) {
@@ -25209,7 +25253,13 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25209
25253
  }
25210
25254
  relation.sourceColumns = reverseRelation.targetColumns;
25211
25255
  relation.targetColumns = reverseRelation.sourceColumns;
25212
- relation.where = reverseRelation.where;
25256
+ relation.through = reverseRelation.through ? {
25257
+ source: reverseRelation.through.target,
25258
+ target: reverseRelation.through.source
25259
+ } : void 0;
25260
+ relation.throughTable = reverseRelation.throughTable;
25261
+ relation.isReversed = !relation.where;
25262
+ relation.where = relation.where ?? reverseRelation.where;
25213
25263
  }
25214
25264
  }
25215
25265
  }
@@ -25224,6 +25274,9 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25224
25274
  __publicField(this, "alias");
25225
25275
  __publicField(this, "where");
25226
25276
  __publicField(this, "sourceTable");
25277
+ __publicField(this, "through");
25278
+ __publicField(this, "throughTable");
25279
+ __publicField(this, "isReversed");
25227
25280
  this.targetTable = targetTable;
25228
25281
  }
25229
25282
  };
@@ -25235,12 +25288,30 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25235
25288
  this.alias = config?.alias;
25236
25289
  this.where = config?.where;
25237
25290
  if (config?.from) {
25238
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25291
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25292
+ this.throughTable ??= it._.through?._.column.table;
25293
+ return it._.column;
25294
+ });
25239
25295
  }
25240
25296
  if (config?.to) {
25241
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25297
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25298
+ this.throughTable ??= it._.through?._.column.table;
25299
+ return it._.column;
25300
+ });
25242
25301
  }
25243
- this.optional = config?.optional ?? false;
25302
+ if (this.throughTable) {
25303
+ this.through = Array.isArray(config?.from) ? {
25304
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25305
+ source: config.from.map((e) => e._.through?._.column),
25306
+ target: (config.to ?? []).map((e) => e._.column)
25307
+ } : {
25308
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25309
+ source: [config?.from?._.through?._.column],
25310
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25311
+ target: [config?.to?._.through?._.column]
25312
+ };
25313
+ }
25314
+ this.optional = config?.optional ?? true;
25244
25315
  }
25245
25316
  };
25246
25317
  __publicField(One, _a120, "OneV2");
@@ -25251,10 +25322,28 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25251
25322
  this.alias = config?.alias;
25252
25323
  this.where = config?.where;
25253
25324
  if (config?.from) {
25254
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25325
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25326
+ this.throughTable ??= it._.through?._.column.table;
25327
+ return it._.column;
25328
+ });
25255
25329
  }
25256
25330
  if (config?.to) {
25257
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25331
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25332
+ this.throughTable ??= it._.through?._.column.table;
25333
+ return it._.column;
25334
+ });
25335
+ }
25336
+ if (this.throughTable) {
25337
+ this.through = Array.isArray(config?.from) ? {
25338
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25339
+ source: config.from.map((e) => e._.through?._.column),
25340
+ target: (config.to ?? []).map((e) => e._.column)
25341
+ } : {
25342
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25343
+ source: [config?.from?._.through?._.column],
25344
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25345
+ target: [config?.to?._.through?._.column]
25346
+ };
25258
25347
  }
25259
25348
  }
25260
25349
  };
@@ -25279,7 +25368,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25279
25368
  if (!this.query) {
25280
25369
  if (!this.table)
25281
25370
  throw new Error("Table must be set before building aggregate field");
25282
- this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
25371
+ const table5 = this.table;
25372
+ this.query = sql`select count(*) as ${sql.identifier("r")} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}`.mapWith(Number);
25283
25373
  }
25284
25374
  return this.query;
25285
25375
  }
@@ -25329,24 +25419,25 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25329
25419
  };
25330
25420
  __publicField(RelationsBuilderTable, _a124, "RelationsBuilderTable");
25331
25421
  _a125 = entityKind;
25332
- RelationsBuilderColumn = class {
25333
- constructor(column5) {
25422
+ _RelationsBuilderColumn = class _RelationsBuilderColumn {
25423
+ constructor(column5, through) {
25334
25424
  __publicField(this, "_");
25335
25425
  this._ = {
25336
25426
  tableName: getTableName(column5.table),
25337
25427
  data: void 0,
25338
- column: column5
25428
+ column: column5,
25429
+ through
25339
25430
  };
25340
25431
  }
25341
25432
  through(column5) {
25342
- this._.through = column5;
25343
- return this;
25433
+ return new _RelationsBuilderColumn(this._.column, column5);
25344
25434
  }
25345
25435
  getSQL() {
25346
25436
  return this._.column.getSQL();
25347
25437
  }
25348
25438
  };
25349
- __publicField(RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25439
+ __publicField(_RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25440
+ RelationsBuilderColumn = _RelationsBuilderColumn;
25350
25441
  _a126 = entityKind;
25351
25442
  RelationsHelperStatic = class {
25352
25443
  constructor(tables) {
@@ -26750,7 +26841,8 @@ var init_dialect = __esm({
26750
26841
  relationWhere,
26751
26842
  mode,
26752
26843
  errorPath,
26753
- depth
26844
+ depth,
26845
+ throughJoin
26754
26846
  }) {
26755
26847
  const selection = [];
26756
26848
  const isSingle = mode === "first";
@@ -26789,10 +26881,15 @@ var init_dialect = __esm({
26789
26881
  const relation = tableConfig.relations[k];
26790
26882
  const isSingle2 = is(relation, One);
26791
26883
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
26792
- const relationFilter = relationToSQL(relation, table5, targetTable);
26793
- selectionArr.push(
26794
- isSingle2 ? sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}` : sql`coalesce(${sql.identifier(k)}.${sql.identifier("r")}, '[]') as ${sql.identifier(k)}`
26884
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
26885
+ const { filter: filter2, joinCondition } = relationToSQL(
26886
+ relation,
26887
+ table5,
26888
+ targetTable,
26889
+ throughTable
26795
26890
  );
26891
+ selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
26892
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
26796
26893
  const innerQuery = this.buildRelationalQuery({
26797
26894
  table: targetTable,
26798
26895
  mode: isSingle2 ? "first" : "many",
@@ -26801,9 +26898,10 @@ var init_dialect = __esm({
26801
26898
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
26802
26899
  tableNamesMap,
26803
26900
  tables,
26804
- relationWhere: relationFilter,
26901
+ relationWhere: filter2,
26805
26902
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
26806
- depth: currentDepth + 1
26903
+ depth: currentDepth + 1,
26904
+ throughJoin: throughJoin2
26807
26905
  });
26808
26906
  selection.push({
26809
26907
  field: targetTable,
@@ -26812,7 +26910,8 @@ var init_dialect = __esm({
26812
26910
  isArray: !isSingle2,
26813
26911
  isOptional: (relation.optional ?? false) || join !== true && !!join.where
26814
26912
  });
26815
- return sql`left join lateral(select ${isSingle2 ? sql`row_to_json(${sql.identifier("t")}.*) ${sql.identifier("r")}` : sql`json_agg(row_to_json(${sql.identifier("t")}.*)) ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)} on true`;
26913
+ const joinQuery = sql`left join lateral(select ${isSingle2 ? sql`row_to_json(${sql.identifier("t")}.*) ${sql.identifier("r")}` : sql`coalesce(json_agg(row_to_json(${sql.identifier("t")}.*)), '[]') as ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)} on true`;
26914
+ return joinQuery;
26816
26915
  }),
26817
26916
  sql` `
26818
26917
  );
@@ -26825,7 +26924,7 @@ var init_dialect = __esm({
26825
26924
  });
26826
26925
  }
26827
26926
  const selectionSet = sql.join(selectionArr.filter((e) => e !== void 0), sql`, `);
26828
- const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${sql` ${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
26927
+ const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${throughJoin}${sql` ${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
26829
26928
  return {
26830
26929
  sql: query,
26831
26930
  selection
@@ -32830,7 +32929,8 @@ var init_dialect2 = __esm({
32830
32929
  mode,
32831
32930
  isNested,
32832
32931
  errorPath,
32833
- depth
32932
+ depth,
32933
+ throughJoin
32834
32934
  }) {
32835
32935
  const selection = [];
32836
32936
  const isSingle = mode === "first";
@@ -32867,7 +32967,14 @@ var init_dialect2 = __esm({
32867
32967
  const relation = tableConfig.relations[k];
32868
32968
  const isSingle2 = is(relation, One);
32869
32969
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
32870
- const relationFilter = relationToSQL(relation, table5, targetTable);
32970
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
32971
+ const { filter: filter2, joinCondition } = relationToSQL(
32972
+ relation,
32973
+ table5,
32974
+ targetTable,
32975
+ throughTable
32976
+ );
32977
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
32871
32978
  const innerQuery = this.buildRelationalQuery({
32872
32979
  table: targetTable,
32873
32980
  mode: isSingle2 ? "first" : "many",
@@ -32876,10 +32983,11 @@ var init_dialect2 = __esm({
32876
32983
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
32877
32984
  tableNamesMap,
32878
32985
  tables,
32879
- relationWhere: relationFilter,
32986
+ relationWhere: filter2,
32880
32987
  isNested: true,
32881
32988
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
32882
- depth: currentDepth + 1
32989
+ depth: currentDepth + 1,
32990
+ throughJoin: throughJoin2
32883
32991
  });
32884
32992
  selection.push({
32885
32993
  field: targetTable,
@@ -32894,7 +33002,9 @@ var init_dialect2 = __esm({
32894
33002
  }),
32895
33003
  sql`, `
32896
33004
  );
32897
- return isNested ? isSingle2 ? sql`(select jsonb_object(${jsonColumns}) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select jsonb_group_array(json_object(${jsonColumns})) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}` : isSingle2 ? sql`(select json_object(${jsonColumns}) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select json_group_array(json_object(${jsonColumns})) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}`;
33005
+ const json4 = isNested ? sql`jsonb` : sql`json`;
33006
+ const joinQuery = isSingle2 ? sql`(select ${json4}_object(${jsonColumns}) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select ${json4}_group_array(json_object(${jsonColumns})) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}`;
33007
+ return joinQuery;
32898
33008
  }),
32899
33009
  sql`, `
32900
33010
  );
@@ -32906,7 +33016,7 @@ var init_dialect2 = __esm({
32906
33016
  });
32907
33017
  }
32908
33018
  const selectionSet = sql.join(selectionArr, sql`, `);
32909
- const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
33019
+ const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${throughJoin}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
32910
33020
  return {
32911
33021
  sql: query,
32912
33022
  selection
@@ -37778,6 +37888,12 @@ var init_dialect3 = __esm({
37778
37888
  columnIdentifiers.push(
37779
37889
  sql`${table5[column5.tsName]} as ${sql.identifier(column5.tsName)}`
37780
37890
  );
37891
+ selection.push(
37892
+ {
37893
+ key: column5.tsName,
37894
+ field: column5.column
37895
+ }
37896
+ );
37781
37897
  }
37782
37898
  return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
37783
37899
  })() : this.unwrapAllColumns(table5, selection));
@@ -38599,7 +38715,9 @@ var init_dialect3 = __esm({
38599
38715
  relationWhere,
38600
38716
  mode,
38601
38717
  errorPath,
38602
- depth
38718
+ depth,
38719
+ isNested,
38720
+ throughJoin
38603
38721
  }) {
38604
38722
  const selection = [];
38605
38723
  const isSingle = mode === "first";
@@ -38633,12 +38751,19 @@ var init_dialect3 = __esm({
38633
38751
  key: k,
38634
38752
  field: relation2
38635
38753
  });
38636
- return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
38754
+ return sql` left join lateral (${query2}) as ${sql.identifier(k)} on true`;
38637
38755
  }
38638
38756
  const relation = tableConfig.relations[k];
38639
38757
  const isSingle2 = is(relation, One);
38640
38758
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
38641
- const relationFilter = relationToSQL(relation, table5, targetTable);
38759
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
38760
+ const { filter: filter2, joinCondition } = relationToSQL(
38761
+ relation,
38762
+ table5,
38763
+ targetTable,
38764
+ throughTable
38765
+ );
38766
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
38642
38767
  const innerQuery = this.buildRelationalQuery({
38643
38768
  table: targetTable,
38644
38769
  mode: isSingle2 ? "first" : "many",
@@ -38647,9 +38772,11 @@ var init_dialect3 = __esm({
38647
38772
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
38648
38773
  tableNamesMap,
38649
38774
  tables,
38650
- relationWhere: relationFilter,
38775
+ relationWhere: filter2,
38651
38776
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
38652
- depth: currentDepth + 1
38777
+ depth: currentDepth + 1,
38778
+ isNested: true,
38779
+ throughJoin: throughJoin2
38653
38780
  });
38654
38781
  selection.push({
38655
38782
  field: targetTable,
@@ -38662,7 +38789,8 @@ var init_dialect3 = __esm({
38662
38789
  innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
38663
38790
  sql`, `
38664
38791
  );
38665
- return sql`, lateral(select ${isSingle2 ? sql`json_object(${jsonColumns}) as ${sql.identifier("r")}` : sql`coalesce(json_arrayagg(json_object(${jsonColumns})), json_array()) as ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}`;
38792
+ const joinQuery = sql` left join lateral(select ${sql`${isSingle2 ? sql`json_object(${jsonColumns})` : sql`coalesce(json_arrayagg(json_object(${jsonColumns})), json_array())`} as ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)} on true`;
38793
+ return joinQuery;
38666
38794
  })
38667
38795
  );
38668
38796
  })() : void 0;
@@ -38673,8 +38801,11 @@ var init_dialect3 = __esm({
38673
38801
  message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
38674
38802
  });
38675
38803
  }
38804
+ if (isNested && order) {
38805
+ selectionArr.push(sql`row_number() over (order by ${order})`);
38806
+ }
38676
38807
  const selectionSet = sql.join(selectionArr, sql`, `);
38677
- const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${sql`${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
38808
+ const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${throughJoin}${sql`${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
38678
38809
  return {
38679
38810
  sql: query,
38680
38811
  selection
package/api.mjs CHANGED
@@ -24847,15 +24847,8 @@ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (valu
24847
24847
  const field = selectionItem.field;
24848
24848
  if (is(field, Table2)) {
24849
24849
  const currentPath = `${path2 ? `${path2}.` : ""}${selectionItem.key}`;
24850
- if (row[selectionItem.key] === null) {
24851
- if (!selectionItem.isOptional) {
24852
- throw new DrizzleError({
24853
- message: `Unexpected null in relational query result on field "${currentPath}".
24854
- Did you forget to mark relation as optional?`
24855
- });
24856
- }
24850
+ if (row[selectionItem.key] === null)
24857
24851
  continue;
24858
- }
24859
24852
  if (parseJson)
24860
24853
  row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
24861
24854
  if (selectionItem.isArray) {
@@ -25065,18 +25058,49 @@ function relationExtrasToSQL(table5, extras) {
25065
25058
  selection
25066
25059
  };
25067
25060
  }
25068
- function relationToSQL(relation, sourceTable, targetTable) {
25061
+ function getTableSql(table5) {
25062
+ return sql`${sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(!table5[IsAlias] && table5[Schema])}`}${table5}`;
25063
+ }
25064
+ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
25065
+ if (relation.through) {
25066
+ const outerColumnWhere = relation.sourceColumns.map((s, i) => {
25067
+ const t = relation.through.source[i];
25068
+ return eq(
25069
+ sql`${getTableSql(sourceTable)}.${sql.identifier(s.name)}`,
25070
+ sql`${getTableSql(throughTable)}.${sql.identifier(t.name)}`
25071
+ );
25072
+ });
25073
+ const innerColumnWhere = relation.targetColumns.map((s, i) => {
25074
+ const t = relation.through.target[i];
25075
+ return eq(
25076
+ sql`${getTableSql(throughTable)}.${sql.identifier(t.name)}`,
25077
+ sql`${getTableSql(targetTable)}.${sql.identifier(s.name)}`
25078
+ );
25079
+ });
25080
+ return {
25081
+ filter: and(
25082
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25083
+ ),
25084
+ joinCondition: and(
25085
+ ...outerColumnWhere,
25086
+ ...innerColumnWhere
25087
+ )
25088
+ };
25089
+ }
25069
25090
  const columnWhere = relation.sourceColumns.map((s, i) => {
25070
25091
  const t = relation.targetColumns[i];
25071
25092
  return eq(
25072
- sql`${sql`${sql`${sql.identifier(sourceTable[Schema] ?? "")}.`.if(sourceTable[Schema] && !sourceTable[IsAlias])}`}${sourceTable}.${sql.identifier(s.name)}`,
25073
- sql`${sql`${sql`${sql.identifier(targetTable[Schema] ?? "")}.`.if(targetTable[Schema] && !targetTable[IsAlias])}`}${targetTable}.${sql.identifier(t.name)}`
25093
+ sql`${getTableSql(sourceTable)}.${sql.identifier(s.name)}`,
25094
+ sql`${getTableSql(targetTable)}.${sql.identifier(t.name)}`
25074
25095
  );
25075
25096
  });
25076
- const targetWhere = relation.where ? and(...columnWhere, relationsFilterToSQL(sourceTable, relation.where)) : and(...columnWhere);
25077
- return targetWhere;
25097
+ const fullWhere = and(
25098
+ ...columnWhere,
25099
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25100
+ );
25101
+ return { filter: fullWhere };
25078
25102
  }
25079
- var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, RelationsBuilderColumn, _a126, RelationsHelperStatic;
25103
+ var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, _RelationsBuilderColumn, RelationsBuilderColumn, _a126, RelationsHelperStatic;
25080
25104
  var init_relations = __esm({
25081
25105
  "../drizzle-orm/dist/relations.js"() {
25082
25106
  "use strict";
@@ -25084,7 +25108,6 @@ var init_relations = __esm({
25084
25108
  init_table();
25085
25109
  init_column();
25086
25110
  init_entity();
25087
- init_errors();
25088
25111
  init_primary_keys();
25089
25112
  init_expressions();
25090
25113
  init_sql();
@@ -25152,9 +25175,30 @@ var init_relations = __esm({
25152
25175
  if (relation.sourceColumns && relation.targetColumns) {
25153
25176
  if (relation.sourceColumns.length !== relation.targetColumns.length) {
25154
25177
  throw new Error(
25155
- `${relationPrintName}: "from" and "to" arrays must have the same length`
25178
+ `${relationPrintName}: "from" and "to" fields must have the same length`
25156
25179
  );
25157
25180
  }
25181
+ if (relation.through) {
25182
+ if (relation.through.source.length !== relation.through.target.length || relation.through.source.length !== relation.sourceColumns.length || relation.through.target.length !== relation.targetColumns.length) {
25183
+ throw new Error(
25184
+ `${relationPrintName}: ".through(column)" must be used either on all columns in "from" and "to" or not defined on any of them`
25185
+ );
25186
+ }
25187
+ for (const column5 of relation.through.source) {
25188
+ if (column5.table !== relation.throughTable) {
25189
+ throw new Error(
25190
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25191
+ );
25192
+ }
25193
+ }
25194
+ for (const column5 of relation.through.target) {
25195
+ if (column5.table !== relation.throughTable) {
25196
+ throw new Error(
25197
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25198
+ );
25199
+ }
25200
+ }
25201
+ }
25158
25202
  continue;
25159
25203
  }
25160
25204
  if (relation.sourceColumns || relation.targetColumns) {
@@ -25214,7 +25258,13 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25214
25258
  }
25215
25259
  relation.sourceColumns = reverseRelation.targetColumns;
25216
25260
  relation.targetColumns = reverseRelation.sourceColumns;
25217
- relation.where = reverseRelation.where;
25261
+ relation.through = reverseRelation.through ? {
25262
+ source: reverseRelation.through.target,
25263
+ target: reverseRelation.through.source
25264
+ } : void 0;
25265
+ relation.throughTable = reverseRelation.throughTable;
25266
+ relation.isReversed = !relation.where;
25267
+ relation.where = relation.where ?? reverseRelation.where;
25218
25268
  }
25219
25269
  }
25220
25270
  }
@@ -25229,6 +25279,9 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25229
25279
  __publicField(this, "alias");
25230
25280
  __publicField(this, "where");
25231
25281
  __publicField(this, "sourceTable");
25282
+ __publicField(this, "through");
25283
+ __publicField(this, "throughTable");
25284
+ __publicField(this, "isReversed");
25232
25285
  this.targetTable = targetTable;
25233
25286
  }
25234
25287
  };
@@ -25240,12 +25293,30 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25240
25293
  this.alias = config?.alias;
25241
25294
  this.where = config?.where;
25242
25295
  if (config?.from) {
25243
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25296
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25297
+ this.throughTable ??= it._.through?._.column.table;
25298
+ return it._.column;
25299
+ });
25244
25300
  }
25245
25301
  if (config?.to) {
25246
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25302
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25303
+ this.throughTable ??= it._.through?._.column.table;
25304
+ return it._.column;
25305
+ });
25247
25306
  }
25248
- this.optional = config?.optional ?? false;
25307
+ if (this.throughTable) {
25308
+ this.through = Array.isArray(config?.from) ? {
25309
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25310
+ source: config.from.map((e) => e._.through?._.column),
25311
+ target: (config.to ?? []).map((e) => e._.column)
25312
+ } : {
25313
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25314
+ source: [config?.from?._.through?._.column],
25315
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25316
+ target: [config?.to?._.through?._.column]
25317
+ };
25318
+ }
25319
+ this.optional = config?.optional ?? true;
25249
25320
  }
25250
25321
  };
25251
25322
  __publicField(One, _a120, "OneV2");
@@ -25256,10 +25327,28 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25256
25327
  this.alias = config?.alias;
25257
25328
  this.where = config?.where;
25258
25329
  if (config?.from) {
25259
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25330
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25331
+ this.throughTable ??= it._.through?._.column.table;
25332
+ return it._.column;
25333
+ });
25260
25334
  }
25261
25335
  if (config?.to) {
25262
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25336
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25337
+ this.throughTable ??= it._.through?._.column.table;
25338
+ return it._.column;
25339
+ });
25340
+ }
25341
+ if (this.throughTable) {
25342
+ this.through = Array.isArray(config?.from) ? {
25343
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25344
+ source: config.from.map((e) => e._.through?._.column),
25345
+ target: (config.to ?? []).map((e) => e._.column)
25346
+ } : {
25347
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25348
+ source: [config?.from?._.through?._.column],
25349
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25350
+ target: [config?.to?._.through?._.column]
25351
+ };
25263
25352
  }
25264
25353
  }
25265
25354
  };
@@ -25284,7 +25373,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25284
25373
  if (!this.query) {
25285
25374
  if (!this.table)
25286
25375
  throw new Error("Table must be set before building aggregate field");
25287
- this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
25376
+ const table5 = this.table;
25377
+ this.query = sql`select count(*) as ${sql.identifier("r")} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}`.mapWith(Number);
25288
25378
  }
25289
25379
  return this.query;
25290
25380
  }
@@ -25334,24 +25424,25 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25334
25424
  };
25335
25425
  __publicField(RelationsBuilderTable, _a124, "RelationsBuilderTable");
25336
25426
  _a125 = entityKind;
25337
- RelationsBuilderColumn = class {
25338
- constructor(column5) {
25427
+ _RelationsBuilderColumn = class _RelationsBuilderColumn {
25428
+ constructor(column5, through) {
25339
25429
  __publicField(this, "_");
25340
25430
  this._ = {
25341
25431
  tableName: getTableName(column5.table),
25342
25432
  data: void 0,
25343
- column: column5
25433
+ column: column5,
25434
+ through
25344
25435
  };
25345
25436
  }
25346
25437
  through(column5) {
25347
- this._.through = column5;
25348
- return this;
25438
+ return new _RelationsBuilderColumn(this._.column, column5);
25349
25439
  }
25350
25440
  getSQL() {
25351
25441
  return this._.column.getSQL();
25352
25442
  }
25353
25443
  };
25354
- __publicField(RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25444
+ __publicField(_RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25445
+ RelationsBuilderColumn = _RelationsBuilderColumn;
25355
25446
  _a126 = entityKind;
25356
25447
  RelationsHelperStatic = class {
25357
25448
  constructor(tables) {
@@ -26755,7 +26846,8 @@ var init_dialect = __esm({
26755
26846
  relationWhere,
26756
26847
  mode,
26757
26848
  errorPath,
26758
- depth
26849
+ depth,
26850
+ throughJoin
26759
26851
  }) {
26760
26852
  const selection = [];
26761
26853
  const isSingle = mode === "first";
@@ -26794,10 +26886,15 @@ var init_dialect = __esm({
26794
26886
  const relation = tableConfig.relations[k];
26795
26887
  const isSingle2 = is(relation, One);
26796
26888
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
26797
- const relationFilter = relationToSQL(relation, table5, targetTable);
26798
- selectionArr.push(
26799
- isSingle2 ? sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}` : sql`coalesce(${sql.identifier(k)}.${sql.identifier("r")}, '[]') as ${sql.identifier(k)}`
26889
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
26890
+ const { filter: filter2, joinCondition } = relationToSQL(
26891
+ relation,
26892
+ table5,
26893
+ targetTable,
26894
+ throughTable
26800
26895
  );
26896
+ selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
26897
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
26801
26898
  const innerQuery = this.buildRelationalQuery({
26802
26899
  table: targetTable,
26803
26900
  mode: isSingle2 ? "first" : "many",
@@ -26806,9 +26903,10 @@ var init_dialect = __esm({
26806
26903
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
26807
26904
  tableNamesMap,
26808
26905
  tables,
26809
- relationWhere: relationFilter,
26906
+ relationWhere: filter2,
26810
26907
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
26811
- depth: currentDepth + 1
26908
+ depth: currentDepth + 1,
26909
+ throughJoin: throughJoin2
26812
26910
  });
26813
26911
  selection.push({
26814
26912
  field: targetTable,
@@ -26817,7 +26915,8 @@ var init_dialect = __esm({
26817
26915
  isArray: !isSingle2,
26818
26916
  isOptional: (relation.optional ?? false) || join !== true && !!join.where
26819
26917
  });
26820
- return sql`left join lateral(select ${isSingle2 ? sql`row_to_json(${sql.identifier("t")}.*) ${sql.identifier("r")}` : sql`json_agg(row_to_json(${sql.identifier("t")}.*)) ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)} on true`;
26918
+ const joinQuery = sql`left join lateral(select ${isSingle2 ? sql`row_to_json(${sql.identifier("t")}.*) ${sql.identifier("r")}` : sql`coalesce(json_agg(row_to_json(${sql.identifier("t")}.*)), '[]') as ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)} on true`;
26919
+ return joinQuery;
26821
26920
  }),
26822
26921
  sql` `
26823
26922
  );
@@ -26830,7 +26929,7 @@ var init_dialect = __esm({
26830
26929
  });
26831
26930
  }
26832
26931
  const selectionSet = sql.join(selectionArr.filter((e) => e !== void 0), sql`, `);
26833
- const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${sql` ${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
26932
+ const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${throughJoin}${sql` ${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
26834
26933
  return {
26835
26934
  sql: query,
26836
26935
  selection
@@ -32835,7 +32934,8 @@ var init_dialect2 = __esm({
32835
32934
  mode,
32836
32935
  isNested,
32837
32936
  errorPath,
32838
- depth
32937
+ depth,
32938
+ throughJoin
32839
32939
  }) {
32840
32940
  const selection = [];
32841
32941
  const isSingle = mode === "first";
@@ -32872,7 +32972,14 @@ var init_dialect2 = __esm({
32872
32972
  const relation = tableConfig.relations[k];
32873
32973
  const isSingle2 = is(relation, One);
32874
32974
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
32875
- const relationFilter = relationToSQL(relation, table5, targetTable);
32975
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
32976
+ const { filter: filter2, joinCondition } = relationToSQL(
32977
+ relation,
32978
+ table5,
32979
+ targetTable,
32980
+ throughTable
32981
+ );
32982
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
32876
32983
  const innerQuery = this.buildRelationalQuery({
32877
32984
  table: targetTable,
32878
32985
  mode: isSingle2 ? "first" : "many",
@@ -32881,10 +32988,11 @@ var init_dialect2 = __esm({
32881
32988
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
32882
32989
  tableNamesMap,
32883
32990
  tables,
32884
- relationWhere: relationFilter,
32991
+ relationWhere: filter2,
32885
32992
  isNested: true,
32886
32993
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
32887
- depth: currentDepth + 1
32994
+ depth: currentDepth + 1,
32995
+ throughJoin: throughJoin2
32888
32996
  });
32889
32997
  selection.push({
32890
32998
  field: targetTable,
@@ -32899,7 +33007,9 @@ var init_dialect2 = __esm({
32899
33007
  }),
32900
33008
  sql`, `
32901
33009
  );
32902
- return isNested ? isSingle2 ? sql`(select jsonb_object(${jsonColumns}) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select jsonb_group_array(json_object(${jsonColumns})) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}` : isSingle2 ? sql`(select json_object(${jsonColumns}) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select json_group_array(json_object(${jsonColumns})) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}`;
33010
+ const json4 = isNested ? sql`jsonb` : sql`json`;
33011
+ const joinQuery = isSingle2 ? sql`(select ${json4}_object(${jsonColumns}) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select ${json4}_group_array(json_object(${jsonColumns})) as ${sql.identifier("r")} from (${innerQuery.sql}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}`;
33012
+ return joinQuery;
32903
33013
  }),
32904
33014
  sql`, `
32905
33015
  );
@@ -32911,7 +33021,7 @@ var init_dialect2 = __esm({
32911
33021
  });
32912
33022
  }
32913
33023
  const selectionSet = sql.join(selectionArr, sql`, `);
32914
- const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
33024
+ const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${throughJoin}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
32915
33025
  return {
32916
33026
  sql: query,
32917
33027
  selection
@@ -37783,6 +37893,12 @@ var init_dialect3 = __esm({
37783
37893
  columnIdentifiers.push(
37784
37894
  sql`${table5[column5.tsName]} as ${sql.identifier(column5.tsName)}`
37785
37895
  );
37896
+ selection.push(
37897
+ {
37898
+ key: column5.tsName,
37899
+ field: column5.column
37900
+ }
37901
+ );
37786
37902
  }
37787
37903
  return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
37788
37904
  })() : this.unwrapAllColumns(table5, selection));
@@ -38604,7 +38720,9 @@ var init_dialect3 = __esm({
38604
38720
  relationWhere,
38605
38721
  mode,
38606
38722
  errorPath,
38607
- depth
38723
+ depth,
38724
+ isNested,
38725
+ throughJoin
38608
38726
  }) {
38609
38727
  const selection = [];
38610
38728
  const isSingle = mode === "first";
@@ -38638,12 +38756,19 @@ var init_dialect3 = __esm({
38638
38756
  key: k,
38639
38757
  field: relation2
38640
38758
  });
38641
- return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
38759
+ return sql` left join lateral (${query2}) as ${sql.identifier(k)} on true`;
38642
38760
  }
38643
38761
  const relation = tableConfig.relations[k];
38644
38762
  const isSingle2 = is(relation, One);
38645
38763
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
38646
- const relationFilter = relationToSQL(relation, table5, targetTable);
38764
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
38765
+ const { filter: filter2, joinCondition } = relationToSQL(
38766
+ relation,
38767
+ table5,
38768
+ targetTable,
38769
+ throughTable
38770
+ );
38771
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
38647
38772
  const innerQuery = this.buildRelationalQuery({
38648
38773
  table: targetTable,
38649
38774
  mode: isSingle2 ? "first" : "many",
@@ -38652,9 +38777,11 @@ var init_dialect3 = __esm({
38652
38777
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
38653
38778
  tableNamesMap,
38654
38779
  tables,
38655
- relationWhere: relationFilter,
38780
+ relationWhere: filter2,
38656
38781
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
38657
- depth: currentDepth + 1
38782
+ depth: currentDepth + 1,
38783
+ isNested: true,
38784
+ throughJoin: throughJoin2
38658
38785
  });
38659
38786
  selection.push({
38660
38787
  field: targetTable,
@@ -38667,7 +38794,8 @@ var init_dialect3 = __esm({
38667
38794
  innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
38668
38795
  sql`, `
38669
38796
  );
38670
- return sql`, lateral(select ${isSingle2 ? sql`json_object(${jsonColumns}) as ${sql.identifier("r")}` : sql`coalesce(json_arrayagg(json_object(${jsonColumns})), json_array()) as ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)}`;
38797
+ const joinQuery = sql` left join lateral(select ${sql`${isSingle2 ? sql`json_object(${jsonColumns})` : sql`coalesce(json_arrayagg(json_object(${jsonColumns})), json_array())`} as ${sql.identifier("r")}`} from (${innerQuery.sql}) as ${sql.identifier("t")}) as ${sql.identifier(k)} on true`;
38798
+ return joinQuery;
38671
38799
  })
38672
38800
  );
38673
38801
  })() : void 0;
@@ -38678,8 +38806,11 @@ var init_dialect3 = __esm({
38678
38806
  message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
38679
38807
  });
38680
38808
  }
38809
+ if (isNested && order) {
38810
+ selectionArr.push(sql`row_number() over (order by ${order})`);
38811
+ }
38681
38812
  const selectionSet = sql.join(selectionArr, sql`, `);
38682
- const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${sql`${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
38813
+ const query = sql`select ${selectionSet} from ${table5[IsAlias] ? sql`${sql`${sql.identifier(table5[Schema] ?? "")}.`.if(table5[Schema])}${sql.identifier(table5[OriginalName])} as ${table5}` : table5}${throughJoin}${sql`${joins}`.if(joins)}${sql` where ${where}`.if(where)}${sql` order by ${order}`.if(order)}${sql` limit ${limit}`.if(limit !== void 0)}${sql` offset ${offset}`.if(offset !== void 0)}`;
38683
38814
  return {
38684
38815
  sql: query,
38685
38816
  selection
package/bin.cjs CHANGED
@@ -92611,7 +92611,7 @@ init_utils5();
92611
92611
  var version2 = async () => {
92612
92612
  const { npmVersion } = await ormCoreVersions();
92613
92613
  const ormVersion = npmVersion ? `drizzle-orm: v${npmVersion}` : "";
92614
- const envVersion = "0.30.1-38fedf0";
92614
+ const envVersion = "0.30.1-5734f71";
92615
92615
  const kitVersion = envVersion ? `v${envVersion}` : "--";
92616
92616
  const versions = `drizzle-kit: ${kitVersion}
92617
92617
  ${ormVersion}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.30.1-38fedf0",
3
+ "version": "0.30.1-5734f71",
4
4
  "homepage": "https://orm.drizzle.team",
5
5
  "keywords": [
6
6
  "drizzle",