drizzle-kit 0.28.0-cc4f208 → 0.28.1-44b6c8a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. package/api.js +917 -702
  2. package/api.mjs +917 -702
  3. package/bin.cjs +1 -1
  4. package/package.json +1 -1
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
 
@@ -22144,7 +22144,7 @@ function haveSameKeys(left, right) {
22144
22144
  }
22145
22145
  function mapUpdateSet(table4, values) {
22146
22146
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22147
- if (is(value, SQL)) {
22147
+ if (is(value, SQL) || is(value, Column2)) {
22148
22148
  return [key, value];
22149
22149
  } else {
22150
22150
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24654,162 +24654,6 @@ var init_delete = __esm({
24654
24654
  }
24655
24655
  });
24656
24656
 
24657
- // ../drizzle-orm/dist/pg-core/query-builders/insert.js
24658
- var _a125, PgInsertBuilder, _a126, _b98, PgInsertBase;
24659
- var init_insert = __esm({
24660
- "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
24661
- "use strict";
24662
- init_entity();
24663
- init_query_promise();
24664
- init_sql();
24665
- init_table();
24666
- init_tracing();
24667
- init_utils2();
24668
- _a125 = entityKind;
24669
- PgInsertBuilder = class {
24670
- constructor(table4, session, dialect4, withList) {
24671
- this.table = table4;
24672
- this.session = session;
24673
- this.dialect = dialect4;
24674
- this.withList = withList;
24675
- }
24676
- values(values) {
24677
- values = Array.isArray(values) ? values : [values];
24678
- if (values.length === 0) {
24679
- throw new Error("values() must be called with at least one value");
24680
- }
24681
- const mappedValues = values.map((entry) => {
24682
- const result = {};
24683
- const cols = this.table[Table2.Symbol.Columns];
24684
- for (const colKey of Object.keys(entry)) {
24685
- const colValue = entry[colKey];
24686
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
24687
- }
24688
- return result;
24689
- });
24690
- return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
24691
- }
24692
- };
24693
- __publicField(PgInsertBuilder, _a125, "PgInsertBuilder");
24694
- PgInsertBase = class extends (_b98 = QueryPromise, _a126 = entityKind, _b98) {
24695
- constructor(table4, values, session, dialect4, withList) {
24696
- super();
24697
- __publicField(this, "config");
24698
- __publicField(this, "execute", (placeholderValues) => {
24699
- return tracer.startActiveSpan("drizzle.operation", () => {
24700
- return this._prepare().execute(placeholderValues);
24701
- });
24702
- });
24703
- this.session = session;
24704
- this.dialect = dialect4;
24705
- this.config = { table: table4, values, withList };
24706
- }
24707
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
24708
- this.config.returning = orderSelectedFields(fields);
24709
- return this;
24710
- }
24711
- /**
24712
- * Adds an `on conflict do nothing` clause to the query.
24713
- *
24714
- * Calling this method simply avoids inserting a row as its alternative action.
24715
- *
24716
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
24717
- *
24718
- * @param config The `target` and `where` clauses.
24719
- *
24720
- * @example
24721
- * ```ts
24722
- * // Insert one row and cancel the insert if there's a conflict
24723
- * await db.insert(cars)
24724
- * .values({ id: 1, brand: 'BMW' })
24725
- * .onConflictDoNothing();
24726
- *
24727
- * // Explicitly specify conflict target
24728
- * await db.insert(cars)
24729
- * .values({ id: 1, brand: 'BMW' })
24730
- * .onConflictDoNothing({ target: cars.id });
24731
- * ```
24732
- */
24733
- onConflictDoNothing(config = {}) {
24734
- if (config.target === void 0) {
24735
- this.config.onConflict = sql`do nothing`;
24736
- } else {
24737
- let targetColumn = "";
24738
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24739
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24740
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
24741
- }
24742
- return this;
24743
- }
24744
- /**
24745
- * Adds an `on conflict do update` clause to the query.
24746
- *
24747
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
24748
- *
24749
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
24750
- *
24751
- * @param config The `target`, `set` and `where` clauses.
24752
- *
24753
- * @example
24754
- * ```ts
24755
- * // Update the row if there's a conflict
24756
- * await db.insert(cars)
24757
- * .values({ id: 1, brand: 'BMW' })
24758
- * .onConflictDoUpdate({
24759
- * target: cars.id,
24760
- * set: { brand: 'Porsche' }
24761
- * });
24762
- *
24763
- * // Upsert with 'where' clause
24764
- * await db.insert(cars)
24765
- * .values({ id: 1, brand: 'BMW' })
24766
- * .onConflictDoUpdate({
24767
- * target: cars.id,
24768
- * set: { brand: 'newBMW' },
24769
- * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
24770
- * });
24771
- * ```
24772
- */
24773
- onConflictDoUpdate(config) {
24774
- if (config.where && (config.targetWhere || config.setWhere)) {
24775
- throw new Error(
24776
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
24777
- );
24778
- }
24779
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24780
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
24781
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
24782
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
24783
- let targetColumn = "";
24784
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24785
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
24786
- return this;
24787
- }
24788
- /** @internal */
24789
- getSQL() {
24790
- return this.dialect.buildInsertQuery(this.config);
24791
- }
24792
- toSQL() {
24793
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
24794
- return rest;
24795
- }
24796
- /** @internal */
24797
- _prepare(name2) {
24798
- return tracer.startActiveSpan("drizzle.prepareQuery", () => {
24799
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
24800
- });
24801
- }
24802
- prepare(name2) {
24803
- return this._prepare(name2);
24804
- }
24805
- $dynamic() {
24806
- return this;
24807
- }
24808
- };
24809
- __publicField(PgInsertBase, _a126, "PgInsert");
24810
- }
24811
- });
24812
-
24813
24657
  // ../drizzle-orm/dist/casing.js
24814
24658
  function toSnakeCase(input) {
24815
24659
  const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
@@ -24825,13 +24669,13 @@ function toCamelCase(input) {
24825
24669
  function noopCase(input) {
24826
24670
  return input;
24827
24671
  }
24828
- var _a127, CasingCache;
24672
+ var _a125, CasingCache;
24829
24673
  var init_casing = __esm({
24830
24674
  "../drizzle-orm/dist/casing.js"() {
24831
24675
  "use strict";
24832
24676
  init_entity();
24833
24677
  init_table();
24834
- _a127 = entityKind;
24678
+ _a125 = entityKind;
24835
24679
  CasingCache = class {
24836
24680
  constructor(casing2) {
24837
24681
  /** @internal */
@@ -24868,25 +24712,25 @@ var init_casing = __esm({
24868
24712
  this.cachedTables = {};
24869
24713
  }
24870
24714
  };
24871
- __publicField(CasingCache, _a127, "CasingCache");
24715
+ __publicField(CasingCache, _a125, "CasingCache");
24872
24716
  }
24873
24717
  });
24874
24718
 
24875
24719
  // ../drizzle-orm/dist/pg-core/view-base.js
24876
- var _a128, _b99, PgViewBase;
24720
+ var _a126, _b98, PgViewBase;
24877
24721
  var init_view_base = __esm({
24878
24722
  "../drizzle-orm/dist/pg-core/view-base.js"() {
24879
24723
  "use strict";
24880
24724
  init_entity();
24881
24725
  init_sql();
24882
- PgViewBase = class extends (_b99 = View3, _a128 = entityKind, _b99) {
24726
+ PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
24883
24727
  };
24884
- __publicField(PgViewBase, _a128, "PgViewBase");
24728
+ __publicField(PgViewBase, _a126, "PgViewBase");
24885
24729
  }
24886
24730
  });
24887
24731
 
24888
24732
  // ../drizzle-orm/dist/pg-core/dialect.js
24889
- var _a129, PgDialect;
24733
+ var _a127, PgDialect;
24890
24734
  var init_dialect = __esm({
24891
24735
  "../drizzle-orm/dist/pg-core/dialect.js"() {
24892
24736
  "use strict";
@@ -24905,7 +24749,7 @@ var init_dialect = __esm({
24905
24749
  init_utils2();
24906
24750
  init_view_common();
24907
24751
  init_view_base();
24908
- _a129 = entityKind;
24752
+ _a127 = entityKind;
24909
24753
  PgDialect = class {
24910
24754
  constructor(config) {
24911
24755
  /** @internal */
@@ -24985,12 +24829,19 @@ var init_dialect = __esm({
24985
24829
  return [res];
24986
24830
  }));
24987
24831
  }
24988
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24832
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24989
24833
  const withSql = this.buildWithCTE(withList);
24834
+ const tableName = table4[PgTable.Symbol.Name];
24835
+ const tableSchema = table4[PgTable.Symbol.Schema];
24836
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24837
+ const alias = tableName === origTableName ? void 0 : tableName;
24838
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
24990
24839
  const setSql = this.buildUpdateSet(table4, set);
24991
- const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
24840
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
24841
+ const joinsSql = this.buildJoins(joins);
24842
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
24992
24843
  const whereSql = where ? sql` where ${where}` : void 0;
24993
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
24844
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24994
24845
  }
24995
24846
  /**
24996
24847
  * Builds selection SQL with provided fields/expressions
@@ -25042,6 +24893,54 @@ var init_dialect = __esm({
25042
24893
  });
25043
24894
  return sql.join(chunks);
25044
24895
  }
24896
+ buildJoins(joins) {
24897
+ if (!joins || joins.length === 0) {
24898
+ return void 0;
24899
+ }
24900
+ const joinsArray = [];
24901
+ for (const [index4, joinMeta] of joins.entries()) {
24902
+ if (index4 === 0) {
24903
+ joinsArray.push(sql` `);
24904
+ }
24905
+ const table4 = joinMeta.table;
24906
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
24907
+ if (is(table4, PgTable)) {
24908
+ const tableName = table4[PgTable.Symbol.Name];
24909
+ const tableSchema = table4[PgTable.Symbol.Schema];
24910
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24911
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
24912
+ joinsArray.push(
24913
+ 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}`
24914
+ );
24915
+ } else if (is(table4, View3)) {
24916
+ const viewName = table4[ViewBaseConfig].name;
24917
+ const viewSchema = table4[ViewBaseConfig].schema;
24918
+ const origViewName = table4[ViewBaseConfig].originalName;
24919
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
24920
+ joinsArray.push(
24921
+ 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}`
24922
+ );
24923
+ } else {
24924
+ joinsArray.push(
24925
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table4} on ${joinMeta.on}`
24926
+ );
24927
+ }
24928
+ if (index4 < joins.length - 1) {
24929
+ joinsArray.push(sql` `);
24930
+ }
24931
+ }
24932
+ return sql.join(joinsArray);
24933
+ }
24934
+ buildFromTable(table4) {
24935
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
24936
+ let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
24937
+ if (table4[Table2.Symbol.Schema]) {
24938
+ fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
24939
+ }
24940
+ return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
24941
+ }
24942
+ return table4;
24943
+ }
25045
24944
  buildSelectQuery({
25046
24945
  withList,
25047
24946
  fields,
@@ -25076,51 +24975,8 @@ var init_dialect = __esm({
25076
24975
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25077
24976
  }
25078
24977
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25079
- const tableSql = (() => {
25080
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25081
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25082
- if (table4[Table2.Symbol.Schema]) {
25083
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25084
- }
25085
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25086
- }
25087
- return table4;
25088
- })();
25089
- const joinsArray = [];
25090
- if (joins) {
25091
- for (const [index4, joinMeta] of joins.entries()) {
25092
- if (index4 === 0) {
25093
- joinsArray.push(sql` `);
25094
- }
25095
- const table22 = joinMeta.table;
25096
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25097
- if (is(table22, PgTable)) {
25098
- const tableName = table22[PgTable.Symbol.Name];
25099
- const tableSchema = table22[PgTable.Symbol.Schema];
25100
- const origTableName = table22[PgTable.Symbol.OriginalName];
25101
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25102
- joinsArray.push(
25103
- 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}`
25104
- );
25105
- } else if (is(table22, View3)) {
25106
- const viewName = table22[ViewBaseConfig].name;
25107
- const viewSchema = table22[ViewBaseConfig].schema;
25108
- const origViewName = table22[ViewBaseConfig].originalName;
25109
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25110
- joinsArray.push(
25111
- 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}`
25112
- );
25113
- } else {
25114
- joinsArray.push(
25115
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25116
- );
25117
- }
25118
- if (index4 < joins.length - 1) {
25119
- joinsArray.push(sql` `);
25120
- }
25121
- }
25122
- }
25123
- const joinsSql = sql.join(joinsArray);
24978
+ const tableSql = this.buildFromTable(table4);
24979
+ const joinsSql = this.buildJoins(joins);
25124
24980
  const whereSql = where ? sql` where ${where}` : void 0;
25125
24981
  const havingSql = having ? sql` having ${having}` : void 0;
25126
24982
  let orderBySql;
@@ -25201,43 +25057,54 @@ var init_dialect = __esm({
25201
25057
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
25202
25058
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
25203
25059
  }
25204
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
25060
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
25205
25061
  const valuesSqlList = [];
25206
25062
  const columns = table4[Table2.Symbol.Columns];
25207
25063
  const colEntries = Object.entries(columns).filter(([_2, col]) => !col.shouldDisableInsert());
25208
25064
  const insertOrder = colEntries.map(
25209
25065
  ([, column4]) => sql.identifier(this.casing.getColumnCasing(column4))
25210
25066
  );
25211
- for (const [valueIndex, value] of values.entries()) {
25212
- const valueList = [];
25213
- for (const [fieldName, col] of colEntries) {
25214
- const colValue = value[fieldName];
25215
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25216
- if (col.defaultFn !== void 0) {
25217
- const defaultFnResult = col.defaultFn();
25218
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25219
- valueList.push(defaultValue);
25220
- } else if (!col.default && col.onUpdateFn !== void 0) {
25221
- const onUpdateFnResult = col.onUpdateFn();
25222
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25223
- valueList.push(newValue);
25067
+ if (select) {
25068
+ const select2 = valuesOrSelect;
25069
+ if (is(select2, SQL)) {
25070
+ valuesSqlList.push(select2);
25071
+ } else {
25072
+ valuesSqlList.push(select2.getSQL());
25073
+ }
25074
+ } else {
25075
+ const values = valuesOrSelect;
25076
+ valuesSqlList.push(sql.raw("values "));
25077
+ for (const [valueIndex, value] of values.entries()) {
25078
+ const valueList = [];
25079
+ for (const [fieldName, col] of colEntries) {
25080
+ const colValue = value[fieldName];
25081
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25082
+ if (col.defaultFn !== void 0) {
25083
+ const defaultFnResult = col.defaultFn();
25084
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25085
+ valueList.push(defaultValue);
25086
+ } else if (!col.default && col.onUpdateFn !== void 0) {
25087
+ const onUpdateFnResult = col.onUpdateFn();
25088
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25089
+ valueList.push(newValue);
25090
+ } else {
25091
+ valueList.push(sql`default`);
25092
+ }
25224
25093
  } else {
25225
- valueList.push(sql`default`);
25094
+ valueList.push(colValue);
25226
25095
  }
25227
- } else {
25228
- valueList.push(colValue);
25229
25096
  }
25230
- }
25231
- valuesSqlList.push(valueList);
25232
- if (valueIndex < values.length - 1) {
25233
- valuesSqlList.push(sql`, `);
25097
+ valuesSqlList.push(valueList);
25098
+ if (valueIndex < values.length - 1) {
25099
+ valuesSqlList.push(sql`, `);
25100
+ }
25234
25101
  }
25235
25102
  }
25236
25103
  const withSql = this.buildWithCTE(withList);
25237
25104
  const valuesSql = sql.join(valuesSqlList);
25238
25105
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25239
25106
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
25240
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
25107
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
25241
25108
  }
25242
25109
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25243
25110
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25973,12 +25840,12 @@ var init_dialect = __esm({
25973
25840
  };
25974
25841
  }
25975
25842
  };
25976
- __publicField(PgDialect, _a129, "PgDialect");
25843
+ __publicField(PgDialect, _a127, "PgDialect");
25977
25844
  }
25978
25845
  });
25979
25846
 
25980
25847
  // ../drizzle-orm/dist/selection-proxy.js
25981
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25848
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25982
25849
  var init_selection_proxy = __esm({
25983
25850
  "../drizzle-orm/dist/selection-proxy.js"() {
25984
25851
  "use strict";
@@ -25988,7 +25855,7 @@ var init_selection_proxy = __esm({
25988
25855
  init_sql();
25989
25856
  init_subquery();
25990
25857
  init_view_common();
25991
- _a130 = entityKind;
25858
+ _a128 = entityKind;
25992
25859
  _SelectionProxyHandler = class _SelectionProxyHandler {
25993
25860
  constructor(config) {
25994
25861
  __publicField(this, "config");
@@ -26054,25 +25921,25 @@ var init_selection_proxy = __esm({
26054
25921
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26055
25922
  }
26056
25923
  };
26057
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25924
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26058
25925
  SelectionProxyHandler = _SelectionProxyHandler;
26059
25926
  }
26060
25927
  });
26061
25928
 
26062
25929
  // ../drizzle-orm/dist/query-builders/query-builder.js
26063
- var _a131, TypedQueryBuilder;
25930
+ var _a129, TypedQueryBuilder;
26064
25931
  var init_query_builder = __esm({
26065
25932
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26066
25933
  "use strict";
26067
25934
  init_entity();
26068
- _a131 = entityKind;
25935
+ _a129 = entityKind;
26069
25936
  TypedQueryBuilder = class {
26070
25937
  /** @internal */
26071
25938
  getSelectedFields() {
26072
25939
  return this._.selectedFields;
26073
25940
  }
26074
25941
  };
26075
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25942
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26076
25943
  }
26077
25944
  });
26078
25945
 
@@ -26094,7 +25961,7 @@ function createSetOperator(type, isAll) {
26094
25961
  return leftSelect.addSetOperators(setOperators);
26095
25962
  };
26096
25963
  }
26097
- var _a132, PgSelectBuilder, _a133, _b100, PgSelectQueryBuilderBase, _a134, _b101, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
25964
+ var _a130, PgSelectBuilder, _a131, _b99, PgSelectQueryBuilderBase, _a132, _b100, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
26098
25965
  var init_select2 = __esm({
26099
25966
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26100
25967
  "use strict";
@@ -26110,7 +25977,7 @@ var init_select2 = __esm({
26110
25977
  init_utils2();
26111
25978
  init_utils2();
26112
25979
  init_view_common();
26113
- _a132 = entityKind;
25980
+ _a130 = entityKind;
26114
25981
  PgSelectBuilder = class {
26115
25982
  constructor(config) {
26116
25983
  __publicField(this, "fields");
@@ -26159,8 +26026,8 @@ var init_select2 = __esm({
26159
26026
  });
26160
26027
  }
26161
26028
  };
26162
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26163
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26029
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26030
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26164
26031
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26165
26032
  super();
26166
26033
  __publicField(this, "_");
@@ -26760,8 +26627,8 @@ var init_select2 = __esm({
26760
26627
  return this;
26761
26628
  }
26762
26629
  };
26763
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26764
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26630
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26631
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26765
26632
  constructor() {
26766
26633
  super(...arguments);
26767
26634
  __publicField(this, "execute", (placeholderValues) => {
@@ -26794,7 +26661,7 @@ var init_select2 = __esm({
26794
26661
  return this._prepare(name2);
26795
26662
  }
26796
26663
  };
26797
- __publicField(PgSelectBase, _a134, "PgSelect");
26664
+ __publicField(PgSelectBase, _a132, "PgSelect");
26798
26665
  applyMixins(PgSelectBase, [QueryPromise]);
26799
26666
  getPgSetOperators = () => ({
26800
26667
  union,
@@ -26814,7 +26681,7 @@ var init_select2 = __esm({
26814
26681
  });
26815
26682
 
26816
26683
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26817
- var _a135, QueryBuilder;
26684
+ var _a133, QueryBuilder;
26818
26685
  var init_query_builder2 = __esm({
26819
26686
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26820
26687
  "use strict";
@@ -26823,7 +26690,7 @@ var init_query_builder2 = __esm({
26823
26690
  init_selection_proxy();
26824
26691
  init_subquery();
26825
26692
  init_select2();
26826
- _a135 = entityKind;
26693
+ _a133 = entityKind;
26827
26694
  QueryBuilder = class {
26828
26695
  constructor(dialect4) {
26829
26696
  __publicField(this, "dialect");
@@ -26904,41 +26771,207 @@ var init_query_builder2 = __esm({
26904
26771
  return this.dialect;
26905
26772
  }
26906
26773
  };
26907
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26774
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26908
26775
  }
26909
26776
  });
26910
26777
 
26911
- // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26912
- var _a136, _b102, PgRefreshMaterializedView;
26913
- var init_refresh_materialized_view = __esm({
26914
- "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26778
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26779
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26780
+ var init_insert = __esm({
26781
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26915
26782
  "use strict";
26916
26783
  init_entity();
26917
26784
  init_query_promise();
26785
+ init_sql();
26786
+ init_table();
26918
26787
  init_tracing();
26919
- PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26920
- constructor(view4, session, dialect4) {
26921
- super();
26922
- __publicField(this, "config");
26923
- __publicField(this, "execute", (placeholderValues) => {
26924
- return tracer.startActiveSpan("drizzle.operation", () => {
26925
- return this._prepare().execute(placeholderValues);
26926
- });
26927
- });
26788
+ init_utils2();
26789
+ init_query_builder2();
26790
+ _a134 = entityKind;
26791
+ PgInsertBuilder = class {
26792
+ constructor(table4, session, dialect4, withList) {
26793
+ this.table = table4;
26928
26794
  this.session = session;
26929
26795
  this.dialect = dialect4;
26930
- this.config = { view: view4 };
26796
+ this.withList = withList;
26931
26797
  }
26932
- concurrently() {
26933
- if (this.config.withNoData !== void 0) {
26934
- throw new Error("Cannot use concurrently and withNoData together");
26798
+ values(values) {
26799
+ values = Array.isArray(values) ? values : [values];
26800
+ if (values.length === 0) {
26801
+ throw new Error("values() must be called with at least one value");
26935
26802
  }
26936
- this.config.concurrently = true;
26937
- return this;
26938
- }
26939
- withNoData() {
26940
- if (this.config.concurrently !== void 0) {
26941
- throw new Error("Cannot use concurrently and withNoData together");
26803
+ const mappedValues = values.map((entry) => {
26804
+ const result = {};
26805
+ const cols = this.table[Table2.Symbol.Columns];
26806
+ for (const colKey of Object.keys(entry)) {
26807
+ const colValue = entry[colKey];
26808
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26809
+ }
26810
+ return result;
26811
+ });
26812
+ return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
26813
+ }
26814
+ select(selectQuery) {
26815
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26816
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26817
+ throw new Error(
26818
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26819
+ );
26820
+ }
26821
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26822
+ }
26823
+ };
26824
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26825
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26826
+ constructor(table4, values, session, dialect4, withList, select) {
26827
+ super();
26828
+ __publicField(this, "config");
26829
+ __publicField(this, "execute", (placeholderValues) => {
26830
+ return tracer.startActiveSpan("drizzle.operation", () => {
26831
+ return this._prepare().execute(placeholderValues);
26832
+ });
26833
+ });
26834
+ this.session = session;
26835
+ this.dialect = dialect4;
26836
+ this.config = { table: table4, values, withList, select };
26837
+ }
26838
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26839
+ this.config.returning = orderSelectedFields(fields);
26840
+ return this;
26841
+ }
26842
+ /**
26843
+ * Adds an `on conflict do nothing` clause to the query.
26844
+ *
26845
+ * Calling this method simply avoids inserting a row as its alternative action.
26846
+ *
26847
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26848
+ *
26849
+ * @param config The `target` and `where` clauses.
26850
+ *
26851
+ * @example
26852
+ * ```ts
26853
+ * // Insert one row and cancel the insert if there's a conflict
26854
+ * await db.insert(cars)
26855
+ * .values({ id: 1, brand: 'BMW' })
26856
+ * .onConflictDoNothing();
26857
+ *
26858
+ * // Explicitly specify conflict target
26859
+ * await db.insert(cars)
26860
+ * .values({ id: 1, brand: 'BMW' })
26861
+ * .onConflictDoNothing({ target: cars.id });
26862
+ * ```
26863
+ */
26864
+ onConflictDoNothing(config = {}) {
26865
+ if (config.target === void 0) {
26866
+ this.config.onConflict = sql`do nothing`;
26867
+ } else {
26868
+ let targetColumn = "";
26869
+ targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
26870
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26871
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26872
+ }
26873
+ return this;
26874
+ }
26875
+ /**
26876
+ * Adds an `on conflict do update` clause to the query.
26877
+ *
26878
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26879
+ *
26880
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26881
+ *
26882
+ * @param config The `target`, `set` and `where` clauses.
26883
+ *
26884
+ * @example
26885
+ * ```ts
26886
+ * // Update the row if there's a conflict
26887
+ * await db.insert(cars)
26888
+ * .values({ id: 1, brand: 'BMW' })
26889
+ * .onConflictDoUpdate({
26890
+ * target: cars.id,
26891
+ * set: { brand: 'Porsche' }
26892
+ * });
26893
+ *
26894
+ * // Upsert with 'where' clause
26895
+ * await db.insert(cars)
26896
+ * .values({ id: 1, brand: 'BMW' })
26897
+ * .onConflictDoUpdate({
26898
+ * target: cars.id,
26899
+ * set: { brand: 'newBMW' },
26900
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26901
+ * });
26902
+ * ```
26903
+ */
26904
+ onConflictDoUpdate(config) {
26905
+ if (config.where && (config.targetWhere || config.setWhere)) {
26906
+ throw new Error(
26907
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26908
+ );
26909
+ }
26910
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26911
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26912
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26913
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26914
+ let targetColumn = "";
26915
+ targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
26916
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26917
+ return this;
26918
+ }
26919
+ /** @internal */
26920
+ getSQL() {
26921
+ return this.dialect.buildInsertQuery(this.config);
26922
+ }
26923
+ toSQL() {
26924
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26925
+ return rest;
26926
+ }
26927
+ /** @internal */
26928
+ _prepare(name2) {
26929
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26930
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26931
+ });
26932
+ }
26933
+ prepare(name2) {
26934
+ return this._prepare(name2);
26935
+ }
26936
+ $dynamic() {
26937
+ return this;
26938
+ }
26939
+ };
26940
+ __publicField(PgInsertBase, _a135, "PgInsert");
26941
+ }
26942
+ });
26943
+
26944
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26945
+ var _a136, _b102, PgRefreshMaterializedView;
26946
+ var init_refresh_materialized_view = __esm({
26947
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26948
+ "use strict";
26949
+ init_entity();
26950
+ init_query_promise();
26951
+ init_tracing();
26952
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26953
+ constructor(view4, session, dialect4) {
26954
+ super();
26955
+ __publicField(this, "config");
26956
+ __publicField(this, "execute", (placeholderValues) => {
26957
+ return tracer.startActiveSpan("drizzle.operation", () => {
26958
+ return this._prepare().execute(placeholderValues);
26959
+ });
26960
+ });
26961
+ this.session = session;
26962
+ this.dialect = dialect4;
26963
+ this.config = { view: view4 };
26964
+ }
26965
+ concurrently() {
26966
+ if (this.config.withNoData !== void 0) {
26967
+ throw new Error("Cannot use concurrently and withNoData together");
26968
+ }
26969
+ this.config.concurrently = true;
26970
+ return this;
26971
+ }
26972
+ withNoData() {
26973
+ if (this.config.concurrently !== void 0) {
26974
+ throw new Error("Cannot use concurrently and withNoData together");
26942
26975
  }
26943
26976
  this.config.withNoData = true;
26944
26977
  return this;
@@ -26978,9 +27011,14 @@ var init_update = __esm({
26978
27011
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26979
27012
  "use strict";
26980
27013
  init_entity();
27014
+ init_table2();
26981
27015
  init_query_promise();
27016
+ init_selection_proxy();
27017
+ init_sql();
27018
+ init_subquery();
26982
27019
  init_table();
26983
27020
  init_utils2();
27021
+ init_view_common();
26984
27022
  _a137 = entityKind;
26985
27023
  PgUpdateBuilder = class {
26986
27024
  constructor(table4, session, dialect4, withList) {
@@ -27004,12 +27042,85 @@ var init_update = __esm({
27004
27042
  constructor(table4, set, session, dialect4, withList) {
27005
27043
  super();
27006
27044
  __publicField(this, "config");
27045
+ __publicField(this, "tableName");
27046
+ __publicField(this, "joinsNotNullableMap");
27047
+ __publicField(this, "leftJoin", this.createJoin("left"));
27048
+ __publicField(this, "rightJoin", this.createJoin("right"));
27049
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27050
+ __publicField(this, "fullJoin", this.createJoin("full"));
27007
27051
  __publicField(this, "execute", (placeholderValues) => {
27008
27052
  return this._prepare().execute(placeholderValues);
27009
27053
  });
27010
27054
  this.session = session;
27011
27055
  this.dialect = dialect4;
27012
- this.config = { set, table: table4, withList };
27056
+ this.config = { set, table: table4, withList, joins: [] };
27057
+ this.tableName = getTableLikeName(table4);
27058
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27059
+ }
27060
+ from(source) {
27061
+ const tableName = getTableLikeName(source);
27062
+ if (typeof tableName === "string") {
27063
+ this.joinsNotNullableMap[tableName] = true;
27064
+ }
27065
+ this.config.from = source;
27066
+ return this;
27067
+ }
27068
+ getTableLikeFields(table4) {
27069
+ if (is(table4, PgTable)) {
27070
+ return table4[Table2.Symbol.Columns];
27071
+ } else if (is(table4, Subquery)) {
27072
+ return table4._.selectedFields;
27073
+ }
27074
+ return table4[ViewBaseConfig].selectedFields;
27075
+ }
27076
+ createJoin(joinType) {
27077
+ return (table4, on) => {
27078
+ const tableName = getTableLikeName(table4);
27079
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27080
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27081
+ }
27082
+ if (typeof on === "function") {
27083
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27084
+ on = on(
27085
+ new Proxy(
27086
+ this.config.table[Table2.Symbol.Columns],
27087
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27088
+ ),
27089
+ from && new Proxy(
27090
+ from,
27091
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27092
+ )
27093
+ );
27094
+ }
27095
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27096
+ if (typeof tableName === "string") {
27097
+ switch (joinType) {
27098
+ case "left": {
27099
+ this.joinsNotNullableMap[tableName] = false;
27100
+ break;
27101
+ }
27102
+ case "right": {
27103
+ this.joinsNotNullableMap = Object.fromEntries(
27104
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27105
+ );
27106
+ this.joinsNotNullableMap[tableName] = true;
27107
+ break;
27108
+ }
27109
+ case "inner": {
27110
+ this.joinsNotNullableMap[tableName] = true;
27111
+ break;
27112
+ }
27113
+ case "full": {
27114
+ this.joinsNotNullableMap = Object.fromEntries(
27115
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27116
+ );
27117
+ this.joinsNotNullableMap[tableName] = false;
27118
+ break;
27119
+ }
27120
+ }
27121
+ }
27122
+ return this;
27123
+ };
27013
27124
  }
27014
27125
  /**
27015
27126
  * Adds a 'where' clause to the query.
@@ -27048,7 +27159,24 @@ var init_update = __esm({
27048
27159
  this.config.where = where;
27049
27160
  return this;
27050
27161
  }
27051
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27162
+ returning(fields) {
27163
+ if (!fields) {
27164
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27165
+ if (this.config.from) {
27166
+ const tableName = getTableLikeName(this.config.from);
27167
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27168
+ const fromFields = this.getTableLikeFields(this.config.from);
27169
+ fields[tableName] = fromFields;
27170
+ }
27171
+ for (const join of this.config.joins) {
27172
+ const tableName2 = getTableLikeName(join.table);
27173
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27174
+ const fromFields = this.getTableLikeFields(join.table);
27175
+ fields[tableName2] = fromFields;
27176
+ }
27177
+ }
27178
+ }
27179
+ }
27052
27180
  this.config.returning = orderSelectedFields(fields);
27053
27181
  return this;
27054
27182
  }
@@ -27062,7 +27190,9 @@ var init_update = __esm({
27062
27190
  }
27063
27191
  /** @internal */
27064
27192
  _prepare(name2) {
27065
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27193
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27194
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27195
+ return query;
27066
27196
  }
27067
27197
  prepare(name2) {
27068
27198
  return this._prepare(name2);
@@ -30823,189 +30953,21 @@ var init_delete2 = __esm({
30823
30953
  }
30824
30954
  });
30825
30955
 
30826
- // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
30827
- var _a197, SQLiteInsertBuilder, _a198, _b142, SQLiteInsertBase;
30828
- var init_insert2 = __esm({
30829
- "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
30956
+ // ../drizzle-orm/dist/sqlite-core/view-base.js
30957
+ var _a197, _b142, SQLiteViewBase;
30958
+ var init_view_base2 = __esm({
30959
+ "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30830
30960
  "use strict";
30831
30961
  init_entity();
30832
- init_query_promise();
30833
30962
  init_sql();
30834
- init_table3();
30835
- init_table();
30836
- init_utils2();
30837
- _a197 = entityKind;
30838
- SQLiteInsertBuilder = class {
30839
- constructor(table4, session, dialect4, withList) {
30840
- this.table = table4;
30841
- this.session = session;
30842
- this.dialect = dialect4;
30843
- this.withList = withList;
30844
- }
30845
- values(values) {
30846
- values = Array.isArray(values) ? values : [values];
30847
- if (values.length === 0) {
30848
- throw new Error("values() must be called with at least one value");
30849
- }
30850
- const mappedValues = values.map((entry) => {
30851
- const result = {};
30852
- const cols = this.table[Table2.Symbol.Columns];
30853
- for (const colKey of Object.keys(entry)) {
30854
- const colValue = entry[colKey];
30855
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
30856
- }
30857
- return result;
30858
- });
30859
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
30860
- }
30861
- };
30862
- __publicField(SQLiteInsertBuilder, _a197, "SQLiteInsertBuilder");
30863
- SQLiteInsertBase = class extends (_b142 = QueryPromise, _a198 = entityKind, _b142) {
30864
- constructor(table4, values, session, dialect4, withList) {
30865
- super();
30866
- /** @internal */
30867
- __publicField(this, "config");
30868
- __publicField(this, "run", (placeholderValues) => {
30869
- return this._prepare().run(placeholderValues);
30870
- });
30871
- __publicField(this, "all", (placeholderValues) => {
30872
- return this._prepare().all(placeholderValues);
30873
- });
30874
- __publicField(this, "get", (placeholderValues) => {
30875
- return this._prepare().get(placeholderValues);
30876
- });
30877
- __publicField(this, "values", (placeholderValues) => {
30878
- return this._prepare().values(placeholderValues);
30879
- });
30880
- this.session = session;
30881
- this.dialect = dialect4;
30882
- this.config = { table: table4, values, withList };
30883
- }
30884
- returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
30885
- this.config.returning = orderSelectedFields(fields);
30886
- return this;
30887
- }
30888
- /**
30889
- * Adds an `on conflict do nothing` clause to the query.
30890
- *
30891
- * Calling this method simply avoids inserting a row as its alternative action.
30892
- *
30893
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
30894
- *
30895
- * @param config The `target` and `where` clauses.
30896
- *
30897
- * @example
30898
- * ```ts
30899
- * // Insert one row and cancel the insert if there's a conflict
30900
- * await db.insert(cars)
30901
- * .values({ id: 1, brand: 'BMW' })
30902
- * .onConflictDoNothing();
30903
- *
30904
- * // Explicitly specify conflict target
30905
- * await db.insert(cars)
30906
- * .values({ id: 1, brand: 'BMW' })
30907
- * .onConflictDoNothing({ target: cars.id });
30908
- * ```
30909
- */
30910
- onConflictDoNothing(config = {}) {
30911
- if (config.target === void 0) {
30912
- this.config.onConflict = sql`do nothing`;
30913
- } else {
30914
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30915
- const whereSql = config.where ? sql` where ${config.where}` : sql``;
30916
- this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
30917
- }
30918
- return this;
30919
- }
30920
- /**
30921
- * Adds an `on conflict do update` clause to the query.
30922
- *
30923
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
30924
- *
30925
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
30926
- *
30927
- * @param config The `target`, `set` and `where` clauses.
30928
- *
30929
- * @example
30930
- * ```ts
30931
- * // Update the row if there's a conflict
30932
- * await db.insert(cars)
30933
- * .values({ id: 1, brand: 'BMW' })
30934
- * .onConflictDoUpdate({
30935
- * target: cars.id,
30936
- * set: { brand: 'Porsche' }
30937
- * });
30938
- *
30939
- * // Upsert with 'where' clause
30940
- * await db.insert(cars)
30941
- * .values({ id: 1, brand: 'BMW' })
30942
- * .onConflictDoUpdate({
30943
- * target: cars.id,
30944
- * set: { brand: 'newBMW' },
30945
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
30946
- * });
30947
- * ```
30948
- */
30949
- onConflictDoUpdate(config) {
30950
- if (config.where && (config.targetWhere || config.setWhere)) {
30951
- throw new Error(
30952
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
30953
- );
30954
- }
30955
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
30956
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
30957
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
30958
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30959
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
30960
- this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
30961
- return this;
30962
- }
30963
- /** @internal */
30964
- getSQL() {
30965
- return this.dialect.buildInsertQuery(this.config);
30966
- }
30967
- toSQL() {
30968
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
30969
- return rest;
30970
- }
30971
- /** @internal */
30972
- _prepare(isOneTimeQuery = true) {
30973
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
30974
- this.dialect.sqlToQuery(this.getSQL()),
30975
- this.config.returning,
30976
- this.config.returning ? "all" : "run",
30977
- true
30978
- );
30979
- }
30980
- prepare() {
30981
- return this._prepare(false);
30982
- }
30983
- async execute() {
30984
- return this.config.returning ? this.all() : this.run();
30985
- }
30986
- $dynamic() {
30987
- return this;
30988
- }
30963
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
30989
30964
  };
30990
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
30991
- }
30992
- });
30993
-
30994
- // ../drizzle-orm/dist/sqlite-core/view-base.js
30995
- var _a199, _b143, SQLiteViewBase;
30996
- var init_view_base2 = __esm({
30997
- "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30998
- "use strict";
30999
- init_entity();
31000
- init_sql();
31001
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31002
- };
31003
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
30965
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31004
30966
  }
31005
30967
  });
31006
30968
 
31007
30969
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31008
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
30970
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31009
30971
  var init_dialect2 = __esm({
31010
30972
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31011
30973
  "use strict";
@@ -31024,7 +30986,7 @@ var init_dialect2 = __esm({
31024
30986
  init_utils2();
31025
30987
  init_view_common();
31026
30988
  init_view_base2();
31027
- _a200 = entityKind;
30989
+ _a198 = entityKind;
31028
30990
  SQLiteDialect = class {
31029
30991
  constructor(config) {
31030
30992
  /** @internal */
@@ -31077,14 +31039,16 @@ var init_dialect2 = __esm({
31077
31039
  return [res];
31078
31040
  }));
31079
31041
  }
31080
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31042
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31081
31043
  const withSql = this.buildWithCTE(withList);
31082
31044
  const setSql = this.buildUpdateSet(table4, set);
31045
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31046
+ const joinsSql = this.buildJoins(joins);
31083
31047
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31084
31048
  const whereSql = where ? sql` where ${where}` : void 0;
31085
31049
  const orderBySql = this.buildOrderBy(orderBy);
31086
31050
  const limitSql = this.buildLimit(limit);
31087
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31051
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31088
31052
  }
31089
31053
  /**
31090
31054
  * Builds selection SQL with provided fields/expressions
@@ -31137,6 +31101,37 @@ var init_dialect2 = __esm({
31137
31101
  });
31138
31102
  return sql.join(chunks);
31139
31103
  }
31104
+ buildJoins(joins) {
31105
+ if (!joins || joins.length === 0) {
31106
+ return void 0;
31107
+ }
31108
+ const joinsArray = [];
31109
+ if (joins) {
31110
+ for (const [index4, joinMeta] of joins.entries()) {
31111
+ if (index4 === 0) {
31112
+ joinsArray.push(sql` `);
31113
+ }
31114
+ const table4 = joinMeta.table;
31115
+ if (is(table4, SQLiteTable)) {
31116
+ const tableName = table4[SQLiteTable.Symbol.Name];
31117
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31118
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31119
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31120
+ joinsArray.push(
31121
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31122
+ );
31123
+ } else {
31124
+ joinsArray.push(
31125
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31126
+ );
31127
+ }
31128
+ if (index4 < joins.length - 1) {
31129
+ joinsArray.push(sql` `);
31130
+ }
31131
+ }
31132
+ }
31133
+ return sql.join(joinsArray);
31134
+ }
31140
31135
  buildLimit(limit) {
31141
31136
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31142
31137
  }
@@ -31152,6 +31147,12 @@ var init_dialect2 = __esm({
31152
31147
  }
31153
31148
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31154
31149
  }
31150
+ buildFromTable(table4) {
31151
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31152
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31153
+ }
31154
+ return table4;
31155
+ }
31155
31156
  buildSelectQuery({
31156
31157
  withList,
31157
31158
  fields,
@@ -31182,38 +31183,8 @@ var init_dialect2 = __esm({
31182
31183
  const withSql = this.buildWithCTE(withList);
31183
31184
  const distinctSql = distinct ? sql` distinct` : void 0;
31184
31185
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31185
- const tableSql = (() => {
31186
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31187
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31188
- }
31189
- return table4;
31190
- })();
31191
- const joinsArray = [];
31192
- if (joins) {
31193
- for (const [index4, joinMeta] of joins.entries()) {
31194
- if (index4 === 0) {
31195
- joinsArray.push(sql` `);
31196
- }
31197
- const table22 = joinMeta.table;
31198
- if (is(table22, SQLiteTable)) {
31199
- const tableName = table22[SQLiteTable.Symbol.Name];
31200
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31201
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31202
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31203
- joinsArray.push(
31204
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31205
- );
31206
- } else {
31207
- joinsArray.push(
31208
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31209
- );
31210
- }
31211
- if (index4 < joins.length - 1) {
31212
- joinsArray.push(sql` `);
31213
- }
31214
- }
31215
- }
31216
- const joinsSql = sql.join(joinsArray);
31186
+ const tableSql = this.buildFromTable(table4);
31187
+ const joinsSql = this.buildJoins(joins);
31217
31188
  const whereSql = where ? sql` where ${where}` : void 0;
31218
31189
  const havingSql = having ? sql` having ${having}` : void 0;
31219
31190
  const groupByList = [];
@@ -31279,45 +31250,56 @@ var init_dialect2 = __esm({
31279
31250
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31280
31251
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31281
31252
  }
31282
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31253
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31283
31254
  const valuesSqlList = [];
31284
31255
  const columns = table4[Table2.Symbol.Columns];
31285
31256
  const colEntries = Object.entries(columns).filter(
31286
31257
  ([_2, col]) => !col.shouldDisableInsert()
31287
31258
  );
31288
31259
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
31289
- for (const [valueIndex, value] of values.entries()) {
31290
- const valueList = [];
31291
- for (const [fieldName, col] of colEntries) {
31292
- const colValue = value[fieldName];
31293
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31294
- let defaultValue;
31295
- if (col.default !== null && col.default !== void 0) {
31296
- defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31297
- } else if (col.defaultFn !== void 0) {
31298
- const defaultFnResult = col.defaultFn();
31299
- defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31300
- } else if (!col.default && col.onUpdateFn !== void 0) {
31301
- const onUpdateFnResult = col.onUpdateFn();
31302
- defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31260
+ if (select) {
31261
+ const select2 = valuesOrSelect;
31262
+ if (is(select2, SQL)) {
31263
+ valuesSqlList.push(select2);
31264
+ } else {
31265
+ valuesSqlList.push(select2.getSQL());
31266
+ }
31267
+ } else {
31268
+ const values = valuesOrSelect;
31269
+ valuesSqlList.push(sql.raw("values "));
31270
+ for (const [valueIndex, value] of values.entries()) {
31271
+ const valueList = [];
31272
+ for (const [fieldName, col] of colEntries) {
31273
+ const colValue = value[fieldName];
31274
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31275
+ let defaultValue;
31276
+ if (col.default !== null && col.default !== void 0) {
31277
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31278
+ } else if (col.defaultFn !== void 0) {
31279
+ const defaultFnResult = col.defaultFn();
31280
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31281
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31282
+ const onUpdateFnResult = col.onUpdateFn();
31283
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31284
+ } else {
31285
+ defaultValue = sql`null`;
31286
+ }
31287
+ valueList.push(defaultValue);
31303
31288
  } else {
31304
- defaultValue = sql`null`;
31289
+ valueList.push(colValue);
31305
31290
  }
31306
- valueList.push(defaultValue);
31307
- } else {
31308
- valueList.push(colValue);
31309
31291
  }
31310
- }
31311
- valuesSqlList.push(valueList);
31312
- if (valueIndex < values.length - 1) {
31313
- valuesSqlList.push(sql`, `);
31292
+ valuesSqlList.push(valueList);
31293
+ if (valueIndex < values.length - 1) {
31294
+ valuesSqlList.push(sql`, `);
31295
+ }
31314
31296
  }
31315
31297
  }
31316
31298
  const withSql = this.buildWithCTE(withList);
31317
31299
  const valuesSql = sql.join(valuesSqlList);
31318
31300
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31319
31301
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31320
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31302
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31321
31303
  }
31322
31304
  sqlToQuery(sql2, invokeSource) {
31323
31305
  return sql2.toQuery({
@@ -31546,8 +31528,8 @@ var init_dialect2 = __esm({
31546
31528
  };
31547
31529
  }
31548
31530
  };
31549
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31550
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31531
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31532
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31551
31533
  migrate(migrations, session, config) {
31552
31534
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31553
31535
  const migrationTableCreate = sql`
@@ -31581,8 +31563,8 @@ var init_dialect2 = __esm({
31581
31563
  }
31582
31564
  }
31583
31565
  };
31584
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31585
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31566
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31567
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31586
31568
  async migrate(migrations, session, config) {
31587
31569
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31588
31570
  const migrationTableCreate = sql`
@@ -31611,7 +31593,7 @@ var init_dialect2 = __esm({
31611
31593
  });
31612
31594
  }
31613
31595
  };
31614
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31596
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31615
31597
  }
31616
31598
  });
31617
31599
 
@@ -31633,7 +31615,7 @@ function createSetOperator2(type, isAll) {
31633
31615
  return leftSelect.addSetOperators(setOperators);
31634
31616
  };
31635
31617
  }
31636
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31618
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31637
31619
  var init_select3 = __esm({
31638
31620
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31639
31621
  "use strict";
@@ -31647,7 +31629,7 @@ var init_select3 = __esm({
31647
31629
  init_utils2();
31648
31630
  init_view_common();
31649
31631
  init_view_base2();
31650
- _a203 = entityKind;
31632
+ _a201 = entityKind;
31651
31633
  SQLiteSelectBuilder = class {
31652
31634
  constructor(config) {
31653
31635
  __publicField(this, "fields");
@@ -31688,8 +31670,8 @@ var init_select3 = __esm({
31688
31670
  });
31689
31671
  }
31690
31672
  };
31691
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31692
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31673
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31674
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31693
31675
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31694
31676
  super();
31695
31677
  __publicField(this, "_");
@@ -32194,8 +32176,8 @@ var init_select3 = __esm({
32194
32176
  return this;
32195
32177
  }
32196
32178
  };
32197
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32198
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32179
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32180
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32199
32181
  constructor() {
32200
32182
  super(...arguments);
32201
32183
  __publicField(this, "run", (placeholderValues) => {
@@ -32233,7 +32215,7 @@ var init_select3 = __esm({
32233
32215
  return this.all();
32234
32216
  }
32235
32217
  };
32236
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32218
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32237
32219
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32238
32220
  getSQLiteSetOperators = () => ({
32239
32221
  union: union2,
@@ -32249,7 +32231,7 @@ var init_select3 = __esm({
32249
32231
  });
32250
32232
 
32251
32233
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32252
- var _a206, QueryBuilder2;
32234
+ var _a204, QueryBuilder2;
32253
32235
  var init_query_builder3 = __esm({
32254
32236
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32255
32237
  "use strict";
@@ -32258,7 +32240,7 @@ var init_query_builder3 = __esm({
32258
32240
  init_dialect2();
32259
32241
  init_subquery();
32260
32242
  init_select3();
32261
- _a206 = entityKind;
32243
+ _a204 = entityKind;
32262
32244
  QueryBuilder2 = class {
32263
32245
  constructor(dialect4) {
32264
32246
  __publicField(this, "dialect");
@@ -32320,7 +32302,185 @@ var init_query_builder3 = __esm({
32320
32302
  return this.dialect;
32321
32303
  }
32322
32304
  };
32323
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32305
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32306
+ }
32307
+ });
32308
+
32309
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32310
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32311
+ var init_insert2 = __esm({
32312
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32313
+ "use strict";
32314
+ init_entity();
32315
+ init_query_promise();
32316
+ init_sql();
32317
+ init_table3();
32318
+ init_table();
32319
+ init_utils2();
32320
+ init_query_builder3();
32321
+ _a205 = entityKind;
32322
+ SQLiteInsertBuilder = class {
32323
+ constructor(table4, session, dialect4, withList) {
32324
+ this.table = table4;
32325
+ this.session = session;
32326
+ this.dialect = dialect4;
32327
+ this.withList = withList;
32328
+ }
32329
+ values(values) {
32330
+ values = Array.isArray(values) ? values : [values];
32331
+ if (values.length === 0) {
32332
+ throw new Error("values() must be called with at least one value");
32333
+ }
32334
+ const mappedValues = values.map((entry) => {
32335
+ const result = {};
32336
+ const cols = this.table[Table2.Symbol.Columns];
32337
+ for (const colKey of Object.keys(entry)) {
32338
+ const colValue = entry[colKey];
32339
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32340
+ }
32341
+ return result;
32342
+ });
32343
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32344
+ }
32345
+ select(selectQuery) {
32346
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32347
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32348
+ throw new Error(
32349
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32350
+ );
32351
+ }
32352
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32353
+ }
32354
+ };
32355
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32356
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32357
+ constructor(table4, values, session, dialect4, withList, select) {
32358
+ super();
32359
+ /** @internal */
32360
+ __publicField(this, "config");
32361
+ __publicField(this, "run", (placeholderValues) => {
32362
+ return this._prepare().run(placeholderValues);
32363
+ });
32364
+ __publicField(this, "all", (placeholderValues) => {
32365
+ return this._prepare().all(placeholderValues);
32366
+ });
32367
+ __publicField(this, "get", (placeholderValues) => {
32368
+ return this._prepare().get(placeholderValues);
32369
+ });
32370
+ __publicField(this, "values", (placeholderValues) => {
32371
+ return this._prepare().values(placeholderValues);
32372
+ });
32373
+ this.session = session;
32374
+ this.dialect = dialect4;
32375
+ this.config = { table: table4, values, withList, select };
32376
+ }
32377
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32378
+ this.config.returning = orderSelectedFields(fields);
32379
+ return this;
32380
+ }
32381
+ /**
32382
+ * Adds an `on conflict do nothing` clause to the query.
32383
+ *
32384
+ * Calling this method simply avoids inserting a row as its alternative action.
32385
+ *
32386
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32387
+ *
32388
+ * @param config The `target` and `where` clauses.
32389
+ *
32390
+ * @example
32391
+ * ```ts
32392
+ * // Insert one row and cancel the insert if there's a conflict
32393
+ * await db.insert(cars)
32394
+ * .values({ id: 1, brand: 'BMW' })
32395
+ * .onConflictDoNothing();
32396
+ *
32397
+ * // Explicitly specify conflict target
32398
+ * await db.insert(cars)
32399
+ * .values({ id: 1, brand: 'BMW' })
32400
+ * .onConflictDoNothing({ target: cars.id });
32401
+ * ```
32402
+ */
32403
+ onConflictDoNothing(config = {}) {
32404
+ if (config.target === void 0) {
32405
+ this.config.onConflict = sql`do nothing`;
32406
+ } else {
32407
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32408
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32409
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32410
+ }
32411
+ return this;
32412
+ }
32413
+ /**
32414
+ * Adds an `on conflict do update` clause to the query.
32415
+ *
32416
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32417
+ *
32418
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32419
+ *
32420
+ * @param config The `target`, `set` and `where` clauses.
32421
+ *
32422
+ * @example
32423
+ * ```ts
32424
+ * // Update the row if there's a conflict
32425
+ * await db.insert(cars)
32426
+ * .values({ id: 1, brand: 'BMW' })
32427
+ * .onConflictDoUpdate({
32428
+ * target: cars.id,
32429
+ * set: { brand: 'Porsche' }
32430
+ * });
32431
+ *
32432
+ * // Upsert with 'where' clause
32433
+ * await db.insert(cars)
32434
+ * .values({ id: 1, brand: 'BMW' })
32435
+ * .onConflictDoUpdate({
32436
+ * target: cars.id,
32437
+ * set: { brand: 'newBMW' },
32438
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32439
+ * });
32440
+ * ```
32441
+ */
32442
+ onConflictDoUpdate(config) {
32443
+ if (config.where && (config.targetWhere || config.setWhere)) {
32444
+ throw new Error(
32445
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32446
+ );
32447
+ }
32448
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32449
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32450
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32451
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32452
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32453
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32454
+ return this;
32455
+ }
32456
+ /** @internal */
32457
+ getSQL() {
32458
+ return this.dialect.buildInsertQuery(this.config);
32459
+ }
32460
+ toSQL() {
32461
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32462
+ return rest;
32463
+ }
32464
+ /** @internal */
32465
+ _prepare(isOneTimeQuery = true) {
32466
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32467
+ this.dialect.sqlToQuery(this.getSQL()),
32468
+ this.config.returning,
32469
+ this.config.returning ? "all" : "run",
32470
+ true
32471
+ );
32472
+ }
32473
+ prepare() {
32474
+ return this._prepare(false);
32475
+ }
32476
+ async execute() {
32477
+ return this.config.returning ? this.all() : this.run();
32478
+ }
32479
+ $dynamic() {
32480
+ return this;
32481
+ }
32482
+ };
32483
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32324
32484
  }
32325
32485
  });
32326
32486
 
@@ -32340,8 +32500,11 @@ var init_update2 = __esm({
32340
32500
  init_query_promise();
32341
32501
  init_selection_proxy();
32342
32502
  init_table3();
32503
+ init_subquery();
32343
32504
  init_table();
32344
32505
  init_utils2();
32506
+ init_view_common();
32507
+ init_view_base2();
32345
32508
  _a207 = entityKind;
32346
32509
  SQLiteUpdateBuilder = class {
32347
32510
  constructor(table4, session, dialect4, withList) {
@@ -32366,6 +32529,10 @@ var init_update2 = __esm({
32366
32529
  super();
32367
32530
  /** @internal */
32368
32531
  __publicField(this, "config");
32532
+ __publicField(this, "leftJoin", this.createJoin("left"));
32533
+ __publicField(this, "rightJoin", this.createJoin("right"));
32534
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32535
+ __publicField(this, "fullJoin", this.createJoin("full"));
32369
32536
  __publicField(this, "run", (placeholderValues) => {
32370
32537
  return this._prepare().run(placeholderValues);
32371
32538
  });
@@ -32380,7 +32547,34 @@ var init_update2 = __esm({
32380
32547
  });
32381
32548
  this.session = session;
32382
32549
  this.dialect = dialect4;
32383
- this.config = { set, table: table4, withList };
32550
+ this.config = { set, table: table4, withList, joins: [] };
32551
+ }
32552
+ from(source) {
32553
+ this.config.from = source;
32554
+ return this;
32555
+ }
32556
+ createJoin(joinType) {
32557
+ return (table4, on) => {
32558
+ const tableName = getTableLikeName(table4);
32559
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32560
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32561
+ }
32562
+ if (typeof on === "function") {
32563
+ 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;
32564
+ on = on(
32565
+ new Proxy(
32566
+ this.config.table[Table2.Symbol.Columns],
32567
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32568
+ ),
32569
+ from && new Proxy(
32570
+ from,
32571
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32572
+ )
32573
+ );
32574
+ }
32575
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32576
+ return this;
32577
+ };
32384
32578
  }
32385
32579
  /**
32386
32580
  * Adds a 'where' clause to the query.
@@ -35678,131 +35872,6 @@ var init_delete3 = __esm({
35678
35872
  }
35679
35873
  });
35680
35874
 
35681
- // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
35682
- var _a299, MySqlInsertBuilder, _a300, _b222, MySqlInsertBase;
35683
- var init_insert3 = __esm({
35684
- "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
35685
- "use strict";
35686
- init_entity();
35687
- init_query_promise();
35688
- init_sql();
35689
- init_table();
35690
- init_utils2();
35691
- _a299 = entityKind;
35692
- MySqlInsertBuilder = class {
35693
- constructor(table4, session, dialect4) {
35694
- __publicField(this, "shouldIgnore", false);
35695
- this.table = table4;
35696
- this.session = session;
35697
- this.dialect = dialect4;
35698
- }
35699
- ignore() {
35700
- this.shouldIgnore = true;
35701
- return this;
35702
- }
35703
- values(values) {
35704
- values = Array.isArray(values) ? values : [values];
35705
- if (values.length === 0) {
35706
- throw new Error("values() must be called with at least one value");
35707
- }
35708
- const mappedValues = values.map((entry) => {
35709
- const result = {};
35710
- const cols = this.table[Table2.Symbol.Columns];
35711
- for (const colKey of Object.keys(entry)) {
35712
- const colValue = entry[colKey];
35713
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
35714
- }
35715
- return result;
35716
- });
35717
- return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
35718
- }
35719
- };
35720
- __publicField(MySqlInsertBuilder, _a299, "MySqlInsertBuilder");
35721
- MySqlInsertBase = class extends (_b222 = QueryPromise, _a300 = entityKind, _b222) {
35722
- constructor(table4, values, ignore, session, dialect4) {
35723
- super();
35724
- __publicField(this, "config");
35725
- __publicField(this, "execute", (placeholderValues) => {
35726
- return this.prepare().execute(placeholderValues);
35727
- });
35728
- __publicField(this, "createIterator", () => {
35729
- const self2 = this;
35730
- return async function* (placeholderValues) {
35731
- yield* self2.prepare().iterator(placeholderValues);
35732
- };
35733
- });
35734
- __publicField(this, "iterator", this.createIterator());
35735
- this.session = session;
35736
- this.dialect = dialect4;
35737
- this.config = { table: table4, values, ignore };
35738
- }
35739
- /**
35740
- * Adds an `on duplicate key update` clause to the query.
35741
- *
35742
- * 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.
35743
- *
35744
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35745
- *
35746
- * @param config The `set` clause
35747
- *
35748
- * @example
35749
- * ```ts
35750
- * await db.insert(cars)
35751
- * .values({ id: 1, brand: 'BMW'})
35752
- * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
35753
- * ```
35754
- *
35755
- * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
35756
- *
35757
- * ```ts
35758
- * import { sql } from 'drizzle-orm';
35759
- *
35760
- * await db.insert(cars)
35761
- * .values({ id: 1, brand: 'BMW' })
35762
- * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
35763
- * ```
35764
- */
35765
- onDuplicateKeyUpdate(config) {
35766
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
35767
- this.config.onConflict = sql`update ${setSql}`;
35768
- return this;
35769
- }
35770
- $returningId() {
35771
- const returning = [];
35772
- for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
35773
- if (value.primary) {
35774
- returning.push({ field: value, path: [key] });
35775
- }
35776
- }
35777
- this.config.returning = returning;
35778
- return this;
35779
- }
35780
- /** @internal */
35781
- getSQL() {
35782
- return this.dialect.buildInsertQuery(this.config).sql;
35783
- }
35784
- toSQL() {
35785
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
35786
- return rest;
35787
- }
35788
- prepare() {
35789
- const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
35790
- return this.session.prepareQuery(
35791
- this.dialect.sqlToQuery(sql2),
35792
- void 0,
35793
- void 0,
35794
- generatedIds,
35795
- this.config.returning
35796
- );
35797
- }
35798
- $dynamic() {
35799
- return this;
35800
- }
35801
- };
35802
- __publicField(MySqlInsertBase, _a300, "MySqlInsert");
35803
- }
35804
- });
35805
-
35806
35875
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35807
35876
  function getMySqlColumnBuilders() {
35808
35877
  return {
@@ -35883,7 +35952,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35883
35952
  }
35884
35953
  return table4;
35885
35954
  }
35886
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
35955
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35887
35956
  var init_table4 = __esm({
35888
35957
  "../drizzle-orm/dist/mysql-core/table.js"() {
35889
35958
  "use strict";
@@ -35891,15 +35960,15 @@ var init_table4 = __esm({
35891
35960
  init_table();
35892
35961
  init_all3();
35893
35962
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35894
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35963
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35895
35964
  constructor() {
35896
35965
  super(...arguments);
35897
35966
  /** @internal */
35898
35967
  __publicField(this, _c9);
35899
35968
  /** @internal */
35900
- __publicField(this, _b223, []);
35969
+ __publicField(this, _b222, []);
35901
35970
  /** @internal */
35902
- __publicField(this, _a301);
35971
+ __publicField(this, _a299);
35903
35972
  }
35904
35973
  };
35905
35974
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35914,20 +35983,20 @@ var init_table4 = __esm({
35914
35983
  });
35915
35984
 
35916
35985
  // ../drizzle-orm/dist/mysql-core/view-base.js
35917
- var _a302, _b224, MySqlViewBase;
35986
+ var _a300, _b223, MySqlViewBase;
35918
35987
  var init_view_base3 = __esm({
35919
35988
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35920
35989
  "use strict";
35921
35990
  init_entity();
35922
35991
  init_sql();
35923
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
35992
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35924
35993
  };
35925
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
35994
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35926
35995
  }
35927
35996
  });
35928
35997
 
35929
35998
  // ../drizzle-orm/dist/mysql-core/dialect.js
35930
- var _a303, MySqlDialect;
35999
+ var _a301, MySqlDialect;
35931
36000
  var init_dialect3 = __esm({
35932
36001
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35933
36002
  "use strict";
@@ -35946,7 +36015,7 @@ var init_dialect3 = __esm({
35946
36015
  init_common4();
35947
36016
  init_table4();
35948
36017
  init_view_base3();
35949
- _a303 = entityKind;
36018
+ _a301 = entityKind;
35950
36019
  MySqlDialect = class {
35951
36020
  constructor(config) {
35952
36021
  /** @internal */
@@ -36229,7 +36298,7 @@ var init_dialect3 = __esm({
36229
36298
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36230
36299
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36231
36300
  }
36232
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36301
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36233
36302
  const valuesSqlList = [];
36234
36303
  const columns = table4[Table2.Symbol.Columns];
36235
36304
  const colEntries = Object.entries(columns).filter(
@@ -36237,42 +36306,53 @@ var init_dialect3 = __esm({
36237
36306
  );
36238
36307
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36239
36308
  const generatedIdsResponse = [];
36240
- for (const [valueIndex, value] of values.entries()) {
36241
- const generatedIds = {};
36242
- const valueList = [];
36243
- for (const [fieldName, col] of colEntries) {
36244
- const colValue = value[fieldName];
36245
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36246
- if (col.defaultFn !== void 0) {
36247
- const defaultFnResult = col.defaultFn();
36248
- generatedIds[fieldName] = defaultFnResult;
36249
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36250
- valueList.push(defaultValue);
36251
- } else if (!col.default && col.onUpdateFn !== void 0) {
36252
- const onUpdateFnResult = col.onUpdateFn();
36253
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36254
- valueList.push(newValue);
36309
+ if (select) {
36310
+ const select2 = valuesOrSelect;
36311
+ if (is(select2, SQL)) {
36312
+ valuesSqlList.push(select2);
36313
+ } else {
36314
+ valuesSqlList.push(select2.getSQL());
36315
+ }
36316
+ } else {
36317
+ const values = valuesOrSelect;
36318
+ valuesSqlList.push(sql.raw("values "));
36319
+ for (const [valueIndex, value] of values.entries()) {
36320
+ const generatedIds = {};
36321
+ const valueList = [];
36322
+ for (const [fieldName, col] of colEntries) {
36323
+ const colValue = value[fieldName];
36324
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36325
+ if (col.defaultFn !== void 0) {
36326
+ const defaultFnResult = col.defaultFn();
36327
+ generatedIds[fieldName] = defaultFnResult;
36328
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36329
+ valueList.push(defaultValue);
36330
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36331
+ const onUpdateFnResult = col.onUpdateFn();
36332
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36333
+ valueList.push(newValue);
36334
+ } else {
36335
+ valueList.push(sql`default`);
36336
+ }
36255
36337
  } else {
36256
- valueList.push(sql`default`);
36257
- }
36258
- } else {
36259
- if (col.defaultFn && is(colValue, Param)) {
36260
- generatedIds[fieldName] = colValue.value;
36338
+ if (col.defaultFn && is(colValue, Param)) {
36339
+ generatedIds[fieldName] = colValue.value;
36340
+ }
36341
+ valueList.push(colValue);
36261
36342
  }
36262
- valueList.push(colValue);
36263
36343
  }
36264
- }
36265
- generatedIdsResponse.push(generatedIds);
36266
- valuesSqlList.push(valueList);
36267
- if (valueIndex < values.length - 1) {
36268
- valuesSqlList.push(sql`, `);
36344
+ generatedIdsResponse.push(generatedIds);
36345
+ valuesSqlList.push(valueList);
36346
+ if (valueIndex < values.length - 1) {
36347
+ valuesSqlList.push(sql`, `);
36348
+ }
36269
36349
  }
36270
36350
  }
36271
36351
  const valuesSql = sql.join(valuesSqlList);
36272
36352
  const ignoreSql = ignore ? sql` ignore` : void 0;
36273
36353
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36274
36354
  return {
36275
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36355
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36276
36356
  generatedIds: generatedIdsResponse
36277
36357
  };
36278
36358
  }
@@ -36732,7 +36812,7 @@ var init_dialect3 = __esm({
36732
36812
  };
36733
36813
  }
36734
36814
  };
36735
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36815
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36736
36816
  }
36737
36817
  });
36738
36818
 
@@ -36754,7 +36834,7 @@ function createSetOperator3(type, isAll) {
36754
36834
  return leftSelect.addSetOperators(setOperators);
36755
36835
  };
36756
36836
  }
36757
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36837
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36758
36838
  var init_select4 = __esm({
36759
36839
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36760
36840
  "use strict";
@@ -36769,7 +36849,7 @@ var init_select4 = __esm({
36769
36849
  init_utils2();
36770
36850
  init_view_common();
36771
36851
  init_view_base3();
36772
- _a304 = entityKind;
36852
+ _a302 = entityKind;
36773
36853
  MySqlSelectBuilder = class {
36774
36854
  constructor(config) {
36775
36855
  __publicField(this, "fields");
@@ -36814,8 +36894,8 @@ var init_select4 = __esm({
36814
36894
  );
36815
36895
  }
36816
36896
  };
36817
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36818
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36897
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36898
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36819
36899
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36820
36900
  super();
36821
36901
  __publicField(this, "_");
@@ -37416,8 +37496,8 @@ var init_select4 = __esm({
37416
37496
  return this;
37417
37497
  }
37418
37498
  };
37419
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37420
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37499
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37500
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37421
37501
  constructor() {
37422
37502
  super(...arguments);
37423
37503
  __publicField(this, "execute", (placeholderValues) => {
@@ -37441,7 +37521,7 @@ var init_select4 = __esm({
37441
37521
  return query;
37442
37522
  }
37443
37523
  };
37444
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37524
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37445
37525
  applyMixins(MySqlSelectBase, [QueryPromise]);
37446
37526
  getMySqlSetOperators = () => ({
37447
37527
  union: union3,
@@ -37461,7 +37541,7 @@ var init_select4 = __esm({
37461
37541
  });
37462
37542
 
37463
37543
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37464
- var _a307, QueryBuilder3;
37544
+ var _a305, QueryBuilder3;
37465
37545
  var init_query_builder4 = __esm({
37466
37546
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37467
37547
  "use strict";
@@ -37470,7 +37550,7 @@ var init_query_builder4 = __esm({
37470
37550
  init_selection_proxy();
37471
37551
  init_subquery();
37472
37552
  init_select4();
37473
- _a307 = entityKind;
37553
+ _a305 = entityKind;
37474
37554
  QueryBuilder3 = class {
37475
37555
  constructor(dialect4) {
37476
37556
  __publicField(this, "dialect");
@@ -37532,7 +37612,142 @@ var init_query_builder4 = __esm({
37532
37612
  return this.dialect;
37533
37613
  }
37534
37614
  };
37535
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37615
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37616
+ }
37617
+ });
37618
+
37619
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37620
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37621
+ var init_insert3 = __esm({
37622
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37623
+ "use strict";
37624
+ init_entity();
37625
+ init_query_promise();
37626
+ init_sql();
37627
+ init_table();
37628
+ init_utils2();
37629
+ init_query_builder4();
37630
+ _a306 = entityKind;
37631
+ MySqlInsertBuilder = class {
37632
+ constructor(table4, session, dialect4) {
37633
+ __publicField(this, "shouldIgnore", false);
37634
+ this.table = table4;
37635
+ this.session = session;
37636
+ this.dialect = dialect4;
37637
+ }
37638
+ ignore() {
37639
+ this.shouldIgnore = true;
37640
+ return this;
37641
+ }
37642
+ values(values) {
37643
+ values = Array.isArray(values) ? values : [values];
37644
+ if (values.length === 0) {
37645
+ throw new Error("values() must be called with at least one value");
37646
+ }
37647
+ const mappedValues = values.map((entry) => {
37648
+ const result = {};
37649
+ const cols = this.table[Table2.Symbol.Columns];
37650
+ for (const colKey of Object.keys(entry)) {
37651
+ const colValue = entry[colKey];
37652
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37653
+ }
37654
+ return result;
37655
+ });
37656
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37657
+ }
37658
+ select(selectQuery) {
37659
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37660
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37661
+ throw new Error(
37662
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37663
+ );
37664
+ }
37665
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37666
+ }
37667
+ };
37668
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37669
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37670
+ constructor(table4, values, ignore, session, dialect4, select) {
37671
+ super();
37672
+ __publicField(this, "config");
37673
+ __publicField(this, "execute", (placeholderValues) => {
37674
+ return this.prepare().execute(placeholderValues);
37675
+ });
37676
+ __publicField(this, "createIterator", () => {
37677
+ const self2 = this;
37678
+ return async function* (placeholderValues) {
37679
+ yield* self2.prepare().iterator(placeholderValues);
37680
+ };
37681
+ });
37682
+ __publicField(this, "iterator", this.createIterator());
37683
+ this.session = session;
37684
+ this.dialect = dialect4;
37685
+ this.config = { table: table4, values, select, ignore };
37686
+ }
37687
+ /**
37688
+ * Adds an `on duplicate key update` clause to the query.
37689
+ *
37690
+ * 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.
37691
+ *
37692
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37693
+ *
37694
+ * @param config The `set` clause
37695
+ *
37696
+ * @example
37697
+ * ```ts
37698
+ * await db.insert(cars)
37699
+ * .values({ id: 1, brand: 'BMW'})
37700
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37701
+ * ```
37702
+ *
37703
+ * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
37704
+ *
37705
+ * ```ts
37706
+ * import { sql } from 'drizzle-orm';
37707
+ *
37708
+ * await db.insert(cars)
37709
+ * .values({ id: 1, brand: 'BMW' })
37710
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37711
+ * ```
37712
+ */
37713
+ onDuplicateKeyUpdate(config) {
37714
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37715
+ this.config.onConflict = sql`update ${setSql}`;
37716
+ return this;
37717
+ }
37718
+ $returningId() {
37719
+ const returning = [];
37720
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37721
+ if (value.primary) {
37722
+ returning.push({ field: value, path: [key] });
37723
+ }
37724
+ }
37725
+ this.config.returning = returning;
37726
+ return this;
37727
+ }
37728
+ /** @internal */
37729
+ getSQL() {
37730
+ return this.dialect.buildInsertQuery(this.config).sql;
37731
+ }
37732
+ toSQL() {
37733
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37734
+ return rest;
37735
+ }
37736
+ prepare() {
37737
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37738
+ return this.session.prepareQuery(
37739
+ this.dialect.sqlToQuery(sql2),
37740
+ void 0,
37741
+ void 0,
37742
+ generatedIds,
37743
+ this.config.returning
37744
+ );
37745
+ }
37746
+ $dynamic() {
37747
+ return this;
37748
+ }
37749
+ };
37750
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37536
37751
  }
37537
37752
  });
37538
37753