drizzle-kit 0.30.1-791f459 → 0.30.1-86fcd29

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 +85 -38
  2. package/api.mjs +85 -38
  3. package/bin.cjs +1 -1
  4. package/package.json +1 -1
package/api.js CHANGED
@@ -24837,24 +24837,40 @@ 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
- if (row[selectionItem.key] === null)
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
24852
  continue;
24853
+ }
24846
24854
  if (parseJson)
24847
24855
  row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
24848
24856
  if (selectionItem.isArray) {
24849
24857
  for (const item of row[selectionItem.key]) {
24850
- mapRelationalRow(item, selectionItem.selection, mapColumnValue);
24858
+ mapRelationalRow(
24859
+ item,
24860
+ selectionItem.selection,
24861
+ mapColumnValue,
24862
+ false,
24863
+ currentPath
24864
+ );
24851
24865
  }
24852
24866
  continue;
24853
24867
  }
24854
24868
  mapRelationalRow(
24855
24869
  row[selectionItem.key],
24856
24870
  selectionItem.selection,
24857
- mapColumnValue
24871
+ mapColumnValue,
24872
+ false,
24873
+ currentPath
24858
24874
  );
24859
24875
  continue;
24860
24876
  }
@@ -24905,7 +24921,7 @@ function defineRelations(schema5, relations) {
24905
24921
  );
24906
24922
  }
24907
24923
  function relationsFieldFilterToSQL(column5, filter2) {
24908
- if (typeof filter2 !== "object" || is(filter2, Placeholder))
24924
+ if (typeof filter2 !== "object")
24909
24925
  return eq(column5, filter2);
24910
24926
  const entries = Object.entries(filter2);
24911
24927
  if (!entries.length)
@@ -24932,6 +24948,21 @@ function relationsFieldFilterToSQL(column5, filter2) {
24932
24948
  );
24933
24949
  continue;
24934
24950
  }
24951
+ case "isNotNull":
24952
+ case "isNull": {
24953
+ if (!value)
24954
+ continue;
24955
+ parts.push(operators[target](column5));
24956
+ continue;
24957
+ }
24958
+ case "in": {
24959
+ parts.push(operators.inArray(column5, value));
24960
+ continue;
24961
+ }
24962
+ case "notIn": {
24963
+ parts.push(operators.notInArray(column5, value));
24964
+ continue;
24965
+ }
24935
24966
  default: {
24936
24967
  parts.push(
24937
24968
  operators[target](
@@ -24999,14 +25030,20 @@ function relationsFilterToSQL(table5, filter2) {
24999
25030
  return and(...parts);
25000
25031
  }
25001
25032
  function relationsOrderToSQL(table5, orders) {
25002
- const data = typeof orders === "function" ? orders(table5[Columns], orderByOperators) : orders;
25003
- return is(data, SQL) ? data : Array.isArray(data) ? data.length ? sql.join(data.map((o) => is(o, SQL) ? o : asc(o)), sql`, `) : void 0 : asc(data);
25033
+ if (typeof orders === "function") {
25034
+ const data = orders(table5, orderByOperators);
25035
+ return is(data, SQL) ? data : Array.isArray(data) ? data.length ? sql.join(data.map((o) => is(o, SQL) ? o : asc(o)), sql`, `) : void 0 : is(data, Column2) ? asc(data) : void 0;
25036
+ }
25037
+ const entries = Object.entries(orders).filter(([_2, value]) => value);
25038
+ if (!entries.length)
25039
+ return void 0;
25040
+ return sql.join(entries.map(([column5, value]) => (value === "asc" ? asc : desc)(table5[Columns][column5])), sql`, `);
25004
25041
  }
25005
25042
  function relationExtrasToSQL(table5, extras) {
25006
25043
  const subqueries = [];
25007
25044
  const selection = [];
25008
25045
  for (const [key, extra] of Object.entries(
25009
- typeof extras === "function" ? extras(table5[Columns], { sql: operators.sql }) : extras
25046
+ typeof extras === "function" ? extras(table5, { sql: operators.sql }) : extras
25010
25047
  )) {
25011
25048
  if (!extra)
25012
25049
  continue;
@@ -25042,6 +25079,7 @@ var init_relations = __esm({
25042
25079
  init_table();
25043
25080
  init_column();
25044
25081
  init_entity();
25082
+ init_errors();
25045
25083
  init_primary_keys();
25046
25084
  init_expressions();
25047
25085
  init_sql();
@@ -25189,7 +25227,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25189
25227
  this.targetTable = targetTable;
25190
25228
  }
25191
25229
  };
25192
- __publicField(Relation, _a119, "Relation");
25230
+ __publicField(Relation, _a119, "RelationV2");
25193
25231
  One = class extends (_b95 = Relation, _a120 = entityKind, _b95) {
25194
25232
  constructor(targetTable, config) {
25195
25233
  super(targetTable);
@@ -25205,7 +25243,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25205
25243
  this.optional = config?.optional ?? false;
25206
25244
  }
25207
25245
  };
25208
- __publicField(One, _a120, "One");
25246
+ __publicField(One, _a120, "OneV2");
25209
25247
  Many = class extends (_b96 = Relation, _a121 = entityKind, _b96) {
25210
25248
  constructor(targetTable, config) {
25211
25249
  super(targetTable);
@@ -25220,7 +25258,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25220
25258
  }
25221
25259
  }
25222
25260
  };
25223
- __publicField(Many, _a121, "Many");
25261
+ __publicField(Many, _a121, "ManyV2");
25224
25262
  _a122 = entityKind;
25225
25263
  AggregatedField = class {
25226
25264
  constructor() {
@@ -25241,7 +25279,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25241
25279
  if (!this.query) {
25242
25280
  if (!this.table)
25243
25281
  throw new Error("Table must be set before building aggregate field");
25244
- this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
25282
+ const table5 = this.table;
25283
+ 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);
25245
25284
  }
25246
25285
  return this.query;
25247
25286
  }
@@ -25302,7 +25341,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25302
25341
  }
25303
25342
  through(column5) {
25304
25343
  this._.through = column5;
25305
- return this;
25344
+ throw new Error("Not implemented");
25306
25345
  }
25307
25346
  getSQL() {
25308
25347
  return this._.column.getSQL();
@@ -26065,16 +26104,14 @@ var init_dialect = __esm({
26065
26104
  sql`, `
26066
26105
  );
26067
26106
  });
26068
- __publicField(this, "buildColumns", (table5, tableConfig, selection, config) => config?.columns ? (() => {
26107
+ __publicField(this, "buildColumns", (table5, selection, config) => config?.columns ? (() => {
26069
26108
  const entries = Object.entries(config.columns);
26070
26109
  const columnIdentifiers = [];
26071
26110
  let colSelectionMode;
26072
26111
  for (const [k, v] of entries) {
26073
- if (colSelectionMode === void 0)
26074
- colSelectionMode = v;
26075
- else if (v !== void 0 && colSelectionMode !== v) {
26076
- throw new Error("Columns cannot be both true and false at the same time");
26077
- }
26112
+ if (v === void 0)
26113
+ continue;
26114
+ colSelectionMode = colSelectionMode || v;
26078
26115
  if (v) {
26079
26116
  columnIdentifiers.push(
26080
26117
  sql`${table5[Columns][k]} as ${sql.identifier(k)}`
@@ -26725,7 +26762,7 @@ var init_dialect = __esm({
26725
26762
  const offset = params?.offset;
26726
26763
  const where = params?.where && relationWhere ? and(relationsFilterToSQL(table5, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table5, params.where) : relationWhere;
26727
26764
  const order = params?.orderBy ? relationsOrderToSQL(table5, params.orderBy) : void 0;
26728
- const columns = this.buildColumns(table5, tableConfig, selection, params);
26765
+ const columns = this.buildColumns(table5, selection, params);
26729
26766
  const extras = params?.extras ? relationExtrasToSQL(table5, params.extras) : void 0;
26730
26767
  if (extras)
26731
26768
  selection.push(...extras.selection);
@@ -26773,7 +26810,8 @@ var init_dialect = __esm({
26773
26810
  field: targetTable,
26774
26811
  key: k,
26775
26812
  selection: innerQuery.selection,
26776
- isArray: !isSingle2
26813
+ isArray: !isSingle2,
26814
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
26777
26815
  });
26778
26816
  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`;
26779
26817
  }),
@@ -32211,11 +32249,9 @@ var init_dialect2 = __esm({
32211
32249
  const entries = Object.entries(columns);
32212
32250
  let colSelectionMode;
32213
32251
  for (const [k, v] of entries) {
32214
- if (colSelectionMode === void 0)
32215
- colSelectionMode = v;
32216
- else if (v !== void 0 && colSelectionMode !== v) {
32217
- throw new Error("Columns cannot be both true and false at the same time");
32218
- }
32252
+ if (v === void 0)
32253
+ continue;
32254
+ colSelectionMode = colSelectionMode || v;
32219
32255
  if (v) {
32220
32256
  selectedColumns.push({
32221
32257
  column: table5[Columns][k],
@@ -32831,7 +32867,7 @@ var init_dialect2 = __esm({
32831
32867
  }
32832
32868
  const relation = tableConfig.relations[k];
32833
32869
  const isSingle2 = is(relation, One);
32834
- const targetTable = relation.targetTable;
32870
+ const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
32835
32871
  const relationFilter = relationToSQL(relation, table5, targetTable);
32836
32872
  const innerQuery = this.buildRelationalQuery({
32837
32873
  table: targetTable,
@@ -32850,7 +32886,8 @@ var init_dialect2 = __esm({
32850
32886
  field: targetTable,
32851
32887
  key: k,
32852
32888
  selection: innerQuery.selection,
32853
- isArray: !isSingle2
32889
+ isArray: !isSingle2,
32890
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
32854
32891
  });
32855
32892
  const jsonColumns = sql.join(
32856
32893
  innerQuery.selection.map((s) => {
@@ -37713,11 +37750,9 @@ var init_dialect3 = __esm({
37713
37750
  const entries = Object.entries(columns);
37714
37751
  let colSelectionMode;
37715
37752
  for (const [k, v] of entries) {
37716
- if (colSelectionMode === void 0)
37717
- colSelectionMode = v;
37718
- else if (v !== void 0 && colSelectionMode !== v) {
37719
- throw new Error("Columns cannot be both true and false at the same time");
37720
- }
37753
+ if (v === void 0)
37754
+ continue;
37755
+ colSelectionMode = colSelectionMode || v;
37721
37756
  if (v) {
37722
37757
  selectedColumns.push({
37723
37758
  column: table5[Columns][k],
@@ -37744,6 +37779,12 @@ var init_dialect3 = __esm({
37744
37779
  columnIdentifiers.push(
37745
37780
  sql`${table5[column5.tsName]} as ${sql.identifier(column5.tsName)}`
37746
37781
  );
37782
+ selection.push(
37783
+ {
37784
+ key: column5.tsName,
37785
+ field: column5.column
37786
+ }
37787
+ );
37747
37788
  }
37748
37789
  return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
37749
37790
  })() : this.unwrapAllColumns(table5, selection));
@@ -38565,7 +38606,8 @@ var init_dialect3 = __esm({
38565
38606
  relationWhere,
38566
38607
  mode,
38567
38608
  errorPath,
38568
- depth
38609
+ depth,
38610
+ isNested
38569
38611
  }) {
38570
38612
  const selection = [];
38571
38613
  const isSingle = mode === "first";
@@ -38599,7 +38641,7 @@ var init_dialect3 = __esm({
38599
38641
  key: k,
38600
38642
  field: relation2
38601
38643
  });
38602
- return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
38644
+ return sql` left join lateral (${query2}) as ${sql.identifier(k)} on true`;
38603
38645
  }
38604
38646
  const relation = tableConfig.relations[k];
38605
38647
  const isSingle2 = is(relation, One);
@@ -38615,19 +38657,21 @@ var init_dialect3 = __esm({
38615
38657
  tables,
38616
38658
  relationWhere: relationFilter,
38617
38659
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
38618
- depth: currentDepth + 1
38660
+ depth: currentDepth + 1,
38661
+ isNested: true
38619
38662
  });
38620
38663
  selection.push({
38621
38664
  field: targetTable,
38622
38665
  key: k,
38623
38666
  selection: innerQuery.selection,
38624
- isArray: !isSingle2
38667
+ isArray: !isSingle2,
38668
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
38625
38669
  });
38626
38670
  const jsonColumns = sql.join(
38627
38671
  innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
38628
38672
  sql`, `
38629
38673
  );
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)}`;
38674
+ return 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`;
38631
38675
  })
38632
38676
  );
38633
38677
  })() : void 0;
@@ -38638,6 +38682,9 @@ var init_dialect3 = __esm({
38638
38682
  message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
38639
38683
  });
38640
38684
  }
38685
+ if (isNested && order) {
38686
+ selectionArr.push(sql`row_number() over (order by ${order})`);
38687
+ }
38641
38688
  const selectionSet = sql.join(selectionArr, sql`, `);
38642
38689
  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)}`;
38643
38690
  return {
package/api.mjs CHANGED
@@ -24842,24 +24842,40 @@ 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
- if (row[selectionItem.key] === null)
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
24857
  continue;
24858
+ }
24851
24859
  if (parseJson)
24852
24860
  row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
24853
24861
  if (selectionItem.isArray) {
24854
24862
  for (const item of row[selectionItem.key]) {
24855
- mapRelationalRow(item, selectionItem.selection, mapColumnValue);
24863
+ mapRelationalRow(
24864
+ item,
24865
+ selectionItem.selection,
24866
+ mapColumnValue,
24867
+ false,
24868
+ currentPath
24869
+ );
24856
24870
  }
24857
24871
  continue;
24858
24872
  }
24859
24873
  mapRelationalRow(
24860
24874
  row[selectionItem.key],
24861
24875
  selectionItem.selection,
24862
- mapColumnValue
24876
+ mapColumnValue,
24877
+ false,
24878
+ currentPath
24863
24879
  );
24864
24880
  continue;
24865
24881
  }
@@ -24910,7 +24926,7 @@ function defineRelations(schema5, relations) {
24910
24926
  );
24911
24927
  }
24912
24928
  function relationsFieldFilterToSQL(column5, filter2) {
24913
- if (typeof filter2 !== "object" || is(filter2, Placeholder))
24929
+ if (typeof filter2 !== "object")
24914
24930
  return eq(column5, filter2);
24915
24931
  const entries = Object.entries(filter2);
24916
24932
  if (!entries.length)
@@ -24937,6 +24953,21 @@ function relationsFieldFilterToSQL(column5, filter2) {
24937
24953
  );
24938
24954
  continue;
24939
24955
  }
24956
+ case "isNotNull":
24957
+ case "isNull": {
24958
+ if (!value)
24959
+ continue;
24960
+ parts.push(operators[target](column5));
24961
+ continue;
24962
+ }
24963
+ case "in": {
24964
+ parts.push(operators.inArray(column5, value));
24965
+ continue;
24966
+ }
24967
+ case "notIn": {
24968
+ parts.push(operators.notInArray(column5, value));
24969
+ continue;
24970
+ }
24940
24971
  default: {
24941
24972
  parts.push(
24942
24973
  operators[target](
@@ -25004,14 +25035,20 @@ function relationsFilterToSQL(table5, filter2) {
25004
25035
  return and(...parts);
25005
25036
  }
25006
25037
  function relationsOrderToSQL(table5, orders) {
25007
- const data = typeof orders === "function" ? orders(table5[Columns], orderByOperators) : orders;
25008
- return is(data, SQL) ? data : Array.isArray(data) ? data.length ? sql.join(data.map((o) => is(o, SQL) ? o : asc(o)), sql`, `) : void 0 : asc(data);
25038
+ if (typeof orders === "function") {
25039
+ const data = orders(table5, orderByOperators);
25040
+ return is(data, SQL) ? data : Array.isArray(data) ? data.length ? sql.join(data.map((o) => is(o, SQL) ? o : asc(o)), sql`, `) : void 0 : is(data, Column2) ? asc(data) : void 0;
25041
+ }
25042
+ const entries = Object.entries(orders).filter(([_2, value]) => value);
25043
+ if (!entries.length)
25044
+ return void 0;
25045
+ return sql.join(entries.map(([column5, value]) => (value === "asc" ? asc : desc)(table5[Columns][column5])), sql`, `);
25009
25046
  }
25010
25047
  function relationExtrasToSQL(table5, extras) {
25011
25048
  const subqueries = [];
25012
25049
  const selection = [];
25013
25050
  for (const [key, extra] of Object.entries(
25014
- typeof extras === "function" ? extras(table5[Columns], { sql: operators.sql }) : extras
25051
+ typeof extras === "function" ? extras(table5, { sql: operators.sql }) : extras
25015
25052
  )) {
25016
25053
  if (!extra)
25017
25054
  continue;
@@ -25047,6 +25084,7 @@ var init_relations = __esm({
25047
25084
  init_table();
25048
25085
  init_column();
25049
25086
  init_entity();
25087
+ init_errors();
25050
25088
  init_primary_keys();
25051
25089
  init_expressions();
25052
25090
  init_sql();
@@ -25194,7 +25232,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25194
25232
  this.targetTable = targetTable;
25195
25233
  }
25196
25234
  };
25197
- __publicField(Relation, _a119, "Relation");
25235
+ __publicField(Relation, _a119, "RelationV2");
25198
25236
  One = class extends (_b95 = Relation, _a120 = entityKind, _b95) {
25199
25237
  constructor(targetTable, config) {
25200
25238
  super(targetTable);
@@ -25210,7 +25248,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25210
25248
  this.optional = config?.optional ?? false;
25211
25249
  }
25212
25250
  };
25213
- __publicField(One, _a120, "One");
25251
+ __publicField(One, _a120, "OneV2");
25214
25252
  Many = class extends (_b96 = Relation, _a121 = entityKind, _b96) {
25215
25253
  constructor(targetTable, config) {
25216
25254
  super(targetTable);
@@ -25225,7 +25263,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25225
25263
  }
25226
25264
  }
25227
25265
  };
25228
- __publicField(Many, _a121, "Many");
25266
+ __publicField(Many, _a121, "ManyV2");
25229
25267
  _a122 = entityKind;
25230
25268
  AggregatedField = class {
25231
25269
  constructor() {
@@ -25246,7 +25284,8 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25246
25284
  if (!this.query) {
25247
25285
  if (!this.table)
25248
25286
  throw new Error("Table must be set before building aggregate field");
25249
- this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
25287
+ const table5 = this.table;
25288
+ 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);
25250
25289
  }
25251
25290
  return this.query;
25252
25291
  }
@@ -25307,7 +25346,7 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
25307
25346
  }
25308
25347
  through(column5) {
25309
25348
  this._.through = column5;
25310
- return this;
25349
+ throw new Error("Not implemented");
25311
25350
  }
25312
25351
  getSQL() {
25313
25352
  return this._.column.getSQL();
@@ -26070,16 +26109,14 @@ var init_dialect = __esm({
26070
26109
  sql`, `
26071
26110
  );
26072
26111
  });
26073
- __publicField(this, "buildColumns", (table5, tableConfig, selection, config) => config?.columns ? (() => {
26112
+ __publicField(this, "buildColumns", (table5, selection, config) => config?.columns ? (() => {
26074
26113
  const entries = Object.entries(config.columns);
26075
26114
  const columnIdentifiers = [];
26076
26115
  let colSelectionMode;
26077
26116
  for (const [k, v] of entries) {
26078
- if (colSelectionMode === void 0)
26079
- colSelectionMode = v;
26080
- else if (v !== void 0 && colSelectionMode !== v) {
26081
- throw new Error("Columns cannot be both true and false at the same time");
26082
- }
26117
+ if (v === void 0)
26118
+ continue;
26119
+ colSelectionMode = colSelectionMode || v;
26083
26120
  if (v) {
26084
26121
  columnIdentifiers.push(
26085
26122
  sql`${table5[Columns][k]} as ${sql.identifier(k)}`
@@ -26730,7 +26767,7 @@ var init_dialect = __esm({
26730
26767
  const offset = params?.offset;
26731
26768
  const where = params?.where && relationWhere ? and(relationsFilterToSQL(table5, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table5, params.where) : relationWhere;
26732
26769
  const order = params?.orderBy ? relationsOrderToSQL(table5, params.orderBy) : void 0;
26733
- const columns = this.buildColumns(table5, tableConfig, selection, params);
26770
+ const columns = this.buildColumns(table5, selection, params);
26734
26771
  const extras = params?.extras ? relationExtrasToSQL(table5, params.extras) : void 0;
26735
26772
  if (extras)
26736
26773
  selection.push(...extras.selection);
@@ -26778,7 +26815,8 @@ var init_dialect = __esm({
26778
26815
  field: targetTable,
26779
26816
  key: k,
26780
26817
  selection: innerQuery.selection,
26781
- isArray: !isSingle2
26818
+ isArray: !isSingle2,
26819
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
26782
26820
  });
26783
26821
  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`;
26784
26822
  }),
@@ -32216,11 +32254,9 @@ var init_dialect2 = __esm({
32216
32254
  const entries = Object.entries(columns);
32217
32255
  let colSelectionMode;
32218
32256
  for (const [k, v] of entries) {
32219
- if (colSelectionMode === void 0)
32220
- colSelectionMode = v;
32221
- else if (v !== void 0 && colSelectionMode !== v) {
32222
- throw new Error("Columns cannot be both true and false at the same time");
32223
- }
32257
+ if (v === void 0)
32258
+ continue;
32259
+ colSelectionMode = colSelectionMode || v;
32224
32260
  if (v) {
32225
32261
  selectedColumns.push({
32226
32262
  column: table5[Columns][k],
@@ -32836,7 +32872,7 @@ var init_dialect2 = __esm({
32836
32872
  }
32837
32873
  const relation = tableConfig.relations[k];
32838
32874
  const isSingle2 = is(relation, One);
32839
- const targetTable = relation.targetTable;
32875
+ const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
32840
32876
  const relationFilter = relationToSQL(relation, table5, targetTable);
32841
32877
  const innerQuery = this.buildRelationalQuery({
32842
32878
  table: targetTable,
@@ -32855,7 +32891,8 @@ var init_dialect2 = __esm({
32855
32891
  field: targetTable,
32856
32892
  key: k,
32857
32893
  selection: innerQuery.selection,
32858
- isArray: !isSingle2
32894
+ isArray: !isSingle2,
32895
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
32859
32896
  });
32860
32897
  const jsonColumns = sql.join(
32861
32898
  innerQuery.selection.map((s) => {
@@ -37718,11 +37755,9 @@ var init_dialect3 = __esm({
37718
37755
  const entries = Object.entries(columns);
37719
37756
  let colSelectionMode;
37720
37757
  for (const [k, v] of entries) {
37721
- if (colSelectionMode === void 0)
37722
- colSelectionMode = v;
37723
- else if (v !== void 0 && colSelectionMode !== v) {
37724
- throw new Error("Columns cannot be both true and false at the same time");
37725
- }
37758
+ if (v === void 0)
37759
+ continue;
37760
+ colSelectionMode = colSelectionMode || v;
37726
37761
  if (v) {
37727
37762
  selectedColumns.push({
37728
37763
  column: table5[Columns][k],
@@ -37749,6 +37784,12 @@ var init_dialect3 = __esm({
37749
37784
  columnIdentifiers.push(
37750
37785
  sql`${table5[column5.tsName]} as ${sql.identifier(column5.tsName)}`
37751
37786
  );
37787
+ selection.push(
37788
+ {
37789
+ key: column5.tsName,
37790
+ field: column5.column
37791
+ }
37792
+ );
37752
37793
  }
37753
37794
  return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
37754
37795
  })() : this.unwrapAllColumns(table5, selection));
@@ -38570,7 +38611,8 @@ var init_dialect3 = __esm({
38570
38611
  relationWhere,
38571
38612
  mode,
38572
38613
  errorPath,
38573
- depth
38614
+ depth,
38615
+ isNested
38574
38616
  }) {
38575
38617
  const selection = [];
38576
38618
  const isSingle = mode === "first";
@@ -38604,7 +38646,7 @@ var init_dialect3 = __esm({
38604
38646
  key: k,
38605
38647
  field: relation2
38606
38648
  });
38607
- return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
38649
+ return sql` left join lateral (${query2}) as ${sql.identifier(k)} on true`;
38608
38650
  }
38609
38651
  const relation = tableConfig.relations[k];
38610
38652
  const isSingle2 = is(relation, One);
@@ -38620,19 +38662,21 @@ var init_dialect3 = __esm({
38620
38662
  tables,
38621
38663
  relationWhere: relationFilter,
38622
38664
  errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
38623
- depth: currentDepth + 1
38665
+ depth: currentDepth + 1,
38666
+ isNested: true
38624
38667
  });
38625
38668
  selection.push({
38626
38669
  field: targetTable,
38627
38670
  key: k,
38628
38671
  selection: innerQuery.selection,
38629
- isArray: !isSingle2
38672
+ isArray: !isSingle2,
38673
+ isOptional: (relation.optional ?? false) || join !== true && !!join.where
38630
38674
  });
38631
38675
  const jsonColumns = sql.join(
38632
38676
  innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
38633
38677
  sql`, `
38634
38678
  );
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)}`;
38679
+ return 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`;
38636
38680
  })
38637
38681
  );
38638
38682
  })() : void 0;
@@ -38643,6 +38687,9 @@ var init_dialect3 = __esm({
38643
38687
  message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
38644
38688
  });
38645
38689
  }
38690
+ if (isNested && order) {
38691
+ selectionArr.push(sql`row_number() over (order by ${order})`);
38692
+ }
38646
38693
  const selectionSet = sql.join(selectionArr, sql`, `);
38647
38694
  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)}`;
38648
38695
  return {
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-791f459";
92614
+ const envVersion = "0.30.1-86fcd29";
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-791f459",
3
+ "version": "0.30.1-86fcd29",
4
4
  "homepage": "https://orm.drizzle.team",
5
5
  "keywords": [
6
6
  "drizzle",