drizzle-kit 0.28.1-5514d51 → 0.28.1-66f461a

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 +162 -192
  2. package/api.mjs +162 -192
  3. package/bin.cjs +37 -37
  4. package/package.json +1 -1
package/api.js CHANGED
@@ -23965,12 +23965,14 @@ var init_primary_keys = __esm({
23965
23965
  function getOrderByOperators() {
23966
23966
  return orderByOperators;
23967
23967
  }
23968
- function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value) {
23968
+ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false) {
23969
23969
  for (const selectionItem of buildQueryResultSelection) {
23970
23970
  const field = selectionItem.field;
23971
23971
  if (is(field, Table2)) {
23972
23972
  if (row[selectionItem.key] === null)
23973
23973
  continue;
23974
+ if (parseJson)
23975
+ row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
23974
23976
  if (selectionItem.isArray) {
23975
23977
  for (const item of row[selectionItem.key]) {
23976
23978
  mapRelationalRow(item, selectionItem.selection, mapColumnValue);
@@ -24031,7 +24033,7 @@ function defineRelations(schema4, relations) {
24031
24033
  );
24032
24034
  }
24033
24035
  function relationsFieldFilterToSQL(column4, filter2) {
24034
- if (typeof filter2 !== "object")
24036
+ if (typeof filter2 !== "object" || is(filter2, Placeholder))
24035
24037
  return eq(column4, filter2);
24036
24038
  const entries = Object.entries(filter2);
24037
24039
  if (!entries.length)
@@ -24041,14 +24043,14 @@ function relationsFieldFilterToSQL(column4, filter2) {
24041
24043
  if (value === void 0)
24042
24044
  continue;
24043
24045
  switch (target) {
24044
- case "$not": {
24046
+ case "NOT": {
24045
24047
  const res = relationsFieldFilterToSQL(column4, value);
24046
24048
  if (!res)
24047
24049
  continue;
24048
24050
  parts.push(not(res));
24049
24051
  continue;
24050
24052
  }
24051
- case "$or": {
24053
+ case "OR": {
24052
24054
  if (!value.length)
24053
24055
  continue;
24054
24056
  parts.push(
@@ -24073,7 +24075,7 @@ function relationsFieldFilterToSQL(column4, filter2) {
24073
24075
  return void 0;
24074
24076
  return and(...parts);
24075
24077
  }
24076
- function relationFilterToSQL(table4, filter2) {
24078
+ function relationsFilterToSQL(table4, filter2) {
24077
24079
  const entries = Object.entries(filter2);
24078
24080
  if (!entries.length)
24079
24081
  return void 0;
@@ -24082,35 +24084,33 @@ function relationFilterToSQL(table4, filter2) {
24082
24084
  if (value === void 0)
24083
24085
  continue;
24084
24086
  switch (target) {
24085
- case "$raw": {
24086
- if (value)
24087
- parts.push(value(operators));
24087
+ case "RAW": {
24088
+ if (value) {
24089
+ parts.push(
24090
+ value(table4[Columns], operators)
24091
+ );
24092
+ }
24088
24093
  continue;
24089
24094
  }
24090
- case "$or": {
24095
+ case "OR": {
24091
24096
  if (!value?.length)
24092
24097
  continue;
24093
24098
  parts.push(
24094
24099
  or(
24095
24100
  ...value.map(
24096
- (subFilter) => relationFilterToSQL(table4, subFilter)
24101
+ (subFilter) => relationsFilterToSQL(table4, subFilter)
24097
24102
  )
24098
24103
  )
24099
24104
  );
24100
24105
  continue;
24101
24106
  }
24102
- case "$not": {
24103
- if (!value?.length)
24107
+ case "NOT": {
24108
+ if (value === void 0)
24104
24109
  continue;
24105
- parts.push(
24106
- not(
24107
- and(
24108
- ...value.map(
24109
- (subFilter) => relationFilterToSQL(table4, subFilter)
24110
- )
24111
- )
24112
- )
24113
- );
24110
+ const built = relationsFilterToSQL(table4, value);
24111
+ if (!built)
24112
+ continue;
24113
+ parts.push(not(built));
24114
24114
  continue;
24115
24115
  }
24116
24116
  default: {
@@ -24151,13 +24151,15 @@ function relationExtrasToSQL(table4, extras) {
24151
24151
  selection
24152
24152
  };
24153
24153
  }
24154
- function relationToSQL(relation) {
24155
- const table4 = relation.sourceTable;
24154
+ function relationToSQL(relation, sourceTable, targetTable) {
24156
24155
  const columnWhere = relation.sourceColumns.map((s, i) => {
24157
24156
  const t = relation.targetColumns[i];
24158
- return eq(s, t);
24157
+ return eq(
24158
+ sql`${sql`${sql`${sql.identifier(sourceTable[Schema] ?? "")}.`.if(sourceTable[Schema] && !sourceTable[IsAlias])}`}${sourceTable}.${sql.identifier(s.name)}`,
24159
+ sql`${sql`${sql`${sql.identifier(targetTable[Schema] ?? "")}.`.if(targetTable[Schema] && !targetTable[IsAlias])}`}${targetTable}.${sql.identifier(t.name)}`
24160
+ );
24159
24161
  });
24160
- const targetWhere = relation.where ? and(...columnWhere, relationFilterToSQL(table4, relation.where)) : and(...columnWhere);
24162
+ const targetWhere = relation.where ? and(...columnWhere, relationsFilterToSQL(sourceTable, relation.where)) : and(...columnWhere);
24161
24163
  return targetWhere;
24162
24164
  }
24163
24165
  var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, RelationsBuilderColumn, _a126, RelationsHelperStatic;
@@ -24182,6 +24184,8 @@ var init_relations = __esm({
24182
24184
  this.tables = tables;
24183
24185
  this.config = config;
24184
24186
  for (const [tsName, table4] of Object.entries(tables)) {
24187
+ if (!is(table4, Table2))
24188
+ continue;
24185
24189
  this.tableNamesMap[getTableUniqueName(table4)] = tsName;
24186
24190
  const tableConfig = this.tablesConfig[tsName] = {
24187
24191
  table: table4,
@@ -24365,12 +24369,12 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
24365
24369
  if (!this.query) {
24366
24370
  if (!this.table)
24367
24371
  throw new Error("Table must be set before building aggregate field");
24368
- this.query = sql`select count(*) as ${sql.identifier("count")} from ${this.table}`.mapWith(Number);
24372
+ this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
24369
24373
  }
24370
24374
  return this.query;
24371
24375
  }
24372
24376
  };
24373
- __publicField(Count, _a123, "Count");
24377
+ __publicField(Count, _a123, "AggregatedFieldCount");
24374
24378
  operators = {
24375
24379
  and,
24376
24380
  between,
@@ -24681,8 +24685,8 @@ __export(dist_exports, {
24681
24685
  param: () => param,
24682
24686
  placeholder: () => placeholder,
24683
24687
  relationExtrasToSQL: () => relationExtrasToSQL,
24684
- relationFilterToSQL: () => relationFilterToSQL,
24685
24688
  relationToSQL: () => relationToSQL,
24689
+ relationsFilterToSQL: () => relationsFilterToSQL,
24686
24690
  relationsOrderToSQL: () => relationsOrderToSQL,
24687
24691
  sql: () => sql,
24688
24692
  sum: () => sum,
@@ -25218,11 +25222,8 @@ var init_dialect = __esm({
25218
25222
  });
25219
25223
  }
25220
25224
  }
25221
- return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : this.unwrapAllColumns(table4, selection);
25222
- })() : (() => {
25223
- const columnIdentifiers = [this.unwrapAllColumns(table4, selection)];
25224
- return sql.join(columnIdentifiers, sql`, `);
25225
- })());
25225
+ return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
25226
+ })() : this.unwrapAllColumns(table4, selection));
25226
25227
  this.casing = new CasingCache(config?.casing);
25227
25228
  }
25228
25229
  async migrate(migrations, session, config) {
@@ -25837,20 +25838,24 @@ var init_dialect = __esm({
25837
25838
  tableConfig,
25838
25839
  queryConfig: config,
25839
25840
  relationWhere,
25840
- mode
25841
+ mode,
25842
+ errorPath,
25843
+ depth
25841
25844
  }) {
25842
25845
  const selection = [];
25843
25846
  const isSingle = mode === "first";
25844
25847
  const params = config === true ? void 0 : config;
25848
+ const currentPath = errorPath ?? "";
25849
+ const currentDepth = depth ?? 0;
25845
25850
  const limit = isSingle ? 1 : params?.limit;
25846
25851
  const offset = params?.offset;
25847
- const where = params?.where && relationWhere ? and(relationFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationFilterToSQL(table4, params.where) : relationWhere;
25852
+ const where = params?.where && relationWhere ? and(relationsFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table4, params.where) : relationWhere;
25848
25853
  const order = params?.orderBy ? relationsOrderToSQL(table4, params.orderBy) : void 0;
25849
25854
  const columns = this.buildColumns(table4, tableConfig, selection, params);
25850
25855
  const extras = params?.extras ? relationExtrasToSQL(table4, params.extras) : void 0;
25851
25856
  if (extras)
25852
25857
  selection.push(...extras.selection);
25853
- const selectionArr = [columns, extras?.sql];
25858
+ const selectionArr = columns ? [columns] : [];
25854
25859
  const joins = params ? (() => {
25855
25860
  const { with: joins2 } = params;
25856
25861
  if (!joins2)
@@ -25860,20 +25865,21 @@ var init_dialect = __esm({
25860
25865
  return;
25861
25866
  return sql.join(
25862
25867
  withEntries.map(([k, join]) => {
25863
- const relation = tableConfig.relations[k];
25864
- if (relation.$brand === "AggregatedField") {
25865
- relation.onTable(table4);
25866
- const query2 = relation.getSQL();
25868
+ if (is(tableConfig.relations[k], AggregatedField)) {
25869
+ const relation2 = tableConfig.relations[k];
25870
+ relation2.onTable(table4);
25871
+ const query2 = relation2.getSQL();
25867
25872
  selection.push({
25868
25873
  key: k,
25869
- field: relation
25874
+ field: relation2
25870
25875
  });
25871
25876
  selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
25872
25877
  return sql`left join lateral(${query2}) as ${sql.identifier(k)} on true`;
25873
25878
  }
25879
+ const relation = tableConfig.relations[k];
25874
25880
  const isSingle2 = is(relation, One);
25875
- const targetTable = relation.targetTable;
25876
- const relationFilter = relationToSQL(relation);
25881
+ const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
25882
+ const relationFilter = relationToSQL(relation, table4, targetTable);
25877
25883
  selectionArr.push(
25878
25884
  isSingle2 ? sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}` : sql`coalesce(${sql.identifier(k)}.${sql.identifier("r")}, '[]') as ${sql.identifier(k)}`
25879
25885
  );
@@ -25882,10 +25888,12 @@ var init_dialect = __esm({
25882
25888
  mode: isSingle2 ? "first" : "many",
25883
25889
  schema: schema4,
25884
25890
  queryConfig: join,
25885
- tableConfig: schema4[tableNamesMap[getTableUniqueName(targetTable)]],
25891
+ tableConfig: schema4[tableNamesMap[getTableUniqueName(relation.targetTable)]],
25886
25892
  tableNamesMap,
25887
25893
  tables,
25888
- relationWhere: relationFilter
25894
+ relationWhere: relationFilter,
25895
+ errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
25896
+ depth: currentDepth + 1
25889
25897
  });
25890
25898
  selection.push({
25891
25899
  field: targetTable,
@@ -25898,8 +25906,15 @@ var init_dialect = __esm({
25898
25906
  sql` `
25899
25907
  );
25900
25908
  })() : void 0;
25909
+ if (extras?.sql)
25910
+ selectionArr.push(extras.sql);
25911
+ if (!selectionArr.length) {
25912
+ throw new DrizzleError({
25913
+ message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
25914
+ });
25915
+ }
25901
25916
  const selectionSet = sql.join(selectionArr.filter((e) => e !== void 0), sql`, `);
25902
- const query = sql`select ${selectionSet} from ${table4}${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)}`;
25917
+ const query = sql`select ${selectionSet} from ${table4[IsAlias] ? sql`${sql`${sql.identifier(table4[Schema] ?? "")}.`.if(table4[Schema])}${sql.identifier(table4[OriginalName])} as ${table4}` : table4}${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)}`;
25903
25918
  return {
25904
25919
  sql: query,
25905
25920
  selection
@@ -31350,73 +31365,18 @@ var init_dialect2 = __esm({
31350
31365
  }
31351
31366
  return selectedColumns;
31352
31367
  });
31353
- __publicField(this, "buildWithColumns", (tableConfig, withContainer, jsonColumns) => {
31354
- for (const [k, v] of Object.entries(
31355
- withContainer
31356
- )) {
31357
- if (!v)
31358
- continue;
31359
- const relation = tableConfig.relations[k];
31360
- if (relation.$brand === "AggregatedField")
31361
- continue;
31362
- const table4 = relation.targetTable;
31363
- const columns = this.getSelectedTableColumns(table4, typeof v === "boolean" ? void 0 : v.columns).map(
31364
- (c) => sql`${sql.raw(this.escapeString(c.tsName))}, ${sql.identifier(c.tsName)}`
31365
- );
31366
- if (typeof v !== "boolean") {
31367
- if (v.extras) {
31368
- if (typeof v.extras === "function") {
31369
- v.extras = v.extras(table4[Columns], { sql });
31370
- }
31371
- for (const [ek, ev] of Object.entries(v.extras)) {
31372
- if (!ev)
31373
- continue;
31374
- columns.push(
31375
- sql`${sql.raw(this.escapeString(ek))}, ${sql.identifier(ek)}`
31376
- );
31377
- }
31378
- }
31379
- if (v.with) {
31380
- const withEntries = Object.entries(v.with);
31381
- for (const [k2, w] of withEntries) {
31382
- if (!w)
31383
- continue;
31384
- columns.push(sql`${sql.raw(this.escapeString(k2))}, jsonb(${sql.identifier(k2)})`);
31385
- }
31386
- }
31387
- }
31388
- jsonColumns[k] = jsonColumns[k] ? sql`${jsonColumns[k]}, ${sql.join(columns, sql`, `)}` : sql.join(columns, sql`, `);
31389
- }
31390
- });
31391
- __publicField(this, "buildColumns", (table4, tableConfig, selection, params) => params?.columns ? (() => {
31392
- const jsonColumns = {};
31368
+ __publicField(this, "buildColumns", (table4, selection, params) => params?.columns ? (() => {
31393
31369
  const columnIdentifiers = [];
31394
31370
  const selectedColumns = this.getSelectedTableColumns(table4, params?.columns);
31395
31371
  for (const column4 of selectedColumns) {
31396
- columnIdentifiers.push(sql`${column4} as ${sql.identifier(column4.tsName)}`);
31372
+ columnIdentifiers.push(sql`${column4.column} as ${sql.identifier(column4.tsName)}`);
31397
31373
  selection.push({
31398
31374
  key: column4.tsName,
31399
31375
  field: column4.column
31400
31376
  });
31401
31377
  }
31402
- if (params?.with) {
31403
- this.buildWithColumns(tableConfig, params.with, jsonColumns);
31404
- }
31405
- return {
31406
- columns: columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : this.unwrapAllColumns(table4, selection),
31407
- jsonColumns
31408
- };
31409
- })() : (() => {
31410
- const columnIdentifiers = [this.unwrapAllColumns(table4, selection)];
31411
- const jsonColumns = {};
31412
- if (params?.with) {
31413
- this.buildWithColumns(tableConfig, params.with, jsonColumns);
31414
- }
31415
- return {
31416
- columns: sql.join(columnIdentifiers, sql`, `),
31417
- jsonColumns
31418
- };
31419
- })());
31378
+ return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
31379
+ })() : this.unwrapAllColumns(table4, selection));
31420
31380
  this.casing = new CasingCache(config?.casing);
31421
31381
  }
31422
31382
  escapeName(name2) {
@@ -31963,15 +31923,19 @@ var init_dialect2 = __esm({
31963
31923
  queryConfig: config,
31964
31924
  relationWhere,
31965
31925
  mode,
31966
- isNested
31926
+ isNested,
31927
+ errorPath,
31928
+ depth
31967
31929
  }) {
31968
31930
  const selection = [];
31969
31931
  const isSingle = mode === "first";
31970
31932
  const params = config === true ? void 0 : config;
31933
+ const currentPath = errorPath ?? "";
31934
+ const currentDepth = depth ?? 0;
31971
31935
  const limit = isSingle ? 1 : params?.limit;
31972
31936
  const offset = params?.offset;
31973
- const { columns, jsonColumns } = this.buildColumns(table4, tableConfig, selection, params);
31974
- const where = params?.where && relationWhere ? and(relationFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationFilterToSQL(table4, params.where) : relationWhere;
31937
+ const columns = this.buildColumns(table4, selection, params);
31938
+ const where = params?.where && relationWhere ? and(relationsFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table4, params.where) : relationWhere;
31975
31939
  const order = params?.orderBy ? relationsOrderToSQL(table4, params.orderBy) : void 0;
31976
31940
  const extras = params?.extras ? relationExtrasToSQL(table4, params.extras) : void 0;
31977
31941
  if (extras)
@@ -31985,29 +31949,32 @@ var init_dialect2 = __esm({
31985
31949
  return;
31986
31950
  return sql.join(
31987
31951
  withEntries.map(([k, join]) => {
31988
- const relation = tableConfig.relations[k];
31989
- if (relation.$brand === "AggregatedField") {
31990
- relation.onTable(table4);
31991
- const query2 = relation.getSQL();
31952
+ if (is(tableConfig.relations[k], AggregatedField)) {
31953
+ const relation2 = tableConfig.relations[k];
31954
+ relation2.onTable(table4);
31955
+ const query2 = relation2.getSQL();
31992
31956
  selection.push({
31993
31957
  key: k,
31994
- field: relation
31958
+ field: relation2
31995
31959
  });
31996
31960
  return sql`(${query2}) as ${sql.identifier(k)}`;
31997
31961
  }
31962
+ const relation = tableConfig.relations[k];
31998
31963
  const isSingle2 = is(relation, One);
31999
31964
  const targetTable = relation.targetTable;
32000
- const relationFilter = relationToSQL(relation);
31965
+ const relationFilter = relationToSQL(relation, table4, targetTable);
32001
31966
  const innerQuery = this.buildRelationalQuery({
32002
31967
  table: targetTable,
32003
31968
  mode: isSingle2 ? "first" : "many",
32004
31969
  schema: schema4,
32005
31970
  queryConfig: join,
32006
- tableConfig: schema4[tableNamesMap[getTableUniqueName(targetTable)]],
31971
+ tableConfig: schema4[tableNamesMap[getTableUniqueName(relation.targetTable)]],
32007
31972
  tableNamesMap,
32008
31973
  tables,
32009
31974
  relationWhere: relationFilter,
32010
- isNested: true
31975
+ isNested: true,
31976
+ errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
31977
+ depth: currentDepth + 1
32011
31978
  });
32012
31979
  selection.push({
32013
31980
  field: targetTable,
@@ -32015,13 +31982,25 @@ var init_dialect2 = __esm({
32015
31982
  selection: innerQuery.selection,
32016
31983
  isArray: !isSingle2
32017
31984
  });
32018
- return isNested ? isSingle2 ? sql`(select ${sql`jsonb_object(${jsonColumns[k]}) as ${sql.identifier("r")}`} from (${innerQuery}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select ${sql`jsonb_group_array(json_object(${jsonColumns[k]})) as ${sql.identifier("r")}`} from (${innerQuery}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}` : isSingle2 ? sql`(select ${sql`json_object(${jsonColumns[k]}) as ${sql.identifier("r")}`} from (${innerQuery}) as ${sql.identifier("t")}) as ${sql.identifier(k)}` : sql`coalesce((select ${sql`json_group_array(json_object(${jsonColumns[k]})) as ${sql.identifier("r")}`} from (${innerQuery}) as ${sql.identifier("t")}), jsonb_array()) as ${sql.identifier(k)}`;
31985
+ const jsonColumns = sql.join(
31986
+ innerQuery.selection.map((s) => {
31987
+ return sql`${sql.raw(this.escapeString(s.key))}, ${s.selection ? sql`jsonb(${sql.identifier(s.key)})` : sql.identifier(s.key)}`;
31988
+ }),
31989
+ sql`, `
31990
+ );
31991
+ 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)}`;
32019
31992
  }),
32020
31993
  sql`, `
32021
31994
  );
32022
31995
  })() : void 0;
32023
- const selectionSet = sql.join([columns, extras?.sql, joins].filter((e) => e !== void 0), sql`, `);
32024
- const query = sql`select ${selectionSet} from ${table4}${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)}`;
31996
+ const selectionArr = [columns, extras?.sql, joins].filter((e) => e !== void 0);
31997
+ if (!selectionArr.length) {
31998
+ throw new DrizzleError({
31999
+ message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
32000
+ });
32001
+ }
32002
+ const selectionSet = sql.join(selectionArr, sql`, `);
32003
+ const query = sql`select ${selectionSet} from ${table4[IsAlias] ? sql`${sql`${sql.identifier(table4[Schema] ?? "")}.`.if(table4[Schema])}${sql.identifier(table4[OriginalName])} as ${table4}` : table4}${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)}`;
32025
32004
  return {
32026
32005
  sql: query,
32027
32006
  selection
@@ -33398,7 +33377,8 @@ var init_query4 = __esm({
33398
33377
  init_relations();
33399
33378
  _a224 = entityKind;
33400
33379
  RelationalQueryBuilder2 = class {
33401
- constructor(tables, schema4, tableNamesMap, table4, tableConfig, dialect4, session) {
33380
+ constructor(mode, tables, schema4, tableNamesMap, table4, tableConfig, dialect4, session) {
33381
+ this.mode = mode;
33402
33382
  this.tables = tables;
33403
33383
  this.schema = schema4;
33404
33384
  this.tableNamesMap = tableNamesMap;
@@ -33408,7 +33388,17 @@ var init_query4 = __esm({
33408
33388
  this.session = session;
33409
33389
  }
33410
33390
  findMany(config) {
33411
- return new SQLiteRelationalQuery2(
33391
+ return this.mode === "sync" ? new SQLiteSyncRelationalQuery2(
33392
+ this.tables,
33393
+ this.schema,
33394
+ this.tableNamesMap,
33395
+ this.table,
33396
+ this.tableConfig,
33397
+ this.dialect,
33398
+ this.session,
33399
+ config ?? true,
33400
+ "many"
33401
+ ) : new SQLiteRelationalQuery2(
33412
33402
  this.tables,
33413
33403
  this.schema,
33414
33404
  this.tableNamesMap,
@@ -33421,7 +33411,17 @@ var init_query4 = __esm({
33421
33411
  );
33422
33412
  }
33423
33413
  findFirst(config) {
33424
- return new SQLiteRelationalQuery2(
33414
+ return this.mode === "sync" ? new SQLiteSyncRelationalQuery2(
33415
+ this.tables,
33416
+ this.schema,
33417
+ this.tableNamesMap,
33418
+ this.table,
33419
+ this.tableConfig,
33420
+ this.dialect,
33421
+ this.session,
33422
+ config ?? true,
33423
+ "first"
33424
+ ) : new SQLiteRelationalQuery2(
33425
33425
  this.tables,
33426
33426
  this.schema,
33427
33427
  this.tableNamesMap,
@@ -33473,7 +33473,7 @@ var init_query4 = __esm({
33473
33473
  void 0,
33474
33474
  this.mode === "first" ? "get" : "all",
33475
33475
  (rawRows, mapColumnValue) => {
33476
- const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue));
33476
+ const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue, true));
33477
33477
  if (this.mode === "first") {
33478
33478
  return rows[0];
33479
33479
  }
@@ -33616,6 +33616,7 @@ var init_db2 = __esm({
33616
33616
  if (relations) {
33617
33617
  for (const [tableName, relation] of Object.entries(relations.tablesConfig)) {
33618
33618
  this.query[tableName] = new RelationalQueryBuilder2(
33619
+ resultKind,
33619
33620
  relations.tables,
33620
33621
  relations.tablesConfig,
33621
33622
  relations.tableNamesMap,
@@ -36850,42 +36851,7 @@ var init_dialect3 = __esm({
36850
36851
  }
36851
36852
  return selectedColumns;
36852
36853
  });
36853
- __publicField(this, "buildWithColumns", (tableConfig, withContainer, jsonColumns, columnIdentifiers) => {
36854
- for (const [k, v] of Object.entries(
36855
- withContainer
36856
- )) {
36857
- if (!v)
36858
- continue;
36859
- const relation = tableConfig.relations[k];
36860
- if (relation.$brand === "AggregatedField")
36861
- continue;
36862
- const table4 = relation.targetTable;
36863
- const columns = this.getSelectedTableColumns(table4, typeof v === "boolean" ? void 0 : v.columns).map(
36864
- (c) => sql`${sql.raw(this.escapeString(c.tsName))}, ${sql.identifier(c.tsName)}`
36865
- );
36866
- if (typeof v === "object" && v.extras) {
36867
- if (typeof v.extras === "function") {
36868
- v.extras = v.extras(table4[Columns], { sql });
36869
- }
36870
- for (const [ek, ev] of Object.entries(v.extras)) {
36871
- if (!ev)
36872
- continue;
36873
- columns.push(
36874
- sql`${sql.raw(this.escapeString(ek))}, ${sql.identifier(ek)}`
36875
- );
36876
- }
36877
- }
36878
- columnIdentifiers.push(
36879
- sql`${table4}.${sql.identifier("r")} as ${sql.identifier(k)}`
36880
- );
36881
- jsonColumns[k] = sql.join(
36882
- columns,
36883
- sql`, `
36884
- );
36885
- }
36886
- });
36887
- __publicField(this, "buildColumns", (table4, tableConfig, selection, params) => params?.columns ? (() => {
36888
- const jsonColumns = {};
36854
+ __publicField(this, "buildColumns", (table4, selection, params) => params?.columns ? (() => {
36889
36855
  const columnIdentifiers = [];
36890
36856
  const selectedColumns = this.getSelectedTableColumns(table4, params?.columns);
36891
36857
  for (const column4 of selectedColumns) {
@@ -36893,24 +36859,8 @@ var init_dialect3 = __esm({
36893
36859
  sql`${table4[column4.tsName]} as ${sql.identifier(column4.tsName)}`
36894
36860
  );
36895
36861
  }
36896
- if (params?.with) {
36897
- this.buildWithColumns(tableConfig, params.with, jsonColumns, columnIdentifiers);
36898
- }
36899
- return {
36900
- columns: columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : this.unwrapAllColumns(table4, selection),
36901
- jsonColumns
36902
- };
36903
- })() : (() => {
36904
- const columnIdentifiers = [this.unwrapAllColumns(table4, selection)];
36905
- const jsonColumns = {};
36906
- if (params?.with) {
36907
- this.buildWithColumns(tableConfig, params.with, jsonColumns, columnIdentifiers);
36908
- }
36909
- return {
36910
- columns: sql.join(columnIdentifiers, sql`, `),
36911
- jsonColumns
36912
- };
36913
- })());
36862
+ return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
36863
+ })() : this.unwrapAllColumns(table4, selection));
36914
36864
  this.casing = new CasingCache(config?.casing);
36915
36865
  }
36916
36866
  async migrate(migrations, session, config) {
@@ -37712,20 +37662,24 @@ var init_dialect3 = __esm({
37712
37662
  tableConfig,
37713
37663
  queryConfig: config,
37714
37664
  relationWhere,
37715
- mode
37665
+ mode,
37666
+ errorPath,
37667
+ depth
37716
37668
  }) {
37717
37669
  const selection = [];
37718
37670
  const isSingle = mode === "first";
37719
37671
  const params = config === true ? void 0 : config;
37672
+ const currentPath = errorPath ?? "";
37673
+ const currentDepth = depth ?? 0;
37720
37674
  const limit = isSingle ? 1 : params?.limit;
37721
37675
  const offset = params?.offset;
37722
- const { columns, jsonColumns } = this.buildColumns(table4, tableConfig, selection, params);
37723
- const where = params?.where && relationWhere ? and(relationFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationFilterToSQL(table4, params.where) : relationWhere;
37676
+ const columns = this.buildColumns(table4, selection, params);
37677
+ const where = params?.where && relationWhere ? and(relationsFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table4, params.where) : relationWhere;
37724
37678
  const order = params?.orderBy ? relationsOrderToSQL(table4, params.orderBy) : void 0;
37725
37679
  const extras = params?.extras ? relationExtrasToSQL(table4, params.extras) : void 0;
37726
37680
  if (extras)
37727
37681
  selection.push(...extras.selection);
37728
- const selectionSet = sql.join([columns, extras].filter((e) => !!e), sql`, `);
37682
+ const selectionArr = columns ? [columns] : [];
37729
37683
  const joins = params ? (() => {
37730
37684
  const { with: joins2 } = params;
37731
37685
  if (!joins2)
@@ -37735,28 +37689,32 @@ var init_dialect3 = __esm({
37735
37689
  return;
37736
37690
  return sql.join(
37737
37691
  withEntries.map(([k, join]) => {
37738
- const relation = tableConfig.relations[k];
37739
- if (relation.$brand === "AggregatedField") {
37740
- relation.onTable(table4);
37741
- const query2 = relation.getSQL();
37692
+ selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
37693
+ if (is(tableConfig.relations[k], AggregatedField)) {
37694
+ const relation2 = tableConfig.relations[k];
37695
+ relation2.onTable(table4);
37696
+ const query2 = relation2.getSQL();
37742
37697
  selection.push({
37743
37698
  key: k,
37744
- field: relation
37699
+ field: relation2
37745
37700
  });
37746
37701
  return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
37747
37702
  }
37703
+ const relation = tableConfig.relations[k];
37748
37704
  const isSingle2 = is(relation, One);
37749
- const targetTable = relation.targetTable;
37750
- const relationFilter = relationToSQL(relation);
37705
+ const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
37706
+ const relationFilter = relationToSQL(relation, table4, targetTable);
37751
37707
  const innerQuery = this.buildRelationalQuery({
37752
37708
  table: targetTable,
37753
37709
  mode: isSingle2 ? "first" : "many",
37754
37710
  schema: schema4,
37755
37711
  queryConfig: join,
37756
- tableConfig: schema4[tableNamesMap[getTableUniqueName(targetTable)]],
37712
+ tableConfig: schema4[tableNamesMap[getTableUniqueName(relation.targetTable)]],
37757
37713
  tableNamesMap,
37758
37714
  tables,
37759
- relationWhere: relationFilter
37715
+ relationWhere: relationFilter,
37716
+ errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
37717
+ depth: currentDepth + 1
37760
37718
  });
37761
37719
  selection.push({
37762
37720
  field: targetTable,
@@ -37764,11 +37722,23 @@ var init_dialect3 = __esm({
37764
37722
  selection: innerQuery.selection,
37765
37723
  isArray: !isSingle2
37766
37724
  });
37767
- return sql`, lateral(select ${isSingle2 ? sql`json_object(${jsonColumns[k]}) as ${sql.identifier("r")}` : sql`coalesce(json_arrayagg(json_object(${jsonColumns[k]})), json_array()) as ${sql.identifier("r")}`} from (${innerQuery}) as ${sql.identifier("t")}) as ${schema4[k]}`;
37725
+ const jsonColumns = sql.join(
37726
+ innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
37727
+ sql`, `
37728
+ );
37729
+ 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)}`;
37768
37730
  })
37769
37731
  );
37770
37732
  })() : void 0;
37771
- const query = sql`select ${selectionSet} from ${table4}${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)}`;
37733
+ if (extras?.sql)
37734
+ selectionArr.push(extras.sql);
37735
+ if (!selectionArr.length) {
37736
+ throw new DrizzleError({
37737
+ message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
37738
+ });
37739
+ }
37740
+ const selectionSet = sql.join(selectionArr, sql`, `);
37741
+ const query = sql`select ${selectionSet} from ${table4[IsAlias] ? sql`${sql`${sql.identifier(table4[Schema] ?? "")}.`.if(table4[Schema])}${sql.identifier(table4[OriginalName])} as ${table4}` : table4}${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)}`;
37772
37742
  return {
37773
37743
  sql: query,
37774
37744
  selection