drizzle-kit 0.28.0-cc4f208 → 0.28.1-cadba24

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.mjs CHANGED
@@ -21126,7 +21126,7 @@ var version;
21126
21126
  var init_version = __esm({
21127
21127
  "../drizzle-orm/dist/version.js"() {
21128
21128
  "use strict";
21129
- version = "0.36.1";
21129
+ version = "0.36.2";
21130
21130
  }
21131
21131
  });
21132
21132
 
@@ -22149,7 +22149,7 @@ function haveSameKeys(left, right) {
22149
22149
  }
22150
22150
  function mapUpdateSet(table4, values) {
22151
22151
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22152
- if (is(value, SQL)) {
22152
+ if (is(value, SQL) || is(value, Column2)) {
22153
22153
  return [key, value];
22154
22154
  } else {
22155
22155
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24659,162 +24659,6 @@ var init_delete = __esm({
24659
24659
  }
24660
24660
  });
24661
24661
 
24662
- // ../drizzle-orm/dist/pg-core/query-builders/insert.js
24663
- var _a125, PgInsertBuilder, _a126, _b98, PgInsertBase;
24664
- var init_insert = __esm({
24665
- "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
24666
- "use strict";
24667
- init_entity();
24668
- init_query_promise();
24669
- init_sql();
24670
- init_table();
24671
- init_tracing();
24672
- init_utils2();
24673
- _a125 = entityKind;
24674
- PgInsertBuilder = class {
24675
- constructor(table4, session, dialect4, withList) {
24676
- this.table = table4;
24677
- this.session = session;
24678
- this.dialect = dialect4;
24679
- this.withList = withList;
24680
- }
24681
- values(values) {
24682
- values = Array.isArray(values) ? values : [values];
24683
- if (values.length === 0) {
24684
- throw new Error("values() must be called with at least one value");
24685
- }
24686
- const mappedValues = values.map((entry) => {
24687
- const result = {};
24688
- const cols = this.table[Table2.Symbol.Columns];
24689
- for (const colKey of Object.keys(entry)) {
24690
- const colValue = entry[colKey];
24691
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
24692
- }
24693
- return result;
24694
- });
24695
- return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
24696
- }
24697
- };
24698
- __publicField(PgInsertBuilder, _a125, "PgInsertBuilder");
24699
- PgInsertBase = class extends (_b98 = QueryPromise, _a126 = entityKind, _b98) {
24700
- constructor(table4, values, session, dialect4, withList) {
24701
- super();
24702
- __publicField(this, "config");
24703
- __publicField(this, "execute", (placeholderValues) => {
24704
- return tracer.startActiveSpan("drizzle.operation", () => {
24705
- return this._prepare().execute(placeholderValues);
24706
- });
24707
- });
24708
- this.session = session;
24709
- this.dialect = dialect4;
24710
- this.config = { table: table4, values, withList };
24711
- }
24712
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
24713
- this.config.returning = orderSelectedFields(fields);
24714
- return this;
24715
- }
24716
- /**
24717
- * Adds an `on conflict do nothing` clause to the query.
24718
- *
24719
- * Calling this method simply avoids inserting a row as its alternative action.
24720
- *
24721
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
24722
- *
24723
- * @param config The `target` and `where` clauses.
24724
- *
24725
- * @example
24726
- * ```ts
24727
- * // Insert one row and cancel the insert if there's a conflict
24728
- * await db.insert(cars)
24729
- * .values({ id: 1, brand: 'BMW' })
24730
- * .onConflictDoNothing();
24731
- *
24732
- * // Explicitly specify conflict target
24733
- * await db.insert(cars)
24734
- * .values({ id: 1, brand: 'BMW' })
24735
- * .onConflictDoNothing({ target: cars.id });
24736
- * ```
24737
- */
24738
- onConflictDoNothing(config = {}) {
24739
- if (config.target === void 0) {
24740
- this.config.onConflict = sql`do nothing`;
24741
- } else {
24742
- let targetColumn = "";
24743
- 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));
24744
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24745
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
24746
- }
24747
- return this;
24748
- }
24749
- /**
24750
- * Adds an `on conflict do update` clause to the query.
24751
- *
24752
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
24753
- *
24754
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
24755
- *
24756
- * @param config The `target`, `set` and `where` clauses.
24757
- *
24758
- * @example
24759
- * ```ts
24760
- * // Update the row if there's a conflict
24761
- * await db.insert(cars)
24762
- * .values({ id: 1, brand: 'BMW' })
24763
- * .onConflictDoUpdate({
24764
- * target: cars.id,
24765
- * set: { brand: 'Porsche' }
24766
- * });
24767
- *
24768
- * // Upsert with 'where' clause
24769
- * await db.insert(cars)
24770
- * .values({ id: 1, brand: 'BMW' })
24771
- * .onConflictDoUpdate({
24772
- * target: cars.id,
24773
- * set: { brand: 'newBMW' },
24774
- * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
24775
- * });
24776
- * ```
24777
- */
24778
- onConflictDoUpdate(config) {
24779
- if (config.where && (config.targetWhere || config.setWhere)) {
24780
- throw new Error(
24781
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
24782
- );
24783
- }
24784
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24785
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
24786
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
24787
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
24788
- let targetColumn = "";
24789
- 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));
24790
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
24791
- return this;
24792
- }
24793
- /** @internal */
24794
- getSQL() {
24795
- return this.dialect.buildInsertQuery(this.config);
24796
- }
24797
- toSQL() {
24798
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
24799
- return rest;
24800
- }
24801
- /** @internal */
24802
- _prepare(name2) {
24803
- return tracer.startActiveSpan("drizzle.prepareQuery", () => {
24804
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
24805
- });
24806
- }
24807
- prepare(name2) {
24808
- return this._prepare(name2);
24809
- }
24810
- $dynamic() {
24811
- return this;
24812
- }
24813
- };
24814
- __publicField(PgInsertBase, _a126, "PgInsert");
24815
- }
24816
- });
24817
-
24818
24662
  // ../drizzle-orm/dist/casing.js
24819
24663
  function toSnakeCase(input) {
24820
24664
  const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
@@ -24830,13 +24674,13 @@ function toCamelCase(input) {
24830
24674
  function noopCase(input) {
24831
24675
  return input;
24832
24676
  }
24833
- var _a127, CasingCache;
24677
+ var _a125, CasingCache;
24834
24678
  var init_casing = __esm({
24835
24679
  "../drizzle-orm/dist/casing.js"() {
24836
24680
  "use strict";
24837
24681
  init_entity();
24838
24682
  init_table();
24839
- _a127 = entityKind;
24683
+ _a125 = entityKind;
24840
24684
  CasingCache = class {
24841
24685
  constructor(casing2) {
24842
24686
  /** @internal */
@@ -24873,25 +24717,25 @@ var init_casing = __esm({
24873
24717
  this.cachedTables = {};
24874
24718
  }
24875
24719
  };
24876
- __publicField(CasingCache, _a127, "CasingCache");
24720
+ __publicField(CasingCache, _a125, "CasingCache");
24877
24721
  }
24878
24722
  });
24879
24723
 
24880
24724
  // ../drizzle-orm/dist/pg-core/view-base.js
24881
- var _a128, _b99, PgViewBase;
24725
+ var _a126, _b98, PgViewBase;
24882
24726
  var init_view_base = __esm({
24883
24727
  "../drizzle-orm/dist/pg-core/view-base.js"() {
24884
24728
  "use strict";
24885
24729
  init_entity();
24886
24730
  init_sql();
24887
- PgViewBase = class extends (_b99 = View3, _a128 = entityKind, _b99) {
24731
+ PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
24888
24732
  };
24889
- __publicField(PgViewBase, _a128, "PgViewBase");
24733
+ __publicField(PgViewBase, _a126, "PgViewBase");
24890
24734
  }
24891
24735
  });
24892
24736
 
24893
24737
  // ../drizzle-orm/dist/pg-core/dialect.js
24894
- var _a129, PgDialect;
24738
+ var _a127, PgDialect;
24895
24739
  var init_dialect = __esm({
24896
24740
  "../drizzle-orm/dist/pg-core/dialect.js"() {
24897
24741
  "use strict";
@@ -24910,7 +24754,7 @@ var init_dialect = __esm({
24910
24754
  init_utils2();
24911
24755
  init_view_common();
24912
24756
  init_view_base();
24913
- _a129 = entityKind;
24757
+ _a127 = entityKind;
24914
24758
  PgDialect = class {
24915
24759
  constructor(config) {
24916
24760
  /** @internal */
@@ -24990,12 +24834,19 @@ var init_dialect = __esm({
24990
24834
  return [res];
24991
24835
  }));
24992
24836
  }
24993
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24837
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24994
24838
  const withSql = this.buildWithCTE(withList);
24839
+ const tableName = table4[PgTable.Symbol.Name];
24840
+ const tableSchema = table4[PgTable.Symbol.Schema];
24841
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24842
+ const alias = tableName === origTableName ? void 0 : tableName;
24843
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
24995
24844
  const setSql = this.buildUpdateSet(table4, set);
24996
- const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
24845
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
24846
+ const joinsSql = this.buildJoins(joins);
24847
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
24997
24848
  const whereSql = where ? sql` where ${where}` : void 0;
24998
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
24849
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24999
24850
  }
25000
24851
  /**
25001
24852
  * Builds selection SQL with provided fields/expressions
@@ -25047,6 +24898,54 @@ var init_dialect = __esm({
25047
24898
  });
25048
24899
  return sql.join(chunks);
25049
24900
  }
24901
+ buildJoins(joins) {
24902
+ if (!joins || joins.length === 0) {
24903
+ return void 0;
24904
+ }
24905
+ const joinsArray = [];
24906
+ for (const [index4, joinMeta] of joins.entries()) {
24907
+ if (index4 === 0) {
24908
+ joinsArray.push(sql` `);
24909
+ }
24910
+ const table4 = joinMeta.table;
24911
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
24912
+ if (is(table4, PgTable)) {
24913
+ const tableName = table4[PgTable.Symbol.Name];
24914
+ const tableSchema = table4[PgTable.Symbol.Schema];
24915
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24916
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
24917
+ joinsArray.push(
24918
+ 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}`
24919
+ );
24920
+ } else if (is(table4, View3)) {
24921
+ const viewName = table4[ViewBaseConfig].name;
24922
+ const viewSchema = table4[ViewBaseConfig].schema;
24923
+ const origViewName = table4[ViewBaseConfig].originalName;
24924
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
24925
+ joinsArray.push(
24926
+ 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}`
24927
+ );
24928
+ } else {
24929
+ joinsArray.push(
24930
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table4} on ${joinMeta.on}`
24931
+ );
24932
+ }
24933
+ if (index4 < joins.length - 1) {
24934
+ joinsArray.push(sql` `);
24935
+ }
24936
+ }
24937
+ return sql.join(joinsArray);
24938
+ }
24939
+ buildFromTable(table4) {
24940
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
24941
+ let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
24942
+ if (table4[Table2.Symbol.Schema]) {
24943
+ fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
24944
+ }
24945
+ return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
24946
+ }
24947
+ return table4;
24948
+ }
25050
24949
  buildSelectQuery({
25051
24950
  withList,
25052
24951
  fields,
@@ -25081,51 +24980,8 @@ var init_dialect = __esm({
25081
24980
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25082
24981
  }
25083
24982
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25084
- const tableSql = (() => {
25085
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25086
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25087
- if (table4[Table2.Symbol.Schema]) {
25088
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25089
- }
25090
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25091
- }
25092
- return table4;
25093
- })();
25094
- const joinsArray = [];
25095
- if (joins) {
25096
- for (const [index4, joinMeta] of joins.entries()) {
25097
- if (index4 === 0) {
25098
- joinsArray.push(sql` `);
25099
- }
25100
- const table22 = joinMeta.table;
25101
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25102
- if (is(table22, PgTable)) {
25103
- const tableName = table22[PgTable.Symbol.Name];
25104
- const tableSchema = table22[PgTable.Symbol.Schema];
25105
- const origTableName = table22[PgTable.Symbol.OriginalName];
25106
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25107
- joinsArray.push(
25108
- 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}`
25109
- );
25110
- } else if (is(table22, View3)) {
25111
- const viewName = table22[ViewBaseConfig].name;
25112
- const viewSchema = table22[ViewBaseConfig].schema;
25113
- const origViewName = table22[ViewBaseConfig].originalName;
25114
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25115
- joinsArray.push(
25116
- 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}`
25117
- );
25118
- } else {
25119
- joinsArray.push(
25120
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25121
- );
25122
- }
25123
- if (index4 < joins.length - 1) {
25124
- joinsArray.push(sql` `);
25125
- }
25126
- }
25127
- }
25128
- const joinsSql = sql.join(joinsArray);
24983
+ const tableSql = this.buildFromTable(table4);
24984
+ const joinsSql = this.buildJoins(joins);
25129
24985
  const whereSql = where ? sql` where ${where}` : void 0;
25130
24986
  const havingSql = having ? sql` having ${having}` : void 0;
25131
24987
  let orderBySql;
@@ -25206,43 +25062,54 @@ var init_dialect = __esm({
25206
25062
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
25207
25063
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
25208
25064
  }
25209
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
25065
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
25210
25066
  const valuesSqlList = [];
25211
25067
  const columns = table4[Table2.Symbol.Columns];
25212
25068
  const colEntries = Object.entries(columns).filter(([_2, col]) => !col.shouldDisableInsert());
25213
25069
  const insertOrder = colEntries.map(
25214
25070
  ([, column4]) => sql.identifier(this.casing.getColumnCasing(column4))
25215
25071
  );
25216
- for (const [valueIndex, value] of values.entries()) {
25217
- const valueList = [];
25218
- for (const [fieldName, col] of colEntries) {
25219
- const colValue = value[fieldName];
25220
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25221
- if (col.defaultFn !== void 0) {
25222
- const defaultFnResult = col.defaultFn();
25223
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25224
- valueList.push(defaultValue);
25225
- } else if (!col.default && col.onUpdateFn !== void 0) {
25226
- const onUpdateFnResult = col.onUpdateFn();
25227
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25228
- valueList.push(newValue);
25072
+ if (select) {
25073
+ const select2 = valuesOrSelect;
25074
+ if (is(select2, SQL)) {
25075
+ valuesSqlList.push(select2);
25076
+ } else {
25077
+ valuesSqlList.push(select2.getSQL());
25078
+ }
25079
+ } else {
25080
+ const values = valuesOrSelect;
25081
+ valuesSqlList.push(sql.raw("values "));
25082
+ for (const [valueIndex, value] of values.entries()) {
25083
+ const valueList = [];
25084
+ for (const [fieldName, col] of colEntries) {
25085
+ const colValue = value[fieldName];
25086
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25087
+ if (col.defaultFn !== void 0) {
25088
+ const defaultFnResult = col.defaultFn();
25089
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25090
+ valueList.push(defaultValue);
25091
+ } else if (!col.default && col.onUpdateFn !== void 0) {
25092
+ const onUpdateFnResult = col.onUpdateFn();
25093
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25094
+ valueList.push(newValue);
25095
+ } else {
25096
+ valueList.push(sql`default`);
25097
+ }
25229
25098
  } else {
25230
- valueList.push(sql`default`);
25099
+ valueList.push(colValue);
25231
25100
  }
25232
- } else {
25233
- valueList.push(colValue);
25234
25101
  }
25235
- }
25236
- valuesSqlList.push(valueList);
25237
- if (valueIndex < values.length - 1) {
25238
- valuesSqlList.push(sql`, `);
25102
+ valuesSqlList.push(valueList);
25103
+ if (valueIndex < values.length - 1) {
25104
+ valuesSqlList.push(sql`, `);
25105
+ }
25239
25106
  }
25240
25107
  }
25241
25108
  const withSql = this.buildWithCTE(withList);
25242
25109
  const valuesSql = sql.join(valuesSqlList);
25243
25110
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25244
25111
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
25245
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
25112
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
25246
25113
  }
25247
25114
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25248
25115
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25978,12 +25845,12 @@ var init_dialect = __esm({
25978
25845
  };
25979
25846
  }
25980
25847
  };
25981
- __publicField(PgDialect, _a129, "PgDialect");
25848
+ __publicField(PgDialect, _a127, "PgDialect");
25982
25849
  }
25983
25850
  });
25984
25851
 
25985
25852
  // ../drizzle-orm/dist/selection-proxy.js
25986
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25853
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25987
25854
  var init_selection_proxy = __esm({
25988
25855
  "../drizzle-orm/dist/selection-proxy.js"() {
25989
25856
  "use strict";
@@ -25993,7 +25860,7 @@ var init_selection_proxy = __esm({
25993
25860
  init_sql();
25994
25861
  init_subquery();
25995
25862
  init_view_common();
25996
- _a130 = entityKind;
25863
+ _a128 = entityKind;
25997
25864
  _SelectionProxyHandler = class _SelectionProxyHandler {
25998
25865
  constructor(config) {
25999
25866
  __publicField(this, "config");
@@ -26059,25 +25926,25 @@ var init_selection_proxy = __esm({
26059
25926
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26060
25927
  }
26061
25928
  };
26062
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25929
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26063
25930
  SelectionProxyHandler = _SelectionProxyHandler;
26064
25931
  }
26065
25932
  });
26066
25933
 
26067
25934
  // ../drizzle-orm/dist/query-builders/query-builder.js
26068
- var _a131, TypedQueryBuilder;
25935
+ var _a129, TypedQueryBuilder;
26069
25936
  var init_query_builder = __esm({
26070
25937
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26071
25938
  "use strict";
26072
25939
  init_entity();
26073
- _a131 = entityKind;
25940
+ _a129 = entityKind;
26074
25941
  TypedQueryBuilder = class {
26075
25942
  /** @internal */
26076
25943
  getSelectedFields() {
26077
25944
  return this._.selectedFields;
26078
25945
  }
26079
25946
  };
26080
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25947
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26081
25948
  }
26082
25949
  });
26083
25950
 
@@ -26099,7 +25966,7 @@ function createSetOperator(type, isAll) {
26099
25966
  return leftSelect.addSetOperators(setOperators);
26100
25967
  };
26101
25968
  }
26102
- var _a132, PgSelectBuilder, _a133, _b100, PgSelectQueryBuilderBase, _a134, _b101, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
25969
+ var _a130, PgSelectBuilder, _a131, _b99, PgSelectQueryBuilderBase, _a132, _b100, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
26103
25970
  var init_select2 = __esm({
26104
25971
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26105
25972
  "use strict";
@@ -26115,7 +25982,7 @@ var init_select2 = __esm({
26115
25982
  init_utils2();
26116
25983
  init_utils2();
26117
25984
  init_view_common();
26118
- _a132 = entityKind;
25985
+ _a130 = entityKind;
26119
25986
  PgSelectBuilder = class {
26120
25987
  constructor(config) {
26121
25988
  __publicField(this, "fields");
@@ -26164,8 +26031,8 @@ var init_select2 = __esm({
26164
26031
  });
26165
26032
  }
26166
26033
  };
26167
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26168
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26034
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26035
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26169
26036
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26170
26037
  super();
26171
26038
  __publicField(this, "_");
@@ -26765,8 +26632,8 @@ var init_select2 = __esm({
26765
26632
  return this;
26766
26633
  }
26767
26634
  };
26768
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26769
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26635
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26636
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26770
26637
  constructor() {
26771
26638
  super(...arguments);
26772
26639
  __publicField(this, "execute", (placeholderValues) => {
@@ -26799,7 +26666,7 @@ var init_select2 = __esm({
26799
26666
  return this._prepare(name2);
26800
26667
  }
26801
26668
  };
26802
- __publicField(PgSelectBase, _a134, "PgSelect");
26669
+ __publicField(PgSelectBase, _a132, "PgSelect");
26803
26670
  applyMixins(PgSelectBase, [QueryPromise]);
26804
26671
  getPgSetOperators = () => ({
26805
26672
  union,
@@ -26819,7 +26686,7 @@ var init_select2 = __esm({
26819
26686
  });
26820
26687
 
26821
26688
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26822
- var _a135, QueryBuilder;
26689
+ var _a133, QueryBuilder;
26823
26690
  var init_query_builder2 = __esm({
26824
26691
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26825
26692
  "use strict";
@@ -26828,7 +26695,7 @@ var init_query_builder2 = __esm({
26828
26695
  init_selection_proxy();
26829
26696
  init_subquery();
26830
26697
  init_select2();
26831
- _a135 = entityKind;
26698
+ _a133 = entityKind;
26832
26699
  QueryBuilder = class {
26833
26700
  constructor(dialect4) {
26834
26701
  __publicField(this, "dialect");
@@ -26909,41 +26776,207 @@ var init_query_builder2 = __esm({
26909
26776
  return this.dialect;
26910
26777
  }
26911
26778
  };
26912
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26779
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26913
26780
  }
26914
26781
  });
26915
26782
 
26916
- // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26917
- var _a136, _b102, PgRefreshMaterializedView;
26918
- var init_refresh_materialized_view = __esm({
26919
- "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26783
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26784
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26785
+ var init_insert = __esm({
26786
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26920
26787
  "use strict";
26921
26788
  init_entity();
26922
26789
  init_query_promise();
26790
+ init_sql();
26791
+ init_table();
26923
26792
  init_tracing();
26924
- PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26925
- constructor(view4, session, dialect4) {
26926
- super();
26927
- __publicField(this, "config");
26928
- __publicField(this, "execute", (placeholderValues) => {
26929
- return tracer.startActiveSpan("drizzle.operation", () => {
26930
- return this._prepare().execute(placeholderValues);
26931
- });
26932
- });
26793
+ init_utils2();
26794
+ init_query_builder2();
26795
+ _a134 = entityKind;
26796
+ PgInsertBuilder = class {
26797
+ constructor(table4, session, dialect4, withList) {
26798
+ this.table = table4;
26933
26799
  this.session = session;
26934
26800
  this.dialect = dialect4;
26935
- this.config = { view: view4 };
26801
+ this.withList = withList;
26936
26802
  }
26937
- concurrently() {
26938
- if (this.config.withNoData !== void 0) {
26939
- throw new Error("Cannot use concurrently and withNoData together");
26803
+ values(values) {
26804
+ values = Array.isArray(values) ? values : [values];
26805
+ if (values.length === 0) {
26806
+ throw new Error("values() must be called with at least one value");
26940
26807
  }
26941
- this.config.concurrently = true;
26942
- return this;
26943
- }
26944
- withNoData() {
26945
- if (this.config.concurrently !== void 0) {
26946
- throw new Error("Cannot use concurrently and withNoData together");
26808
+ const mappedValues = values.map((entry) => {
26809
+ const result = {};
26810
+ const cols = this.table[Table2.Symbol.Columns];
26811
+ for (const colKey of Object.keys(entry)) {
26812
+ const colValue = entry[colKey];
26813
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26814
+ }
26815
+ return result;
26816
+ });
26817
+ return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
26818
+ }
26819
+ select(selectQuery) {
26820
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26821
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26822
+ throw new Error(
26823
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26824
+ );
26825
+ }
26826
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26827
+ }
26828
+ };
26829
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26830
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26831
+ constructor(table4, values, session, dialect4, withList, select) {
26832
+ super();
26833
+ __publicField(this, "config");
26834
+ __publicField(this, "execute", (placeholderValues) => {
26835
+ return tracer.startActiveSpan("drizzle.operation", () => {
26836
+ return this._prepare().execute(placeholderValues);
26837
+ });
26838
+ });
26839
+ this.session = session;
26840
+ this.dialect = dialect4;
26841
+ this.config = { table: table4, values, withList, select };
26842
+ }
26843
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26844
+ this.config.returning = orderSelectedFields(fields);
26845
+ return this;
26846
+ }
26847
+ /**
26848
+ * Adds an `on conflict do nothing` clause to the query.
26849
+ *
26850
+ * Calling this method simply avoids inserting a row as its alternative action.
26851
+ *
26852
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26853
+ *
26854
+ * @param config The `target` and `where` clauses.
26855
+ *
26856
+ * @example
26857
+ * ```ts
26858
+ * // Insert one row and cancel the insert if there's a conflict
26859
+ * await db.insert(cars)
26860
+ * .values({ id: 1, brand: 'BMW' })
26861
+ * .onConflictDoNothing();
26862
+ *
26863
+ * // Explicitly specify conflict target
26864
+ * await db.insert(cars)
26865
+ * .values({ id: 1, brand: 'BMW' })
26866
+ * .onConflictDoNothing({ target: cars.id });
26867
+ * ```
26868
+ */
26869
+ onConflictDoNothing(config = {}) {
26870
+ if (config.target === void 0) {
26871
+ this.config.onConflict = sql`do nothing`;
26872
+ } else {
26873
+ let targetColumn = "";
26874
+ 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));
26875
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26876
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26877
+ }
26878
+ return this;
26879
+ }
26880
+ /**
26881
+ * Adds an `on conflict do update` clause to the query.
26882
+ *
26883
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26884
+ *
26885
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26886
+ *
26887
+ * @param config The `target`, `set` and `where` clauses.
26888
+ *
26889
+ * @example
26890
+ * ```ts
26891
+ * // Update the row if there's a conflict
26892
+ * await db.insert(cars)
26893
+ * .values({ id: 1, brand: 'BMW' })
26894
+ * .onConflictDoUpdate({
26895
+ * target: cars.id,
26896
+ * set: { brand: 'Porsche' }
26897
+ * });
26898
+ *
26899
+ * // Upsert with 'where' clause
26900
+ * await db.insert(cars)
26901
+ * .values({ id: 1, brand: 'BMW' })
26902
+ * .onConflictDoUpdate({
26903
+ * target: cars.id,
26904
+ * set: { brand: 'newBMW' },
26905
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26906
+ * });
26907
+ * ```
26908
+ */
26909
+ onConflictDoUpdate(config) {
26910
+ if (config.where && (config.targetWhere || config.setWhere)) {
26911
+ throw new Error(
26912
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26913
+ );
26914
+ }
26915
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26916
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26917
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26918
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26919
+ let targetColumn = "";
26920
+ 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));
26921
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26922
+ return this;
26923
+ }
26924
+ /** @internal */
26925
+ getSQL() {
26926
+ return this.dialect.buildInsertQuery(this.config);
26927
+ }
26928
+ toSQL() {
26929
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26930
+ return rest;
26931
+ }
26932
+ /** @internal */
26933
+ _prepare(name2) {
26934
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26935
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26936
+ });
26937
+ }
26938
+ prepare(name2) {
26939
+ return this._prepare(name2);
26940
+ }
26941
+ $dynamic() {
26942
+ return this;
26943
+ }
26944
+ };
26945
+ __publicField(PgInsertBase, _a135, "PgInsert");
26946
+ }
26947
+ });
26948
+
26949
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26950
+ var _a136, _b102, PgRefreshMaterializedView;
26951
+ var init_refresh_materialized_view = __esm({
26952
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26953
+ "use strict";
26954
+ init_entity();
26955
+ init_query_promise();
26956
+ init_tracing();
26957
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26958
+ constructor(view4, session, dialect4) {
26959
+ super();
26960
+ __publicField(this, "config");
26961
+ __publicField(this, "execute", (placeholderValues) => {
26962
+ return tracer.startActiveSpan("drizzle.operation", () => {
26963
+ return this._prepare().execute(placeholderValues);
26964
+ });
26965
+ });
26966
+ this.session = session;
26967
+ this.dialect = dialect4;
26968
+ this.config = { view: view4 };
26969
+ }
26970
+ concurrently() {
26971
+ if (this.config.withNoData !== void 0) {
26972
+ throw new Error("Cannot use concurrently and withNoData together");
26973
+ }
26974
+ this.config.concurrently = true;
26975
+ return this;
26976
+ }
26977
+ withNoData() {
26978
+ if (this.config.concurrently !== void 0) {
26979
+ throw new Error("Cannot use concurrently and withNoData together");
26947
26980
  }
26948
26981
  this.config.withNoData = true;
26949
26982
  return this;
@@ -26983,9 +27016,14 @@ var init_update = __esm({
26983
27016
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26984
27017
  "use strict";
26985
27018
  init_entity();
27019
+ init_table2();
26986
27020
  init_query_promise();
27021
+ init_selection_proxy();
27022
+ init_sql();
27023
+ init_subquery();
26987
27024
  init_table();
26988
27025
  init_utils2();
27026
+ init_view_common();
26989
27027
  _a137 = entityKind;
26990
27028
  PgUpdateBuilder = class {
26991
27029
  constructor(table4, session, dialect4, withList) {
@@ -27009,12 +27047,85 @@ var init_update = __esm({
27009
27047
  constructor(table4, set, session, dialect4, withList) {
27010
27048
  super();
27011
27049
  __publicField(this, "config");
27050
+ __publicField(this, "tableName");
27051
+ __publicField(this, "joinsNotNullableMap");
27052
+ __publicField(this, "leftJoin", this.createJoin("left"));
27053
+ __publicField(this, "rightJoin", this.createJoin("right"));
27054
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27055
+ __publicField(this, "fullJoin", this.createJoin("full"));
27012
27056
  __publicField(this, "execute", (placeholderValues) => {
27013
27057
  return this._prepare().execute(placeholderValues);
27014
27058
  });
27015
27059
  this.session = session;
27016
27060
  this.dialect = dialect4;
27017
- this.config = { set, table: table4, withList };
27061
+ this.config = { set, table: table4, withList, joins: [] };
27062
+ this.tableName = getTableLikeName(table4);
27063
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27064
+ }
27065
+ from(source) {
27066
+ const tableName = getTableLikeName(source);
27067
+ if (typeof tableName === "string") {
27068
+ this.joinsNotNullableMap[tableName] = true;
27069
+ }
27070
+ this.config.from = source;
27071
+ return this;
27072
+ }
27073
+ getTableLikeFields(table4) {
27074
+ if (is(table4, PgTable)) {
27075
+ return table4[Table2.Symbol.Columns];
27076
+ } else if (is(table4, Subquery)) {
27077
+ return table4._.selectedFields;
27078
+ }
27079
+ return table4[ViewBaseConfig].selectedFields;
27080
+ }
27081
+ createJoin(joinType) {
27082
+ return (table4, on) => {
27083
+ const tableName = getTableLikeName(table4);
27084
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27085
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27086
+ }
27087
+ if (typeof on === "function") {
27088
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27089
+ on = on(
27090
+ new Proxy(
27091
+ this.config.table[Table2.Symbol.Columns],
27092
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27093
+ ),
27094
+ from && new Proxy(
27095
+ from,
27096
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27097
+ )
27098
+ );
27099
+ }
27100
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27101
+ if (typeof tableName === "string") {
27102
+ switch (joinType) {
27103
+ case "left": {
27104
+ this.joinsNotNullableMap[tableName] = false;
27105
+ break;
27106
+ }
27107
+ case "right": {
27108
+ this.joinsNotNullableMap = Object.fromEntries(
27109
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27110
+ );
27111
+ this.joinsNotNullableMap[tableName] = true;
27112
+ break;
27113
+ }
27114
+ case "inner": {
27115
+ this.joinsNotNullableMap[tableName] = true;
27116
+ break;
27117
+ }
27118
+ case "full": {
27119
+ this.joinsNotNullableMap = Object.fromEntries(
27120
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27121
+ );
27122
+ this.joinsNotNullableMap[tableName] = false;
27123
+ break;
27124
+ }
27125
+ }
27126
+ }
27127
+ return this;
27128
+ };
27018
27129
  }
27019
27130
  /**
27020
27131
  * Adds a 'where' clause to the query.
@@ -27053,7 +27164,24 @@ var init_update = __esm({
27053
27164
  this.config.where = where;
27054
27165
  return this;
27055
27166
  }
27056
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27167
+ returning(fields) {
27168
+ if (!fields) {
27169
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27170
+ if (this.config.from) {
27171
+ const tableName = getTableLikeName(this.config.from);
27172
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27173
+ const fromFields = this.getTableLikeFields(this.config.from);
27174
+ fields[tableName] = fromFields;
27175
+ }
27176
+ for (const join of this.config.joins) {
27177
+ const tableName2 = getTableLikeName(join.table);
27178
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27179
+ const fromFields = this.getTableLikeFields(join.table);
27180
+ fields[tableName2] = fromFields;
27181
+ }
27182
+ }
27183
+ }
27184
+ }
27057
27185
  this.config.returning = orderSelectedFields(fields);
27058
27186
  return this;
27059
27187
  }
@@ -27067,7 +27195,9 @@ var init_update = __esm({
27067
27195
  }
27068
27196
  /** @internal */
27069
27197
  _prepare(name2) {
27070
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27198
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27199
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27200
+ return query;
27071
27201
  }
27072
27202
  prepare(name2) {
27073
27203
  return this._prepare(name2);
@@ -30828,189 +30958,21 @@ var init_delete2 = __esm({
30828
30958
  }
30829
30959
  });
30830
30960
 
30831
- // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
30832
- var _a197, SQLiteInsertBuilder, _a198, _b142, SQLiteInsertBase;
30833
- var init_insert2 = __esm({
30834
- "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
30961
+ // ../drizzle-orm/dist/sqlite-core/view-base.js
30962
+ var _a197, _b142, SQLiteViewBase;
30963
+ var init_view_base2 = __esm({
30964
+ "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30835
30965
  "use strict";
30836
30966
  init_entity();
30837
- init_query_promise();
30838
30967
  init_sql();
30839
- init_table3();
30840
- init_table();
30841
- init_utils2();
30842
- _a197 = entityKind;
30843
- SQLiteInsertBuilder = class {
30844
- constructor(table4, session, dialect4, withList) {
30845
- this.table = table4;
30846
- this.session = session;
30847
- this.dialect = dialect4;
30848
- this.withList = withList;
30849
- }
30850
- values(values) {
30851
- values = Array.isArray(values) ? values : [values];
30852
- if (values.length === 0) {
30853
- throw new Error("values() must be called with at least one value");
30854
- }
30855
- const mappedValues = values.map((entry) => {
30856
- const result = {};
30857
- const cols = this.table[Table2.Symbol.Columns];
30858
- for (const colKey of Object.keys(entry)) {
30859
- const colValue = entry[colKey];
30860
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
30861
- }
30862
- return result;
30863
- });
30864
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
30865
- }
30866
- };
30867
- __publicField(SQLiteInsertBuilder, _a197, "SQLiteInsertBuilder");
30868
- SQLiteInsertBase = class extends (_b142 = QueryPromise, _a198 = entityKind, _b142) {
30869
- constructor(table4, values, session, dialect4, withList) {
30870
- super();
30871
- /** @internal */
30872
- __publicField(this, "config");
30873
- __publicField(this, "run", (placeholderValues) => {
30874
- return this._prepare().run(placeholderValues);
30875
- });
30876
- __publicField(this, "all", (placeholderValues) => {
30877
- return this._prepare().all(placeholderValues);
30878
- });
30879
- __publicField(this, "get", (placeholderValues) => {
30880
- return this._prepare().get(placeholderValues);
30881
- });
30882
- __publicField(this, "values", (placeholderValues) => {
30883
- return this._prepare().values(placeholderValues);
30884
- });
30885
- this.session = session;
30886
- this.dialect = dialect4;
30887
- this.config = { table: table4, values, withList };
30888
- }
30889
- returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
30890
- this.config.returning = orderSelectedFields(fields);
30891
- return this;
30892
- }
30893
- /**
30894
- * Adds an `on conflict do nothing` clause to the query.
30895
- *
30896
- * Calling this method simply avoids inserting a row as its alternative action.
30897
- *
30898
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
30899
- *
30900
- * @param config The `target` and `where` clauses.
30901
- *
30902
- * @example
30903
- * ```ts
30904
- * // Insert one row and cancel the insert if there's a conflict
30905
- * await db.insert(cars)
30906
- * .values({ id: 1, brand: 'BMW' })
30907
- * .onConflictDoNothing();
30908
- *
30909
- * // Explicitly specify conflict target
30910
- * await db.insert(cars)
30911
- * .values({ id: 1, brand: 'BMW' })
30912
- * .onConflictDoNothing({ target: cars.id });
30913
- * ```
30914
- */
30915
- onConflictDoNothing(config = {}) {
30916
- if (config.target === void 0) {
30917
- this.config.onConflict = sql`do nothing`;
30918
- } else {
30919
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30920
- const whereSql = config.where ? sql` where ${config.where}` : sql``;
30921
- this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
30922
- }
30923
- return this;
30924
- }
30925
- /**
30926
- * Adds an `on conflict do update` clause to the query.
30927
- *
30928
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
30929
- *
30930
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
30931
- *
30932
- * @param config The `target`, `set` and `where` clauses.
30933
- *
30934
- * @example
30935
- * ```ts
30936
- * // Update the row if there's a conflict
30937
- * await db.insert(cars)
30938
- * .values({ id: 1, brand: 'BMW' })
30939
- * .onConflictDoUpdate({
30940
- * target: cars.id,
30941
- * set: { brand: 'Porsche' }
30942
- * });
30943
- *
30944
- * // Upsert with 'where' clause
30945
- * await db.insert(cars)
30946
- * .values({ id: 1, brand: 'BMW' })
30947
- * .onConflictDoUpdate({
30948
- * target: cars.id,
30949
- * set: { brand: 'newBMW' },
30950
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
30951
- * });
30952
- * ```
30953
- */
30954
- onConflictDoUpdate(config) {
30955
- if (config.where && (config.targetWhere || config.setWhere)) {
30956
- throw new Error(
30957
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
30958
- );
30959
- }
30960
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
30961
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
30962
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
30963
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30964
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
30965
- this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
30966
- return this;
30967
- }
30968
- /** @internal */
30969
- getSQL() {
30970
- return this.dialect.buildInsertQuery(this.config);
30971
- }
30972
- toSQL() {
30973
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
30974
- return rest;
30975
- }
30976
- /** @internal */
30977
- _prepare(isOneTimeQuery = true) {
30978
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
30979
- this.dialect.sqlToQuery(this.getSQL()),
30980
- this.config.returning,
30981
- this.config.returning ? "all" : "run",
30982
- true
30983
- );
30984
- }
30985
- prepare() {
30986
- return this._prepare(false);
30987
- }
30988
- async execute() {
30989
- return this.config.returning ? this.all() : this.run();
30990
- }
30991
- $dynamic() {
30992
- return this;
30993
- }
30968
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
30994
30969
  };
30995
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
30996
- }
30997
- });
30998
-
30999
- // ../drizzle-orm/dist/sqlite-core/view-base.js
31000
- var _a199, _b143, SQLiteViewBase;
31001
- var init_view_base2 = __esm({
31002
- "../drizzle-orm/dist/sqlite-core/view-base.js"() {
31003
- "use strict";
31004
- init_entity();
31005
- init_sql();
31006
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31007
- };
31008
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
30970
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31009
30971
  }
31010
30972
  });
31011
30973
 
31012
30974
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31013
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
30975
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31014
30976
  var init_dialect2 = __esm({
31015
30977
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31016
30978
  "use strict";
@@ -31029,7 +30991,7 @@ var init_dialect2 = __esm({
31029
30991
  init_utils2();
31030
30992
  init_view_common();
31031
30993
  init_view_base2();
31032
- _a200 = entityKind;
30994
+ _a198 = entityKind;
31033
30995
  SQLiteDialect = class {
31034
30996
  constructor(config) {
31035
30997
  /** @internal */
@@ -31082,14 +31044,16 @@ var init_dialect2 = __esm({
31082
31044
  return [res];
31083
31045
  }));
31084
31046
  }
31085
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31047
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31086
31048
  const withSql = this.buildWithCTE(withList);
31087
31049
  const setSql = this.buildUpdateSet(table4, set);
31050
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31051
+ const joinsSql = this.buildJoins(joins);
31088
31052
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31089
31053
  const whereSql = where ? sql` where ${where}` : void 0;
31090
31054
  const orderBySql = this.buildOrderBy(orderBy);
31091
31055
  const limitSql = this.buildLimit(limit);
31092
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31056
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31093
31057
  }
31094
31058
  /**
31095
31059
  * Builds selection SQL with provided fields/expressions
@@ -31142,6 +31106,37 @@ var init_dialect2 = __esm({
31142
31106
  });
31143
31107
  return sql.join(chunks);
31144
31108
  }
31109
+ buildJoins(joins) {
31110
+ if (!joins || joins.length === 0) {
31111
+ return void 0;
31112
+ }
31113
+ const joinsArray = [];
31114
+ if (joins) {
31115
+ for (const [index4, joinMeta] of joins.entries()) {
31116
+ if (index4 === 0) {
31117
+ joinsArray.push(sql` `);
31118
+ }
31119
+ const table4 = joinMeta.table;
31120
+ if (is(table4, SQLiteTable)) {
31121
+ const tableName = table4[SQLiteTable.Symbol.Name];
31122
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31123
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31124
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31125
+ joinsArray.push(
31126
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31127
+ );
31128
+ } else {
31129
+ joinsArray.push(
31130
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31131
+ );
31132
+ }
31133
+ if (index4 < joins.length - 1) {
31134
+ joinsArray.push(sql` `);
31135
+ }
31136
+ }
31137
+ }
31138
+ return sql.join(joinsArray);
31139
+ }
31145
31140
  buildLimit(limit) {
31146
31141
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31147
31142
  }
@@ -31157,6 +31152,12 @@ var init_dialect2 = __esm({
31157
31152
  }
31158
31153
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31159
31154
  }
31155
+ buildFromTable(table4) {
31156
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31157
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31158
+ }
31159
+ return table4;
31160
+ }
31160
31161
  buildSelectQuery({
31161
31162
  withList,
31162
31163
  fields,
@@ -31187,38 +31188,8 @@ var init_dialect2 = __esm({
31187
31188
  const withSql = this.buildWithCTE(withList);
31188
31189
  const distinctSql = distinct ? sql` distinct` : void 0;
31189
31190
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31190
- const tableSql = (() => {
31191
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31192
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31193
- }
31194
- return table4;
31195
- })();
31196
- const joinsArray = [];
31197
- if (joins) {
31198
- for (const [index4, joinMeta] of joins.entries()) {
31199
- if (index4 === 0) {
31200
- joinsArray.push(sql` `);
31201
- }
31202
- const table22 = joinMeta.table;
31203
- if (is(table22, SQLiteTable)) {
31204
- const tableName = table22[SQLiteTable.Symbol.Name];
31205
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31206
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31207
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31208
- joinsArray.push(
31209
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31210
- );
31211
- } else {
31212
- joinsArray.push(
31213
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31214
- );
31215
- }
31216
- if (index4 < joins.length - 1) {
31217
- joinsArray.push(sql` `);
31218
- }
31219
- }
31220
- }
31221
- const joinsSql = sql.join(joinsArray);
31191
+ const tableSql = this.buildFromTable(table4);
31192
+ const joinsSql = this.buildJoins(joins);
31222
31193
  const whereSql = where ? sql` where ${where}` : void 0;
31223
31194
  const havingSql = having ? sql` having ${having}` : void 0;
31224
31195
  const groupByList = [];
@@ -31284,45 +31255,56 @@ var init_dialect2 = __esm({
31284
31255
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31285
31256
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31286
31257
  }
31287
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31258
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31288
31259
  const valuesSqlList = [];
31289
31260
  const columns = table4[Table2.Symbol.Columns];
31290
31261
  const colEntries = Object.entries(columns).filter(
31291
31262
  ([_2, col]) => !col.shouldDisableInsert()
31292
31263
  );
31293
31264
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
31294
- for (const [valueIndex, value] of values.entries()) {
31295
- const valueList = [];
31296
- for (const [fieldName, col] of colEntries) {
31297
- const colValue = value[fieldName];
31298
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31299
- let defaultValue;
31300
- if (col.default !== null && col.default !== void 0) {
31301
- defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31302
- } else if (col.defaultFn !== void 0) {
31303
- const defaultFnResult = col.defaultFn();
31304
- defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31305
- } else if (!col.default && col.onUpdateFn !== void 0) {
31306
- const onUpdateFnResult = col.onUpdateFn();
31307
- defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31265
+ if (select) {
31266
+ const select2 = valuesOrSelect;
31267
+ if (is(select2, SQL)) {
31268
+ valuesSqlList.push(select2);
31269
+ } else {
31270
+ valuesSqlList.push(select2.getSQL());
31271
+ }
31272
+ } else {
31273
+ const values = valuesOrSelect;
31274
+ valuesSqlList.push(sql.raw("values "));
31275
+ for (const [valueIndex, value] of values.entries()) {
31276
+ const valueList = [];
31277
+ for (const [fieldName, col] of colEntries) {
31278
+ const colValue = value[fieldName];
31279
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31280
+ let defaultValue;
31281
+ if (col.default !== null && col.default !== void 0) {
31282
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31283
+ } else if (col.defaultFn !== void 0) {
31284
+ const defaultFnResult = col.defaultFn();
31285
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31286
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31287
+ const onUpdateFnResult = col.onUpdateFn();
31288
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31289
+ } else {
31290
+ defaultValue = sql`null`;
31291
+ }
31292
+ valueList.push(defaultValue);
31308
31293
  } else {
31309
- defaultValue = sql`null`;
31294
+ valueList.push(colValue);
31310
31295
  }
31311
- valueList.push(defaultValue);
31312
- } else {
31313
- valueList.push(colValue);
31314
31296
  }
31315
- }
31316
- valuesSqlList.push(valueList);
31317
- if (valueIndex < values.length - 1) {
31318
- valuesSqlList.push(sql`, `);
31297
+ valuesSqlList.push(valueList);
31298
+ if (valueIndex < values.length - 1) {
31299
+ valuesSqlList.push(sql`, `);
31300
+ }
31319
31301
  }
31320
31302
  }
31321
31303
  const withSql = this.buildWithCTE(withList);
31322
31304
  const valuesSql = sql.join(valuesSqlList);
31323
31305
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31324
31306
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31325
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31307
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31326
31308
  }
31327
31309
  sqlToQuery(sql2, invokeSource) {
31328
31310
  return sql2.toQuery({
@@ -31551,8 +31533,8 @@ var init_dialect2 = __esm({
31551
31533
  };
31552
31534
  }
31553
31535
  };
31554
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31555
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31536
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31537
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31556
31538
  migrate(migrations, session, config) {
31557
31539
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31558
31540
  const migrationTableCreate = sql`
@@ -31586,8 +31568,8 @@ var init_dialect2 = __esm({
31586
31568
  }
31587
31569
  }
31588
31570
  };
31589
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31590
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31571
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31572
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31591
31573
  async migrate(migrations, session, config) {
31592
31574
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31593
31575
  const migrationTableCreate = sql`
@@ -31616,7 +31598,7 @@ var init_dialect2 = __esm({
31616
31598
  });
31617
31599
  }
31618
31600
  };
31619
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31601
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31620
31602
  }
31621
31603
  });
31622
31604
 
@@ -31638,7 +31620,7 @@ function createSetOperator2(type, isAll) {
31638
31620
  return leftSelect.addSetOperators(setOperators);
31639
31621
  };
31640
31622
  }
31641
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31623
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31642
31624
  var init_select3 = __esm({
31643
31625
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31644
31626
  "use strict";
@@ -31652,7 +31634,7 @@ var init_select3 = __esm({
31652
31634
  init_utils2();
31653
31635
  init_view_common();
31654
31636
  init_view_base2();
31655
- _a203 = entityKind;
31637
+ _a201 = entityKind;
31656
31638
  SQLiteSelectBuilder = class {
31657
31639
  constructor(config) {
31658
31640
  __publicField(this, "fields");
@@ -31693,8 +31675,8 @@ var init_select3 = __esm({
31693
31675
  });
31694
31676
  }
31695
31677
  };
31696
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31697
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31678
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31679
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31698
31680
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31699
31681
  super();
31700
31682
  __publicField(this, "_");
@@ -32199,8 +32181,8 @@ var init_select3 = __esm({
32199
32181
  return this;
32200
32182
  }
32201
32183
  };
32202
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32203
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32184
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32185
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32204
32186
  constructor() {
32205
32187
  super(...arguments);
32206
32188
  __publicField(this, "run", (placeholderValues) => {
@@ -32238,7 +32220,7 @@ var init_select3 = __esm({
32238
32220
  return this.all();
32239
32221
  }
32240
32222
  };
32241
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32223
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32242
32224
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32243
32225
  getSQLiteSetOperators = () => ({
32244
32226
  union: union2,
@@ -32254,7 +32236,7 @@ var init_select3 = __esm({
32254
32236
  });
32255
32237
 
32256
32238
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32257
- var _a206, QueryBuilder2;
32239
+ var _a204, QueryBuilder2;
32258
32240
  var init_query_builder3 = __esm({
32259
32241
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32260
32242
  "use strict";
@@ -32263,7 +32245,7 @@ var init_query_builder3 = __esm({
32263
32245
  init_dialect2();
32264
32246
  init_subquery();
32265
32247
  init_select3();
32266
- _a206 = entityKind;
32248
+ _a204 = entityKind;
32267
32249
  QueryBuilder2 = class {
32268
32250
  constructor(dialect4) {
32269
32251
  __publicField(this, "dialect");
@@ -32325,7 +32307,185 @@ var init_query_builder3 = __esm({
32325
32307
  return this.dialect;
32326
32308
  }
32327
32309
  };
32328
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32310
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32311
+ }
32312
+ });
32313
+
32314
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32315
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32316
+ var init_insert2 = __esm({
32317
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32318
+ "use strict";
32319
+ init_entity();
32320
+ init_query_promise();
32321
+ init_sql();
32322
+ init_table3();
32323
+ init_table();
32324
+ init_utils2();
32325
+ init_query_builder3();
32326
+ _a205 = entityKind;
32327
+ SQLiteInsertBuilder = class {
32328
+ constructor(table4, session, dialect4, withList) {
32329
+ this.table = table4;
32330
+ this.session = session;
32331
+ this.dialect = dialect4;
32332
+ this.withList = withList;
32333
+ }
32334
+ values(values) {
32335
+ values = Array.isArray(values) ? values : [values];
32336
+ if (values.length === 0) {
32337
+ throw new Error("values() must be called with at least one value");
32338
+ }
32339
+ const mappedValues = values.map((entry) => {
32340
+ const result = {};
32341
+ const cols = this.table[Table2.Symbol.Columns];
32342
+ for (const colKey of Object.keys(entry)) {
32343
+ const colValue = entry[colKey];
32344
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32345
+ }
32346
+ return result;
32347
+ });
32348
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32349
+ }
32350
+ select(selectQuery) {
32351
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32352
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32353
+ throw new Error(
32354
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32355
+ );
32356
+ }
32357
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32358
+ }
32359
+ };
32360
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32361
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32362
+ constructor(table4, values, session, dialect4, withList, select) {
32363
+ super();
32364
+ /** @internal */
32365
+ __publicField(this, "config");
32366
+ __publicField(this, "run", (placeholderValues) => {
32367
+ return this._prepare().run(placeholderValues);
32368
+ });
32369
+ __publicField(this, "all", (placeholderValues) => {
32370
+ return this._prepare().all(placeholderValues);
32371
+ });
32372
+ __publicField(this, "get", (placeholderValues) => {
32373
+ return this._prepare().get(placeholderValues);
32374
+ });
32375
+ __publicField(this, "values", (placeholderValues) => {
32376
+ return this._prepare().values(placeholderValues);
32377
+ });
32378
+ this.session = session;
32379
+ this.dialect = dialect4;
32380
+ this.config = { table: table4, values, withList, select };
32381
+ }
32382
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32383
+ this.config.returning = orderSelectedFields(fields);
32384
+ return this;
32385
+ }
32386
+ /**
32387
+ * Adds an `on conflict do nothing` clause to the query.
32388
+ *
32389
+ * Calling this method simply avoids inserting a row as its alternative action.
32390
+ *
32391
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32392
+ *
32393
+ * @param config The `target` and `where` clauses.
32394
+ *
32395
+ * @example
32396
+ * ```ts
32397
+ * // Insert one row and cancel the insert if there's a conflict
32398
+ * await db.insert(cars)
32399
+ * .values({ id: 1, brand: 'BMW' })
32400
+ * .onConflictDoNothing();
32401
+ *
32402
+ * // Explicitly specify conflict target
32403
+ * await db.insert(cars)
32404
+ * .values({ id: 1, brand: 'BMW' })
32405
+ * .onConflictDoNothing({ target: cars.id });
32406
+ * ```
32407
+ */
32408
+ onConflictDoNothing(config = {}) {
32409
+ if (config.target === void 0) {
32410
+ this.config.onConflict = sql`do nothing`;
32411
+ } else {
32412
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32413
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32414
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32415
+ }
32416
+ return this;
32417
+ }
32418
+ /**
32419
+ * Adds an `on conflict do update` clause to the query.
32420
+ *
32421
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32422
+ *
32423
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32424
+ *
32425
+ * @param config The `target`, `set` and `where` clauses.
32426
+ *
32427
+ * @example
32428
+ * ```ts
32429
+ * // Update the row if there's a conflict
32430
+ * await db.insert(cars)
32431
+ * .values({ id: 1, brand: 'BMW' })
32432
+ * .onConflictDoUpdate({
32433
+ * target: cars.id,
32434
+ * set: { brand: 'Porsche' }
32435
+ * });
32436
+ *
32437
+ * // Upsert with 'where' clause
32438
+ * await db.insert(cars)
32439
+ * .values({ id: 1, brand: 'BMW' })
32440
+ * .onConflictDoUpdate({
32441
+ * target: cars.id,
32442
+ * set: { brand: 'newBMW' },
32443
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32444
+ * });
32445
+ * ```
32446
+ */
32447
+ onConflictDoUpdate(config) {
32448
+ if (config.where && (config.targetWhere || config.setWhere)) {
32449
+ throw new Error(
32450
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32451
+ );
32452
+ }
32453
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32454
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32455
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32456
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32457
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32458
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32459
+ return this;
32460
+ }
32461
+ /** @internal */
32462
+ getSQL() {
32463
+ return this.dialect.buildInsertQuery(this.config);
32464
+ }
32465
+ toSQL() {
32466
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32467
+ return rest;
32468
+ }
32469
+ /** @internal */
32470
+ _prepare(isOneTimeQuery = true) {
32471
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32472
+ this.dialect.sqlToQuery(this.getSQL()),
32473
+ this.config.returning,
32474
+ this.config.returning ? "all" : "run",
32475
+ true
32476
+ );
32477
+ }
32478
+ prepare() {
32479
+ return this._prepare(false);
32480
+ }
32481
+ async execute() {
32482
+ return this.config.returning ? this.all() : this.run();
32483
+ }
32484
+ $dynamic() {
32485
+ return this;
32486
+ }
32487
+ };
32488
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32329
32489
  }
32330
32490
  });
32331
32491
 
@@ -32345,8 +32505,11 @@ var init_update2 = __esm({
32345
32505
  init_query_promise();
32346
32506
  init_selection_proxy();
32347
32507
  init_table3();
32508
+ init_subquery();
32348
32509
  init_table();
32349
32510
  init_utils2();
32511
+ init_view_common();
32512
+ init_view_base2();
32350
32513
  _a207 = entityKind;
32351
32514
  SQLiteUpdateBuilder = class {
32352
32515
  constructor(table4, session, dialect4, withList) {
@@ -32371,6 +32534,10 @@ var init_update2 = __esm({
32371
32534
  super();
32372
32535
  /** @internal */
32373
32536
  __publicField(this, "config");
32537
+ __publicField(this, "leftJoin", this.createJoin("left"));
32538
+ __publicField(this, "rightJoin", this.createJoin("right"));
32539
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32540
+ __publicField(this, "fullJoin", this.createJoin("full"));
32374
32541
  __publicField(this, "run", (placeholderValues) => {
32375
32542
  return this._prepare().run(placeholderValues);
32376
32543
  });
@@ -32385,7 +32552,34 @@ var init_update2 = __esm({
32385
32552
  });
32386
32553
  this.session = session;
32387
32554
  this.dialect = dialect4;
32388
- this.config = { set, table: table4, withList };
32555
+ this.config = { set, table: table4, withList, joins: [] };
32556
+ }
32557
+ from(source) {
32558
+ this.config.from = source;
32559
+ return this;
32560
+ }
32561
+ createJoin(joinType) {
32562
+ return (table4, on) => {
32563
+ const tableName = getTableLikeName(table4);
32564
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32565
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32566
+ }
32567
+ if (typeof on === "function") {
32568
+ 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;
32569
+ on = on(
32570
+ new Proxy(
32571
+ this.config.table[Table2.Symbol.Columns],
32572
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32573
+ ),
32574
+ from && new Proxy(
32575
+ from,
32576
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32577
+ )
32578
+ );
32579
+ }
32580
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32581
+ return this;
32582
+ };
32389
32583
  }
32390
32584
  /**
32391
32585
  * Adds a 'where' clause to the query.
@@ -35683,131 +35877,6 @@ var init_delete3 = __esm({
35683
35877
  }
35684
35878
  });
35685
35879
 
35686
- // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
35687
- var _a299, MySqlInsertBuilder, _a300, _b222, MySqlInsertBase;
35688
- var init_insert3 = __esm({
35689
- "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
35690
- "use strict";
35691
- init_entity();
35692
- init_query_promise();
35693
- init_sql();
35694
- init_table();
35695
- init_utils2();
35696
- _a299 = entityKind;
35697
- MySqlInsertBuilder = class {
35698
- constructor(table4, session, dialect4) {
35699
- __publicField(this, "shouldIgnore", false);
35700
- this.table = table4;
35701
- this.session = session;
35702
- this.dialect = dialect4;
35703
- }
35704
- ignore() {
35705
- this.shouldIgnore = true;
35706
- return this;
35707
- }
35708
- values(values) {
35709
- values = Array.isArray(values) ? values : [values];
35710
- if (values.length === 0) {
35711
- throw new Error("values() must be called with at least one value");
35712
- }
35713
- const mappedValues = values.map((entry) => {
35714
- const result = {};
35715
- const cols = this.table[Table2.Symbol.Columns];
35716
- for (const colKey of Object.keys(entry)) {
35717
- const colValue = entry[colKey];
35718
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
35719
- }
35720
- return result;
35721
- });
35722
- return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
35723
- }
35724
- };
35725
- __publicField(MySqlInsertBuilder, _a299, "MySqlInsertBuilder");
35726
- MySqlInsertBase = class extends (_b222 = QueryPromise, _a300 = entityKind, _b222) {
35727
- constructor(table4, values, ignore, session, dialect4) {
35728
- super();
35729
- __publicField(this, "config");
35730
- __publicField(this, "execute", (placeholderValues) => {
35731
- return this.prepare().execute(placeholderValues);
35732
- });
35733
- __publicField(this, "createIterator", () => {
35734
- const self2 = this;
35735
- return async function* (placeholderValues) {
35736
- yield* self2.prepare().iterator(placeholderValues);
35737
- };
35738
- });
35739
- __publicField(this, "iterator", this.createIterator());
35740
- this.session = session;
35741
- this.dialect = dialect4;
35742
- this.config = { table: table4, values, ignore };
35743
- }
35744
- /**
35745
- * Adds an `on duplicate key update` clause to the query.
35746
- *
35747
- * 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.
35748
- *
35749
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35750
- *
35751
- * @param config The `set` clause
35752
- *
35753
- * @example
35754
- * ```ts
35755
- * await db.insert(cars)
35756
- * .values({ id: 1, brand: 'BMW'})
35757
- * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
35758
- * ```
35759
- *
35760
- * 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:
35761
- *
35762
- * ```ts
35763
- * import { sql } from 'drizzle-orm';
35764
- *
35765
- * await db.insert(cars)
35766
- * .values({ id: 1, brand: 'BMW' })
35767
- * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
35768
- * ```
35769
- */
35770
- onDuplicateKeyUpdate(config) {
35771
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
35772
- this.config.onConflict = sql`update ${setSql}`;
35773
- return this;
35774
- }
35775
- $returningId() {
35776
- const returning = [];
35777
- for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
35778
- if (value.primary) {
35779
- returning.push({ field: value, path: [key] });
35780
- }
35781
- }
35782
- this.config.returning = returning;
35783
- return this;
35784
- }
35785
- /** @internal */
35786
- getSQL() {
35787
- return this.dialect.buildInsertQuery(this.config).sql;
35788
- }
35789
- toSQL() {
35790
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
35791
- return rest;
35792
- }
35793
- prepare() {
35794
- const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
35795
- return this.session.prepareQuery(
35796
- this.dialect.sqlToQuery(sql2),
35797
- void 0,
35798
- void 0,
35799
- generatedIds,
35800
- this.config.returning
35801
- );
35802
- }
35803
- $dynamic() {
35804
- return this;
35805
- }
35806
- };
35807
- __publicField(MySqlInsertBase, _a300, "MySqlInsert");
35808
- }
35809
- });
35810
-
35811
35880
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35812
35881
  function getMySqlColumnBuilders() {
35813
35882
  return {
@@ -35888,7 +35957,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35888
35957
  }
35889
35958
  return table4;
35890
35959
  }
35891
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
35960
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35892
35961
  var init_table4 = __esm({
35893
35962
  "../drizzle-orm/dist/mysql-core/table.js"() {
35894
35963
  "use strict";
@@ -35896,15 +35965,15 @@ var init_table4 = __esm({
35896
35965
  init_table();
35897
35966
  init_all3();
35898
35967
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35899
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35968
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35900
35969
  constructor() {
35901
35970
  super(...arguments);
35902
35971
  /** @internal */
35903
35972
  __publicField(this, _c9);
35904
35973
  /** @internal */
35905
- __publicField(this, _b223, []);
35974
+ __publicField(this, _b222, []);
35906
35975
  /** @internal */
35907
- __publicField(this, _a301);
35976
+ __publicField(this, _a299);
35908
35977
  }
35909
35978
  };
35910
35979
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35919,20 +35988,20 @@ var init_table4 = __esm({
35919
35988
  });
35920
35989
 
35921
35990
  // ../drizzle-orm/dist/mysql-core/view-base.js
35922
- var _a302, _b224, MySqlViewBase;
35991
+ var _a300, _b223, MySqlViewBase;
35923
35992
  var init_view_base3 = __esm({
35924
35993
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35925
35994
  "use strict";
35926
35995
  init_entity();
35927
35996
  init_sql();
35928
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
35997
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35929
35998
  };
35930
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
35999
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35931
36000
  }
35932
36001
  });
35933
36002
 
35934
36003
  // ../drizzle-orm/dist/mysql-core/dialect.js
35935
- var _a303, MySqlDialect;
36004
+ var _a301, MySqlDialect;
35936
36005
  var init_dialect3 = __esm({
35937
36006
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35938
36007
  "use strict";
@@ -35951,7 +36020,7 @@ var init_dialect3 = __esm({
35951
36020
  init_common4();
35952
36021
  init_table4();
35953
36022
  init_view_base3();
35954
- _a303 = entityKind;
36023
+ _a301 = entityKind;
35955
36024
  MySqlDialect = class {
35956
36025
  constructor(config) {
35957
36026
  /** @internal */
@@ -36234,7 +36303,7 @@ var init_dialect3 = __esm({
36234
36303
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36235
36304
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36236
36305
  }
36237
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36306
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36238
36307
  const valuesSqlList = [];
36239
36308
  const columns = table4[Table2.Symbol.Columns];
36240
36309
  const colEntries = Object.entries(columns).filter(
@@ -36242,42 +36311,53 @@ var init_dialect3 = __esm({
36242
36311
  );
36243
36312
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36244
36313
  const generatedIdsResponse = [];
36245
- for (const [valueIndex, value] of values.entries()) {
36246
- const generatedIds = {};
36247
- const valueList = [];
36248
- for (const [fieldName, col] of colEntries) {
36249
- const colValue = value[fieldName];
36250
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36251
- if (col.defaultFn !== void 0) {
36252
- const defaultFnResult = col.defaultFn();
36253
- generatedIds[fieldName] = defaultFnResult;
36254
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36255
- valueList.push(defaultValue);
36256
- } else if (!col.default && col.onUpdateFn !== void 0) {
36257
- const onUpdateFnResult = col.onUpdateFn();
36258
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36259
- valueList.push(newValue);
36314
+ if (select) {
36315
+ const select2 = valuesOrSelect;
36316
+ if (is(select2, SQL)) {
36317
+ valuesSqlList.push(select2);
36318
+ } else {
36319
+ valuesSqlList.push(select2.getSQL());
36320
+ }
36321
+ } else {
36322
+ const values = valuesOrSelect;
36323
+ valuesSqlList.push(sql.raw("values "));
36324
+ for (const [valueIndex, value] of values.entries()) {
36325
+ const generatedIds = {};
36326
+ const valueList = [];
36327
+ for (const [fieldName, col] of colEntries) {
36328
+ const colValue = value[fieldName];
36329
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36330
+ if (col.defaultFn !== void 0) {
36331
+ const defaultFnResult = col.defaultFn();
36332
+ generatedIds[fieldName] = defaultFnResult;
36333
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36334
+ valueList.push(defaultValue);
36335
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36336
+ const onUpdateFnResult = col.onUpdateFn();
36337
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36338
+ valueList.push(newValue);
36339
+ } else {
36340
+ valueList.push(sql`default`);
36341
+ }
36260
36342
  } else {
36261
- valueList.push(sql`default`);
36262
- }
36263
- } else {
36264
- if (col.defaultFn && is(colValue, Param)) {
36265
- generatedIds[fieldName] = colValue.value;
36343
+ if (col.defaultFn && is(colValue, Param)) {
36344
+ generatedIds[fieldName] = colValue.value;
36345
+ }
36346
+ valueList.push(colValue);
36266
36347
  }
36267
- valueList.push(colValue);
36268
36348
  }
36269
- }
36270
- generatedIdsResponse.push(generatedIds);
36271
- valuesSqlList.push(valueList);
36272
- if (valueIndex < values.length - 1) {
36273
- valuesSqlList.push(sql`, `);
36349
+ generatedIdsResponse.push(generatedIds);
36350
+ valuesSqlList.push(valueList);
36351
+ if (valueIndex < values.length - 1) {
36352
+ valuesSqlList.push(sql`, `);
36353
+ }
36274
36354
  }
36275
36355
  }
36276
36356
  const valuesSql = sql.join(valuesSqlList);
36277
36357
  const ignoreSql = ignore ? sql` ignore` : void 0;
36278
36358
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36279
36359
  return {
36280
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36360
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36281
36361
  generatedIds: generatedIdsResponse
36282
36362
  };
36283
36363
  }
@@ -36737,7 +36817,7 @@ var init_dialect3 = __esm({
36737
36817
  };
36738
36818
  }
36739
36819
  };
36740
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36820
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36741
36821
  }
36742
36822
  });
36743
36823
 
@@ -36759,7 +36839,7 @@ function createSetOperator3(type, isAll) {
36759
36839
  return leftSelect.addSetOperators(setOperators);
36760
36840
  };
36761
36841
  }
36762
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36842
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36763
36843
  var init_select4 = __esm({
36764
36844
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36765
36845
  "use strict";
@@ -36774,7 +36854,7 @@ var init_select4 = __esm({
36774
36854
  init_utils2();
36775
36855
  init_view_common();
36776
36856
  init_view_base3();
36777
- _a304 = entityKind;
36857
+ _a302 = entityKind;
36778
36858
  MySqlSelectBuilder = class {
36779
36859
  constructor(config) {
36780
36860
  __publicField(this, "fields");
@@ -36819,8 +36899,8 @@ var init_select4 = __esm({
36819
36899
  );
36820
36900
  }
36821
36901
  };
36822
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36823
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36902
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36903
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36824
36904
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36825
36905
  super();
36826
36906
  __publicField(this, "_");
@@ -37421,8 +37501,8 @@ var init_select4 = __esm({
37421
37501
  return this;
37422
37502
  }
37423
37503
  };
37424
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37425
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37504
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37505
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37426
37506
  constructor() {
37427
37507
  super(...arguments);
37428
37508
  __publicField(this, "execute", (placeholderValues) => {
@@ -37446,7 +37526,7 @@ var init_select4 = __esm({
37446
37526
  return query;
37447
37527
  }
37448
37528
  };
37449
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37529
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37450
37530
  applyMixins(MySqlSelectBase, [QueryPromise]);
37451
37531
  getMySqlSetOperators = () => ({
37452
37532
  union: union3,
@@ -37466,7 +37546,7 @@ var init_select4 = __esm({
37466
37546
  });
37467
37547
 
37468
37548
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37469
- var _a307, QueryBuilder3;
37549
+ var _a305, QueryBuilder3;
37470
37550
  var init_query_builder4 = __esm({
37471
37551
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37472
37552
  "use strict";
@@ -37475,7 +37555,7 @@ var init_query_builder4 = __esm({
37475
37555
  init_selection_proxy();
37476
37556
  init_subquery();
37477
37557
  init_select4();
37478
- _a307 = entityKind;
37558
+ _a305 = entityKind;
37479
37559
  QueryBuilder3 = class {
37480
37560
  constructor(dialect4) {
37481
37561
  __publicField(this, "dialect");
@@ -37537,7 +37617,142 @@ var init_query_builder4 = __esm({
37537
37617
  return this.dialect;
37538
37618
  }
37539
37619
  };
37540
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37620
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37621
+ }
37622
+ });
37623
+
37624
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37625
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37626
+ var init_insert3 = __esm({
37627
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37628
+ "use strict";
37629
+ init_entity();
37630
+ init_query_promise();
37631
+ init_sql();
37632
+ init_table();
37633
+ init_utils2();
37634
+ init_query_builder4();
37635
+ _a306 = entityKind;
37636
+ MySqlInsertBuilder = class {
37637
+ constructor(table4, session, dialect4) {
37638
+ __publicField(this, "shouldIgnore", false);
37639
+ this.table = table4;
37640
+ this.session = session;
37641
+ this.dialect = dialect4;
37642
+ }
37643
+ ignore() {
37644
+ this.shouldIgnore = true;
37645
+ return this;
37646
+ }
37647
+ values(values) {
37648
+ values = Array.isArray(values) ? values : [values];
37649
+ if (values.length === 0) {
37650
+ throw new Error("values() must be called with at least one value");
37651
+ }
37652
+ const mappedValues = values.map((entry) => {
37653
+ const result = {};
37654
+ const cols = this.table[Table2.Symbol.Columns];
37655
+ for (const colKey of Object.keys(entry)) {
37656
+ const colValue = entry[colKey];
37657
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37658
+ }
37659
+ return result;
37660
+ });
37661
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37662
+ }
37663
+ select(selectQuery) {
37664
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37665
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37666
+ throw new Error(
37667
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37668
+ );
37669
+ }
37670
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37671
+ }
37672
+ };
37673
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37674
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37675
+ constructor(table4, values, ignore, session, dialect4, select) {
37676
+ super();
37677
+ __publicField(this, "config");
37678
+ __publicField(this, "execute", (placeholderValues) => {
37679
+ return this.prepare().execute(placeholderValues);
37680
+ });
37681
+ __publicField(this, "createIterator", () => {
37682
+ const self2 = this;
37683
+ return async function* (placeholderValues) {
37684
+ yield* self2.prepare().iterator(placeholderValues);
37685
+ };
37686
+ });
37687
+ __publicField(this, "iterator", this.createIterator());
37688
+ this.session = session;
37689
+ this.dialect = dialect4;
37690
+ this.config = { table: table4, values, select, ignore };
37691
+ }
37692
+ /**
37693
+ * Adds an `on duplicate key update` clause to the query.
37694
+ *
37695
+ * 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.
37696
+ *
37697
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37698
+ *
37699
+ * @param config The `set` clause
37700
+ *
37701
+ * @example
37702
+ * ```ts
37703
+ * await db.insert(cars)
37704
+ * .values({ id: 1, brand: 'BMW'})
37705
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37706
+ * ```
37707
+ *
37708
+ * 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:
37709
+ *
37710
+ * ```ts
37711
+ * import { sql } from 'drizzle-orm';
37712
+ *
37713
+ * await db.insert(cars)
37714
+ * .values({ id: 1, brand: 'BMW' })
37715
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37716
+ * ```
37717
+ */
37718
+ onDuplicateKeyUpdate(config) {
37719
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37720
+ this.config.onConflict = sql`update ${setSql}`;
37721
+ return this;
37722
+ }
37723
+ $returningId() {
37724
+ const returning = [];
37725
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37726
+ if (value.primary) {
37727
+ returning.push({ field: value, path: [key] });
37728
+ }
37729
+ }
37730
+ this.config.returning = returning;
37731
+ return this;
37732
+ }
37733
+ /** @internal */
37734
+ getSQL() {
37735
+ return this.dialect.buildInsertQuery(this.config).sql;
37736
+ }
37737
+ toSQL() {
37738
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37739
+ return rest;
37740
+ }
37741
+ prepare() {
37742
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37743
+ return this.session.prepareQuery(
37744
+ this.dialect.sqlToQuery(sql2),
37745
+ void 0,
37746
+ void 0,
37747
+ generatedIds,
37748
+ this.config.returning
37749
+ );
37750
+ }
37751
+ $dynamic() {
37752
+ return this;
37753
+ }
37754
+ };
37755
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37541
37756
  }
37542
37757
  });
37543
37758