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.mjs CHANGED
@@ -23970,12 +23970,14 @@ var init_primary_keys = __esm({
23970
23970
  function getOrderByOperators() {
23971
23971
  return orderByOperators;
23972
23972
  }
23973
- function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value) {
23973
+ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false) {
23974
23974
  for (const selectionItem of buildQueryResultSelection) {
23975
23975
  const field = selectionItem.field;
23976
23976
  if (is(field, Table2)) {
23977
23977
  if (row[selectionItem.key] === null)
23978
23978
  continue;
23979
+ if (parseJson)
23980
+ row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
23979
23981
  if (selectionItem.isArray) {
23980
23982
  for (const item of row[selectionItem.key]) {
23981
23983
  mapRelationalRow(item, selectionItem.selection, mapColumnValue);
@@ -24036,7 +24038,7 @@ function defineRelations(schema4, relations) {
24036
24038
  );
24037
24039
  }
24038
24040
  function relationsFieldFilterToSQL(column4, filter2) {
24039
- if (typeof filter2 !== "object")
24041
+ if (typeof filter2 !== "object" || is(filter2, Placeholder))
24040
24042
  return eq(column4, filter2);
24041
24043
  const entries = Object.entries(filter2);
24042
24044
  if (!entries.length)
@@ -24046,14 +24048,14 @@ function relationsFieldFilterToSQL(column4, filter2) {
24046
24048
  if (value === void 0)
24047
24049
  continue;
24048
24050
  switch (target) {
24049
- case "$not": {
24051
+ case "NOT": {
24050
24052
  const res = relationsFieldFilterToSQL(column4, value);
24051
24053
  if (!res)
24052
24054
  continue;
24053
24055
  parts.push(not(res));
24054
24056
  continue;
24055
24057
  }
24056
- case "$or": {
24058
+ case "OR": {
24057
24059
  if (!value.length)
24058
24060
  continue;
24059
24061
  parts.push(
@@ -24078,7 +24080,7 @@ function relationsFieldFilterToSQL(column4, filter2) {
24078
24080
  return void 0;
24079
24081
  return and(...parts);
24080
24082
  }
24081
- function relationFilterToSQL(table4, filter2) {
24083
+ function relationsFilterToSQL(table4, filter2) {
24082
24084
  const entries = Object.entries(filter2);
24083
24085
  if (!entries.length)
24084
24086
  return void 0;
@@ -24087,35 +24089,33 @@ function relationFilterToSQL(table4, filter2) {
24087
24089
  if (value === void 0)
24088
24090
  continue;
24089
24091
  switch (target) {
24090
- case "$raw": {
24091
- if (value)
24092
- parts.push(value(operators));
24092
+ case "RAW": {
24093
+ if (value) {
24094
+ parts.push(
24095
+ value(table4[Columns], operators)
24096
+ );
24097
+ }
24093
24098
  continue;
24094
24099
  }
24095
- case "$or": {
24100
+ case "OR": {
24096
24101
  if (!value?.length)
24097
24102
  continue;
24098
24103
  parts.push(
24099
24104
  or(
24100
24105
  ...value.map(
24101
- (subFilter) => relationFilterToSQL(table4, subFilter)
24106
+ (subFilter) => relationsFilterToSQL(table4, subFilter)
24102
24107
  )
24103
24108
  )
24104
24109
  );
24105
24110
  continue;
24106
24111
  }
24107
- case "$not": {
24108
- if (!value?.length)
24112
+ case "NOT": {
24113
+ if (value === void 0)
24109
24114
  continue;
24110
- parts.push(
24111
- not(
24112
- and(
24113
- ...value.map(
24114
- (subFilter) => relationFilterToSQL(table4, subFilter)
24115
- )
24116
- )
24117
- )
24118
- );
24115
+ const built = relationsFilterToSQL(table4, value);
24116
+ if (!built)
24117
+ continue;
24118
+ parts.push(not(built));
24119
24119
  continue;
24120
24120
  }
24121
24121
  default: {
@@ -24156,13 +24156,15 @@ function relationExtrasToSQL(table4, extras) {
24156
24156
  selection
24157
24157
  };
24158
24158
  }
24159
- function relationToSQL(relation) {
24160
- const table4 = relation.sourceTable;
24159
+ function relationToSQL(relation, sourceTable, targetTable) {
24161
24160
  const columnWhere = relation.sourceColumns.map((s, i) => {
24162
24161
  const t = relation.targetColumns[i];
24163
- return eq(s, t);
24162
+ return eq(
24163
+ sql`${sql`${sql`${sql.identifier(sourceTable[Schema] ?? "")}.`.if(sourceTable[Schema] && !sourceTable[IsAlias])}`}${sourceTable}.${sql.identifier(s.name)}`,
24164
+ sql`${sql`${sql`${sql.identifier(targetTable[Schema] ?? "")}.`.if(targetTable[Schema] && !targetTable[IsAlias])}`}${targetTable}.${sql.identifier(t.name)}`
24165
+ );
24164
24166
  });
24165
- const targetWhere = relation.where ? and(...columnWhere, relationFilterToSQL(table4, relation.where)) : and(...columnWhere);
24167
+ const targetWhere = relation.where ? and(...columnWhere, relationsFilterToSQL(sourceTable, relation.where)) : and(...columnWhere);
24166
24168
  return targetWhere;
24167
24169
  }
24168
24170
  var _a118, Relations, _a119, Relation, _a120, _b95, One, _a121, _b96, Many, _a122, AggregatedField, _a123, _b97, Count, operators, orderByOperators, _a124, RelationsBuilderTable, _a125, RelationsBuilderColumn, _a126, RelationsHelperStatic;
@@ -24187,6 +24189,8 @@ var init_relations = __esm({
24187
24189
  this.tables = tables;
24188
24190
  this.config = config;
24189
24191
  for (const [tsName, table4] of Object.entries(tables)) {
24192
+ if (!is(table4, Table2))
24193
+ continue;
24190
24194
  this.tableNamesMap[getTableUniqueName(table4)] = tsName;
24191
24195
  const tableConfig = this.tablesConfig[tsName] = {
24192
24196
  table: table4,
@@ -24370,12 +24374,12 @@ Hint: you can specify "alias" on both sides of the relation with the same value`
24370
24374
  if (!this.query) {
24371
24375
  if (!this.table)
24372
24376
  throw new Error("Table must be set before building aggregate field");
24373
- this.query = sql`select count(*) as ${sql.identifier("count")} from ${this.table}`.mapWith(Number);
24377
+ this.query = sql`select count(*) as ${sql.identifier("r")} from ${this.table}`.mapWith(Number);
24374
24378
  }
24375
24379
  return this.query;
24376
24380
  }
24377
24381
  };
24378
- __publicField(Count, _a123, "Count");
24382
+ __publicField(Count, _a123, "AggregatedFieldCount");
24379
24383
  operators = {
24380
24384
  and,
24381
24385
  between,
@@ -24686,8 +24690,8 @@ __export(dist_exports, {
24686
24690
  param: () => param,
24687
24691
  placeholder: () => placeholder,
24688
24692
  relationExtrasToSQL: () => relationExtrasToSQL,
24689
- relationFilterToSQL: () => relationFilterToSQL,
24690
24693
  relationToSQL: () => relationToSQL,
24694
+ relationsFilterToSQL: () => relationsFilterToSQL,
24691
24695
  relationsOrderToSQL: () => relationsOrderToSQL,
24692
24696
  sql: () => sql,
24693
24697
  sum: () => sum,
@@ -25223,11 +25227,8 @@ var init_dialect = __esm({
25223
25227
  });
25224
25228
  }
25225
25229
  }
25226
- return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : this.unwrapAllColumns(table4, selection);
25227
- })() : (() => {
25228
- const columnIdentifiers = [this.unwrapAllColumns(table4, selection)];
25229
- return sql.join(columnIdentifiers, sql`, `);
25230
- })());
25230
+ return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
25231
+ })() : this.unwrapAllColumns(table4, selection));
25231
25232
  this.casing = new CasingCache(config?.casing);
25232
25233
  }
25233
25234
  async migrate(migrations, session, config) {
@@ -25842,20 +25843,24 @@ var init_dialect = __esm({
25842
25843
  tableConfig,
25843
25844
  queryConfig: config,
25844
25845
  relationWhere,
25845
- mode
25846
+ mode,
25847
+ errorPath,
25848
+ depth
25846
25849
  }) {
25847
25850
  const selection = [];
25848
25851
  const isSingle = mode === "first";
25849
25852
  const params = config === true ? void 0 : config;
25853
+ const currentPath = errorPath ?? "";
25854
+ const currentDepth = depth ?? 0;
25850
25855
  const limit = isSingle ? 1 : params?.limit;
25851
25856
  const offset = params?.offset;
25852
- const where = params?.where && relationWhere ? and(relationFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationFilterToSQL(table4, params.where) : relationWhere;
25857
+ const where = params?.where && relationWhere ? and(relationsFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table4, params.where) : relationWhere;
25853
25858
  const order = params?.orderBy ? relationsOrderToSQL(table4, params.orderBy) : void 0;
25854
25859
  const columns = this.buildColumns(table4, tableConfig, selection, params);
25855
25860
  const extras = params?.extras ? relationExtrasToSQL(table4, params.extras) : void 0;
25856
25861
  if (extras)
25857
25862
  selection.push(...extras.selection);
25858
- const selectionArr = [columns, extras?.sql];
25863
+ const selectionArr = columns ? [columns] : [];
25859
25864
  const joins = params ? (() => {
25860
25865
  const { with: joins2 } = params;
25861
25866
  if (!joins2)
@@ -25865,20 +25870,21 @@ var init_dialect = __esm({
25865
25870
  return;
25866
25871
  return sql.join(
25867
25872
  withEntries.map(([k, join]) => {
25868
- const relation = tableConfig.relations[k];
25869
- if (relation.$brand === "AggregatedField") {
25870
- relation.onTable(table4);
25871
- const query2 = relation.getSQL();
25873
+ if (is(tableConfig.relations[k], AggregatedField)) {
25874
+ const relation2 = tableConfig.relations[k];
25875
+ relation2.onTable(table4);
25876
+ const query2 = relation2.getSQL();
25872
25877
  selection.push({
25873
25878
  key: k,
25874
- field: relation
25879
+ field: relation2
25875
25880
  });
25876
25881
  selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
25877
25882
  return sql`left join lateral(${query2}) as ${sql.identifier(k)} on true`;
25878
25883
  }
25884
+ const relation = tableConfig.relations[k];
25879
25885
  const isSingle2 = is(relation, One);
25880
- const targetTable = relation.targetTable;
25881
- const relationFilter = relationToSQL(relation);
25886
+ const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
25887
+ const relationFilter = relationToSQL(relation, table4, targetTable);
25882
25888
  selectionArr.push(
25883
25889
  isSingle2 ? sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}` : sql`coalesce(${sql.identifier(k)}.${sql.identifier("r")}, '[]') as ${sql.identifier(k)}`
25884
25890
  );
@@ -25887,10 +25893,12 @@ var init_dialect = __esm({
25887
25893
  mode: isSingle2 ? "first" : "many",
25888
25894
  schema: schema4,
25889
25895
  queryConfig: join,
25890
- tableConfig: schema4[tableNamesMap[getTableUniqueName(targetTable)]],
25896
+ tableConfig: schema4[tableNamesMap[getTableUniqueName(relation.targetTable)]],
25891
25897
  tableNamesMap,
25892
25898
  tables,
25893
- relationWhere: relationFilter
25899
+ relationWhere: relationFilter,
25900
+ errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
25901
+ depth: currentDepth + 1
25894
25902
  });
25895
25903
  selection.push({
25896
25904
  field: targetTable,
@@ -25903,8 +25911,15 @@ var init_dialect = __esm({
25903
25911
  sql` `
25904
25912
  );
25905
25913
  })() : void 0;
25914
+ if (extras?.sql)
25915
+ selectionArr.push(extras.sql);
25916
+ if (!selectionArr.length) {
25917
+ throw new DrizzleError({
25918
+ message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
25919
+ });
25920
+ }
25906
25921
  const selectionSet = sql.join(selectionArr.filter((e) => e !== void 0), sql`, `);
25907
- 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)}`;
25922
+ 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)}`;
25908
25923
  return {
25909
25924
  sql: query,
25910
25925
  selection
@@ -31355,73 +31370,18 @@ var init_dialect2 = __esm({
31355
31370
  }
31356
31371
  return selectedColumns;
31357
31372
  });
31358
- __publicField(this, "buildWithColumns", (tableConfig, withContainer, jsonColumns) => {
31359
- for (const [k, v] of Object.entries(
31360
- withContainer
31361
- )) {
31362
- if (!v)
31363
- continue;
31364
- const relation = tableConfig.relations[k];
31365
- if (relation.$brand === "AggregatedField")
31366
- continue;
31367
- const table4 = relation.targetTable;
31368
- const columns = this.getSelectedTableColumns(table4, typeof v === "boolean" ? void 0 : v.columns).map(
31369
- (c) => sql`${sql.raw(this.escapeString(c.tsName))}, ${sql.identifier(c.tsName)}`
31370
- );
31371
- if (typeof v !== "boolean") {
31372
- if (v.extras) {
31373
- if (typeof v.extras === "function") {
31374
- v.extras = v.extras(table4[Columns], { sql });
31375
- }
31376
- for (const [ek, ev] of Object.entries(v.extras)) {
31377
- if (!ev)
31378
- continue;
31379
- columns.push(
31380
- sql`${sql.raw(this.escapeString(ek))}, ${sql.identifier(ek)}`
31381
- );
31382
- }
31383
- }
31384
- if (v.with) {
31385
- const withEntries = Object.entries(v.with);
31386
- for (const [k2, w] of withEntries) {
31387
- if (!w)
31388
- continue;
31389
- columns.push(sql`${sql.raw(this.escapeString(k2))}, jsonb(${sql.identifier(k2)})`);
31390
- }
31391
- }
31392
- }
31393
- jsonColumns[k] = jsonColumns[k] ? sql`${jsonColumns[k]}, ${sql.join(columns, sql`, `)}` : sql.join(columns, sql`, `);
31394
- }
31395
- });
31396
- __publicField(this, "buildColumns", (table4, tableConfig, selection, params) => params?.columns ? (() => {
31397
- const jsonColumns = {};
31373
+ __publicField(this, "buildColumns", (table4, selection, params) => params?.columns ? (() => {
31398
31374
  const columnIdentifiers = [];
31399
31375
  const selectedColumns = this.getSelectedTableColumns(table4, params?.columns);
31400
31376
  for (const column4 of selectedColumns) {
31401
- columnIdentifiers.push(sql`${column4} as ${sql.identifier(column4.tsName)}`);
31377
+ columnIdentifiers.push(sql`${column4.column} as ${sql.identifier(column4.tsName)}`);
31402
31378
  selection.push({
31403
31379
  key: column4.tsName,
31404
31380
  field: column4.column
31405
31381
  });
31406
31382
  }
31407
- if (params?.with) {
31408
- this.buildWithColumns(tableConfig, params.with, jsonColumns);
31409
- }
31410
- return {
31411
- columns: columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : this.unwrapAllColumns(table4, selection),
31412
- jsonColumns
31413
- };
31414
- })() : (() => {
31415
- const columnIdentifiers = [this.unwrapAllColumns(table4, selection)];
31416
- const jsonColumns = {};
31417
- if (params?.with) {
31418
- this.buildWithColumns(tableConfig, params.with, jsonColumns);
31419
- }
31420
- return {
31421
- columns: sql.join(columnIdentifiers, sql`, `),
31422
- jsonColumns
31423
- };
31424
- })());
31383
+ return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
31384
+ })() : this.unwrapAllColumns(table4, selection));
31425
31385
  this.casing = new CasingCache(config?.casing);
31426
31386
  }
31427
31387
  escapeName(name2) {
@@ -31968,15 +31928,19 @@ var init_dialect2 = __esm({
31968
31928
  queryConfig: config,
31969
31929
  relationWhere,
31970
31930
  mode,
31971
- isNested
31931
+ isNested,
31932
+ errorPath,
31933
+ depth
31972
31934
  }) {
31973
31935
  const selection = [];
31974
31936
  const isSingle = mode === "first";
31975
31937
  const params = config === true ? void 0 : config;
31938
+ const currentPath = errorPath ?? "";
31939
+ const currentDepth = depth ?? 0;
31976
31940
  const limit = isSingle ? 1 : params?.limit;
31977
31941
  const offset = params?.offset;
31978
- const { columns, jsonColumns } = this.buildColumns(table4, tableConfig, selection, params);
31979
- const where = params?.where && relationWhere ? and(relationFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationFilterToSQL(table4, params.where) : relationWhere;
31942
+ const columns = this.buildColumns(table4, selection, params);
31943
+ const where = params?.where && relationWhere ? and(relationsFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table4, params.where) : relationWhere;
31980
31944
  const order = params?.orderBy ? relationsOrderToSQL(table4, params.orderBy) : void 0;
31981
31945
  const extras = params?.extras ? relationExtrasToSQL(table4, params.extras) : void 0;
31982
31946
  if (extras)
@@ -31990,29 +31954,32 @@ var init_dialect2 = __esm({
31990
31954
  return;
31991
31955
  return sql.join(
31992
31956
  withEntries.map(([k, join]) => {
31993
- const relation = tableConfig.relations[k];
31994
- if (relation.$brand === "AggregatedField") {
31995
- relation.onTable(table4);
31996
- const query2 = relation.getSQL();
31957
+ if (is(tableConfig.relations[k], AggregatedField)) {
31958
+ const relation2 = tableConfig.relations[k];
31959
+ relation2.onTable(table4);
31960
+ const query2 = relation2.getSQL();
31997
31961
  selection.push({
31998
31962
  key: k,
31999
- field: relation
31963
+ field: relation2
32000
31964
  });
32001
31965
  return sql`(${query2}) as ${sql.identifier(k)}`;
32002
31966
  }
31967
+ const relation = tableConfig.relations[k];
32003
31968
  const isSingle2 = is(relation, One);
32004
31969
  const targetTable = relation.targetTable;
32005
- const relationFilter = relationToSQL(relation);
31970
+ const relationFilter = relationToSQL(relation, table4, targetTable);
32006
31971
  const innerQuery = this.buildRelationalQuery({
32007
31972
  table: targetTable,
32008
31973
  mode: isSingle2 ? "first" : "many",
32009
31974
  schema: schema4,
32010
31975
  queryConfig: join,
32011
- tableConfig: schema4[tableNamesMap[getTableUniqueName(targetTable)]],
31976
+ tableConfig: schema4[tableNamesMap[getTableUniqueName(relation.targetTable)]],
32012
31977
  tableNamesMap,
32013
31978
  tables,
32014
31979
  relationWhere: relationFilter,
32015
- isNested: true
31980
+ isNested: true,
31981
+ errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
31982
+ depth: currentDepth + 1
32016
31983
  });
32017
31984
  selection.push({
32018
31985
  field: targetTable,
@@ -32020,13 +31987,25 @@ var init_dialect2 = __esm({
32020
31987
  selection: innerQuery.selection,
32021
31988
  isArray: !isSingle2
32022
31989
  });
32023
- 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)}`;
31990
+ const jsonColumns = sql.join(
31991
+ innerQuery.selection.map((s) => {
31992
+ return sql`${sql.raw(this.escapeString(s.key))}, ${s.selection ? sql`jsonb(${sql.identifier(s.key)})` : sql.identifier(s.key)}`;
31993
+ }),
31994
+ sql`, `
31995
+ );
31996
+ 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)}`;
32024
31997
  }),
32025
31998
  sql`, `
32026
31999
  );
32027
32000
  })() : void 0;
32028
- const selectionSet = sql.join([columns, extras?.sql, joins].filter((e) => e !== void 0), sql`, `);
32029
- 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)}`;
32001
+ const selectionArr = [columns, extras?.sql, joins].filter((e) => e !== void 0);
32002
+ if (!selectionArr.length) {
32003
+ throw new DrizzleError({
32004
+ message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
32005
+ });
32006
+ }
32007
+ const selectionSet = sql.join(selectionArr, sql`, `);
32008
+ 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)}`;
32030
32009
  return {
32031
32010
  sql: query,
32032
32011
  selection
@@ -33403,7 +33382,8 @@ var init_query4 = __esm({
33403
33382
  init_relations();
33404
33383
  _a224 = entityKind;
33405
33384
  RelationalQueryBuilder2 = class {
33406
- constructor(tables, schema4, tableNamesMap, table4, tableConfig, dialect4, session) {
33385
+ constructor(mode, tables, schema4, tableNamesMap, table4, tableConfig, dialect4, session) {
33386
+ this.mode = mode;
33407
33387
  this.tables = tables;
33408
33388
  this.schema = schema4;
33409
33389
  this.tableNamesMap = tableNamesMap;
@@ -33413,7 +33393,17 @@ var init_query4 = __esm({
33413
33393
  this.session = session;
33414
33394
  }
33415
33395
  findMany(config) {
33416
- return new SQLiteRelationalQuery2(
33396
+ return this.mode === "sync" ? new SQLiteSyncRelationalQuery2(
33397
+ this.tables,
33398
+ this.schema,
33399
+ this.tableNamesMap,
33400
+ this.table,
33401
+ this.tableConfig,
33402
+ this.dialect,
33403
+ this.session,
33404
+ config ?? true,
33405
+ "many"
33406
+ ) : new SQLiteRelationalQuery2(
33417
33407
  this.tables,
33418
33408
  this.schema,
33419
33409
  this.tableNamesMap,
@@ -33426,7 +33416,17 @@ var init_query4 = __esm({
33426
33416
  );
33427
33417
  }
33428
33418
  findFirst(config) {
33429
- return new SQLiteRelationalQuery2(
33419
+ return this.mode === "sync" ? new SQLiteSyncRelationalQuery2(
33420
+ this.tables,
33421
+ this.schema,
33422
+ this.tableNamesMap,
33423
+ this.table,
33424
+ this.tableConfig,
33425
+ this.dialect,
33426
+ this.session,
33427
+ config ?? true,
33428
+ "first"
33429
+ ) : new SQLiteRelationalQuery2(
33430
33430
  this.tables,
33431
33431
  this.schema,
33432
33432
  this.tableNamesMap,
@@ -33478,7 +33478,7 @@ var init_query4 = __esm({
33478
33478
  void 0,
33479
33479
  this.mode === "first" ? "get" : "all",
33480
33480
  (rawRows, mapColumnValue) => {
33481
- const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue));
33481
+ const rows = rawRows.map((row) => mapRelationalRow(row, query.selection, mapColumnValue, true));
33482
33482
  if (this.mode === "first") {
33483
33483
  return rows[0];
33484
33484
  }
@@ -33621,6 +33621,7 @@ var init_db2 = __esm({
33621
33621
  if (relations) {
33622
33622
  for (const [tableName, relation] of Object.entries(relations.tablesConfig)) {
33623
33623
  this.query[tableName] = new RelationalQueryBuilder2(
33624
+ resultKind,
33624
33625
  relations.tables,
33625
33626
  relations.tablesConfig,
33626
33627
  relations.tableNamesMap,
@@ -36855,42 +36856,7 @@ var init_dialect3 = __esm({
36855
36856
  }
36856
36857
  return selectedColumns;
36857
36858
  });
36858
- __publicField(this, "buildWithColumns", (tableConfig, withContainer, jsonColumns, columnIdentifiers) => {
36859
- for (const [k, v] of Object.entries(
36860
- withContainer
36861
- )) {
36862
- if (!v)
36863
- continue;
36864
- const relation = tableConfig.relations[k];
36865
- if (relation.$brand === "AggregatedField")
36866
- continue;
36867
- const table4 = relation.targetTable;
36868
- const columns = this.getSelectedTableColumns(table4, typeof v === "boolean" ? void 0 : v.columns).map(
36869
- (c) => sql`${sql.raw(this.escapeString(c.tsName))}, ${sql.identifier(c.tsName)}`
36870
- );
36871
- if (typeof v === "object" && v.extras) {
36872
- if (typeof v.extras === "function") {
36873
- v.extras = v.extras(table4[Columns], { sql });
36874
- }
36875
- for (const [ek, ev] of Object.entries(v.extras)) {
36876
- if (!ev)
36877
- continue;
36878
- columns.push(
36879
- sql`${sql.raw(this.escapeString(ek))}, ${sql.identifier(ek)}`
36880
- );
36881
- }
36882
- }
36883
- columnIdentifiers.push(
36884
- sql`${table4}.${sql.identifier("r")} as ${sql.identifier(k)}`
36885
- );
36886
- jsonColumns[k] = sql.join(
36887
- columns,
36888
- sql`, `
36889
- );
36890
- }
36891
- });
36892
- __publicField(this, "buildColumns", (table4, tableConfig, selection, params) => params?.columns ? (() => {
36893
- const jsonColumns = {};
36859
+ __publicField(this, "buildColumns", (table4, selection, params) => params?.columns ? (() => {
36894
36860
  const columnIdentifiers = [];
36895
36861
  const selectedColumns = this.getSelectedTableColumns(table4, params?.columns);
36896
36862
  for (const column4 of selectedColumns) {
@@ -36898,24 +36864,8 @@ var init_dialect3 = __esm({
36898
36864
  sql`${table4[column4.tsName]} as ${sql.identifier(column4.tsName)}`
36899
36865
  );
36900
36866
  }
36901
- if (params?.with) {
36902
- this.buildWithColumns(tableConfig, params.with, jsonColumns, columnIdentifiers);
36903
- }
36904
- return {
36905
- columns: columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : this.unwrapAllColumns(table4, selection),
36906
- jsonColumns
36907
- };
36908
- })() : (() => {
36909
- const columnIdentifiers = [this.unwrapAllColumns(table4, selection)];
36910
- const jsonColumns = {};
36911
- if (params?.with) {
36912
- this.buildWithColumns(tableConfig, params.with, jsonColumns, columnIdentifiers);
36913
- }
36914
- return {
36915
- columns: sql.join(columnIdentifiers, sql`, `),
36916
- jsonColumns
36917
- };
36918
- })());
36867
+ return columnIdentifiers.length ? sql.join(columnIdentifiers, sql`, `) : void 0;
36868
+ })() : this.unwrapAllColumns(table4, selection));
36919
36869
  this.casing = new CasingCache(config?.casing);
36920
36870
  }
36921
36871
  async migrate(migrations, session, config) {
@@ -37717,20 +37667,24 @@ var init_dialect3 = __esm({
37717
37667
  tableConfig,
37718
37668
  queryConfig: config,
37719
37669
  relationWhere,
37720
- mode
37670
+ mode,
37671
+ errorPath,
37672
+ depth
37721
37673
  }) {
37722
37674
  const selection = [];
37723
37675
  const isSingle = mode === "first";
37724
37676
  const params = config === true ? void 0 : config;
37677
+ const currentPath = errorPath ?? "";
37678
+ const currentDepth = depth ?? 0;
37725
37679
  const limit = isSingle ? 1 : params?.limit;
37726
37680
  const offset = params?.offset;
37727
- const { columns, jsonColumns } = this.buildColumns(table4, tableConfig, selection, params);
37728
- const where = params?.where && relationWhere ? and(relationFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationFilterToSQL(table4, params.where) : relationWhere;
37681
+ const columns = this.buildColumns(table4, selection, params);
37682
+ const where = params?.where && relationWhere ? and(relationsFilterToSQL(table4, params.where), relationWhere) : params?.where ? relationsFilterToSQL(table4, params.where) : relationWhere;
37729
37683
  const order = params?.orderBy ? relationsOrderToSQL(table4, params.orderBy) : void 0;
37730
37684
  const extras = params?.extras ? relationExtrasToSQL(table4, params.extras) : void 0;
37731
37685
  if (extras)
37732
37686
  selection.push(...extras.selection);
37733
- const selectionSet = sql.join([columns, extras].filter((e) => !!e), sql`, `);
37687
+ const selectionArr = columns ? [columns] : [];
37734
37688
  const joins = params ? (() => {
37735
37689
  const { with: joins2 } = params;
37736
37690
  if (!joins2)
@@ -37740,28 +37694,32 @@ var init_dialect3 = __esm({
37740
37694
  return;
37741
37695
  return sql.join(
37742
37696
  withEntries.map(([k, join]) => {
37743
- const relation = tableConfig.relations[k];
37744
- if (relation.$brand === "AggregatedField") {
37745
- relation.onTable(table4);
37746
- const query2 = relation.getSQL();
37697
+ selectionArr.push(sql`${sql.identifier(k)}.${sql.identifier("r")} as ${sql.identifier(k)}`);
37698
+ if (is(tableConfig.relations[k], AggregatedField)) {
37699
+ const relation2 = tableConfig.relations[k];
37700
+ relation2.onTable(table4);
37701
+ const query2 = relation2.getSQL();
37747
37702
  selection.push({
37748
37703
  key: k,
37749
- field: relation
37704
+ field: relation2
37750
37705
  });
37751
37706
  return sql`, lateral(${query2}) as ${sql.identifier(k)}`;
37752
37707
  }
37708
+ const relation = tableConfig.relations[k];
37753
37709
  const isSingle2 = is(relation, One);
37754
- const targetTable = relation.targetTable;
37755
- const relationFilter = relationToSQL(relation);
37710
+ const targetTable = aliasedTable(relation.targetTable, `d${currentDepth + 1}`);
37711
+ const relationFilter = relationToSQL(relation, table4, targetTable);
37756
37712
  const innerQuery = this.buildRelationalQuery({
37757
37713
  table: targetTable,
37758
37714
  mode: isSingle2 ? "first" : "many",
37759
37715
  schema: schema4,
37760
37716
  queryConfig: join,
37761
- tableConfig: schema4[tableNamesMap[getTableUniqueName(targetTable)]],
37717
+ tableConfig: schema4[tableNamesMap[getTableUniqueName(relation.targetTable)]],
37762
37718
  tableNamesMap,
37763
37719
  tables,
37764
- relationWhere: relationFilter
37720
+ relationWhere: relationFilter,
37721
+ errorPath: `${currentPath.length ? `${currentPath}.` : ""}${k}`,
37722
+ depth: currentDepth + 1
37765
37723
  });
37766
37724
  selection.push({
37767
37725
  field: targetTable,
@@ -37769,11 +37727,23 @@ var init_dialect3 = __esm({
37769
37727
  selection: innerQuery.selection,
37770
37728
  isArray: !isSingle2
37771
37729
  });
37772
- 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]}`;
37730
+ const jsonColumns = sql.join(
37731
+ innerQuery.selection.map((s) => sql`${sql.raw(this.escapeString(s.key))}, ${sql.identifier(s.key)}`),
37732
+ sql`, `
37733
+ );
37734
+ 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)}`;
37773
37735
  })
37774
37736
  );
37775
37737
  })() : void 0;
37776
- 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)}`;
37738
+ if (extras?.sql)
37739
+ selectionArr.push(extras.sql);
37740
+ if (!selectionArr.length) {
37741
+ throw new DrizzleError({
37742
+ message: `No fields selected for table "${tableConfig.tsName}"${currentPath ? ` ("${currentPath}")` : ""}`
37743
+ });
37744
+ }
37745
+ const selectionSet = sql.join(selectionArr, sql`, `);
37746
+ 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)}`;
37777
37747
  return {
37778
37748
  sql: query,
37779
37749
  selection