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.mjs CHANGED
@@ -22260,7 +22260,7 @@ var init_sql = __esm({
22260
22260
  const schemaName = chunk[Table2.Symbol.Schema];
22261
22261
  const tableName = chunk[Table2.Symbol.Name];
22262
22262
  return {
22263
- sql: schemaName === void 0 ? escapeName(tableName) : escapeName(schemaName) + "." + escapeName(tableName),
22263
+ sql: schemaName === void 0 || chunk[Table2.Symbol.IsAlias] ? escapeName(tableName) : escapeName(schemaName) + "." + escapeName(tableName),
22264
22264
  params: []
22265
22265
  };
22266
22266
  }
@@ -24842,24 +24842,33 @@ var init_primary_keys = __esm({
24842
24842
  function getOrderByOperators() {
24843
24843
  return orderByOperators;
24844
24844
  }
24845
- function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false) {
24845
+ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false, path2) {
24846
24846
  for (const selectionItem of buildQueryResultSelection) {
24847
24847
  const field = selectionItem.field;
24848
24848
  if (is(field, Table2)) {
24849
+ const currentPath = `${path2 ? `${path2}.` : ""}${selectionItem.key}`;
24849
24850
  if (row[selectionItem.key] === null)
24850
24851
  continue;
24851
24852
  if (parseJson)
24852
24853
  row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
24853
24854
  if (selectionItem.isArray) {
24854
24855
  for (const item of row[selectionItem.key]) {
24855
- mapRelationalRow(item, selectionItem.selection, mapColumnValue);
24856
+ mapRelationalRow(
24857
+ item,
24858
+ selectionItem.selection,
24859
+ mapColumnValue,
24860
+ false,
24861
+ currentPath
24862
+ );
24856
24863
  }
24857
24864
  continue;
24858
24865
  }
24859
24866
  mapRelationalRow(
24860
24867
  row[selectionItem.key],
24861
24868
  selectionItem.selection,
24862
- mapColumnValue
24869
+ mapColumnValue,
24870
+ false,
24871
+ currentPath
24863
24872
  );
24864
24873
  continue;
24865
24874
  }
@@ -24910,7 +24919,7 @@ function defineRelations(schema5, relations) {
24910
24919
  );
24911
24920
  }
24912
24921
  function relationsFieldFilterToSQL(column5, filter2) {
24913
- if (typeof filter2 !== "object" || is(filter2, Placeholder))
24922
+ if (typeof filter2 !== "object")
24914
24923
  return eq(column5, filter2);
24915
24924
  const entries = Object.entries(filter2);
24916
24925
  if (!entries.length)
@@ -24937,6 +24946,21 @@ function relationsFieldFilterToSQL(column5, filter2) {
24937
24946
  );
24938
24947
  continue;
24939
24948
  }
24949
+ case "isNotNull":
24950
+ case "isNull": {
24951
+ if (!value)
24952
+ continue;
24953
+ parts.push(operators[target](column5));
24954
+ continue;
24955
+ }
24956
+ case "in": {
24957
+ parts.push(operators.inArray(column5, value));
24958
+ continue;
24959
+ }
24960
+ case "notIn": {
24961
+ parts.push(operators.notInArray(column5, value));
24962
+ continue;
24963
+ }
24940
24964
  default: {
24941
24965
  parts.push(
24942
24966
  operators[target](
@@ -24964,7 +24988,7 @@ function relationsFilterToSQL(table5, filter2) {
24964
24988
  case "RAW": {
24965
24989
  if (value) {
24966
24990
  parts.push(
24967
- value(table5[Columns], operators)
24991
+ value(table5, operators)
24968
24992
  );
24969
24993
  }
24970
24994
  continue;
@@ -25034,18 +25058,46 @@ function relationExtrasToSQL(table5, extras) {
25034
25058
  selection
25035
25059
  };
25036
25060
  }
25037
- function relationToSQL(relation, sourceTable, targetTable) {
25061
+ function relationToSQL(relation, sourceTable, targetTable, throughTable) {
25062
+ if (relation.through) {
25063
+ const outerColumnWhere = relation.sourceColumns.map((s, i) => {
25064
+ const t = relation.through.source[i];
25065
+ return eq(
25066
+ sql`${sourceTable}.${sql.identifier(s.name)}`,
25067
+ sql`${throughTable}.${sql.identifier(t.name)}`
25068
+ );
25069
+ });
25070
+ const innerColumnWhere = relation.targetColumns.map((s, i) => {
25071
+ const t = relation.through.target[i];
25072
+ return eq(
25073
+ sql`${throughTable}.${sql.identifier(t.name)}`,
25074
+ sql`${targetTable}.${sql.identifier(s.name)}`
25075
+ );
25076
+ });
25077
+ return {
25078
+ filter: and(
25079
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25080
+ ),
25081
+ joinCondition: and(
25082
+ ...outerColumnWhere,
25083
+ ...innerColumnWhere
25084
+ )
25085
+ };
25086
+ }
25038
25087
  const columnWhere = relation.sourceColumns.map((s, i) => {
25039
25088
  const t = relation.targetColumns[i];
25040
25089
  return eq(
25041
- sql`${sql`${sql`${sql.identifier(sourceTable[Schema] ?? "")}.`.if(sourceTable[Schema] && !sourceTable[IsAlias])}`}${sourceTable}.${sql.identifier(s.name)}`,
25042
- sql`${sql`${sql`${sql.identifier(targetTable[Schema] ?? "")}.`.if(targetTable[Schema] && !targetTable[IsAlias])}`}${targetTable}.${sql.identifier(t.name)}`
25090
+ sql`${sourceTable}.${sql.identifier(s.name)}`,
25091
+ sql`${targetTable}.${sql.identifier(t.name)}`
25043
25092
  );
25044
25093
  });
25045
- const targetWhere = relation.where ? and(...columnWhere, relationsFilterToSQL(sourceTable, relation.where)) : and(...columnWhere);
25046
- return targetWhere;
25094
+ const fullWhere = and(
25095
+ ...columnWhere,
25096
+ relation.where ? relation.isReversed ? relationsFilterToSQL(targetTable, relation.where) : relationsFilterToSQL(sourceTable, relation.where) : void 0
25097
+ );
25098
+ return { filter: fullWhere };
25047
25099
  }
25048
- var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, RelationsBuilderColumn, _a126, RelationsHelperStatic;
25100
+ 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;
25049
25101
  var init_relations = __esm({
25050
25102
  "../drizzle-orm/dist/relations.js"() {
25051
25103
  "use strict";
@@ -25120,9 +25172,30 @@ var init_relations = __esm({
25120
25172
  if (relation.sourceColumns && relation.targetColumns) {
25121
25173
  if (relation.sourceColumns.length !== relation.targetColumns.length) {
25122
25174
  throw new Error(
25123
- `${relationPrintName}: "from" and "to" arrays must have the same length`
25175
+ `${relationPrintName}: "from" and "to" fields must have the same length`
25124
25176
  );
25125
25177
  }
25178
+ if (relation.through) {
25179
+ if (relation.through.source.length !== relation.through.target.length || relation.through.source.length !== relation.sourceColumns.length || relation.through.target.length !== relation.targetColumns.length) {
25180
+ throw new Error(
25181
+ `${relationPrintName}: ".through(column)" must be used either on all columns in "from" and "to" or not defined on any of them`
25182
+ );
25183
+ }
25184
+ for (const column5 of relation.through.source) {
25185
+ if (column5.table !== relation.throughTable) {
25186
+ throw new Error(
25187
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25188
+ );
25189
+ }
25190
+ }
25191
+ for (const column5 of relation.through.target) {
25192
+ if (column5.table !== relation.throughTable) {
25193
+ throw new Error(
25194
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
25195
+ );
25196
+ }
25197
+ }
25198
+ }
25126
25199
  continue;
25127
25200
  }
25128
25201
  if (relation.sourceColumns || relation.targetColumns) {
@@ -25182,7 +25255,13 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25182
25255
  }
25183
25256
  relation.sourceColumns = reverseRelation.targetColumns;
25184
25257
  relation.targetColumns = reverseRelation.sourceColumns;
25185
- relation.where = reverseRelation.where;
25258
+ relation.through = reverseRelation.through ? {
25259
+ source: reverseRelation.through.target,
25260
+ target: reverseRelation.through.source
25261
+ } : void 0;
25262
+ relation.throughTable = reverseRelation.throughTable;
25263
+ relation.isReversed = !relation.where;
25264
+ relation.where = relation.where ?? reverseRelation.where;
25186
25265
  }
25187
25266
  }
25188
25267
  }
@@ -25197,6 +25276,9 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25197
25276
  __publicField(this, "alias");
25198
25277
  __publicField(this, "where");
25199
25278
  __publicField(this, "sourceTable");
25279
+ __publicField(this, "through");
25280
+ __publicField(this, "throughTable");
25281
+ __publicField(this, "isReversed");
25200
25282
  this.targetTable = targetTable;
25201
25283
  }
25202
25284
  };
@@ -25208,12 +25290,30 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25208
25290
  this.alias = config?.alias;
25209
25291
  this.where = config?.where;
25210
25292
  if (config?.from) {
25211
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25293
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25294
+ this.throughTable ??= it._.through?._.column.table;
25295
+ return it._.column;
25296
+ });
25212
25297
  }
25213
25298
  if (config?.to) {
25214
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25299
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25300
+ this.throughTable ??= it._.through?._.column.table;
25301
+ return it._.column;
25302
+ });
25215
25303
  }
25216
- this.optional = config?.optional ?? false;
25304
+ if (this.throughTable) {
25305
+ this.through = Array.isArray(config?.from) ? {
25306
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25307
+ source: config.from.map((e) => e._.through?._.column),
25308
+ target: (config.to ?? []).map((e) => e._.column)
25309
+ } : {
25310
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25311
+ source: [config?.from?._.through?._.column],
25312
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25313
+ target: [config?.to?._.through?._.column]
25314
+ };
25315
+ }
25316
+ this.optional = config?.optional ?? true;
25217
25317
  }
25218
25318
  };
25219
25319
  __publicField(One, _a120, "OneV2");
@@ -25224,10 +25324,28 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25224
25324
  this.alias = config?.alias;
25225
25325
  this.where = config?.where;
25226
25326
  if (config?.from) {
25227
- this.sourceColumns = Array.isArray(config.from) ? config.from.map((it) => it._.column) : [config.from._.column];
25327
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
25328
+ this.throughTable ??= it._.through?._.column.table;
25329
+ return it._.column;
25330
+ });
25228
25331
  }
25229
25332
  if (config?.to) {
25230
- this.targetColumns = Array.isArray(config.to) ? config.to.map((it) => it._.column) : [config.to._.column];
25333
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
25334
+ this.throughTable ??= it._.through?._.column.table;
25335
+ return it._.column;
25336
+ });
25337
+ }
25338
+ if (this.throughTable) {
25339
+ this.through = Array.isArray(config?.from) ? {
25340
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25341
+ source: config.from.map((e) => e._.through?._.column),
25342
+ target: (config.to ?? []).map((e) => e._.column)
25343
+ } : {
25344
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain -- in case it's undefined, error will be thrown in Relations constructor
25345
+ source: [config?.from?._.through?._.column],
25346
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
25347
+ target: [config?.to?._.through?._.column]
25348
+ };
25231
25349
  }
25232
25350
  }
25233
25351
  };
@@ -25252,7 +25370,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25252
25370
  if (!this.query) {
25253
25371
  if (!this.table)
25254
25372
  throw new Error("Table must be set before building aggregate field");
25255
- this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
25373
+ const table5 = this.table;
25374
+ 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);
25256
25375
  }
25257
25376
  return this.query;
25258
25377
  }
@@ -25302,24 +25421,25 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25302
25421
  };
25303
25422
  __publicField(RelationsBuilderTable, _a124, "RelationsBuilderTable");
25304
25423
  _a125 = entityKind;
25305
- RelationsBuilderColumn = class {
25306
- constructor(column5) {
25424
+ _RelationsBuilderColumn = class _RelationsBuilderColumn {
25425
+ constructor(column5, through) {
25307
25426
  __publicField(this, "_");
25308
25427
  this._ = {
25309
25428
  tableName: getTableName(column5.table),
25310
25429
  data: void 0,
25311
- column: column5
25430
+ column: column5,
25431
+ through
25312
25432
  };
25313
25433
  }
25314
25434
  through(column5) {
25315
- this._.through = column5;
25316
- return this;
25435
+ return new _RelationsBuilderColumn(this._.column, column5);
25317
25436
  }
25318
25437
  getSQL() {
25319
25438
  return this._.column.getSQL();
25320
25439
  }
25321
25440
  };
25322
- __publicField(RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25441
+ __publicField(_RelationsBuilderColumn, _a125, "RelationsBuilderColumn");
25442
+ RelationsBuilderColumn = _RelationsBuilderColumn;
25323
25443
  _a126 = entityKind;
25324
25444
  RelationsHelperStatic = class {
25325
25445
  constructor(tables) {
@@ -26076,7 +26196,7 @@ var init_dialect = __esm({
26076
26196
  sql`, `
26077
26197
  );
26078
26198
  });
26079
- __publicField(this, "buildColumns", (table5, tableConfig, selection, config) => config?.columns ? (() => {
26199
+ __publicField(this, "buildColumns", (table5, selection, config) => config?.columns ? (() => {
26080
26200
  const entries = Object.entries(config.columns);
26081
26201
  const columnIdentifiers = [];
26082
26202
  let colSelectionMode;
@@ -26723,7 +26843,8 @@ var init_dialect = __esm({
26723
26843
  relationWhere,
26724
26844
  mode,
26725
26845
  errorPath,
26726
- depth
26846
+ depth,
26847
+ throughJoin
26727
26848
  }) {
26728
26849
  const selection = [];
26729
26850
  const isSingle = mode === "first";
@@ -26734,7 +26855,7 @@ var init_dialect = __esm({
26734
26855
  const offset = params?.offset;
26735
26856
  const where = params?.where && relationWhere ? and(relationsFilterToSQL(table5, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table5, params.where) : relationWhere;
26736
26857
  const order = params?.orderBy ? relationsOrderToSQL(table5, params.orderBy) : void 0;
26737
- const columns = this.buildColumns(table5, tableConfig, selection, params);
26858
+ const columns = this.buildColumns(table5, selection, params);
26738
26859
  const extras = params?.extras ? relationExtrasToSQL(table5, params.extras) : void 0;
26739
26860
  if (extras)
26740
26861
  selection.push(...extras.selection);
@@ -26762,10 +26883,15 @@ var init_dialect = __esm({
26762
26883
  const relation = tableConfig.relations[k];
26763
26884
  const isSingle2 = is(relation, One);
26764
26885
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
26765
- const relationFilter = relationToSQL(relation, table5, targetTable);
26766
- selectionArr.push(
26767
- isSingle2 ? sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}` : sql`coalesce(${sql.identifier(k)}.${sql.identifier("r")}, '[]') as ${sql.identifier(k)}`
26886
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
26887
+ const { filter: filter2, joinCondition } = relationToSQL(
26888
+ relation,
26889
+ table5,
26890
+ targetTable,
26891
+ throughTable
26768
26892
  );
26893
+ selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
26894
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
26769
26895
  const innerQuery = this.buildRelationalQuery({
26770
26896
  table: targetTable,
26771
26897
  mode: isSingle2 ? "first" : "many",
@@ -26774,17 +26900,20 @@ var init_dialect = __esm({
26774
26900
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
26775
26901
  tableNamesMap,
26776
26902
  tables,
26777
- relationWhere: relationFilter,
26903
+ relationWhere: filter2,
26778
26904
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
26779
- depth: currentDepth + 1
26905
+ depth: currentDepth + 1,
26906
+ throughJoin: throughJoin2
26780
26907
  });
26781
26908
  selection.push({
26782
26909
  field: targetTable,
26783
26910
  key: k,
26784
26911
  selection: innerQuery.selection,
26785
- isArray: !isSingle2
26912
+ isArray: !isSingle2,
26913
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
26786
26914
  });
26787
- 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`;
26915
+ 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`;
26916
+ return joinQuery;
26788
26917
  }),
26789
26918
  sql` `
26790
26919
  );
@@ -26797,7 +26926,7 @@ var init_dialect = __esm({
26797
26926
  });
26798
26927
  }
26799
26928
  const selectionSet = sql.join(selectionArr.filter((e) => e !== void 0), sql`, `);
26800
- 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)}`;
26929
+ 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)}`;
26801
26930
  return {
26802
26931
  sql: query,
26803
26932
  selection
@@ -32802,7 +32931,8 @@ var init_dialect2 = __esm({
32802
32931
  mode,
32803
32932
  isNested,
32804
32933
  errorPath,
32805
- depth
32934
+ depth,
32935
+ throughJoin
32806
32936
  }) {
32807
32937
  const selection = [];
32808
32938
  const isSingle = mode === "first";
@@ -32839,7 +32969,14 @@ var init_dialect2 = __esm({
32839
32969
  const relation = tableConfig.relations[k];
32840
32970
  const isSingle2 = is(relation, One);
32841
32971
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
32842
- const relationFilter = relationToSQL(relation, table5, targetTable);
32972
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
32973
+ const { filter: filter2, joinCondition } = relationToSQL(
32974
+ relation,
32975
+ table5,
32976
+ targetTable,
32977
+ throughTable
32978
+ );
32979
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
32843
32980
  const innerQuery = this.buildRelationalQuery({
32844
32981
  table: targetTable,
32845
32982
  mode: isSingle2 ? "first" : "many",
@@ -32848,16 +32985,18 @@ var init_dialect2 = __esm({
32848
32985
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
32849
32986
  tableNamesMap,
32850
32987
  tables,
32851
- relationWhere: relationFilter,
32988
+ relationWhere: filter2,
32852
32989
  isNested: true,
32853
32990
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
32854
- depth: currentDepth + 1
32991
+ depth: currentDepth + 1,
32992
+ throughJoin: throughJoin2
32855
32993
  });
32856
32994
  selection.push({
32857
32995
  field: targetTable,
32858
32996
  key: k,
32859
32997
  selection: innerQuery.selection,
32860
- isArray: !isSingle2
32998
+ isArray: !isSingle2,
32999
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
32861
33000
  });
32862
33001
  const jsonColumns = sql.join(
32863
33002
  innerQuery.selection.map((s) => {
@@ -32865,7 +33004,9 @@ var init_dialect2 = __esm({
32865
33004
  }),
32866
33005
  sql`, `
32867
33006
  );
32868
- 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)}`;
33007
+ const json4 = isNested ? sql`jsonb` : sql`json`;
33008
+ 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)}`;
33009
+ return joinQuery;
32869
33010
  }),
32870
33011
  sql`, `
32871
33012
  );
@@ -32877,7 +33018,7 @@ var init_dialect2 = __esm({
32877
33018
  });
32878
33019
  }
32879
33020
  const selectionSet = sql.join(selectionArr, sql`, `);
32880
- 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)}`;
33021
+ 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)}`;
32881
33022
  return {
32882
33023
  sql: query,
32883
33024
  selection
@@ -34252,9 +34393,10 @@ var init_query4 = __esm({
34252
34393
  init_entity();
34253
34394
  init_query_promise();
34254
34395
  init_relations();
34396
+ init_sql();
34255
34397
  _a224 = entityKind;
34256
34398
  RelationalQueryBuilder2 = class {
34257
- constructor(mode, tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session) {
34399
+ constructor(mode, tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session, rowMode) {
34258
34400
  this.mode = mode;
34259
34401
  this.tables = tables;
34260
34402
  this.schema = schema5;
@@ -34263,6 +34405,7 @@ var init_query4 = __esm({
34263
34405
  this.tableConfig = tableConfig;
34264
34406
  this.dialect = dialect6;
34265
34407
  this.session = session;
34408
+ this.rowMode = rowMode;
34266
34409
  }
34267
34410
  findMany(config) {
34268
34411
  return this.mode === "sync" ? new SQLiteSyncRelationalQuery2(
@@ -34274,7 +34417,8 @@ var init_query4 = __esm({
34274
34417
  this.dialect,
34275
34418
  this.session,
34276
34419
  config ?? true,
34277
- "many"
34420
+ "many",
34421
+ this.rowMode
34278
34422
  ) : new SQLiteRelationalQuery2(
34279
34423
  this.tables,
34280
34424
  this.schema,
@@ -34284,7 +34428,8 @@ var init_query4 = __esm({
34284
34428
  this.dialect,
34285
34429
  this.session,
34286
34430
  config ?? true,
34287
- "many"
34431
+ "many",
34432
+ this.rowMode
34288
34433
  );
34289
34434
  }
34290
34435
  findFirst(config) {
@@ -34297,7 +34442,8 @@ var init_query4 = __esm({
34297
34442
  this.dialect,
34298
34443
  this.session,
34299
34444
  config ?? true,
34300
- "first"
34445
+ "first",
34446
+ this.rowMode
34301
34447
  ) : new SQLiteRelationalQuery2(
34302
34448
  this.tables,
34303
34449
  this.schema,
@@ -34307,13 +34453,14 @@ var init_query4 = __esm({
34307
34453
  this.dialect,
34308
34454
  this.session,
34309
34455
  config ?? true,
34310
- "first"
34456
+ "first",
34457
+ this.rowMode
34311
34458
  );
34312
34459
  }
34313
34460
  };
34314
34461
  __publicField(RelationalQueryBuilder2, _a224, "SQLiteAsyncRelationalQueryBuilderV2");
34315
34462
  SQLiteRelationalQuery2 = class extends (_b156 = QueryPromise, _a225 = entityKind, _b156) {
34316
- constructor(tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session, config, mode) {
34463
+ constructor(tables, schema5, tableNamesMap, table5, tableConfig, dialect6, session, config, mode, rowMode) {
34317
34464
  super();
34318
34465
  /** @internal */
34319
34466
  __publicField(this, "mode");
@@ -34326,6 +34473,7 @@ var init_query4 = __esm({
34326
34473
  this.dialect = dialect6;
34327
34474
  this.session = session;
34328
34475
  this.config = config;
34476
+ this.rowMode = rowMode;
34329
34477
  this.mode = mode;
34330
34478
  this.table = table5;
34331
34479
  }
@@ -34350,7 +34498,7 @@ var init_query4 = __esm({
34350
34498
  void 0,
34351
34499
  this.mode === "first" ? "get" : "all",
34352
34500
  (rawRows, mapColumnValue) => {
34353
- const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue, true));
34501
+ const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue, !this.rowMode));
34354
34502
  if (this.mode === "first") {
34355
34503
  return rows[0];
34356
34504
  }
@@ -34362,15 +34510,26 @@ var init_query4 = __esm({
34362
34510
  return this._prepare(false);
34363
34511
  }
34364
34512
  _getQuery() {
34365
- return this.dialect.buildRelationalQuery({
34513
+ const query = this.dialect.buildRelationalQuery({
34366
34514
  schema: this.schema,
34367
34515
  tableNamesMap: this.tableNamesMap,
34368
34516
  table: this.table,
34369
34517
  tableConfig: this.tableConfig,
34370
34518
  queryConfig: this.config,
34371
34519
  tables: this.tables,
34372
- mode: this.mode
34520
+ mode: this.mode,
34521
+ isNested: this.rowMode
34373
34522
  });
34523
+ if (this.rowMode) {
34524
+ const jsonColumns = sql.join(
34525
+ query.selection.map((s) => {
34526
+ return sql`${sql.raw(this.dialect.escapeString(s.key))}, ${s.selection ? sql`jsonb(${sql.identifier(s.key)})` : sql.identifier(s.key)}`;
34527
+ }),
34528
+ sql`, `
34529
+ );
34530
+ query.sql = sql`select json_object(${jsonColumns}) as ${sql.identifier("r")} from (${query.sql}) as ${sql.identifier("t")}`;
34531
+ }
34532
+ return query;
34374
34533
  }
34375
34534
  _toSQL() {
34376
34535
  const query = this._getQuery();
@@ -34453,7 +34612,7 @@ var init_db2 = __esm({
34453
34612
  init_raw2();
34454
34613
  _a228 = entityKind;
34455
34614
  BaseSQLiteDatabase = class {
34456
- constructor(resultKind, dialect6, session, relations, _schema) {
34615
+ constructor(resultKind, dialect6, session, relations, _schema, rowModeRQB) {
34457
34616
  /** @deprecated */
34458
34617
  __publicField(this, "_query");
34459
34618
  // TO-DO: Figure out how to pass DrizzleTypeError without breaking withReplicas
@@ -34461,6 +34620,7 @@ var init_db2 = __esm({
34461
34620
  this.resultKind = resultKind;
34462
34621
  this.dialect = dialect6;
34463
34622
  this.session = session;
34623
+ this.rowModeRQB = rowModeRQB;
34464
34624
  const rel = relations ?? {};
34465
34625
  this._ = _schema ? {
34466
34626
  schema: _schema.schema,
@@ -34500,7 +34660,8 @@ var init_db2 = __esm({
34500
34660
  relation.table,
34501
34661
  relation,
34502
34662
  dialect6,
34503
- session
34663
+ session,
34664
+ rowModeRQB
34504
34665
  );
34505
34666
  }
34506
34667
  }
@@ -34966,8 +35127,8 @@ var init_session2 = __esm({
34966
35127
  };
34967
35128
  __publicField(SQLiteSession, _a236, "SQLiteSession");
34968
35129
  SQLiteTransaction = class extends (_b160 = BaseSQLiteDatabase, _a237 = entityKind, _b160) {
34969
- constructor(resultType, dialect6, session, relations, schema5, nestedIndex = 0) {
34970
- super(resultType, dialect6, session, relations, schema5);
35130
+ constructor(resultType, dialect6, session, relations, schema5, nestedIndex = 0, rowModeRQB) {
35131
+ super(resultType, dialect6, session, relations, schema5, rowModeRQB);
34971
35132
  this.relations = relations;
34972
35133
  this.schema = schema5;
34973
35134
  this.nestedIndex = nestedIndex;
@@ -37746,9 +37907,11 @@ var init_dialect3 = __esm({
37746
37907
  const columnIdentifiers = [];
37747
37908
  const selectedColumns = this.getSelectedTableColumns(table5, params?.columns);
37748
37909
  for (const column5 of selectedColumns) {
37749
- columnIdentifiers.push(
37750
- sql`${table5[column5.tsName]} as ${sql.identifier(column5.tsName)}`
37751
- );
37910
+ columnIdentifiers.push(sql`${column5.column} as ${sql.identifier(column5.tsName)}`);
37911
+ selection.push({
37912
+ key: column5.tsName,
37913
+ field: column5.column
37914
+ });
37752
37915
  }
37753
37916
  return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
37754
37917
  })() : this.unwrapAllColumns(table5, selection));
@@ -38570,7 +38733,9 @@ var init_dialect3 = __esm({
38570
38733
  relationWhere,
38571
38734
  mode,
38572
38735
  errorPath,
38573
- depth
38736
+ depth,
38737
+ isNested,
38738
+ throughJoin
38574
38739
  }) {
38575
38740
  const selection = [];
38576
38741
  const isSingle = mode === "first";
@@ -38604,12 +38769,19 @@ var init_dialect3 = __esm({
38604
38769
  key: k,
38605
38770
  field: relation2
38606
38771
  });
38607
- return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
38772
+ return sql` left join lateral (${query2}) as ${sql.identifier(k)} on true`;
38608
38773
  }
38609
38774
  const relation = tableConfig.relations[k];
38610
38775
  const isSingle2 = is(relation, One);
38611
38776
  const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
38612
- const relationFilter = relationToSQL(relation, table5, targetTable);
38777
+ const throughTable = relation.throughTable ? aliasedTable(relation.throughTable, `tr${currentDepth}`) : void 0;
38778
+ const { filter: filter2, joinCondition } = relationToSQL(
38779
+ relation,
38780
+ table5,
38781
+ targetTable,
38782
+ throughTable
38783
+ );
38784
+ const throughJoin2 = throughTable ? sql` inner join ${sql`${sql.identifier(throughTable[Schema] ?? "")}.`.if(throughTable[Schema])}${sql.identifier(throughTable[OriginalName])} as ${throughTable} on ${joinCondition}` : void 0;
38613
38785
  const innerQuery = this.buildRelationalQuery({
38614
38786
  table: targetTable,
38615
38787
  mode: isSingle2 ? "first" : "many",
@@ -38618,21 +38790,25 @@ var init_dialect3 = __esm({
38618
38790
  tableConfig: schema5[tableNamesMap[getTableUniqueName(relation.targetTable)]],
38619
38791
  tableNamesMap,
38620
38792
  tables,
38621
- relationWhere: relationFilter,
38793
+ relationWhere: filter2,
38622
38794
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
38623
- depth: currentDepth + 1
38795
+ depth: currentDepth + 1,
38796
+ isNested: true,
38797
+ throughJoin: throughJoin2
38624
38798
  });
38625
38799
  selection.push({
38626
38800
  field: targetTable,
38627
38801
  key: k,
38628
38802
  selection: innerQuery.selection,
38629
- isArray: !isSingle2
38803
+ isArray: !isSingle2,
38804
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
38630
38805
  });
38631
38806
  const jsonColumns = sql.join(
38632
38807
  innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
38633
38808
  sql`, `
38634
38809
  );
38635
- 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)}`;
38810
+ 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`;
38811
+ return joinQuery;
38636
38812
  })
38637
38813
  );
38638
38814
  })() : void 0;
@@ -38643,8 +38819,11 @@ var init_dialect3 = __esm({
38643
38819
  message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
38644
38820
  });
38645
38821
  }
38822
+ if (isNested && order) {
38823
+ selectionArr.push(sql`row_number() over (order by ${order})`);
38824
+ }
38646
38825
  const selectionSet = sql.join(selectionArr, sql`, `);
38647
- 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)}`;
38826
+ 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)}`;
38648
38827
  return {
38649
38828
  sql: query,
38650
38829
  selection