drizzle-kit 0.28.0 → 0.28.1-d7e3535

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. package/README.md +7 -7
  2. package/api.js +247 -91
  3. package/api.mjs +247 -91
  4. package/bin.cjs +8 -8
  5. package/package.json +1 -1
package/README.md CHANGED
@@ -1,21 +1,21 @@
1
1
  ## Drizzle Kit
2
2
 
3
- DrizzleKit - is a CLI migrator tool for DrizzleORM. It is probably one and only tool that lets you completely automatically generate SQL migrations and covers ~95% of the common cases like deletions and renames by prompting user input.
3
+ Drizzle Kit is a CLI migrator tool for Drizzle ORM. It is probably the one and only tool that lets you completely automatically generate SQL migrations and covers ~95% of the common cases like deletions and renames by prompting user input.
4
4
  <https://github.com/drizzle-team/drizzle-kit-mirror> - is a mirror repository for issues.
5
5
 
6
6
  ## Documentation
7
7
 
8
- Check the full documenation on [the website](https://orm.drizzle.team/kit-docs/overview)
8
+ Check the full documentation on [the website](https://orm.drizzle.team/kit-docs/overview).
9
9
 
10
10
  ### How it works
11
11
 
12
- `drizzle-kit` will traverse `schema folder` or `schema file`, generate schema snapshot and compare it to the previous version, if there's one.
13
- Based on the difference it will generate all needed SQL migrations and if there are any `automatically unresolvable` cases like `renames` it will prompt user for input.
12
+ Drizzle Kit traverses a schema module and generates a snapshot to compare with the previous version, if there is one.
13
+ Based on the difference, it will generate all needed SQL migrations. If there are any cases that can't be resolved automatically, such as renames, it will prompt the user for input.
14
14
 
15
- For schema file:
15
+ For example, for this schema module:
16
16
 
17
17
  ```typescript
18
- // ./src/db/schema.ts
18
+ // src/db/schema.ts
19
19
 
20
20
  import { integer, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
21
21
 
@@ -63,7 +63,7 @@ CREATE INDEX IF NOT EXISTS users_full_name_index ON users (full_name);
63
63
  npm install -D drizzle-kit
64
64
  ```
65
65
 
66
- Running with CLI options
66
+ Running with CLI options:
67
67
 
68
68
  ```jsonc
69
69
  // package.json
package/api.js CHANGED
@@ -21121,7 +21121,7 @@ var version;
21121
21121
  var init_version = __esm({
21122
21122
  "../drizzle-orm/dist/version.js"() {
21123
21123
  "use strict";
21124
- version = "0.36.1";
21124
+ version = "0.36.2";
21125
21125
  }
21126
21126
  });
21127
21127
 
@@ -21407,7 +21407,11 @@ var init_sql = __esm({
21407
21407
  if (_config.invokeSource === "indexes") {
21408
21408
  return { sql: escapeName(columnName), params: [] };
21409
21409
  }
21410
- return { sql: escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName), params: [] };
21410
+ const schemaName = chunk.table[Table2.Symbol.Schema];
21411
+ return {
21412
+ sql: chunk.table[IsAlias] || schemaName === void 0 ? escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName) : escapeName(schemaName) + "." + escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName),
21413
+ params: []
21414
+ };
21411
21415
  }
21412
21416
  if (is(chunk, View3)) {
21413
21417
  const schemaName = chunk[ViewBaseConfig].schema;
@@ -22140,7 +22144,7 @@ function haveSameKeys(left, right) {
22140
22144
  }
22141
22145
  function mapUpdateSet(table4, values) {
22142
22146
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22143
- if (is(value, SQL)) {
22147
+ if (is(value, SQL) || is(value, Column2)) {
22144
22148
  return [key, value];
22145
22149
  } else {
22146
22150
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24981,12 +24985,19 @@ var init_dialect = __esm({
24981
24985
  return [res];
24982
24986
  }));
24983
24987
  }
24984
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24988
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24985
24989
  const withSql = this.buildWithCTE(withList);
24990
+ const tableName = table4[PgTable.Symbol.Name];
24991
+ const tableSchema = table4[PgTable.Symbol.Schema];
24992
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24993
+ const alias = tableName === origTableName ? void 0 : tableName;
24994
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
24986
24995
  const setSql = this.buildUpdateSet(table4, set);
24987
- const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
24996
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
24997
+ const joinsSql = this.buildJoins(joins);
24998
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
24988
24999
  const whereSql = where ? sql` where ${where}` : void 0;
24989
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
25000
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24990
25001
  }
24991
25002
  /**
24992
25003
  * Builds selection SQL with provided fields/expressions
@@ -25038,6 +25049,54 @@ var init_dialect = __esm({
25038
25049
  });
25039
25050
  return sql.join(chunks);
25040
25051
  }
25052
+ buildJoins(joins) {
25053
+ if (!joins || joins.length === 0) {
25054
+ return void 0;
25055
+ }
25056
+ const joinsArray = [];
25057
+ for (const [index4, joinMeta] of joins.entries()) {
25058
+ if (index4 === 0) {
25059
+ joinsArray.push(sql` `);
25060
+ }
25061
+ const table4 = joinMeta.table;
25062
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25063
+ if (is(table4, PgTable)) {
25064
+ const tableName = table4[PgTable.Symbol.Name];
25065
+ const tableSchema = table4[PgTable.Symbol.Schema];
25066
+ const origTableName = table4[PgTable.Symbol.OriginalName];
25067
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25068
+ joinsArray.push(
25069
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25070
+ );
25071
+ } else if (is(table4, View3)) {
25072
+ const viewName = table4[ViewBaseConfig].name;
25073
+ const viewSchema = table4[ViewBaseConfig].schema;
25074
+ const origViewName = table4[ViewBaseConfig].originalName;
25075
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25076
+ joinsArray.push(
25077
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25078
+ );
25079
+ } else {
25080
+ joinsArray.push(
25081
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table4} on ${joinMeta.on}`
25082
+ );
25083
+ }
25084
+ if (index4 < joins.length - 1) {
25085
+ joinsArray.push(sql` `);
25086
+ }
25087
+ }
25088
+ return sql.join(joinsArray);
25089
+ }
25090
+ buildFromTable(table4) {
25091
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25092
+ let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25093
+ if (table4[Table2.Symbol.Schema]) {
25094
+ fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25095
+ }
25096
+ return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25097
+ }
25098
+ return table4;
25099
+ }
25041
25100
  buildSelectQuery({
25042
25101
  withList,
25043
25102
  fields,
@@ -25072,51 +25131,8 @@ var init_dialect = __esm({
25072
25131
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25073
25132
  }
25074
25133
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25075
- const tableSql = (() => {
25076
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25077
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25078
- if (table4[Table2.Symbol.Schema]) {
25079
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25080
- }
25081
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25082
- }
25083
- return table4;
25084
- })();
25085
- const joinsArray = [];
25086
- if (joins) {
25087
- for (const [index4, joinMeta] of joins.entries()) {
25088
- if (index4 === 0) {
25089
- joinsArray.push(sql` `);
25090
- }
25091
- const table22 = joinMeta.table;
25092
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25093
- if (is(table22, PgTable)) {
25094
- const tableName = table22[PgTable.Symbol.Name];
25095
- const tableSchema = table22[PgTable.Symbol.Schema];
25096
- const origTableName = table22[PgTable.Symbol.OriginalName];
25097
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25098
- joinsArray.push(
25099
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25100
- );
25101
- } else if (is(table22, View3)) {
25102
- const viewName = table22[ViewBaseConfig].name;
25103
- const viewSchema = table22[ViewBaseConfig].schema;
25104
- const origViewName = table22[ViewBaseConfig].originalName;
25105
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25106
- joinsArray.push(
25107
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25108
- );
25109
- } else {
25110
- joinsArray.push(
25111
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25112
- );
25113
- }
25114
- if (index4 < joins.length - 1) {
25115
- joinsArray.push(sql` `);
25116
- }
25117
- }
25118
- }
25119
- const joinsSql = sql.join(joinsArray);
25134
+ const tableSql = this.buildFromTable(table4);
25135
+ const joinsSql = this.buildJoins(joins);
25120
25136
  const whereSql = where ? sql` where ${where}` : void 0;
25121
25137
  const havingSql = having ? sql` having ${having}` : void 0;
25122
25138
  let orderBySql;
@@ -26974,9 +26990,14 @@ var init_update = __esm({
26974
26990
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26975
26991
  "use strict";
26976
26992
  init_entity();
26993
+ init_table2();
26977
26994
  init_query_promise();
26995
+ init_selection_proxy();
26996
+ init_sql();
26997
+ init_subquery();
26978
26998
  init_table();
26979
26999
  init_utils2();
27000
+ init_view_common();
26980
27001
  _a137 = entityKind;
26981
27002
  PgUpdateBuilder = class {
26982
27003
  constructor(table4, session, dialect4, withList) {
@@ -27000,12 +27021,85 @@ var init_update = __esm({
27000
27021
  constructor(table4, set, session, dialect4, withList) {
27001
27022
  super();
27002
27023
  __publicField(this, "config");
27024
+ __publicField(this, "tableName");
27025
+ __publicField(this, "joinsNotNullableMap");
27026
+ __publicField(this, "leftJoin", this.createJoin("left"));
27027
+ __publicField(this, "rightJoin", this.createJoin("right"));
27028
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27029
+ __publicField(this, "fullJoin", this.createJoin("full"));
27003
27030
  __publicField(this, "execute", (placeholderValues) => {
27004
27031
  return this._prepare().execute(placeholderValues);
27005
27032
  });
27006
27033
  this.session = session;
27007
27034
  this.dialect = dialect4;
27008
- this.config = { set, table: table4, withList };
27035
+ this.config = { set, table: table4, withList, joins: [] };
27036
+ this.tableName = getTableLikeName(table4);
27037
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27038
+ }
27039
+ from(source) {
27040
+ const tableName = getTableLikeName(source);
27041
+ if (typeof tableName === "string") {
27042
+ this.joinsNotNullableMap[tableName] = true;
27043
+ }
27044
+ this.config.from = source;
27045
+ return this;
27046
+ }
27047
+ getTableLikeFields(table4) {
27048
+ if (is(table4, PgTable)) {
27049
+ return table4[Table2.Symbol.Columns];
27050
+ } else if (is(table4, Subquery)) {
27051
+ return table4._.selectedFields;
27052
+ }
27053
+ return table4[ViewBaseConfig].selectedFields;
27054
+ }
27055
+ createJoin(joinType) {
27056
+ return (table4, on) => {
27057
+ const tableName = getTableLikeName(table4);
27058
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27059
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27060
+ }
27061
+ if (typeof on === "function") {
27062
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27063
+ on = on(
27064
+ new Proxy(
27065
+ this.config.table[Table2.Symbol.Columns],
27066
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27067
+ ),
27068
+ from && new Proxy(
27069
+ from,
27070
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27071
+ )
27072
+ );
27073
+ }
27074
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27075
+ if (typeof tableName === "string") {
27076
+ switch (joinType) {
27077
+ case "left": {
27078
+ this.joinsNotNullableMap[tableName] = false;
27079
+ break;
27080
+ }
27081
+ case "right": {
27082
+ this.joinsNotNullableMap = Object.fromEntries(
27083
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27084
+ );
27085
+ this.joinsNotNullableMap[tableName] = true;
27086
+ break;
27087
+ }
27088
+ case "inner": {
27089
+ this.joinsNotNullableMap[tableName] = true;
27090
+ break;
27091
+ }
27092
+ case "full": {
27093
+ this.joinsNotNullableMap = Object.fromEntries(
27094
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27095
+ );
27096
+ this.joinsNotNullableMap[tableName] = false;
27097
+ break;
27098
+ }
27099
+ }
27100
+ }
27101
+ return this;
27102
+ };
27009
27103
  }
27010
27104
  /**
27011
27105
  * Adds a 'where' clause to the query.
@@ -27044,7 +27138,24 @@ var init_update = __esm({
27044
27138
  this.config.where = where;
27045
27139
  return this;
27046
27140
  }
27047
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27141
+ returning(fields) {
27142
+ if (!fields) {
27143
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27144
+ if (this.config.from) {
27145
+ const tableName = getTableLikeName(this.config.from);
27146
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27147
+ const fromFields = this.getTableLikeFields(this.config.from);
27148
+ fields[tableName] = fromFields;
27149
+ }
27150
+ for (const join of this.config.joins) {
27151
+ const tableName2 = getTableLikeName(join.table);
27152
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27153
+ const fromFields = this.getTableLikeFields(join.table);
27154
+ fields[tableName2] = fromFields;
27155
+ }
27156
+ }
27157
+ }
27158
+ }
27048
27159
  this.config.returning = orderSelectedFields(fields);
27049
27160
  return this;
27050
27161
  }
@@ -27058,7 +27169,9 @@ var init_update = __esm({
27058
27169
  }
27059
27170
  /** @internal */
27060
27171
  _prepare(name2) {
27061
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27172
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27173
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27174
+ return query;
27062
27175
  }
27063
27176
  prepare(name2) {
27064
27177
  return this._prepare(name2);
@@ -31073,14 +31186,16 @@ var init_dialect2 = __esm({
31073
31186
  return [res];
31074
31187
  }));
31075
31188
  }
31076
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31189
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31077
31190
  const withSql = this.buildWithCTE(withList);
31078
31191
  const setSql = this.buildUpdateSet(table4, set);
31192
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31193
+ const joinsSql = this.buildJoins(joins);
31079
31194
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31080
31195
  const whereSql = where ? sql` where ${where}` : void 0;
31081
31196
  const orderBySql = this.buildOrderBy(orderBy);
31082
31197
  const limitSql = this.buildLimit(limit);
31083
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31198
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31084
31199
  }
31085
31200
  /**
31086
31201
  * Builds selection SQL with provided fields/expressions
@@ -31133,6 +31248,37 @@ var init_dialect2 = __esm({
31133
31248
  });
31134
31249
  return sql.join(chunks);
31135
31250
  }
31251
+ buildJoins(joins) {
31252
+ if (!joins || joins.length === 0) {
31253
+ return void 0;
31254
+ }
31255
+ const joinsArray = [];
31256
+ if (joins) {
31257
+ for (const [index4, joinMeta] of joins.entries()) {
31258
+ if (index4 === 0) {
31259
+ joinsArray.push(sql` `);
31260
+ }
31261
+ const table4 = joinMeta.table;
31262
+ if (is(table4, SQLiteTable)) {
31263
+ const tableName = table4[SQLiteTable.Symbol.Name];
31264
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31265
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31266
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31267
+ joinsArray.push(
31268
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31269
+ );
31270
+ } else {
31271
+ joinsArray.push(
31272
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31273
+ );
31274
+ }
31275
+ if (index4 < joins.length - 1) {
31276
+ joinsArray.push(sql` `);
31277
+ }
31278
+ }
31279
+ }
31280
+ return sql.join(joinsArray);
31281
+ }
31136
31282
  buildLimit(limit) {
31137
31283
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31138
31284
  }
@@ -31148,6 +31294,12 @@ var init_dialect2 = __esm({
31148
31294
  }
31149
31295
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31150
31296
  }
31297
+ buildFromTable(table4) {
31298
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31299
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31300
+ }
31301
+ return table4;
31302
+ }
31151
31303
  buildSelectQuery({
31152
31304
  withList,
31153
31305
  fields,
@@ -31178,38 +31330,8 @@ var init_dialect2 = __esm({
31178
31330
  const withSql = this.buildWithCTE(withList);
31179
31331
  const distinctSql = distinct ? sql` distinct` : void 0;
31180
31332
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31181
- const tableSql = (() => {
31182
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31183
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31184
- }
31185
- return table4;
31186
- })();
31187
- const joinsArray = [];
31188
- if (joins) {
31189
- for (const [index4, joinMeta] of joins.entries()) {
31190
- if (index4 === 0) {
31191
- joinsArray.push(sql` `);
31192
- }
31193
- const table22 = joinMeta.table;
31194
- if (is(table22, SQLiteTable)) {
31195
- const tableName = table22[SQLiteTable.Symbol.Name];
31196
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31197
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31198
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31199
- joinsArray.push(
31200
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31201
- );
31202
- } else {
31203
- joinsArray.push(
31204
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31205
- );
31206
- }
31207
- if (index4 < joins.length - 1) {
31208
- joinsArray.push(sql` `);
31209
- }
31210
- }
31211
- }
31212
- const joinsSql = sql.join(joinsArray);
31333
+ const tableSql = this.buildFromTable(table4);
31334
+ const joinsSql = this.buildJoins(joins);
31213
31335
  const whereSql = where ? sql` where ${where}` : void 0;
31214
31336
  const havingSql = having ? sql` having ${having}` : void 0;
31215
31337
  const groupByList = [];
@@ -32336,8 +32458,11 @@ var init_update2 = __esm({
32336
32458
  init_query_promise();
32337
32459
  init_selection_proxy();
32338
32460
  init_table3();
32461
+ init_subquery();
32339
32462
  init_table();
32340
32463
  init_utils2();
32464
+ init_view_common();
32465
+ init_view_base2();
32341
32466
  _a207 = entityKind;
32342
32467
  SQLiteUpdateBuilder = class {
32343
32468
  constructor(table4, session, dialect4, withList) {
@@ -32362,6 +32487,10 @@ var init_update2 = __esm({
32362
32487
  super();
32363
32488
  /** @internal */
32364
32489
  __publicField(this, "config");
32490
+ __publicField(this, "leftJoin", this.createJoin("left"));
32491
+ __publicField(this, "rightJoin", this.createJoin("right"));
32492
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32493
+ __publicField(this, "fullJoin", this.createJoin("full"));
32365
32494
  __publicField(this, "run", (placeholderValues) => {
32366
32495
  return this._prepare().run(placeholderValues);
32367
32496
  });
@@ -32376,7 +32505,34 @@ var init_update2 = __esm({
32376
32505
  });
32377
32506
  this.session = session;
32378
32507
  this.dialect = dialect4;
32379
- this.config = { set, table: table4, withList };
32508
+ this.config = { set, table: table4, withList, joins: [] };
32509
+ }
32510
+ from(source) {
32511
+ this.config.from = source;
32512
+ return this;
32513
+ }
32514
+ createJoin(joinType) {
32515
+ return (table4, on) => {
32516
+ const tableName = getTableLikeName(table4);
32517
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32518
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32519
+ }
32520
+ if (typeof on === "function") {
32521
+ const from = this.config.from ? is(table4, SQLiteTable) ? table4[Table2.Symbol.Columns] : is(table4, Subquery) ? table4._.selectedFields : is(table4, SQLiteViewBase) ? table4[ViewBaseConfig].selectedFields : void 0 : void 0;
32522
+ on = on(
32523
+ new Proxy(
32524
+ this.config.table[Table2.Symbol.Columns],
32525
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32526
+ ),
32527
+ from && new Proxy(
32528
+ from,
32529
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32530
+ )
32531
+ );
32532
+ }
32533
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32534
+ return this;
32535
+ };
32380
32536
  }
32381
32537
  /**
32382
32538
  * Adds a 'where' clause to the query.
@@ -35735,7 +35891,7 @@ var init_insert3 = __esm({
35735
35891
  /**
35736
35892
  * Adds an `on duplicate key update` clause to the query.
35737
35893
  *
35738
- * Calling this method will update update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35894
+ * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35739
35895
  *
35740
35896
  * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35741
35897
  *
@@ -35770,7 +35926,7 @@ var init_insert3 = __esm({
35770
35926
  returning.push({ field: value, path: [key] });
35771
35927
  }
35772
35928
  }
35773
- this.config.returning = orderSelectedFields(this.config.table[Table2.Symbol.Columns]);
35929
+ this.config.returning = returning;
35774
35930
  return this;
35775
35931
  }
35776
35932
  /** @internal */
package/api.mjs CHANGED
@@ -21126,7 +21126,7 @@ var version;
21126
21126
  var init_version = __esm({
21127
21127
  "../drizzle-orm/dist/version.js"() {
21128
21128
  "use strict";
21129
- version = "0.36.1";
21129
+ version = "0.36.2";
21130
21130
  }
21131
21131
  });
21132
21132
 
@@ -21412,7 +21412,11 @@ var init_sql = __esm({
21412
21412
  if (_config.invokeSource === "indexes") {
21413
21413
  return { sql: escapeName(columnName), params: [] };
21414
21414
  }
21415
- return { sql: escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName), params: [] };
21415
+ const schemaName = chunk.table[Table2.Symbol.Schema];
21416
+ return {
21417
+ sql: chunk.table[IsAlias] || schemaName === void 0 ? escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName) : escapeName(schemaName) + "." + escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName),
21418
+ params: []
21419
+ };
21416
21420
  }
21417
21421
  if (is(chunk, View3)) {
21418
21422
  const schemaName = chunk[ViewBaseConfig].schema;
@@ -22145,7 +22149,7 @@ function haveSameKeys(left, right) {
22145
22149
  }
22146
22150
  function mapUpdateSet(table4, values) {
22147
22151
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22148
- if (is(value, SQL)) {
22152
+ if (is(value, SQL) || is(value, Column2)) {
22149
22153
  return [key, value];
22150
22154
  } else {
22151
22155
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24986,12 +24990,19 @@ var init_dialect = __esm({
24986
24990
  return [res];
24987
24991
  }));
24988
24992
  }
24989
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24993
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24990
24994
  const withSql = this.buildWithCTE(withList);
24995
+ const tableName = table4[PgTable.Symbol.Name];
24996
+ const tableSchema = table4[PgTable.Symbol.Schema];
24997
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24998
+ const alias = tableName === origTableName ? void 0 : tableName;
24999
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
24991
25000
  const setSql = this.buildUpdateSet(table4, set);
24992
- const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25001
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
25002
+ const joinsSql = this.buildJoins(joins);
25003
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
24993
25004
  const whereSql = where ? sql` where ${where}` : void 0;
24994
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
25005
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24995
25006
  }
24996
25007
  /**
24997
25008
  * Builds selection SQL with provided fields/expressions
@@ -25043,6 +25054,54 @@ var init_dialect = __esm({
25043
25054
  });
25044
25055
  return sql.join(chunks);
25045
25056
  }
25057
+ buildJoins(joins) {
25058
+ if (!joins || joins.length === 0) {
25059
+ return void 0;
25060
+ }
25061
+ const joinsArray = [];
25062
+ for (const [index4, joinMeta] of joins.entries()) {
25063
+ if (index4 === 0) {
25064
+ joinsArray.push(sql` `);
25065
+ }
25066
+ const table4 = joinMeta.table;
25067
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25068
+ if (is(table4, PgTable)) {
25069
+ const tableName = table4[PgTable.Symbol.Name];
25070
+ const tableSchema = table4[PgTable.Symbol.Schema];
25071
+ const origTableName = table4[PgTable.Symbol.OriginalName];
25072
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25073
+ joinsArray.push(
25074
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25075
+ );
25076
+ } else if (is(table4, View3)) {
25077
+ const viewName = table4[ViewBaseConfig].name;
25078
+ const viewSchema = table4[ViewBaseConfig].schema;
25079
+ const origViewName = table4[ViewBaseConfig].originalName;
25080
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25081
+ joinsArray.push(
25082
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25083
+ );
25084
+ } else {
25085
+ joinsArray.push(
25086
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table4} on ${joinMeta.on}`
25087
+ );
25088
+ }
25089
+ if (index4 < joins.length - 1) {
25090
+ joinsArray.push(sql` `);
25091
+ }
25092
+ }
25093
+ return sql.join(joinsArray);
25094
+ }
25095
+ buildFromTable(table4) {
25096
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25097
+ let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25098
+ if (table4[Table2.Symbol.Schema]) {
25099
+ fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25100
+ }
25101
+ return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25102
+ }
25103
+ return table4;
25104
+ }
25046
25105
  buildSelectQuery({
25047
25106
  withList,
25048
25107
  fields,
@@ -25077,51 +25136,8 @@ var init_dialect = __esm({
25077
25136
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25078
25137
  }
25079
25138
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25080
- const tableSql = (() => {
25081
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25082
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25083
- if (table4[Table2.Symbol.Schema]) {
25084
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25085
- }
25086
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25087
- }
25088
- return table4;
25089
- })();
25090
- const joinsArray = [];
25091
- if (joins) {
25092
- for (const [index4, joinMeta] of joins.entries()) {
25093
- if (index4 === 0) {
25094
- joinsArray.push(sql` `);
25095
- }
25096
- const table22 = joinMeta.table;
25097
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25098
- if (is(table22, PgTable)) {
25099
- const tableName = table22[PgTable.Symbol.Name];
25100
- const tableSchema = table22[PgTable.Symbol.Schema];
25101
- const origTableName = table22[PgTable.Symbol.OriginalName];
25102
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25103
- joinsArray.push(
25104
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25105
- );
25106
- } else if (is(table22, View3)) {
25107
- const viewName = table22[ViewBaseConfig].name;
25108
- const viewSchema = table22[ViewBaseConfig].schema;
25109
- const origViewName = table22[ViewBaseConfig].originalName;
25110
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25111
- joinsArray.push(
25112
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25113
- );
25114
- } else {
25115
- joinsArray.push(
25116
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25117
- );
25118
- }
25119
- if (index4 < joins.length - 1) {
25120
- joinsArray.push(sql` `);
25121
- }
25122
- }
25123
- }
25124
- const joinsSql = sql.join(joinsArray);
25139
+ const tableSql = this.buildFromTable(table4);
25140
+ const joinsSql = this.buildJoins(joins);
25125
25141
  const whereSql = where ? sql` where ${where}` : void 0;
25126
25142
  const havingSql = having ? sql` having ${having}` : void 0;
25127
25143
  let orderBySql;
@@ -26979,9 +26995,14 @@ var init_update = __esm({
26979
26995
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26980
26996
  "use strict";
26981
26997
  init_entity();
26998
+ init_table2();
26982
26999
  init_query_promise();
27000
+ init_selection_proxy();
27001
+ init_sql();
27002
+ init_subquery();
26983
27003
  init_table();
26984
27004
  init_utils2();
27005
+ init_view_common();
26985
27006
  _a137 = entityKind;
26986
27007
  PgUpdateBuilder = class {
26987
27008
  constructor(table4, session, dialect4, withList) {
@@ -27005,12 +27026,85 @@ var init_update = __esm({
27005
27026
  constructor(table4, set, session, dialect4, withList) {
27006
27027
  super();
27007
27028
  __publicField(this, "config");
27029
+ __publicField(this, "tableName");
27030
+ __publicField(this, "joinsNotNullableMap");
27031
+ __publicField(this, "leftJoin", this.createJoin("left"));
27032
+ __publicField(this, "rightJoin", this.createJoin("right"));
27033
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27034
+ __publicField(this, "fullJoin", this.createJoin("full"));
27008
27035
  __publicField(this, "execute", (placeholderValues) => {
27009
27036
  return this._prepare().execute(placeholderValues);
27010
27037
  });
27011
27038
  this.session = session;
27012
27039
  this.dialect = dialect4;
27013
- this.config = { set, table: table4, withList };
27040
+ this.config = { set, table: table4, withList, joins: [] };
27041
+ this.tableName = getTableLikeName(table4);
27042
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27043
+ }
27044
+ from(source) {
27045
+ const tableName = getTableLikeName(source);
27046
+ if (typeof tableName === "string") {
27047
+ this.joinsNotNullableMap[tableName] = true;
27048
+ }
27049
+ this.config.from = source;
27050
+ return this;
27051
+ }
27052
+ getTableLikeFields(table4) {
27053
+ if (is(table4, PgTable)) {
27054
+ return table4[Table2.Symbol.Columns];
27055
+ } else if (is(table4, Subquery)) {
27056
+ return table4._.selectedFields;
27057
+ }
27058
+ return table4[ViewBaseConfig].selectedFields;
27059
+ }
27060
+ createJoin(joinType) {
27061
+ return (table4, on) => {
27062
+ const tableName = getTableLikeName(table4);
27063
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27064
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27065
+ }
27066
+ if (typeof on === "function") {
27067
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27068
+ on = on(
27069
+ new Proxy(
27070
+ this.config.table[Table2.Symbol.Columns],
27071
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27072
+ ),
27073
+ from && new Proxy(
27074
+ from,
27075
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27076
+ )
27077
+ );
27078
+ }
27079
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27080
+ if (typeof tableName === "string") {
27081
+ switch (joinType) {
27082
+ case "left": {
27083
+ this.joinsNotNullableMap[tableName] = false;
27084
+ break;
27085
+ }
27086
+ case "right": {
27087
+ this.joinsNotNullableMap = Object.fromEntries(
27088
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27089
+ );
27090
+ this.joinsNotNullableMap[tableName] = true;
27091
+ break;
27092
+ }
27093
+ case "inner": {
27094
+ this.joinsNotNullableMap[tableName] = true;
27095
+ break;
27096
+ }
27097
+ case "full": {
27098
+ this.joinsNotNullableMap = Object.fromEntries(
27099
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27100
+ );
27101
+ this.joinsNotNullableMap[tableName] = false;
27102
+ break;
27103
+ }
27104
+ }
27105
+ }
27106
+ return this;
27107
+ };
27014
27108
  }
27015
27109
  /**
27016
27110
  * Adds a 'where' clause to the query.
@@ -27049,7 +27143,24 @@ var init_update = __esm({
27049
27143
  this.config.where = where;
27050
27144
  return this;
27051
27145
  }
27052
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27146
+ returning(fields) {
27147
+ if (!fields) {
27148
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27149
+ if (this.config.from) {
27150
+ const tableName = getTableLikeName(this.config.from);
27151
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27152
+ const fromFields = this.getTableLikeFields(this.config.from);
27153
+ fields[tableName] = fromFields;
27154
+ }
27155
+ for (const join of this.config.joins) {
27156
+ const tableName2 = getTableLikeName(join.table);
27157
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27158
+ const fromFields = this.getTableLikeFields(join.table);
27159
+ fields[tableName2] = fromFields;
27160
+ }
27161
+ }
27162
+ }
27163
+ }
27053
27164
  this.config.returning = orderSelectedFields(fields);
27054
27165
  return this;
27055
27166
  }
@@ -27063,7 +27174,9 @@ var init_update = __esm({
27063
27174
  }
27064
27175
  /** @internal */
27065
27176
  _prepare(name2) {
27066
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27177
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27178
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27179
+ return query;
27067
27180
  }
27068
27181
  prepare(name2) {
27069
27182
  return this._prepare(name2);
@@ -31078,14 +31191,16 @@ var init_dialect2 = __esm({
31078
31191
  return [res];
31079
31192
  }));
31080
31193
  }
31081
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31194
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31082
31195
  const withSql = this.buildWithCTE(withList);
31083
31196
  const setSql = this.buildUpdateSet(table4, set);
31197
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31198
+ const joinsSql = this.buildJoins(joins);
31084
31199
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31085
31200
  const whereSql = where ? sql` where ${where}` : void 0;
31086
31201
  const orderBySql = this.buildOrderBy(orderBy);
31087
31202
  const limitSql = this.buildLimit(limit);
31088
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31203
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31089
31204
  }
31090
31205
  /**
31091
31206
  * Builds selection SQL with provided fields/expressions
@@ -31138,6 +31253,37 @@ var init_dialect2 = __esm({
31138
31253
  });
31139
31254
  return sql.join(chunks);
31140
31255
  }
31256
+ buildJoins(joins) {
31257
+ if (!joins || joins.length === 0) {
31258
+ return void 0;
31259
+ }
31260
+ const joinsArray = [];
31261
+ if (joins) {
31262
+ for (const [index4, joinMeta] of joins.entries()) {
31263
+ if (index4 === 0) {
31264
+ joinsArray.push(sql` `);
31265
+ }
31266
+ const table4 = joinMeta.table;
31267
+ if (is(table4, SQLiteTable)) {
31268
+ const tableName = table4[SQLiteTable.Symbol.Name];
31269
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31270
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31271
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31272
+ joinsArray.push(
31273
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31274
+ );
31275
+ } else {
31276
+ joinsArray.push(
31277
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31278
+ );
31279
+ }
31280
+ if (index4 < joins.length - 1) {
31281
+ joinsArray.push(sql` `);
31282
+ }
31283
+ }
31284
+ }
31285
+ return sql.join(joinsArray);
31286
+ }
31141
31287
  buildLimit(limit) {
31142
31288
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31143
31289
  }
@@ -31153,6 +31299,12 @@ var init_dialect2 = __esm({
31153
31299
  }
31154
31300
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31155
31301
  }
31302
+ buildFromTable(table4) {
31303
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31304
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31305
+ }
31306
+ return table4;
31307
+ }
31156
31308
  buildSelectQuery({
31157
31309
  withList,
31158
31310
  fields,
@@ -31183,38 +31335,8 @@ var init_dialect2 = __esm({
31183
31335
  const withSql = this.buildWithCTE(withList);
31184
31336
  const distinctSql = distinct ? sql` distinct` : void 0;
31185
31337
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31186
- const tableSql = (() => {
31187
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31188
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31189
- }
31190
- return table4;
31191
- })();
31192
- const joinsArray = [];
31193
- if (joins) {
31194
- for (const [index4, joinMeta] of joins.entries()) {
31195
- if (index4 === 0) {
31196
- joinsArray.push(sql` `);
31197
- }
31198
- const table22 = joinMeta.table;
31199
- if (is(table22, SQLiteTable)) {
31200
- const tableName = table22[SQLiteTable.Symbol.Name];
31201
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31202
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31203
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31204
- joinsArray.push(
31205
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31206
- );
31207
- } else {
31208
- joinsArray.push(
31209
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31210
- );
31211
- }
31212
- if (index4 < joins.length - 1) {
31213
- joinsArray.push(sql` `);
31214
- }
31215
- }
31216
- }
31217
- const joinsSql = sql.join(joinsArray);
31338
+ const tableSql = this.buildFromTable(table4);
31339
+ const joinsSql = this.buildJoins(joins);
31218
31340
  const whereSql = where ? sql` where ${where}` : void 0;
31219
31341
  const havingSql = having ? sql` having ${having}` : void 0;
31220
31342
  const groupByList = [];
@@ -32341,8 +32463,11 @@ var init_update2 = __esm({
32341
32463
  init_query_promise();
32342
32464
  init_selection_proxy();
32343
32465
  init_table3();
32466
+ init_subquery();
32344
32467
  init_table();
32345
32468
  init_utils2();
32469
+ init_view_common();
32470
+ init_view_base2();
32346
32471
  _a207 = entityKind;
32347
32472
  SQLiteUpdateBuilder = class {
32348
32473
  constructor(table4, session, dialect4, withList) {
@@ -32367,6 +32492,10 @@ var init_update2 = __esm({
32367
32492
  super();
32368
32493
  /** @internal */
32369
32494
  __publicField(this, "config");
32495
+ __publicField(this, "leftJoin", this.createJoin("left"));
32496
+ __publicField(this, "rightJoin", this.createJoin("right"));
32497
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32498
+ __publicField(this, "fullJoin", this.createJoin("full"));
32370
32499
  __publicField(this, "run", (placeholderValues) => {
32371
32500
  return this._prepare().run(placeholderValues);
32372
32501
  });
@@ -32381,7 +32510,34 @@ var init_update2 = __esm({
32381
32510
  });
32382
32511
  this.session = session;
32383
32512
  this.dialect = dialect4;
32384
- this.config = { set, table: table4, withList };
32513
+ this.config = { set, table: table4, withList, joins: [] };
32514
+ }
32515
+ from(source) {
32516
+ this.config.from = source;
32517
+ return this;
32518
+ }
32519
+ createJoin(joinType) {
32520
+ return (table4, on) => {
32521
+ const tableName = getTableLikeName(table4);
32522
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32523
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32524
+ }
32525
+ if (typeof on === "function") {
32526
+ const from = this.config.from ? is(table4, SQLiteTable) ? table4[Table2.Symbol.Columns] : is(table4, Subquery) ? table4._.selectedFields : is(table4, SQLiteViewBase) ? table4[ViewBaseConfig].selectedFields : void 0 : void 0;
32527
+ on = on(
32528
+ new Proxy(
32529
+ this.config.table[Table2.Symbol.Columns],
32530
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32531
+ ),
32532
+ from && new Proxy(
32533
+ from,
32534
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32535
+ )
32536
+ );
32537
+ }
32538
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32539
+ return this;
32540
+ };
32385
32541
  }
32386
32542
  /**
32387
32543
  * Adds a 'where' clause to the query.
@@ -35740,7 +35896,7 @@ var init_insert3 = __esm({
35740
35896
  /**
35741
35897
  * Adds an `on duplicate key update` clause to the query.
35742
35898
  *
35743
- * Calling this method will update update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35899
+ * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35744
35900
  *
35745
35901
  * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35746
35902
  *
@@ -35775,7 +35931,7 @@ var init_insert3 = __esm({
35775
35931
  returning.push({ field: value, path: [key] });
35776
35932
  }
35777
35933
  }
35778
- this.config.returning = orderSelectedFields(this.config.table[Table2.Symbol.Columns]);
35934
+ this.config.returning = returning;
35779
35935
  return this;
35780
35936
  }
35781
35937
  /** @internal */
package/bin.cjs CHANGED
@@ -33614,7 +33614,7 @@ ${sql}
33614
33614
  }
33615
33615
  if (type === "custom") {
33616
33616
  console.log("Prepared empty file for your custom SQL migration!");
33617
- sql = "-- Custom SQL migration file, put you code below! --";
33617
+ sql = "-- Custom SQL migration file, put your code below! --";
33618
33618
  }
33619
33619
  journal.entries.push({
33620
33620
  idx,
@@ -81593,12 +81593,12 @@ var init_introspect = __esm({
81593
81593
  (0, import_hanji12.render)(
81594
81594
  `[${source_default.green(
81595
81595
  "\u2713"
81596
- )}] You schema file is ready \u279C ${source_default.bold.underline.blue(schemaFile)} \u{1F680}`
81596
+ )}] Your schema file is ready \u279C ${source_default.bold.underline.blue(schemaFile)} \u{1F680}`
81597
81597
  );
81598
81598
  (0, import_hanji12.render)(
81599
81599
  `[${source_default.green(
81600
81600
  "\u2713"
81601
- )}] You relations file is ready \u279C ${source_default.bold.underline.blue(
81601
+ )}] Your relations file is ready \u279C ${source_default.bold.underline.blue(
81602
81602
  relationsFile
81603
81603
  )} \u{1F680}`
81604
81604
  );
@@ -81676,12 +81676,12 @@ var init_introspect = __esm({
81676
81676
  (0, import_hanji12.render)(
81677
81677
  `[${source_default.green(
81678
81678
  "\u2713"
81679
- )}] You schema file is ready \u279C ${source_default.bold.underline.blue(schemaFile)} \u{1F680}`
81679
+ )}] Your schema file is ready \u279C ${source_default.bold.underline.blue(schemaFile)} \u{1F680}`
81680
81680
  );
81681
81681
  (0, import_hanji12.render)(
81682
81682
  `[${source_default.green(
81683
81683
  "\u2713"
81684
- )}] You relations file is ready \u279C ${source_default.bold.underline.blue(
81684
+ )}] Your relations file is ready \u279C ${source_default.bold.underline.blue(
81685
81685
  relationsFile
81686
81686
  )} \u{1F680}`
81687
81687
  );
@@ -81840,12 +81840,12 @@ var init_introspect = __esm({
81840
81840
  (0, import_hanji12.render)(
81841
81841
  `[${source_default.green(
81842
81842
  "\u2713"
81843
- )}] You schema file is ready \u279C ${source_default.bold.underline.blue(schemaFile)} \u{1F680}`
81843
+ )}] Your schema file is ready \u279C ${source_default.bold.underline.blue(schemaFile)} \u{1F680}`
81844
81844
  );
81845
81845
  (0, import_hanji12.render)(
81846
81846
  `[${source_default.green(
81847
81847
  "\u2713"
81848
- )}] You relations file is ready \u279C ${source_default.bold.underline.blue(
81848
+ )}] Your relations file is ready \u279C ${source_default.bold.underline.blue(
81849
81849
  relationsFile
81850
81850
  )} \u{1F680}`
81851
81851
  );
@@ -89049,7 +89049,7 @@ init_utils2();
89049
89049
  var version2 = async () => {
89050
89050
  const { npmVersion } = await ormCoreVersions();
89051
89051
  const ormVersion = npmVersion ? `drizzle-orm: v${npmVersion}` : "";
89052
- const envVersion = "0.28.0";
89052
+ const envVersion = "0.28.1-d7e3535";
89053
89053
  const kitVersion = envVersion ? `v${envVersion}` : "--";
89054
89054
  const versions = `drizzle-kit: ${kitVersion}
89055
89055
  ${ormVersion}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-kit",
3
- "version": "0.28.0",
3
+ "version": "0.28.1-d7e3535",
4
4
  "homepage": "https://orm.drizzle.team",
5
5
  "keywords": [
6
6
  "drizzle",