drizzle-kit 0.30.1-e6823b4 → 0.30.1-efed06b

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 +247 -68
  2. package/api.mjs +247 -68
  3. package/bin.cjs +1 -1
  4. package/package.json +1 -1
package/api.js CHANGED
@@ -22255,7 +22255,7 @@ var init_sql = __esm({
22255
22255
  const schemaName = chunk[Table2.Symbol.Schema];
22256
22256
  const tableName = chunk[Table2.Symbol.Name];
22257
22257
  return {
22258
- sql: schemaName === void 0 ? escapeName(tableName) : escapeName(schemaName) + "." + escapeName(tableName),
22258
+ sql: schemaName === void 0 || chunk[Table2.Symbol.IsAlias] ? escapeName(tableName) : escapeName(schemaName) + "." + escapeName(tableName),
22259
22259
  params: []
22260
22260
  };
22261
22261
  }
@@ -24837,24 +24837,33 @@ var init_primary_keys = __esm({
24837
24837
  function getOrderByOperators() {
24838
24838
  return orderByOperators;
24839
24839
  }
24840
- function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false) {
24840
+ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false, path2) {
24841
24841
  for (const selectionItem of buildQueryResultSelection) {
24842
24842
  const field = selectionItem.field;
24843
24843
  if (is(field, Table2)) {
24844
+ const currentPath = `${path2 ? `${path2}.` : ""}${selectionItem.key}`;
24844
24845
  if (row[selectionItem.key] === null)
24845
24846
  continue;
24846
24847
  if (parseJson)
24847
24848
  row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
24848
24849
  if (selectionItem.isArray) {
24849
24850
  for (const item of row[selectionItem.key]) {
24850
- mapRelationalRow(item, selectionItem.selection, mapColumnValue);
24851
+ mapRelationalRow(
24852
+ item,
24853
+ selectionItem.selection,
24854
+ mapColumnValue,
24855
+ false,
24856
+ currentPath
24857
+ );
24851
24858
  }
24852
24859
  continue;
24853
24860
  }
24854
24861
  mapRelationalRow(
24855
24862
  row[selectionItem.key],
24856
24863
  selectionItem.selection,
24857
- mapColumnValue
24864
+ mapColumnValue,
24865
+ false,
24866
+ currentPath
24858
24867
  );
24859
24868
  continue;
24860
24869
  }
@@ -24905,7 +24914,7 @@ function defineRelations(schema5, relations) {
24905
24914
  );
24906
24915
  }
24907
24916
  function relationsFieldFilterToSQL(column5, filter2) {
24908
- if (typeof filter2 !== "object" || is(filter2, Placeholder))
24917
+ if (typeof filter2 !== "object")
24909
24918
  return eq(column5, filter2);
24910
24919
  const entries = Object.entries(filter2);
24911
24920
  if (!entries.length)
@@ -24932,6 +24941,21 @@ function relationsFieldFilterToSQL(column5, filter2) {
24932
24941
  );
24933
24942
  continue;
24934
24943
  }
24944
+ case "isNotNull":
24945
+ case "isNull": {
24946
+ if (!value)
24947
+ continue;
24948
+ parts.push(operators[target](column5));
24949
+ continue;
24950
+ }
24951
+ case "in": {
24952
+ parts.push(operators.inArray(column5, value));
24953
+ continue;
24954
+ }
24955
+ case "notIn": {
24956
+ parts.push(operators.notInArray(column5, value));
24957
+ continue;
24958
+ }
24935
24959
  default: {
24936
24960
  parts.push(
24937
24961
  operators[target](
@@ -24959,7 +24983,7 @@ function relationsFilterToSQL(table5, filter2) {
24959
24983
  case "RAW": {
24960
24984
  if (value) {
24961
24985
  parts.push(
24962
- value(table5[Columns], operators)
24986
+ value(table5, operators)
24963
24987
  );
24964
24988
  }
24965
24989
  continue;
@@ -25029,18 +25053,46 @@ function relationExtrasToSQL(table5, extras) {
25029
25053
  selection
25030
25054
  };
25031
25055
  }
25032
- function relationToSQL(relation, sourceTable, targetTable) {
25056
+ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
25057
+ if (relation.through) {
25058
+ const outerColumnWhere = relation.sourceColumns.map((s, i) => {
25059
+ const t = relation.through.source[i];
25060
+ return eq(
25061
+ sql`${sourceTable}.${sql.identifier(s.name)}`,
25062
+ sql`${throughTable}.${sql.identifier(t.name)}`
25063
+ );
25064
+ });
25065
+ const innerColumnWhere = relation.targetColumns.map((s, i) => {
25066
+ const t = relation.through.target[i];
25067
+ return eq(
25068
+ sql`${throughTable}.${sql.identifier(t.name)}`,
25069
+ sql`${targetTable}.${sql.identifier(s.name)}`
25070
+ );
25071
+ });
25072
+ return {
25073
+ filter: and(
25074
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25075
+ ),
25076
+ joinCondition: and(
25077
+ ...outerColumnWhere,
25078
+ ...innerColumnWhere
25079
+ )
25080
+ };
25081
+ }
25033
25082
  const columnWhere = relation.sourceColumns.map((s, i) => {
25034
25083
  const t = relation.targetColumns[i];
25035
25084
  return eq(
25036
- sql`${sql`${sql`${sql.identifier(sourceTable[Schema] ?? "")}.`.if(sourceTable[Schema] && !sourceTable[IsAlias])}`}${sourceTable}.${sql.identifier(s.name)}`,
25037
- sql`${sql`${sql`${sql.identifier(targetTable[Schema] ?? "")}.`.if(targetTable[Schema] && !targetTable[IsAlias])}`}${targetTable}.${sql.identifier(t.name)}`
25085
+ sql`${sourceTable}.${sql.identifier(s.name)}`,
25086
+ sql`${targetTable}.${sql.identifier(t.name)}`
25038
25087
  );
25039
25088
  });
25040
- const targetWhere = relation.where ? and(...columnWhere, relationsFilterToSQL(sourceTable, relation.where)) : and(...columnWhere);
25041
- return targetWhere;
25089
+ const fullWhere = and(
25090
+ ...columnWhere,
25091
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25092
+ );
25093
+ return { filter: fullWhere };
25042
25094
  }
25043
- var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, RelationsBuilderColumn, _a126, RelationsHelperStatic;
25095
+ 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;
25044
25096
  var init_relations = __esm({
25045
25097
  "../drizzle-orm/dist/relations.js"() {
25046
25098
  "use strict";
@@ -25115,9 +25167,30 @@ var init_relations = __esm({
25115
25167
  if (relation.sourceColumns && relation.targetColumns) {
25116
25168
  if (relation.sourceColumns.length !== relation.targetColumns.length) {
25117
25169
  throw new Error(
25118
- `${relationPrintName}: "from" and "to" arrays must have the same length`
25170
+ `${relationPrintName}: "from" and "to" fields must have the same length`
25119
25171
  );
25120
25172
  }
25173
+ if (relation.through) {
25174
+ if (relation.through.source.length !== relation.through.target.length || relation.through.source.length !== relation.sourceColumns.length || relation.through.target.length !== relation.targetColumns.length) {
25175
+ throw new Error(
25176
+ `${relationPrintName}: ".through(column)" must be used either on all columns in "from" and "to" or not defined on any of them`
25177
+ );
25178
+ }
25179
+ for (const column5 of relation.through.source) {
25180
+ if (column5.table !== relation.throughTable) {
25181
+ throw new Error(
25182
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25183
+ );
25184
+ }
25185
+ }
25186
+ for (const column5 of relation.through.target) {
25187
+ if (column5.table !== relation.throughTable) {
25188
+ throw new Error(
25189
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25190
+ );
25191
+ }
25192
+ }
25193
+ }
25121
25194
  continue;
25122
25195
  }
25123
25196
  if (relation.sourceColumns || relation.targetColumns) {
@@ -25177,7 +25250,13 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25177
25250
  }
25178
25251
  relation.sourceColumns = reverseRelation.targetColumns;
25179
25252
  relation.targetColumns = reverseRelation.sourceColumns;
25180
- relation.where = reverseRelation.where;
25253
+ relation.through = reverseRelation.through ? {
25254
+ source: reverseRelation.through.target,
25255
+ target: reverseRelation.through.source
25256
+ } : void 0;
25257
+ relation.throughTable = reverseRelation.throughTable;
25258
+ relation.isReversed = !relation.where;
25259
+ relation.where = relation.where ?? reverseRelation.where;
25181
25260
  }
25182
25261
  }
25183
25262
  }
@@ -25192,6 +25271,9 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25192
25271
  __publicField(this, "alias");
25193
25272
  __publicField(this, "where");
25194
25273
  __publicField(this, "sourceTable");
25274
+ __publicField(this, "through");
25275
+ __publicField(this, "throughTable");
25276
+ __publicField(this, "isReversed");
25195
25277
  this.targetTable = targetTable;
25196
25278
  }
25197
25279
  };
@@ -25203,12 +25285,30 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25203
25285
  this.alias = config?.alias;
25204
25286
  this.where = config?.where;
25205
25287
  if (config?.from) {
25206
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25288
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25289
+ this.throughTable ??= it._.through?._.column.table;
25290
+ return it._.column;
25291
+ });
25207
25292
  }
25208
25293
  if (config?.to) {
25209
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25294
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25295
+ this.throughTable ??= it._.through?._.column.table;
25296
+ return it._.column;
25297
+ });
25210
25298
  }
25211
- this.optional = config?.optional ?? false;
25299
+ if (this.throughTable) {
25300
+ this.through = Array.isArray(config?.from) ? {
25301
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25302
+ source: config.from.map((e) => e._.through?._.column),
25303
+ target: (config.to ?? []).map((e) => e._.column)
25304
+ } : {
25305
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25306
+ source: [config?.from?._.through?._.column],
25307
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25308
+ target: [config?.to?._.through?._.column]
25309
+ };
25310
+ }
25311
+ this.optional = config?.optional ?? true;
25212
25312
  }
25213
25313
  };
25214
25314
  __publicField(One, _a120, "OneV2");
@@ -25219,10 +25319,28 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25219
25319
  this.alias = config?.alias;
25220
25320
  this.where = config?.where;
25221
25321
  if (config?.from) {
25222
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25322
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25323
+ this.throughTable ??= it._.through?._.column.table;
25324
+ return it._.column;
25325
+ });
25223
25326
  }
25224
25327
  if (config?.to) {
25225
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25328
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25329
+ this.throughTable ??= it._.through?._.column.table;
25330
+ return it._.column;
25331
+ });
25332
+ }
25333
+ if (this.throughTable) {
25334
+ this.through = Array.isArray(config?.from) ? {
25335
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25336
+ source: config.from.map((e) => e._.through?._.column),
25337
+ target: (config.to ?? []).map((e) => e._.column)
25338
+ } : {
25339
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25340
+ source: [config?.from?._.through?._.column],
25341
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25342
+ target: [config?.to?._.through?._.column]
25343
+ };
25226
25344
  }
25227
25345
  }
25228
25346
  };
@@ -25247,7 +25365,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25247
25365
  if (!this.query) {
25248
25366
  if (!this.table)
25249
25367
  throw new Error("Table must be set before building aggregate field");
25250
- this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
25368
+ const table5 = this.table;
25369
+ 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);
25251
25370
  }
25252
25371
  return this.query;
25253
25372
  }
@@ -25297,24 +25416,25 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25297
25416
  };
25298
25417
  __publicField(RelationsBuilderTable, _a124, "RelationsBuilderTable");
25299
25418
  _a125 = entityKind;
25300
- RelationsBuilderColumn = class {
25301
- constructor(column5) {
25419
+ _RelationsBuilderColumn = class _RelationsBuilderColumn {
25420
+ constructor(column5, through) {
25302
25421
  __publicField(this, "_");
25303
25422
  this._ = {
25304
25423
  tableName: getTableName(column5.table),
25305
25424
  data: void 0,
25306
- column: column5
25425
+ column: column5,
25426
+ through
25307
25427
  };
25308
25428
  }
25309
25429
  through(column5) {
25310
- this._.through = column5;
25311
- return this;
25430
+ return new _RelationsBuilderColumn(this._.column, column5);
25312
25431
  }
25313
25432
  getSQL() {
25314
25433
  return this._.column.getSQL();
25315
25434
  }
25316
25435
  };
25317
- __publicField(RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25436
+ __publicField(_RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25437
+ RelationsBuilderColumn = _RelationsBuilderColumn;
25318
25438
  _a126 = entityKind;
25319
25439
  RelationsHelperStatic = class {
25320
25440
  constructor(tables) {
@@ -26071,7 +26191,7 @@ var init_dialect = __esm({
26071
26191
  sql`, `
26072
26192
  );
26073
26193
  });
26074
- __publicField(this, "buildColumns", (table5, tableConfig, selection, config) => config?.columns ? (() => {
26194
+ __publicField(this, "buildColumns", (table5, selection, config) => config?.columns ? (() => {
26075
26195
  const entries = Object.entries(config.columns);
26076
26196
  const columnIdentifiers = [];
26077
26197
  let colSelectionMode;
@@ -26718,7 +26838,8 @@ var init_dialect = __esm({
26718
26838
  relationWhere,
26719
26839
  mode,
26720
26840
  errorPath,
26721
- depth
26841
+ depth,
26842
+ throughJoin
26722
26843
  }) {
26723
26844
  const selection = [];
26724
26845
  const isSingle = mode === "first";
@@ -26729,7 +26850,7 @@ var init_dialect = __esm({
26729
26850
  const offset = params?.offset;
26730
26851
  const where = params?.where && relationWhere ? and(relationsFilterToSQL(table5, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table5, params.where) : relationWhere;
26731
26852
  const order = params?.orderBy ? relationsOrderToSQL(table5, params.orderBy) : void 0;
26732
- const columns = this.buildColumns(table5, tableConfig, selection, params);
26853
+ const columns = this.buildColumns(table5, selection, params);
26733
26854
  const extras = params?.extras ? relationExtrasToSQL(table5, params.extras) : void 0;
26734
26855
  if (extras)
26735
26856
  selection.push(...extras.selection);
@@ -26757,10 +26878,15 @@ var init_dialect = __esm({
26757
26878
  const relation = tableConfig.relations[k];
26758
26879
  const isSingle2 = is(relation, One);
26759
26880
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
26760
- const relationFilter = relationToSQL(relation, table5, targetTable);
26761
- selectionArr.push(
26762
- isSingle2 ? sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}` : sql`coalesce(${sql.identifier(k)}.${sql.identifier("r")}, '[]') as ${sql.identifier(k)}`
26881
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
26882
+ const { filter: filter2, joinCondition } = relationToSQL(
26883
+ relation,
26884
+ table5,
26885
+ targetTable,
26886
+ throughTable
26763
26887
  );
26888
+ selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
26889
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
26764
26890
  const innerQuery = this.buildRelationalQuery({
26765
26891
  table: targetTable,
26766
26892
  mode: isSingle2 ? "first" : "many",
@@ -26769,17 +26895,20 @@ var init_dialect = __esm({
26769
26895
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
26770
26896
  tableNamesMap,
26771
26897
  tables,
26772
- relationWhere: relationFilter,
26898
+ relationWhere: filter2,
26773
26899
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
26774
- depth: currentDepth + 1
26900
+ depth: currentDepth + 1,
26901
+ throughJoin: throughJoin2
26775
26902
  });
26776
26903
  selection.push({
26777
26904
  field: targetTable,
26778
26905
  key: k,
26779
26906
  selection: innerQuery.selection,
26780
- isArray: !isSingle2
26907
+ isArray: !isSingle2,
26908
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
26781
26909
  });
26782
- 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`;
26910
+ 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`;
26911
+ return joinQuery;
26783
26912
  }),
26784
26913
  sql` `
26785
26914
  );
@@ -26792,7 +26921,7 @@ var init_dialect = __esm({
26792
26921
  });
26793
26922
  }
26794
26923
  const selectionSet = sql.join(selectionArr.filter((e) => e !== void 0), sql`, `);
26795
- 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)}`;
26924
+ 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)}`;
26796
26925
  return {
26797
26926
  sql: query,
26798
26927
  selection
@@ -32797,7 +32926,8 @@ var init_dialect2 = __esm({
32797
32926
  mode,
32798
32927
  isNested,
32799
32928
  errorPath,
32800
- depth
32929
+ depth,
32930
+ throughJoin
32801
32931
  }) {
32802
32932
  const selection = [];
32803
32933
  const isSingle = mode === "first";
@@ -32834,7 +32964,14 @@ var init_dialect2 = __esm({
32834
32964
  const relation = tableConfig.relations[k];
32835
32965
  const isSingle2 = is(relation, One);
32836
32966
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
32837
- const relationFilter = relationToSQL(relation, table5, targetTable);
32967
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
32968
+ const { filter: filter2, joinCondition } = relationToSQL(
32969
+ relation,
32970
+ table5,
32971
+ targetTable,
32972
+ throughTable
32973
+ );
32974
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
32838
32975
  const innerQuery = this.buildRelationalQuery({
32839
32976
  table: targetTable,
32840
32977
  mode: isSingle2 ? "first" : "many",
@@ -32843,16 +32980,18 @@ var init_dialect2 = __esm({
32843
32980
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
32844
32981
  tableNamesMap,
32845
32982
  tables,
32846
- relationWhere: relationFilter,
32983
+ relationWhere: filter2,
32847
32984
  isNested: true,
32848
32985
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
32849
- depth: currentDepth + 1
32986
+ depth: currentDepth + 1,
32987
+ throughJoin: throughJoin2
32850
32988
  });
32851
32989
  selection.push({
32852
32990
  field: targetTable,
32853
32991
  key: k,
32854
32992
  selection: innerQuery.selection,
32855
- isArray: !isSingle2
32993
+ isArray: !isSingle2,
32994
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
32856
32995
  });
32857
32996
  const jsonColumns = sql.join(
32858
32997
  innerQuery.selection.map((s) => {
@@ -32860,7 +32999,9 @@ var init_dialect2 = __esm({
32860
32999
  }),
32861
33000
  sql`, `
32862
33001
  );
32863
- 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)}`;
33002
+ const json4 = isNested ? sql`jsonb` : sql`json`;
33003
+ 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)}`;
33004
+ return joinQuery;
32864
33005
  }),
32865
33006
  sql`, `
32866
33007
  );
@@ -32872,7 +33013,7 @@ var init_dialect2 = __esm({
32872
33013
  });
32873
33014
  }
32874
33015
  const selectionSet = sql.join(selectionArr, sql`, `);
32875
- 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)}`;
33016
+ 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)}`;
32876
33017
  return {
32877
33018
  sql: query,
32878
33019
  selection
@@ -34247,9 +34388,10 @@ var init_query4 = __esm({
34247
34388
  init_entity();
34248
34389
  init_query_promise();
34249
34390
  init_relations();
34391
+ init_sql();
34250
34392
  _a224 = entityKind;
34251
34393
  RelationalQueryBuilder2 = class {
34252
- constructor(mode, tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session) {
34394
+ constructor(mode, tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session, rowMode) {
34253
34395
  this.mode = mode;
34254
34396
  this.tables = tables;
34255
34397
  this.schema = schema5;
@@ -34258,6 +34400,7 @@ var init_query4 = __esm({
34258
34400
  this.tableConfig = tableConfig;
34259
34401
  this.dialect = dialect6;
34260
34402
  this.session = session;
34403
+ this.rowMode = rowMode;
34261
34404
  }
34262
34405
  findMany(config) {
34263
34406
  return this.mode === "sync" ? new SQLiteSyncRelationalQuery2(
@@ -34269,7 +34412,8 @@ var init_query4 = __esm({
34269
34412
  this.dialect,
34270
34413
  this.session,
34271
34414
  config ?? true,
34272
- "many"
34415
+ "many",
34416
+ this.rowMode
34273
34417
  ) : new SQLiteRelationalQuery2(
34274
34418
  this.tables,
34275
34419
  this.schema,
@@ -34279,7 +34423,8 @@ var init_query4 = __esm({
34279
34423
  this.dialect,
34280
34424
  this.session,
34281
34425
  config ?? true,
34282
- "many"
34426
+ "many",
34427
+ this.rowMode
34283
34428
  );
34284
34429
  }
34285
34430
  findFirst(config) {
@@ -34292,7 +34437,8 @@ var init_query4 = __esm({
34292
34437
  this.dialect,
34293
34438
  this.session,
34294
34439
  config ?? true,
34295
- "first"
34440
+ "first",
34441
+ this.rowMode
34296
34442
  ) : new SQLiteRelationalQuery2(
34297
34443
  this.tables,
34298
34444
  this.schema,
@@ -34302,13 +34448,14 @@ var init_query4 = __esm({
34302
34448
  this.dialect,
34303
34449
  this.session,
34304
34450
  config ?? true,
34305
- "first"
34451
+ "first",
34452
+ this.rowMode
34306
34453
  );
34307
34454
  }
34308
34455
  };
34309
34456
  __publicField(RelationalQueryBuilder2, _a224, "SQLiteAsyncRelationalQueryBuilderV2");
34310
34457
  SQLiteRelationalQuery2 = class extends (_b156 = QueryPromise, _a225 = entityKind, _b156) {
34311
- constructor(tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session, config, mode) {
34458
+ constructor(tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session, config, mode, rowMode) {
34312
34459
  super();
34313
34460
  /** @internal */
34314
34461
  __publicField(this, "mode");
@@ -34321,6 +34468,7 @@ var init_query4 = __esm({
34321
34468
  this.dialect = dialect6;
34322
34469
  this.session = session;
34323
34470
  this.config = config;
34471
+ this.rowMode = rowMode;
34324
34472
  this.mode = mode;
34325
34473
  this.table = table5;
34326
34474
  }
@@ -34345,7 +34493,7 @@ var init_query4 = __esm({
34345
34493
  void 0,
34346
34494
  this.mode === "first" ? "get" : "all",
34347
34495
  (rawRows, mapColumnValue) => {
34348
- const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue, true));
34496
+ const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue, !this.rowMode));
34349
34497
  if (this.mode === "first") {
34350
34498
  return rows[0];
34351
34499
  }
@@ -34357,15 +34505,26 @@ var init_query4 = __esm({
34357
34505
  return this._prepare(false);
34358
34506
  }
34359
34507
  _getQuery() {
34360
- return this.dialect.buildRelationalQuery({
34508
+ const query = this.dialect.buildRelationalQuery({
34361
34509
  schema: this.schema,
34362
34510
  tableNamesMap: this.tableNamesMap,
34363
34511
  table: this.table,
34364
34512
  tableConfig: this.tableConfig,
34365
34513
  queryConfig: this.config,
34366
34514
  tables: this.tables,
34367
- mode: this.mode
34515
+ mode: this.mode,
34516
+ isNested: this.rowMode
34368
34517
  });
34518
+ if (this.rowMode) {
34519
+ const jsonColumns = sql.join(
34520
+ query.selection.map((s) => {
34521
+ return sql`${sql.raw(this.dialect.escapeString(s.key))}, ${s.selection ? sql`jsonb(${sql.identifier(s.key)})` : sql.identifier(s.key)}`;
34522
+ }),
34523
+ sql`, `
34524
+ );
34525
+ query.sql = sql`select json_object(${jsonColumns}) as ${sql.identifier("r")} from (${query.sql}) as ${sql.identifier("t")}`;
34526
+ }
34527
+ return query;
34369
34528
  }
34370
34529
  _toSQL() {
34371
34530
  const query = this._getQuery();
@@ -34448,7 +34607,7 @@ var init_db2 = __esm({
34448
34607
  init_raw2();
34449
34608
  _a228 = entityKind;
34450
34609
  BaseSQLiteDatabase = class {
34451
- constructor(resultKind, dialect6, session, relations, _schema) {
34610
+ constructor(resultKind, dialect6, session, relations, _schema, rowModeRQB) {
34452
34611
  /** @deprecated */
34453
34612
  __publicField(this, "_query");
34454
34613
  // TO-DO: Figure out how to pass DrizzleTypeError without breaking withReplicas
@@ -34456,6 +34615,7 @@ var init_db2 = __esm({
34456
34615
  this.resultKind = resultKind;
34457
34616
  this.dialect = dialect6;
34458
34617
  this.session = session;
34618
+ this.rowModeRQB = rowModeRQB;
34459
34619
  const rel = relations ?? {};
34460
34620
  this._ = _schema ? {
34461
34621
  schema: _schema.schema,
@@ -34495,7 +34655,8 @@ var init_db2 = __esm({
34495
34655
  relation.table,
34496
34656
  relation,
34497
34657
  dialect6,
34498
- session
34658
+ session,
34659
+ rowModeRQB
34499
34660
  );
34500
34661
  }
34501
34662
  }
@@ -34961,8 +35122,8 @@ var init_session2 = __esm({
34961
35122
  };
34962
35123
  __publicField(SQLiteSession, _a236, "SQLiteSession");
34963
35124
  SQLiteTransaction = class extends (_b160 = BaseSQLiteDatabase, _a237 = entityKind, _b160) {
34964
- constructor(resultType, dialect6, session, relations, schema5, nestedIndex = 0) {
34965
- super(resultType, dialect6, session, relations, schema5);
35125
+ constructor(resultType, dialect6, session, relations, schema5, nestedIndex = 0, rowModeRQB) {
35126
+ super(resultType, dialect6, session, relations, schema5, rowModeRQB);
34966
35127
  this.relations = relations;
34967
35128
  this.schema = schema5;
34968
35129
  this.nestedIndex = nestedIndex;
@@ -37741,9 +37902,11 @@ var init_dialect3 = __esm({
37741
37902
  const columnIdentifiers = [];
37742
37903
  const selectedColumns = this.getSelectedTableColumns(table5, params?.columns);
37743
37904
  for (const column5 of selectedColumns) {
37744
- columnIdentifiers.push(
37745
- sql`${table5[column5.tsName]} as ${sql.identifier(column5.tsName)}`
37746
- );
37905
+ columnIdentifiers.push(sql`${column5.column} as ${sql.identifier(column5.tsName)}`);
37906
+ selection.push({
37907
+ key: column5.tsName,
37908
+ field: column5.column
37909
+ });
37747
37910
  }
37748
37911
  return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
37749
37912
  })() : this.unwrapAllColumns(table5, selection));
@@ -38565,7 +38728,9 @@ var init_dialect3 = __esm({
38565
38728
  relationWhere,
38566
38729
  mode,
38567
38730
  errorPath,
38568
- depth
38731
+ depth,
38732
+ isNested,
38733
+ throughJoin
38569
38734
  }) {
38570
38735
  const selection = [];
38571
38736
  const isSingle = mode === "first";
@@ -38599,12 +38764,19 @@ var init_dialect3 = __esm({
38599
38764
  key: k,
38600
38765
  field: relation2
38601
38766
  });
38602
- return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
38767
+ return sql` left join lateral (${query2}) as ${sql.identifier(k)} on true`;
38603
38768
  }
38604
38769
  const relation = tableConfig.relations[k];
38605
38770
  const isSingle2 = is(relation, One);
38606
38771
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
38607
- const relationFilter = relationToSQL(relation, table5, targetTable);
38772
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
38773
+ const { filter: filter2, joinCondition } = relationToSQL(
38774
+ relation,
38775
+ table5,
38776
+ targetTable,
38777
+ throughTable
38778
+ );
38779
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
38608
38780
  const innerQuery = this.buildRelationalQuery({
38609
38781
  table: targetTable,
38610
38782
  mode: isSingle2 ? "first" : "many",
@@ -38613,21 +38785,25 @@ var init_dialect3 = __esm({
38613
38785
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
38614
38786
  tableNamesMap,
38615
38787
  tables,
38616
- relationWhere: relationFilter,
38788
+ relationWhere: filter2,
38617
38789
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
38618
- depth: currentDepth + 1
38790
+ depth: currentDepth + 1,
38791
+ isNested: true,
38792
+ throughJoin: throughJoin2
38619
38793
  });
38620
38794
  selection.push({
38621
38795
  field: targetTable,
38622
38796
  key: k,
38623
38797
  selection: innerQuery.selection,
38624
- isArray: !isSingle2
38798
+ isArray: !isSingle2,
38799
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
38625
38800
  });
38626
38801
  const jsonColumns = sql.join(
38627
38802
  innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
38628
38803
  sql`, `
38629
38804
  );
38630
- 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)}`;
38805
+ 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`;
38806
+ return joinQuery;
38631
38807
  })
38632
38808
  );
38633
38809
  })() : void 0;
@@ -38638,8 +38814,11 @@ var init_dialect3 = __esm({
38638
38814
  message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
38639
38815
  });
38640
38816
  }
38817
+ if (isNested && order) {
38818
+ selectionArr.push(sql`row_number() over (order by ${order})`);
38819
+ }
38641
38820
  const selectionSet = sql.join(selectionArr, sql`, `);
38642
- 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)}`;
38821
+ 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)}`;
38643
38822
  return {
38644
38823
  sql: query,
38645
38824
  selection