drizzle-kit 0.28.0 → 0.28.1-44b6c8a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. package/README.md +7 -7
  2. package/api.js +937 -718
  3. package/api.mjs +937 -718
  4. package/bin.cjs +8 -8
  5. 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
 
@@ -21412,7 +21412,11 @@ var init_sql = __esm({
21412
21412
  if (_config.invokeSource === "indexes") {
21413
21413
  return { sql: escapeName(columnName), params: [] };
21414
21414
  }
21415
- return { sql: escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName), params: [] };
21415
+ const schemaName = chunk.table[Table2.Symbol.Schema];
21416
+ return {
21417
+ sql: chunk.table[IsAlias] || schemaName === void 0 ? escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName) : escapeName(schemaName) + "." + escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName),
21418
+ params: []
21419
+ };
21416
21420
  }
21417
21421
  if (is(chunk, View3)) {
21418
21422
  const schemaName = chunk[ViewBaseConfig].schema;
@@ -22145,7 +22149,7 @@ function haveSameKeys(left, right) {
22145
22149
  }
22146
22150
  function mapUpdateSet(table4, values) {
22147
22151
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22148
- if (is(value, SQL)) {
22152
+ if (is(value, SQL) || is(value, Column2)) {
22149
22153
  return [key, value];
22150
22154
  } else {
22151
22155
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24655,162 +24659,6 @@ var init_delete = __esm({
24655
24659
  }
24656
24660
  });
24657
24661
 
24658
- // ../drizzle-orm/dist/pg-core/query-builders/insert.js
24659
- var _a125, PgInsertBuilder, _a126, _b98, PgInsertBase;
24660
- var init_insert = __esm({
24661
- "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
24662
- "use strict";
24663
- init_entity();
24664
- init_query_promise();
24665
- init_sql();
24666
- init_table();
24667
- init_tracing();
24668
- init_utils2();
24669
- _a125 = entityKind;
24670
- PgInsertBuilder = class {
24671
- constructor(table4, session, dialect4, withList) {
24672
- this.table = table4;
24673
- this.session = session;
24674
- this.dialect = dialect4;
24675
- this.withList = withList;
24676
- }
24677
- values(values) {
24678
- values = Array.isArray(values) ? values : [values];
24679
- if (values.length === 0) {
24680
- throw new Error("values() must be called with at least one value");
24681
- }
24682
- const mappedValues = values.map((entry) => {
24683
- const result = {};
24684
- const cols = this.table[Table2.Symbol.Columns];
24685
- for (const colKey of Object.keys(entry)) {
24686
- const colValue = entry[colKey];
24687
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
24688
- }
24689
- return result;
24690
- });
24691
- return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
24692
- }
24693
- };
24694
- __publicField(PgInsertBuilder, _a125, "PgInsertBuilder");
24695
- PgInsertBase = class extends (_b98 = QueryPromise, _a126 = entityKind, _b98) {
24696
- constructor(table4, values, session, dialect4, withList) {
24697
- super();
24698
- __publicField(this, "config");
24699
- __publicField(this, "execute", (placeholderValues) => {
24700
- return tracer.startActiveSpan("drizzle.operation", () => {
24701
- return this._prepare().execute(placeholderValues);
24702
- });
24703
- });
24704
- this.session = session;
24705
- this.dialect = dialect4;
24706
- this.config = { table: table4, values, withList };
24707
- }
24708
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
24709
- this.config.returning = orderSelectedFields(fields);
24710
- return this;
24711
- }
24712
- /**
24713
- * Adds an `on conflict do nothing` clause to the query.
24714
- *
24715
- * Calling this method simply avoids inserting a row as its alternative action.
24716
- *
24717
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
24718
- *
24719
- * @param config The `target` and `where` clauses.
24720
- *
24721
- * @example
24722
- * ```ts
24723
- * // Insert one row and cancel the insert if there's a conflict
24724
- * await db.insert(cars)
24725
- * .values({ id: 1, brand: 'BMW' })
24726
- * .onConflictDoNothing();
24727
- *
24728
- * // Explicitly specify conflict target
24729
- * await db.insert(cars)
24730
- * .values({ id: 1, brand: 'BMW' })
24731
- * .onConflictDoNothing({ target: cars.id });
24732
- * ```
24733
- */
24734
- onConflictDoNothing(config = {}) {
24735
- if (config.target === void 0) {
24736
- this.config.onConflict = sql`do nothing`;
24737
- } else {
24738
- let targetColumn = "";
24739
- 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));
24740
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24741
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
24742
- }
24743
- return this;
24744
- }
24745
- /**
24746
- * Adds an `on conflict do update` clause to the query.
24747
- *
24748
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
24749
- *
24750
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
24751
- *
24752
- * @param config The `target`, `set` and `where` clauses.
24753
- *
24754
- * @example
24755
- * ```ts
24756
- * // Update the row if there's a conflict
24757
- * await db.insert(cars)
24758
- * .values({ id: 1, brand: 'BMW' })
24759
- * .onConflictDoUpdate({
24760
- * target: cars.id,
24761
- * set: { brand: 'Porsche' }
24762
- * });
24763
- *
24764
- * // Upsert with 'where' clause
24765
- * await db.insert(cars)
24766
- * .values({ id: 1, brand: 'BMW' })
24767
- * .onConflictDoUpdate({
24768
- * target: cars.id,
24769
- * set: { brand: 'newBMW' },
24770
- * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
24771
- * });
24772
- * ```
24773
- */
24774
- onConflictDoUpdate(config) {
24775
- if (config.where && (config.targetWhere || config.setWhere)) {
24776
- throw new Error(
24777
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
24778
- );
24779
- }
24780
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24781
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
24782
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
24783
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
24784
- let targetColumn = "";
24785
- 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));
24786
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
24787
- return this;
24788
- }
24789
- /** @internal */
24790
- getSQL() {
24791
- return this.dialect.buildInsertQuery(this.config);
24792
- }
24793
- toSQL() {
24794
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
24795
- return rest;
24796
- }
24797
- /** @internal */
24798
- _prepare(name2) {
24799
- return tracer.startActiveSpan("drizzle.prepareQuery", () => {
24800
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
24801
- });
24802
- }
24803
- prepare(name2) {
24804
- return this._prepare(name2);
24805
- }
24806
- $dynamic() {
24807
- return this;
24808
- }
24809
- };
24810
- __publicField(PgInsertBase, _a126, "PgInsert");
24811
- }
24812
- });
24813
-
24814
24662
  // ../drizzle-orm/dist/casing.js
24815
24663
  function toSnakeCase(input) {
24816
24664
  const words = input.replace(/['\u2019]/g, "").match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];
@@ -24826,13 +24674,13 @@ function toCamelCase(input) {
24826
24674
  function noopCase(input) {
24827
24675
  return input;
24828
24676
  }
24829
- var _a127, CasingCache;
24677
+ var _a125, CasingCache;
24830
24678
  var init_casing = __esm({
24831
24679
  "../drizzle-orm/dist/casing.js"() {
24832
24680
  "use strict";
24833
24681
  init_entity();
24834
24682
  init_table();
24835
- _a127 = entityKind;
24683
+ _a125 = entityKind;
24836
24684
  CasingCache = class {
24837
24685
  constructor(casing2) {
24838
24686
  /** @internal */
@@ -24869,25 +24717,25 @@ var init_casing = __esm({
24869
24717
  this.cachedTables = {};
24870
24718
  }
24871
24719
  };
24872
- __publicField(CasingCache, _a127, "CasingCache");
24720
+ __publicField(CasingCache, _a125, "CasingCache");
24873
24721
  }
24874
24722
  });
24875
24723
 
24876
24724
  // ../drizzle-orm/dist/pg-core/view-base.js
24877
- var _a128, _b99, PgViewBase;
24725
+ var _a126, _b98, PgViewBase;
24878
24726
  var init_view_base = __esm({
24879
24727
  "../drizzle-orm/dist/pg-core/view-base.js"() {
24880
24728
  "use strict";
24881
24729
  init_entity();
24882
24730
  init_sql();
24883
- PgViewBase = class extends (_b99 = View3, _a128 = entityKind, _b99) {
24731
+ PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
24884
24732
  };
24885
- __publicField(PgViewBase, _a128, "PgViewBase");
24733
+ __publicField(PgViewBase, _a126, "PgViewBase");
24886
24734
  }
24887
24735
  });
24888
24736
 
24889
24737
  // ../drizzle-orm/dist/pg-core/dialect.js
24890
- var _a129, PgDialect;
24738
+ var _a127, PgDialect;
24891
24739
  var init_dialect = __esm({
24892
24740
  "../drizzle-orm/dist/pg-core/dialect.js"() {
24893
24741
  "use strict";
@@ -24906,7 +24754,7 @@ var init_dialect = __esm({
24906
24754
  init_utils2();
24907
24755
  init_view_common();
24908
24756
  init_view_base();
24909
- _a129 = entityKind;
24757
+ _a127 = entityKind;
24910
24758
  PgDialect = class {
24911
24759
  constructor(config) {
24912
24760
  /** @internal */
@@ -24986,12 +24834,19 @@ var init_dialect = __esm({
24986
24834
  return [res];
24987
24835
  }));
24988
24836
  }
24989
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24837
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24990
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)}`}`;
24991
24844
  const setSql = this.buildUpdateSet(table4, set);
24992
- 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;
24993
24848
  const whereSql = where ? sql` where ${where}` : void 0;
24994
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
24849
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24995
24850
  }
24996
24851
  /**
24997
24852
  * Builds selection SQL with provided fields/expressions
@@ -25043,6 +24898,54 @@ var init_dialect = __esm({
25043
24898
  });
25044
24899
  return sql.join(chunks);
25045
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
+ }
25046
24949
  buildSelectQuery({
25047
24950
  withList,
25048
24951
  fields,
@@ -25077,51 +24980,8 @@ var init_dialect = __esm({
25077
24980
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25078
24981
  }
25079
24982
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25080
- const tableSql = (() => {
25081
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25082
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25083
- if (table4[Table2.Symbol.Schema]) {
25084
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25085
- }
25086
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25087
- }
25088
- return table4;
25089
- })();
25090
- const joinsArray = [];
25091
- if (joins) {
25092
- for (const [index4, joinMeta] of joins.entries()) {
25093
- if (index4 === 0) {
25094
- joinsArray.push(sql` `);
25095
- }
25096
- const table22 = joinMeta.table;
25097
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25098
- if (is(table22, PgTable)) {
25099
- const tableName = table22[PgTable.Symbol.Name];
25100
- const tableSchema = table22[PgTable.Symbol.Schema];
25101
- const origTableName = table22[PgTable.Symbol.OriginalName];
25102
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25103
- joinsArray.push(
25104
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25105
- );
25106
- } else if (is(table22, View3)) {
25107
- const viewName = table22[ViewBaseConfig].name;
25108
- const viewSchema = table22[ViewBaseConfig].schema;
25109
- const origViewName = table22[ViewBaseConfig].originalName;
25110
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25111
- joinsArray.push(
25112
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25113
- );
25114
- } else {
25115
- joinsArray.push(
25116
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25117
- );
25118
- }
25119
- if (index4 < joins.length - 1) {
25120
- joinsArray.push(sql` `);
25121
- }
25122
- }
25123
- }
25124
- const joinsSql = sql.join(joinsArray);
24983
+ const tableSql = this.buildFromTable(table4);
24984
+ const joinsSql = this.buildJoins(joins);
25125
24985
  const whereSql = where ? sql` where ${where}` : void 0;
25126
24986
  const havingSql = having ? sql` having ${having}` : void 0;
25127
24987
  let orderBySql;
@@ -25202,43 +25062,54 @@ var init_dialect = __esm({
25202
25062
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
25203
25063
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
25204
25064
  }
25205
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
25065
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
25206
25066
  const valuesSqlList = [];
25207
25067
  const columns = table4[Table2.Symbol.Columns];
25208
25068
  const colEntries = Object.entries(columns).filter(([_2, col]) => !col.shouldDisableInsert());
25209
25069
  const insertOrder = colEntries.map(
25210
25070
  ([, column4]) => sql.identifier(this.casing.getColumnCasing(column4))
25211
25071
  );
25212
- for (const [valueIndex, value] of values.entries()) {
25213
- const valueList = [];
25214
- for (const [fieldName, col] of colEntries) {
25215
- const colValue = value[fieldName];
25216
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25217
- if (col.defaultFn !== void 0) {
25218
- const defaultFnResult = col.defaultFn();
25219
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25220
- valueList.push(defaultValue);
25221
- } else if (!col.default && col.onUpdateFn !== void 0) {
25222
- const onUpdateFnResult = col.onUpdateFn();
25223
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25224
- 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
+ }
25225
25098
  } else {
25226
- valueList.push(sql`default`);
25099
+ valueList.push(colValue);
25227
25100
  }
25228
- } else {
25229
- valueList.push(colValue);
25230
25101
  }
25231
- }
25232
- valuesSqlList.push(valueList);
25233
- if (valueIndex < values.length - 1) {
25234
- valuesSqlList.push(sql`, `);
25102
+ valuesSqlList.push(valueList);
25103
+ if (valueIndex < values.length - 1) {
25104
+ valuesSqlList.push(sql`, `);
25105
+ }
25235
25106
  }
25236
25107
  }
25237
25108
  const withSql = this.buildWithCTE(withList);
25238
25109
  const valuesSql = sql.join(valuesSqlList);
25239
25110
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25240
25111
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
25241
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
25112
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
25242
25113
  }
25243
25114
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25244
25115
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25974,12 +25845,12 @@ var init_dialect = __esm({
25974
25845
  };
25975
25846
  }
25976
25847
  };
25977
- __publicField(PgDialect, _a129, "PgDialect");
25848
+ __publicField(PgDialect, _a127, "PgDialect");
25978
25849
  }
25979
25850
  });
25980
25851
 
25981
25852
  // ../drizzle-orm/dist/selection-proxy.js
25982
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25853
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25983
25854
  var init_selection_proxy = __esm({
25984
25855
  "../drizzle-orm/dist/selection-proxy.js"() {
25985
25856
  "use strict";
@@ -25989,7 +25860,7 @@ var init_selection_proxy = __esm({
25989
25860
  init_sql();
25990
25861
  init_subquery();
25991
25862
  init_view_common();
25992
- _a130 = entityKind;
25863
+ _a128 = entityKind;
25993
25864
  _SelectionProxyHandler = class _SelectionProxyHandler {
25994
25865
  constructor(config) {
25995
25866
  __publicField(this, "config");
@@ -26055,25 +25926,25 @@ var init_selection_proxy = __esm({
26055
25926
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26056
25927
  }
26057
25928
  };
26058
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25929
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26059
25930
  SelectionProxyHandler = _SelectionProxyHandler;
26060
25931
  }
26061
25932
  });
26062
25933
 
26063
25934
  // ../drizzle-orm/dist/query-builders/query-builder.js
26064
- var _a131, TypedQueryBuilder;
25935
+ var _a129, TypedQueryBuilder;
26065
25936
  var init_query_builder = __esm({
26066
25937
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26067
25938
  "use strict";
26068
25939
  init_entity();
26069
- _a131 = entityKind;
25940
+ _a129 = entityKind;
26070
25941
  TypedQueryBuilder = class {
26071
25942
  /** @internal */
26072
25943
  getSelectedFields() {
26073
25944
  return this._.selectedFields;
26074
25945
  }
26075
25946
  };
26076
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25947
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26077
25948
  }
26078
25949
  });
26079
25950
 
@@ -26095,7 +25966,7 @@ function createSetOperator(type, isAll) {
26095
25966
  return leftSelect.addSetOperators(setOperators);
26096
25967
  };
26097
25968
  }
26098
- 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;
26099
25970
  var init_select2 = __esm({
26100
25971
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26101
25972
  "use strict";
@@ -26111,7 +25982,7 @@ var init_select2 = __esm({
26111
25982
  init_utils2();
26112
25983
  init_utils2();
26113
25984
  init_view_common();
26114
- _a132 = entityKind;
25985
+ _a130 = entityKind;
26115
25986
  PgSelectBuilder = class {
26116
25987
  constructor(config) {
26117
25988
  __publicField(this, "fields");
@@ -26160,8 +26031,8 @@ var init_select2 = __esm({
26160
26031
  });
26161
26032
  }
26162
26033
  };
26163
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26164
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26034
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26035
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26165
26036
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26166
26037
  super();
26167
26038
  __publicField(this, "_");
@@ -26761,8 +26632,8 @@ var init_select2 = __esm({
26761
26632
  return this;
26762
26633
  }
26763
26634
  };
26764
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26765
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26635
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26636
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26766
26637
  constructor() {
26767
26638
  super(...arguments);
26768
26639
  __publicField(this, "execute", (placeholderValues) => {
@@ -26795,7 +26666,7 @@ var init_select2 = __esm({
26795
26666
  return this._prepare(name2);
26796
26667
  }
26797
26668
  };
26798
- __publicField(PgSelectBase, _a134, "PgSelect");
26669
+ __publicField(PgSelectBase, _a132, "PgSelect");
26799
26670
  applyMixins(PgSelectBase, [QueryPromise]);
26800
26671
  getPgSetOperators = () => ({
26801
26672
  union,
@@ -26815,7 +26686,7 @@ var init_select2 = __esm({
26815
26686
  });
26816
26687
 
26817
26688
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26818
- var _a135, QueryBuilder;
26689
+ var _a133, QueryBuilder;
26819
26690
  var init_query_builder2 = __esm({
26820
26691
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26821
26692
  "use strict";
@@ -26824,7 +26695,7 @@ var init_query_builder2 = __esm({
26824
26695
  init_selection_proxy();
26825
26696
  init_subquery();
26826
26697
  init_select2();
26827
- _a135 = entityKind;
26698
+ _a133 = entityKind;
26828
26699
  QueryBuilder = class {
26829
26700
  constructor(dialect4) {
26830
26701
  __publicField(this, "dialect");
@@ -26905,38 +26776,204 @@ var init_query_builder2 = __esm({
26905
26776
  return this.dialect;
26906
26777
  }
26907
26778
  };
26908
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26779
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26909
26780
  }
26910
26781
  });
26911
26782
 
26912
- // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26913
- var _a136, _b102, PgRefreshMaterializedView;
26914
- var init_refresh_materialized_view = __esm({
26915
- "../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"() {
26916
26787
  "use strict";
26917
26788
  init_entity();
26918
26789
  init_query_promise();
26790
+ init_sql();
26791
+ init_table();
26919
26792
  init_tracing();
26920
- PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26921
- constructor(view4, session, dialect4) {
26922
- super();
26923
- __publicField(this, "config");
26924
- __publicField(this, "execute", (placeholderValues) => {
26925
- return tracer.startActiveSpan("drizzle.operation", () => {
26926
- return this._prepare().execute(placeholderValues);
26927
- });
26928
- });
26793
+ init_utils2();
26794
+ init_query_builder2();
26795
+ _a134 = entityKind;
26796
+ PgInsertBuilder = class {
26797
+ constructor(table4, session, dialect4, withList) {
26798
+ this.table = table4;
26929
26799
  this.session = session;
26930
26800
  this.dialect = dialect4;
26931
- this.config = { view: view4 };
26801
+ this.withList = withList;
26932
26802
  }
26933
- concurrently() {
26934
- if (this.config.withNoData !== void 0) {
26935
- 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");
26936
26807
  }
26937
- this.config.concurrently = true;
26938
- return this;
26939
- }
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
+ }
26940
26977
  withNoData() {
26941
26978
  if (this.config.concurrently !== void 0) {
26942
26979
  throw new Error("Cannot use concurrently and withNoData together");
@@ -26979,9 +27016,14 @@ var init_update = __esm({
26979
27016
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26980
27017
  "use strict";
26981
27018
  init_entity();
27019
+ init_table2();
26982
27020
  init_query_promise();
27021
+ init_selection_proxy();
27022
+ init_sql();
27023
+ init_subquery();
26983
27024
  init_table();
26984
27025
  init_utils2();
27026
+ init_view_common();
26985
27027
  _a137 = entityKind;
26986
27028
  PgUpdateBuilder = class {
26987
27029
  constructor(table4, session, dialect4, withList) {
@@ -27005,12 +27047,85 @@ var init_update = __esm({
27005
27047
  constructor(table4, set, session, dialect4, withList) {
27006
27048
  super();
27007
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"));
27008
27056
  __publicField(this, "execute", (placeholderValues) => {
27009
27057
  return this._prepare().execute(placeholderValues);
27010
27058
  });
27011
27059
  this.session = session;
27012
27060
  this.dialect = dialect4;
27013
- 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
+ };
27014
27129
  }
27015
27130
  /**
27016
27131
  * Adds a 'where' clause to the query.
@@ -27049,7 +27164,24 @@ var init_update = __esm({
27049
27164
  this.config.where = where;
27050
27165
  return this;
27051
27166
  }
27052
- 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
+ }
27053
27185
  this.config.returning = orderSelectedFields(fields);
27054
27186
  return this;
27055
27187
  }
@@ -27063,7 +27195,9 @@ var init_update = __esm({
27063
27195
  }
27064
27196
  /** @internal */
27065
27197
  _prepare(name2) {
27066
- 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;
27067
27201
  }
27068
27202
  prepare(name2) {
27069
27203
  return this._prepare(name2);
@@ -30824,208 +30958,40 @@ var init_delete2 = __esm({
30824
30958
  }
30825
30959
  });
30826
30960
 
30827
- // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
30828
- var _a197, SQLiteInsertBuilder, _a198, _b142, SQLiteInsertBase;
30829
- var init_insert2 = __esm({
30830
- "../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"() {
30831
30965
  "use strict";
30832
30966
  init_entity();
30833
- init_query_promise();
30834
30967
  init_sql();
30968
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
30969
+ };
30970
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
30971
+ }
30972
+ });
30973
+
30974
+ // ../drizzle-orm/dist/sqlite-core/dialect.js
30975
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
30976
+ var init_dialect2 = __esm({
30977
+ "../drizzle-orm/dist/sqlite-core/dialect.js"() {
30978
+ "use strict";
30979
+ init_alias();
30980
+ init_casing();
30981
+ init_column();
30982
+ init_entity();
30983
+ init_errors();
30984
+ init_relations();
30985
+ init_sql2();
30986
+ init_sql();
30987
+ init_columns2();
30835
30988
  init_table3();
30836
- init_table();
30837
- init_utils2();
30838
- _a197 = entityKind;
30839
- SQLiteInsertBuilder = class {
30840
- constructor(table4, session, dialect4, withList) {
30841
- this.table = table4;
30842
- this.session = session;
30843
- this.dialect = dialect4;
30844
- this.withList = withList;
30845
- }
30846
- values(values) {
30847
- values = Array.isArray(values) ? values : [values];
30848
- if (values.length === 0) {
30849
- throw new Error("values() must be called with at least one value");
30850
- }
30851
- const mappedValues = values.map((entry) => {
30852
- const result = {};
30853
- const cols = this.table[Table2.Symbol.Columns];
30854
- for (const colKey of Object.keys(entry)) {
30855
- const colValue = entry[colKey];
30856
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
30857
- }
30858
- return result;
30859
- });
30860
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
30861
- }
30862
- };
30863
- __publicField(SQLiteInsertBuilder, _a197, "SQLiteInsertBuilder");
30864
- SQLiteInsertBase = class extends (_b142 = QueryPromise, _a198 = entityKind, _b142) {
30865
- constructor(table4, values, session, dialect4, withList) {
30866
- super();
30867
- /** @internal */
30868
- __publicField(this, "config");
30869
- __publicField(this, "run", (placeholderValues) => {
30870
- return this._prepare().run(placeholderValues);
30871
- });
30872
- __publicField(this, "all", (placeholderValues) => {
30873
- return this._prepare().all(placeholderValues);
30874
- });
30875
- __publicField(this, "get", (placeholderValues) => {
30876
- return this._prepare().get(placeholderValues);
30877
- });
30878
- __publicField(this, "values", (placeholderValues) => {
30879
- return this._prepare().values(placeholderValues);
30880
- });
30881
- this.session = session;
30882
- this.dialect = dialect4;
30883
- this.config = { table: table4, values, withList };
30884
- }
30885
- returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
30886
- this.config.returning = orderSelectedFields(fields);
30887
- return this;
30888
- }
30889
- /**
30890
- * Adds an `on conflict do nothing` clause to the query.
30891
- *
30892
- * Calling this method simply avoids inserting a row as its alternative action.
30893
- *
30894
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
30895
- *
30896
- * @param config The `target` and `where` clauses.
30897
- *
30898
- * @example
30899
- * ```ts
30900
- * // Insert one row and cancel the insert if there's a conflict
30901
- * await db.insert(cars)
30902
- * .values({ id: 1, brand: 'BMW' })
30903
- * .onConflictDoNothing();
30904
- *
30905
- * // Explicitly specify conflict target
30906
- * await db.insert(cars)
30907
- * .values({ id: 1, brand: 'BMW' })
30908
- * .onConflictDoNothing({ target: cars.id });
30909
- * ```
30910
- */
30911
- onConflictDoNothing(config = {}) {
30912
- if (config.target === void 0) {
30913
- this.config.onConflict = sql`do nothing`;
30914
- } else {
30915
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30916
- const whereSql = config.where ? sql` where ${config.where}` : sql``;
30917
- this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
30918
- }
30919
- return this;
30920
- }
30921
- /**
30922
- * Adds an `on conflict do update` clause to the query.
30923
- *
30924
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
30925
- *
30926
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
30927
- *
30928
- * @param config The `target`, `set` and `where` clauses.
30929
- *
30930
- * @example
30931
- * ```ts
30932
- * // Update the row if there's a conflict
30933
- * await db.insert(cars)
30934
- * .values({ id: 1, brand: 'BMW' })
30935
- * .onConflictDoUpdate({
30936
- * target: cars.id,
30937
- * set: { brand: 'Porsche' }
30938
- * });
30939
- *
30940
- * // Upsert with 'where' clause
30941
- * await db.insert(cars)
30942
- * .values({ id: 1, brand: 'BMW' })
30943
- * .onConflictDoUpdate({
30944
- * target: cars.id,
30945
- * set: { brand: 'newBMW' },
30946
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
30947
- * });
30948
- * ```
30949
- */
30950
- onConflictDoUpdate(config) {
30951
- if (config.where && (config.targetWhere || config.setWhere)) {
30952
- throw new Error(
30953
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
30954
- );
30955
- }
30956
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
30957
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
30958
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
30959
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30960
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
30961
- this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
30962
- return this;
30963
- }
30964
- /** @internal */
30965
- getSQL() {
30966
- return this.dialect.buildInsertQuery(this.config);
30967
- }
30968
- toSQL() {
30969
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
30970
- return rest;
30971
- }
30972
- /** @internal */
30973
- _prepare(isOneTimeQuery = true) {
30974
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
30975
- this.dialect.sqlToQuery(this.getSQL()),
30976
- this.config.returning,
30977
- this.config.returning ? "all" : "run",
30978
- true
30979
- );
30980
- }
30981
- prepare() {
30982
- return this._prepare(false);
30983
- }
30984
- async execute() {
30985
- return this.config.returning ? this.all() : this.run();
30986
- }
30987
- $dynamic() {
30988
- return this;
30989
- }
30990
- };
30991
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
30992
- }
30993
- });
30994
-
30995
- // ../drizzle-orm/dist/sqlite-core/view-base.js
30996
- var _a199, _b143, SQLiteViewBase;
30997
- var init_view_base2 = __esm({
30998
- "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30999
- "use strict";
31000
- init_entity();
31001
- init_sql();
31002
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31003
- };
31004
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
31005
- }
31006
- });
31007
-
31008
- // ../drizzle-orm/dist/sqlite-core/dialect.js
31009
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
31010
- var init_dialect2 = __esm({
31011
- "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31012
- "use strict";
31013
- init_alias();
31014
- init_casing();
31015
- init_column();
31016
- init_entity();
31017
- init_errors();
31018
- init_relations();
31019
- init_sql2();
31020
- init_sql();
31021
- init_columns2();
31022
- init_table3();
31023
- init_subquery();
30989
+ init_subquery();
31024
30990
  init_table();
31025
30991
  init_utils2();
31026
30992
  init_view_common();
31027
30993
  init_view_base2();
31028
- _a200 = entityKind;
30994
+ _a198 = entityKind;
31029
30995
  SQLiteDialect = class {
31030
30996
  constructor(config) {
31031
30997
  /** @internal */
@@ -31078,14 +31044,16 @@ var init_dialect2 = __esm({
31078
31044
  return [res];
31079
31045
  }));
31080
31046
  }
31081
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31047
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31082
31048
  const withSql = this.buildWithCTE(withList);
31083
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);
31084
31052
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31085
31053
  const whereSql = where ? sql` where ${where}` : void 0;
31086
31054
  const orderBySql = this.buildOrderBy(orderBy);
31087
31055
  const limitSql = this.buildLimit(limit);
31088
- 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}`;
31089
31057
  }
31090
31058
  /**
31091
31059
  * Builds selection SQL with provided fields/expressions
@@ -31138,6 +31106,37 @@ var init_dialect2 = __esm({
31138
31106
  });
31139
31107
  return sql.join(chunks);
31140
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
+ }
31141
31140
  buildLimit(limit) {
31142
31141
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31143
31142
  }
@@ -31153,6 +31152,12 @@ var init_dialect2 = __esm({
31153
31152
  }
31154
31153
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31155
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
+ }
31156
31161
  buildSelectQuery({
31157
31162
  withList,
31158
31163
  fields,
@@ -31183,38 +31188,8 @@ var init_dialect2 = __esm({
31183
31188
  const withSql = this.buildWithCTE(withList);
31184
31189
  const distinctSql = distinct ? sql` distinct` : void 0;
31185
31190
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31186
- const tableSql = (() => {
31187
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31188
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31189
- }
31190
- return table4;
31191
- })();
31192
- const joinsArray = [];
31193
- if (joins) {
31194
- for (const [index4, joinMeta] of joins.entries()) {
31195
- if (index4 === 0) {
31196
- joinsArray.push(sql` `);
31197
- }
31198
- const table22 = joinMeta.table;
31199
- if (is(table22, SQLiteTable)) {
31200
- const tableName = table22[SQLiteTable.Symbol.Name];
31201
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31202
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31203
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31204
- joinsArray.push(
31205
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31206
- );
31207
- } else {
31208
- joinsArray.push(
31209
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31210
- );
31211
- }
31212
- if (index4 < joins.length - 1) {
31213
- joinsArray.push(sql` `);
31214
- }
31215
- }
31216
- }
31217
- const joinsSql = sql.join(joinsArray);
31191
+ const tableSql = this.buildFromTable(table4);
31192
+ const joinsSql = this.buildJoins(joins);
31218
31193
  const whereSql = where ? sql` where ${where}` : void 0;
31219
31194
  const havingSql = having ? sql` having ${having}` : void 0;
31220
31195
  const groupByList = [];
@@ -31280,45 +31255,56 @@ var init_dialect2 = __esm({
31280
31255
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31281
31256
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31282
31257
  }
31283
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31258
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31284
31259
  const valuesSqlList = [];
31285
31260
  const columns = table4[Table2.Symbol.Columns];
31286
31261
  const colEntries = Object.entries(columns).filter(
31287
31262
  ([_2, col]) => !col.shouldDisableInsert()
31288
31263
  );
31289
31264
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
31290
- for (const [valueIndex, value] of values.entries()) {
31291
- const valueList = [];
31292
- for (const [fieldName, col] of colEntries) {
31293
- const colValue = value[fieldName];
31294
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31295
- let defaultValue;
31296
- if (col.default !== null && col.default !== void 0) {
31297
- defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31298
- } else if (col.defaultFn !== void 0) {
31299
- const defaultFnResult = col.defaultFn();
31300
- defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31301
- } else if (!col.default && col.onUpdateFn !== void 0) {
31302
- const onUpdateFnResult = col.onUpdateFn();
31303
- 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);
31304
31293
  } else {
31305
- defaultValue = sql`null`;
31294
+ valueList.push(colValue);
31306
31295
  }
31307
- valueList.push(defaultValue);
31308
- } else {
31309
- valueList.push(colValue);
31310
31296
  }
31311
- }
31312
- valuesSqlList.push(valueList);
31313
- if (valueIndex < values.length - 1) {
31314
- valuesSqlList.push(sql`, `);
31297
+ valuesSqlList.push(valueList);
31298
+ if (valueIndex < values.length - 1) {
31299
+ valuesSqlList.push(sql`, `);
31300
+ }
31315
31301
  }
31316
31302
  }
31317
31303
  const withSql = this.buildWithCTE(withList);
31318
31304
  const valuesSql = sql.join(valuesSqlList);
31319
31305
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31320
31306
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31321
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31307
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31322
31308
  }
31323
31309
  sqlToQuery(sql2, invokeSource) {
31324
31310
  return sql2.toQuery({
@@ -31547,8 +31533,8 @@ var init_dialect2 = __esm({
31547
31533
  };
31548
31534
  }
31549
31535
  };
31550
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31551
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31536
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31537
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31552
31538
  migrate(migrations, session, config) {
31553
31539
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31554
31540
  const migrationTableCreate = sql`
@@ -31582,8 +31568,8 @@ var init_dialect2 = __esm({
31582
31568
  }
31583
31569
  }
31584
31570
  };
31585
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31586
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31571
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31572
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31587
31573
  async migrate(migrations, session, config) {
31588
31574
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31589
31575
  const migrationTableCreate = sql`
@@ -31612,7 +31598,7 @@ var init_dialect2 = __esm({
31612
31598
  });
31613
31599
  }
31614
31600
  };
31615
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31601
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31616
31602
  }
31617
31603
  });
31618
31604
 
@@ -31634,7 +31620,7 @@ function createSetOperator2(type, isAll) {
31634
31620
  return leftSelect.addSetOperators(setOperators);
31635
31621
  };
31636
31622
  }
31637
- 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;
31638
31624
  var init_select3 = __esm({
31639
31625
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31640
31626
  "use strict";
@@ -31648,7 +31634,7 @@ var init_select3 = __esm({
31648
31634
  init_utils2();
31649
31635
  init_view_common();
31650
31636
  init_view_base2();
31651
- _a203 = entityKind;
31637
+ _a201 = entityKind;
31652
31638
  SQLiteSelectBuilder = class {
31653
31639
  constructor(config) {
31654
31640
  __publicField(this, "fields");
@@ -31689,8 +31675,8 @@ var init_select3 = __esm({
31689
31675
  });
31690
31676
  }
31691
31677
  };
31692
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31693
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31678
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31679
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31694
31680
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31695
31681
  super();
31696
31682
  __publicField(this, "_");
@@ -32195,8 +32181,8 @@ var init_select3 = __esm({
32195
32181
  return this;
32196
32182
  }
32197
32183
  };
32198
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32199
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32184
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32185
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32200
32186
  constructor() {
32201
32187
  super(...arguments);
32202
32188
  __publicField(this, "run", (placeholderValues) => {
@@ -32234,7 +32220,7 @@ var init_select3 = __esm({
32234
32220
  return this.all();
32235
32221
  }
32236
32222
  };
32237
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32223
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32238
32224
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32239
32225
  getSQLiteSetOperators = () => ({
32240
32226
  union: union2,
@@ -32250,7 +32236,7 @@ var init_select3 = __esm({
32250
32236
  });
32251
32237
 
32252
32238
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32253
- var _a206, QueryBuilder2;
32239
+ var _a204, QueryBuilder2;
32254
32240
  var init_query_builder3 = __esm({
32255
32241
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32256
32242
  "use strict";
@@ -32259,7 +32245,7 @@ var init_query_builder3 = __esm({
32259
32245
  init_dialect2();
32260
32246
  init_subquery();
32261
32247
  init_select3();
32262
- _a206 = entityKind;
32248
+ _a204 = entityKind;
32263
32249
  QueryBuilder2 = class {
32264
32250
  constructor(dialect4) {
32265
32251
  __publicField(this, "dialect");
@@ -32321,7 +32307,185 @@ var init_query_builder3 = __esm({
32321
32307
  return this.dialect;
32322
32308
  }
32323
32309
  };
32324
- __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");
32325
32489
  }
32326
32490
  });
32327
32491
 
@@ -32341,8 +32505,11 @@ var init_update2 = __esm({
32341
32505
  init_query_promise();
32342
32506
  init_selection_proxy();
32343
32507
  init_table3();
32508
+ init_subquery();
32344
32509
  init_table();
32345
32510
  init_utils2();
32511
+ init_view_common();
32512
+ init_view_base2();
32346
32513
  _a207 = entityKind;
32347
32514
  SQLiteUpdateBuilder = class {
32348
32515
  constructor(table4, session, dialect4, withList) {
@@ -32367,6 +32534,10 @@ var init_update2 = __esm({
32367
32534
  super();
32368
32535
  /** @internal */
32369
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"));
32370
32541
  __publicField(this, "run", (placeholderValues) => {
32371
32542
  return this._prepare().run(placeholderValues);
32372
32543
  });
@@ -32381,7 +32552,34 @@ var init_update2 = __esm({
32381
32552
  });
32382
32553
  this.session = session;
32383
32554
  this.dialect = dialect4;
32384
- 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
+ };
32385
32583
  }
32386
32584
  /**
32387
32585
  * Adds a 'where' clause to the query.
@@ -35679,131 +35877,6 @@ var init_delete3 = __esm({
35679
35877
  }
35680
35878
  });
35681
35879
 
35682
- // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
35683
- var _a299, MySqlInsertBuilder, _a300, _b222, MySqlInsertBase;
35684
- var init_insert3 = __esm({
35685
- "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
35686
- "use strict";
35687
- init_entity();
35688
- init_query_promise();
35689
- init_sql();
35690
- init_table();
35691
- init_utils2();
35692
- _a299 = entityKind;
35693
- MySqlInsertBuilder = class {
35694
- constructor(table4, session, dialect4) {
35695
- __publicField(this, "shouldIgnore", false);
35696
- this.table = table4;
35697
- this.session = session;
35698
- this.dialect = dialect4;
35699
- }
35700
- ignore() {
35701
- this.shouldIgnore = true;
35702
- return this;
35703
- }
35704
- values(values) {
35705
- values = Array.isArray(values) ? values : [values];
35706
- if (values.length === 0) {
35707
- throw new Error("values() must be called with at least one value");
35708
- }
35709
- const mappedValues = values.map((entry) => {
35710
- const result = {};
35711
- const cols = this.table[Table2.Symbol.Columns];
35712
- for (const colKey of Object.keys(entry)) {
35713
- const colValue = entry[colKey];
35714
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
35715
- }
35716
- return result;
35717
- });
35718
- return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
35719
- }
35720
- };
35721
- __publicField(MySqlInsertBuilder, _a299, "MySqlInsertBuilder");
35722
- MySqlInsertBase = class extends (_b222 = QueryPromise, _a300 = entityKind, _b222) {
35723
- constructor(table4, values, ignore, session, dialect4) {
35724
- super();
35725
- __publicField(this, "config");
35726
- __publicField(this, "execute", (placeholderValues) => {
35727
- return this.prepare().execute(placeholderValues);
35728
- });
35729
- __publicField(this, "createIterator", () => {
35730
- const self2 = this;
35731
- return async function* (placeholderValues) {
35732
- yield* self2.prepare().iterator(placeholderValues);
35733
- };
35734
- });
35735
- __publicField(this, "iterator", this.createIterator());
35736
- this.session = session;
35737
- this.dialect = dialect4;
35738
- this.config = { table: table4, values, ignore };
35739
- }
35740
- /**
35741
- * Adds an `on duplicate key update` clause to the query.
35742
- *
35743
- * Calling this method will update update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35744
- *
35745
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35746
- *
35747
- * @param config The `set` clause
35748
- *
35749
- * @example
35750
- * ```ts
35751
- * await db.insert(cars)
35752
- * .values({ id: 1, brand: 'BMW'})
35753
- * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
35754
- * ```
35755
- *
35756
- * 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:
35757
- *
35758
- * ```ts
35759
- * import { sql } from 'drizzle-orm';
35760
- *
35761
- * await db.insert(cars)
35762
- * .values({ id: 1, brand: 'BMW' })
35763
- * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
35764
- * ```
35765
- */
35766
- onDuplicateKeyUpdate(config) {
35767
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
35768
- this.config.onConflict = sql`update ${setSql}`;
35769
- return this;
35770
- }
35771
- $returningId() {
35772
- const returning = [];
35773
- for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
35774
- if (value.primary) {
35775
- returning.push({ field: value, path: [key] });
35776
- }
35777
- }
35778
- this.config.returning = orderSelectedFields(this.config.table[Table2.Symbol.Columns]);
35779
- return this;
35780
- }
35781
- /** @internal */
35782
- getSQL() {
35783
- return this.dialect.buildInsertQuery(this.config).sql;
35784
- }
35785
- toSQL() {
35786
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
35787
- return rest;
35788
- }
35789
- prepare() {
35790
- const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
35791
- return this.session.prepareQuery(
35792
- this.dialect.sqlToQuery(sql2),
35793
- void 0,
35794
- void 0,
35795
- generatedIds,
35796
- this.config.returning
35797
- );
35798
- }
35799
- $dynamic() {
35800
- return this;
35801
- }
35802
- };
35803
- __publicField(MySqlInsertBase, _a300, "MySqlInsert");
35804
- }
35805
- });
35806
-
35807
35880
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35808
35881
  function getMySqlColumnBuilders() {
35809
35882
  return {
@@ -35884,7 +35957,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35884
35957
  }
35885
35958
  return table4;
35886
35959
  }
35887
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
35960
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35888
35961
  var init_table4 = __esm({
35889
35962
  "../drizzle-orm/dist/mysql-core/table.js"() {
35890
35963
  "use strict";
@@ -35892,15 +35965,15 @@ var init_table4 = __esm({
35892
35965
  init_table();
35893
35966
  init_all3();
35894
35967
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35895
- 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) {
35896
35969
  constructor() {
35897
35970
  super(...arguments);
35898
35971
  /** @internal */
35899
35972
  __publicField(this, _c9);
35900
35973
  /** @internal */
35901
- __publicField(this, _b223, []);
35974
+ __publicField(this, _b222, []);
35902
35975
  /** @internal */
35903
- __publicField(this, _a301);
35976
+ __publicField(this, _a299);
35904
35977
  }
35905
35978
  };
35906
35979
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35915,20 +35988,20 @@ var init_table4 = __esm({
35915
35988
  });
35916
35989
 
35917
35990
  // ../drizzle-orm/dist/mysql-core/view-base.js
35918
- var _a302, _b224, MySqlViewBase;
35991
+ var _a300, _b223, MySqlViewBase;
35919
35992
  var init_view_base3 = __esm({
35920
35993
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35921
35994
  "use strict";
35922
35995
  init_entity();
35923
35996
  init_sql();
35924
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
35997
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35925
35998
  };
35926
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
35999
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35927
36000
  }
35928
36001
  });
35929
36002
 
35930
36003
  // ../drizzle-orm/dist/mysql-core/dialect.js
35931
- var _a303, MySqlDialect;
36004
+ var _a301, MySqlDialect;
35932
36005
  var init_dialect3 = __esm({
35933
36006
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35934
36007
  "use strict";
@@ -35947,7 +36020,7 @@ var init_dialect3 = __esm({
35947
36020
  init_common4();
35948
36021
  init_table4();
35949
36022
  init_view_base3();
35950
- _a303 = entityKind;
36023
+ _a301 = entityKind;
35951
36024
  MySqlDialect = class {
35952
36025
  constructor(config) {
35953
36026
  /** @internal */
@@ -36230,7 +36303,7 @@ var init_dialect3 = __esm({
36230
36303
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36231
36304
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36232
36305
  }
36233
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36306
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36234
36307
  const valuesSqlList = [];
36235
36308
  const columns = table4[Table2.Symbol.Columns];
36236
36309
  const colEntries = Object.entries(columns).filter(
@@ -36238,42 +36311,53 @@ var init_dialect3 = __esm({
36238
36311
  );
36239
36312
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36240
36313
  const generatedIdsResponse = [];
36241
- for (const [valueIndex, value] of values.entries()) {
36242
- const generatedIds = {};
36243
- const valueList = [];
36244
- for (const [fieldName, col] of colEntries) {
36245
- const colValue = value[fieldName];
36246
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36247
- if (col.defaultFn !== void 0) {
36248
- const defaultFnResult = col.defaultFn();
36249
- generatedIds[fieldName] = defaultFnResult;
36250
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36251
- valueList.push(defaultValue);
36252
- } else if (!col.default && col.onUpdateFn !== void 0) {
36253
- const onUpdateFnResult = col.onUpdateFn();
36254
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36255
- 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
+ }
36256
36342
  } else {
36257
- valueList.push(sql`default`);
36258
- }
36259
- } else {
36260
- if (col.defaultFn && is(colValue, Param)) {
36261
- generatedIds[fieldName] = colValue.value;
36343
+ if (col.defaultFn && is(colValue, Param)) {
36344
+ generatedIds[fieldName] = colValue.value;
36345
+ }
36346
+ valueList.push(colValue);
36262
36347
  }
36263
- valueList.push(colValue);
36264
36348
  }
36265
- }
36266
- generatedIdsResponse.push(generatedIds);
36267
- valuesSqlList.push(valueList);
36268
- if (valueIndex < values.length - 1) {
36269
- valuesSqlList.push(sql`, `);
36349
+ generatedIdsResponse.push(generatedIds);
36350
+ valuesSqlList.push(valueList);
36351
+ if (valueIndex < values.length - 1) {
36352
+ valuesSqlList.push(sql`, `);
36353
+ }
36270
36354
  }
36271
36355
  }
36272
36356
  const valuesSql = sql.join(valuesSqlList);
36273
36357
  const ignoreSql = ignore ? sql` ignore` : void 0;
36274
36358
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36275
36359
  return {
36276
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36360
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36277
36361
  generatedIds: generatedIdsResponse
36278
36362
  };
36279
36363
  }
@@ -36733,7 +36817,7 @@ var init_dialect3 = __esm({
36733
36817
  };
36734
36818
  }
36735
36819
  };
36736
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36820
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36737
36821
  }
36738
36822
  });
36739
36823
 
@@ -36755,7 +36839,7 @@ function createSetOperator3(type, isAll) {
36755
36839
  return leftSelect.addSetOperators(setOperators);
36756
36840
  };
36757
36841
  }
36758
- 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;
36759
36843
  var init_select4 = __esm({
36760
36844
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36761
36845
  "use strict";
@@ -36770,7 +36854,7 @@ var init_select4 = __esm({
36770
36854
  init_utils2();
36771
36855
  init_view_common();
36772
36856
  init_view_base3();
36773
- _a304 = entityKind;
36857
+ _a302 = entityKind;
36774
36858
  MySqlSelectBuilder = class {
36775
36859
  constructor(config) {
36776
36860
  __publicField(this, "fields");
@@ -36815,8 +36899,8 @@ var init_select4 = __esm({
36815
36899
  );
36816
36900
  }
36817
36901
  };
36818
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36819
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36902
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36903
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36820
36904
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36821
36905
  super();
36822
36906
  __publicField(this, "_");
@@ -37417,8 +37501,8 @@ var init_select4 = __esm({
37417
37501
  return this;
37418
37502
  }
37419
37503
  };
37420
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37421
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37504
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37505
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37422
37506
  constructor() {
37423
37507
  super(...arguments);
37424
37508
  __publicField(this, "execute", (placeholderValues) => {
@@ -37442,7 +37526,7 @@ var init_select4 = __esm({
37442
37526
  return query;
37443
37527
  }
37444
37528
  };
37445
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37529
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37446
37530
  applyMixins(MySqlSelectBase, [QueryPromise]);
37447
37531
  getMySqlSetOperators = () => ({
37448
37532
  union: union3,
@@ -37462,7 +37546,7 @@ var init_select4 = __esm({
37462
37546
  });
37463
37547
 
37464
37548
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37465
- var _a307, QueryBuilder3;
37549
+ var _a305, QueryBuilder3;
37466
37550
  var init_query_builder4 = __esm({
37467
37551
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37468
37552
  "use strict";
@@ -37471,7 +37555,7 @@ var init_query_builder4 = __esm({
37471
37555
  init_selection_proxy();
37472
37556
  init_subquery();
37473
37557
  init_select4();
37474
- _a307 = entityKind;
37558
+ _a305 = entityKind;
37475
37559
  QueryBuilder3 = class {
37476
37560
  constructor(dialect4) {
37477
37561
  __publicField(this, "dialect");
@@ -37533,7 +37617,142 @@ var init_query_builder4 = __esm({
37533
37617
  return this.dialect;
37534
37618
  }
37535
37619
  };
37536
- __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");
37537
37756
  }
37538
37757
  });
37539
37758