drizzle-kit 0.28.0-cc4f208 → 0.28.1-08d2486

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/api.js +1011 -714
  2. package/api.mjs +1011 -714
  3. package/bin.cjs +673 -78
  4. package/package.json +1 -1
package/api.js CHANGED
@@ -21121,7 +21121,7 @@ var version;
21121
21121
  var init_version = __esm({
21122
21122
  "../drizzle-orm/dist/version.js"() {
21123
21123
  "use strict";
21124
- version = "0.36.1";
21124
+ version = "0.36.4";
21125
21125
  }
21126
21126
  });
21127
21127
 
@@ -22144,7 +22144,7 @@ function haveSameKeys(left, right) {
22144
22144
  }
22145
22145
  function mapUpdateSet(table4, values) {
22146
22146
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22147
- if (is(value, SQL)) {
22147
+ if (is(value, SQL) || is(value, Column2)) {
22148
22148
  return [key, value];
22149
22149
  } else {
22150
22150
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24583,9 +24583,10 @@ var init_delete = __esm({
24583
24583
  constructor(table4, session, dialect4, withList) {
24584
24584
  super();
24585
24585
  __publicField(this, "config");
24586
+ __publicField(this, "authToken");
24586
24587
  __publicField(this, "execute", (placeholderValues) => {
24587
24588
  return tracer.startActiveSpan("drizzle.operation", () => {
24588
- return this._prepare().execute(placeholderValues);
24589
+ return this._prepare().execute(placeholderValues, this.authToken);
24589
24590
  });
24590
24591
  });
24591
24592
  this.session = session;
@@ -24646,167 +24647,16 @@ var init_delete = __esm({
24646
24647
  prepare(name2) {
24647
24648
  return this._prepare(name2);
24648
24649
  }
24649
- $dynamic() {
24650
- return this;
24651
- }
24652
- };
24653
- __publicField(PgDeleteBase, _a124, "PgDelete");
24654
- }
24655
- });
24656
-
24657
- // ../drizzle-orm/dist/pg-core/query-builders/insert.js
24658
- var _a125, PgInsertBuilder, _a126, _b98, PgInsertBase;
24659
- var init_insert = __esm({
24660
- "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
24661
- "use strict";
24662
- init_entity();
24663
- init_query_promise();
24664
- init_sql();
24665
- init_table();
24666
- init_tracing();
24667
- init_utils2();
24668
- _a125 = entityKind;
24669
- PgInsertBuilder = class {
24670
- constructor(table4, session, dialect4, withList) {
24671
- this.table = table4;
24672
- this.session = session;
24673
- this.dialect = dialect4;
24674
- this.withList = withList;
24675
- }
24676
- values(values) {
24677
- values = Array.isArray(values) ? values : [values];
24678
- if (values.length === 0) {
24679
- throw new Error("values() must be called with at least one value");
24680
- }
24681
- const mappedValues = values.map((entry) => {
24682
- const result = {};
24683
- const cols = this.table[Table2.Symbol.Columns];
24684
- for (const colKey of Object.keys(entry)) {
24685
- const colValue = entry[colKey];
24686
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
24687
- }
24688
- return result;
24689
- });
24690
- return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
24691
- }
24692
- };
24693
- __publicField(PgInsertBuilder, _a125, "PgInsertBuilder");
24694
- PgInsertBase = class extends (_b98 = QueryPromise, _a126 = entityKind, _b98) {
24695
- constructor(table4, values, session, dialect4, withList) {
24696
- super();
24697
- __publicField(this, "config");
24698
- __publicField(this, "execute", (placeholderValues) => {
24699
- return tracer.startActiveSpan("drizzle.operation", () => {
24700
- return this._prepare().execute(placeholderValues);
24701
- });
24702
- });
24703
- this.session = session;
24704
- this.dialect = dialect4;
24705
- this.config = { table: table4, values, withList };
24706
- }
24707
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
24708
- this.config.returning = orderSelectedFields(fields);
24709
- return this;
24710
- }
24711
- /**
24712
- * Adds an `on conflict do nothing` clause to the query.
24713
- *
24714
- * Calling this method simply avoids inserting a row as its alternative action.
24715
- *
24716
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
24717
- *
24718
- * @param config The `target` and `where` clauses.
24719
- *
24720
- * @example
24721
- * ```ts
24722
- * // Insert one row and cancel the insert if there's a conflict
24723
- * await db.insert(cars)
24724
- * .values({ id: 1, brand: 'BMW' })
24725
- * .onConflictDoNothing();
24726
- *
24727
- * // Explicitly specify conflict target
24728
- * await db.insert(cars)
24729
- * .values({ id: 1, brand: 'BMW' })
24730
- * .onConflictDoNothing({ target: cars.id });
24731
- * ```
24732
- */
24733
- onConflictDoNothing(config = {}) {
24734
- if (config.target === void 0) {
24735
- this.config.onConflict = sql`do nothing`;
24736
- } else {
24737
- let targetColumn = "";
24738
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24739
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24740
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
24741
- }
24742
- return this;
24743
- }
24744
- /**
24745
- * Adds an `on conflict do update` clause to the query.
24746
- *
24747
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
24748
- *
24749
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
24750
- *
24751
- * @param config The `target`, `set` and `where` clauses.
24752
- *
24753
- * @example
24754
- * ```ts
24755
- * // Update the row if there's a conflict
24756
- * await db.insert(cars)
24757
- * .values({ id: 1, brand: 'BMW' })
24758
- * .onConflictDoUpdate({
24759
- * target: cars.id,
24760
- * set: { brand: 'Porsche' }
24761
- * });
24762
- *
24763
- * // Upsert with 'where' clause
24764
- * await db.insert(cars)
24765
- * .values({ id: 1, brand: 'BMW' })
24766
- * .onConflictDoUpdate({
24767
- * target: cars.id,
24768
- * set: { brand: 'newBMW' },
24769
- * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
24770
- * });
24771
- * ```
24772
- */
24773
- onConflictDoUpdate(config) {
24774
- if (config.where && (config.targetWhere || config.setWhere)) {
24775
- throw new Error(
24776
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
24777
- );
24778
- }
24779
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24780
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
24781
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
24782
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
24783
- let targetColumn = "";
24784
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24785
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
24786
- return this;
24787
- }
24788
- /** @internal */
24789
- getSQL() {
24790
- return this.dialect.buildInsertQuery(this.config);
24791
- }
24792
- toSQL() {
24793
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
24794
- return rest;
24795
- }
24796
24650
  /** @internal */
24797
- _prepare(name2) {
24798
- return tracer.startActiveSpan("drizzle.prepareQuery", () => {
24799
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
24800
- });
24801
- }
24802
- prepare(name2) {
24803
- return this._prepare(name2);
24651
+ setToken(token) {
24652
+ this.authToken = token;
24653
+ return this;
24804
24654
  }
24805
24655
  $dynamic() {
24806
24656
  return this;
24807
24657
  }
24808
24658
  };
24809
- __publicField(PgInsertBase, _a126, "PgInsert");
24659
+ __publicField(PgDeleteBase, _a124, "PgDelete");
24810
24660
  }
24811
24661
  });
24812
24662
 
@@ -24825,13 +24675,13 @@ function toCamelCase(input) {
24825
24675
  function noopCase(input) {
24826
24676
  return input;
24827
24677
  }
24828
- var _a127, CasingCache;
24678
+ var _a125, CasingCache;
24829
24679
  var init_casing = __esm({
24830
24680
  "../drizzle-orm/dist/casing.js"() {
24831
24681
  "use strict";
24832
24682
  init_entity();
24833
24683
  init_table();
24834
- _a127 = entityKind;
24684
+ _a125 = entityKind;
24835
24685
  CasingCache = class {
24836
24686
  constructor(casing2) {
24837
24687
  /** @internal */
@@ -24868,25 +24718,25 @@ var init_casing = __esm({
24868
24718
  this.cachedTables = {};
24869
24719
  }
24870
24720
  };
24871
- __publicField(CasingCache, _a127, "CasingCache");
24721
+ __publicField(CasingCache, _a125, "CasingCache");
24872
24722
  }
24873
24723
  });
24874
24724
 
24875
24725
  // ../drizzle-orm/dist/pg-core/view-base.js
24876
- var _a128, _b99, PgViewBase;
24726
+ var _a126, _b98, PgViewBase;
24877
24727
  var init_view_base = __esm({
24878
24728
  "../drizzle-orm/dist/pg-core/view-base.js"() {
24879
24729
  "use strict";
24880
24730
  init_entity();
24881
24731
  init_sql();
24882
- PgViewBase = class extends (_b99 = View3, _a128 = entityKind, _b99) {
24732
+ PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
24883
24733
  };
24884
- __publicField(PgViewBase, _a128, "PgViewBase");
24734
+ __publicField(PgViewBase, _a126, "PgViewBase");
24885
24735
  }
24886
24736
  });
24887
24737
 
24888
24738
  // ../drizzle-orm/dist/pg-core/dialect.js
24889
- var _a129, PgDialect;
24739
+ var _a127, PgDialect;
24890
24740
  var init_dialect = __esm({
24891
24741
  "../drizzle-orm/dist/pg-core/dialect.js"() {
24892
24742
  "use strict";
@@ -24905,7 +24755,7 @@ var init_dialect = __esm({
24905
24755
  init_utils2();
24906
24756
  init_view_common();
24907
24757
  init_view_base();
24908
- _a129 = entityKind;
24758
+ _a127 = entityKind;
24909
24759
  PgDialect = class {
24910
24760
  constructor(config) {
24911
24761
  /** @internal */
@@ -24985,12 +24835,19 @@ var init_dialect = __esm({
24985
24835
  return [res];
24986
24836
  }));
24987
24837
  }
24988
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24838
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24989
24839
  const withSql = this.buildWithCTE(withList);
24840
+ const tableName = table4[PgTable.Symbol.Name];
24841
+ const tableSchema = table4[PgTable.Symbol.Schema];
24842
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24843
+ const alias = tableName === origTableName ? void 0 : tableName;
24844
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
24990
24845
  const setSql = this.buildUpdateSet(table4, set);
24991
- const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
24846
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
24847
+ const joinsSql = this.buildJoins(joins);
24848
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
24992
24849
  const whereSql = where ? sql` where ${where}` : void 0;
24993
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
24850
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24994
24851
  }
24995
24852
  /**
24996
24853
  * Builds selection SQL with provided fields/expressions
@@ -25042,6 +24899,54 @@ var init_dialect = __esm({
25042
24899
  });
25043
24900
  return sql.join(chunks);
25044
24901
  }
24902
+ buildJoins(joins) {
24903
+ if (!joins || joins.length === 0) {
24904
+ return void 0;
24905
+ }
24906
+ const joinsArray = [];
24907
+ for (const [index4, joinMeta] of joins.entries()) {
24908
+ if (index4 === 0) {
24909
+ joinsArray.push(sql` `);
24910
+ }
24911
+ const table4 = joinMeta.table;
24912
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
24913
+ if (is(table4, PgTable)) {
24914
+ const tableName = table4[PgTable.Symbol.Name];
24915
+ const tableSchema = table4[PgTable.Symbol.Schema];
24916
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24917
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
24918
+ joinsArray.push(
24919
+ 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}`
24920
+ );
24921
+ } else if (is(table4, View3)) {
24922
+ const viewName = table4[ViewBaseConfig].name;
24923
+ const viewSchema = table4[ViewBaseConfig].schema;
24924
+ const origViewName = table4[ViewBaseConfig].originalName;
24925
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
24926
+ joinsArray.push(
24927
+ 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}`
24928
+ );
24929
+ } else {
24930
+ joinsArray.push(
24931
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table4} on ${joinMeta.on}`
24932
+ );
24933
+ }
24934
+ if (index4 < joins.length - 1) {
24935
+ joinsArray.push(sql` `);
24936
+ }
24937
+ }
24938
+ return sql.join(joinsArray);
24939
+ }
24940
+ buildFromTable(table4) {
24941
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
24942
+ let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
24943
+ if (table4[Table2.Symbol.Schema]) {
24944
+ fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
24945
+ }
24946
+ return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
24947
+ }
24948
+ return table4;
24949
+ }
25045
24950
  buildSelectQuery({
25046
24951
  withList,
25047
24952
  fields,
@@ -25076,51 +24981,8 @@ var init_dialect = __esm({
25076
24981
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25077
24982
  }
25078
24983
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25079
- const tableSql = (() => {
25080
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25081
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25082
- if (table4[Table2.Symbol.Schema]) {
25083
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25084
- }
25085
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25086
- }
25087
- return table4;
25088
- })();
25089
- const joinsArray = [];
25090
- if (joins) {
25091
- for (const [index4, joinMeta] of joins.entries()) {
25092
- if (index4 === 0) {
25093
- joinsArray.push(sql` `);
25094
- }
25095
- const table22 = joinMeta.table;
25096
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25097
- if (is(table22, PgTable)) {
25098
- const tableName = table22[PgTable.Symbol.Name];
25099
- const tableSchema = table22[PgTable.Symbol.Schema];
25100
- const origTableName = table22[PgTable.Symbol.OriginalName];
25101
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25102
- joinsArray.push(
25103
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25104
- );
25105
- } else if (is(table22, View3)) {
25106
- const viewName = table22[ViewBaseConfig].name;
25107
- const viewSchema = table22[ViewBaseConfig].schema;
25108
- const origViewName = table22[ViewBaseConfig].originalName;
25109
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25110
- joinsArray.push(
25111
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25112
- );
25113
- } else {
25114
- joinsArray.push(
25115
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25116
- );
25117
- }
25118
- if (index4 < joins.length - 1) {
25119
- joinsArray.push(sql` `);
25120
- }
25121
- }
25122
- }
25123
- const joinsSql = sql.join(joinsArray);
24984
+ const tableSql = this.buildFromTable(table4);
24985
+ const joinsSql = this.buildJoins(joins);
25124
24986
  const whereSql = where ? sql` where ${where}` : void 0;
25125
24987
  const havingSql = having ? sql` having ${having}` : void 0;
25126
24988
  let orderBySql;
@@ -25201,43 +25063,55 @@ var init_dialect = __esm({
25201
25063
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
25202
25064
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
25203
25065
  }
25204
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
25066
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }) {
25205
25067
  const valuesSqlList = [];
25206
25068
  const columns = table4[Table2.Symbol.Columns];
25207
25069
  const colEntries = Object.entries(columns).filter(([_2, col]) => !col.shouldDisableInsert());
25208
25070
  const insertOrder = colEntries.map(
25209
25071
  ([, column4]) => sql.identifier(this.casing.getColumnCasing(column4))
25210
25072
  );
25211
- for (const [valueIndex, value] of values.entries()) {
25212
- const valueList = [];
25213
- for (const [fieldName, col] of colEntries) {
25214
- const colValue = value[fieldName];
25215
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25216
- if (col.defaultFn !== void 0) {
25217
- const defaultFnResult = col.defaultFn();
25218
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25219
- valueList.push(defaultValue);
25220
- } else if (!col.default && col.onUpdateFn !== void 0) {
25221
- const onUpdateFnResult = col.onUpdateFn();
25222
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25223
- valueList.push(newValue);
25073
+ if (select) {
25074
+ const select2 = valuesOrSelect;
25075
+ if (is(select2, SQL)) {
25076
+ valuesSqlList.push(select2);
25077
+ } else {
25078
+ valuesSqlList.push(select2.getSQL());
25079
+ }
25080
+ } else {
25081
+ const values = valuesOrSelect;
25082
+ valuesSqlList.push(sql.raw("values "));
25083
+ for (const [valueIndex, value] of values.entries()) {
25084
+ const valueList = [];
25085
+ for (const [fieldName, col] of colEntries) {
25086
+ const colValue = value[fieldName];
25087
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25088
+ if (col.defaultFn !== void 0) {
25089
+ const defaultFnResult = col.defaultFn();
25090
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25091
+ valueList.push(defaultValue);
25092
+ } else if (!col.default && col.onUpdateFn !== void 0) {
25093
+ const onUpdateFnResult = col.onUpdateFn();
25094
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25095
+ valueList.push(newValue);
25096
+ } else {
25097
+ valueList.push(sql`default`);
25098
+ }
25224
25099
  } else {
25225
- valueList.push(sql`default`);
25100
+ valueList.push(colValue);
25226
25101
  }
25227
- } else {
25228
- valueList.push(colValue);
25229
25102
  }
25230
- }
25231
- valuesSqlList.push(valueList);
25232
- if (valueIndex < values.length - 1) {
25233
- valuesSqlList.push(sql`, `);
25103
+ valuesSqlList.push(valueList);
25104
+ if (valueIndex < values.length - 1) {
25105
+ valuesSqlList.push(sql`, `);
25106
+ }
25234
25107
  }
25235
25108
  }
25236
25109
  const withSql = this.buildWithCTE(withList);
25237
25110
  const valuesSql = sql.join(valuesSqlList);
25238
25111
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25239
25112
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
25240
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
25113
+ const overridingSql = overridingSystemValue_ === true ? sql`overriding system value ` : void 0;
25114
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${overridingSql}${valuesSql}${onConflictSql}${returningSql}`;
25241
25115
  }
25242
25116
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25243
25117
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25973,12 +25847,12 @@ var init_dialect = __esm({
25973
25847
  };
25974
25848
  }
25975
25849
  };
25976
- __publicField(PgDialect, _a129, "PgDialect");
25850
+ __publicField(PgDialect, _a127, "PgDialect");
25977
25851
  }
25978
25852
  });
25979
25853
 
25980
25854
  // ../drizzle-orm/dist/selection-proxy.js
25981
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25855
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25982
25856
  var init_selection_proxy = __esm({
25983
25857
  "../drizzle-orm/dist/selection-proxy.js"() {
25984
25858
  "use strict";
@@ -25988,7 +25862,7 @@ var init_selection_proxy = __esm({
25988
25862
  init_sql();
25989
25863
  init_subquery();
25990
25864
  init_view_common();
25991
- _a130 = entityKind;
25865
+ _a128 = entityKind;
25992
25866
  _SelectionProxyHandler = class _SelectionProxyHandler {
25993
25867
  constructor(config) {
25994
25868
  __publicField(this, "config");
@@ -26054,25 +25928,25 @@ var init_selection_proxy = __esm({
26054
25928
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26055
25929
  }
26056
25930
  };
26057
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25931
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26058
25932
  SelectionProxyHandler = _SelectionProxyHandler;
26059
25933
  }
26060
25934
  });
26061
25935
 
26062
25936
  // ../drizzle-orm/dist/query-builders/query-builder.js
26063
- var _a131, TypedQueryBuilder;
25937
+ var _a129, TypedQueryBuilder;
26064
25938
  var init_query_builder = __esm({
26065
25939
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26066
25940
  "use strict";
26067
25941
  init_entity();
26068
- _a131 = entityKind;
25942
+ _a129 = entityKind;
26069
25943
  TypedQueryBuilder = class {
26070
25944
  /** @internal */
26071
25945
  getSelectedFields() {
26072
25946
  return this._.selectedFields;
26073
25947
  }
26074
25948
  };
26075
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25949
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26076
25950
  }
26077
25951
  });
26078
25952
 
@@ -26094,7 +25968,7 @@ function createSetOperator(type, isAll) {
26094
25968
  return leftSelect.addSetOperators(setOperators);
26095
25969
  };
26096
25970
  }
26097
- var _a132, PgSelectBuilder, _a133, _b100, PgSelectQueryBuilderBase, _a134, _b101, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
25971
+ var _a130, PgSelectBuilder, _a131, _b99, PgSelectQueryBuilderBase, _a132, _b100, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
26098
25972
  var init_select2 = __esm({
26099
25973
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26100
25974
  "use strict";
@@ -26110,7 +25984,7 @@ var init_select2 = __esm({
26110
25984
  init_utils2();
26111
25985
  init_utils2();
26112
25986
  init_view_common();
26113
- _a132 = entityKind;
25987
+ _a130 = entityKind;
26114
25988
  PgSelectBuilder = class {
26115
25989
  constructor(config) {
26116
25990
  __publicField(this, "fields");
@@ -26118,6 +25992,7 @@ var init_select2 = __esm({
26118
25992
  __publicField(this, "dialect");
26119
25993
  __publicField(this, "withList", []);
26120
25994
  __publicField(this, "distinct");
25995
+ __publicField(this, "authToken");
26121
25996
  this.fields = config.fields;
26122
25997
  this.session = config.session;
26123
25998
  this.dialect = config.dialect;
@@ -26126,6 +26001,11 @@ var init_select2 = __esm({
26126
26001
  }
26127
26002
  this.distinct = config.distinct;
26128
26003
  }
26004
+ /** @internal */
26005
+ setToken(token) {
26006
+ this.authToken = token;
26007
+ return this;
26008
+ }
26129
26009
  /**
26130
26010
  * Specify the table, subquery, or other target that you're
26131
26011
  * building a select query against.
@@ -26156,11 +26036,11 @@ var init_select2 = __esm({
26156
26036
  dialect: this.dialect,
26157
26037
  withList: this.withList,
26158
26038
  distinct: this.distinct
26159
- });
26039
+ }).setToken(this.authToken);
26160
26040
  }
26161
26041
  };
26162
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26163
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26042
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26043
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26164
26044
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26165
26045
  super();
26166
26046
  __publicField(this, "_");
@@ -26760,19 +26640,20 @@ var init_select2 = __esm({
26760
26640
  return this;
26761
26641
  }
26762
26642
  };
26763
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26764
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26643
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26644
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26765
26645
  constructor() {
26766
26646
  super(...arguments);
26647
+ __publicField(this, "authToken");
26767
26648
  __publicField(this, "execute", (placeholderValues) => {
26768
26649
  return tracer.startActiveSpan("drizzle.operation", () => {
26769
- return this._prepare().execute(placeholderValues);
26650
+ return this._prepare().execute(placeholderValues, this.authToken);
26770
26651
  });
26771
26652
  });
26772
26653
  }
26773
26654
  /** @internal */
26774
26655
  _prepare(name2) {
26775
- const { session, config, dialect: dialect4, joinsNotNullableMap } = this;
26656
+ const { session, config, dialect: dialect4, joinsNotNullableMap, authToken } = this;
26776
26657
  if (!session) {
26777
26658
  throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
26778
26659
  }
@@ -26780,7 +26661,7 @@ var init_select2 = __esm({
26780
26661
  const fieldsList = orderSelectedFields(config.fields);
26781
26662
  const query = session.prepareQuery(dialect4.sqlToQuery(this.getSQL()), fieldsList, name2, true);
26782
26663
  query.joinsNotNullableMap = joinsNotNullableMap;
26783
- return query;
26664
+ return query.setToken(authToken);
26784
26665
  });
26785
26666
  }
26786
26667
  /**
@@ -26793,8 +26674,13 @@ var init_select2 = __esm({
26793
26674
  prepare(name2) {
26794
26675
  return this._prepare(name2);
26795
26676
  }
26677
+ /** @internal */
26678
+ setToken(token) {
26679
+ this.authToken = token;
26680
+ return this;
26681
+ }
26796
26682
  };
26797
- __publicField(PgSelectBase, _a134, "PgSelect");
26683
+ __publicField(PgSelectBase, _a132, "PgSelect");
26798
26684
  applyMixins(PgSelectBase, [QueryPromise]);
26799
26685
  getPgSetOperators = () => ({
26800
26686
  union,
@@ -26814,7 +26700,7 @@ var init_select2 = __esm({
26814
26700
  });
26815
26701
 
26816
26702
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26817
- var _a135, QueryBuilder;
26703
+ var _a133, QueryBuilder;
26818
26704
  var init_query_builder2 = __esm({
26819
26705
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26820
26706
  "use strict";
@@ -26823,7 +26709,7 @@ var init_query_builder2 = __esm({
26823
26709
  init_selection_proxy();
26824
26710
  init_subquery();
26825
26711
  init_select2();
26826
- _a135 = entityKind;
26712
+ _a133 = entityKind;
26827
26713
  QueryBuilder = class {
26828
26714
  constructor(dialect4) {
26829
26715
  __publicField(this, "dialect");
@@ -26904,40 +26790,232 @@ var init_query_builder2 = __esm({
26904
26790
  return this.dialect;
26905
26791
  }
26906
26792
  };
26907
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26793
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26908
26794
  }
26909
26795
  });
26910
26796
 
26911
- // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26912
- var _a136, _b102, PgRefreshMaterializedView;
26913
- var init_refresh_materialized_view = __esm({
26914
- "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26797
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26798
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26799
+ var init_insert = __esm({
26800
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26915
26801
  "use strict";
26916
26802
  init_entity();
26917
26803
  init_query_promise();
26804
+ init_sql();
26805
+ init_table();
26918
26806
  init_tracing();
26919
- PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26920
- constructor(view4, session, dialect4) {
26921
- super();
26922
- __publicField(this, "config");
26923
- __publicField(this, "execute", (placeholderValues) => {
26924
- return tracer.startActiveSpan("drizzle.operation", () => {
26925
- return this._prepare().execute(placeholderValues);
26926
- });
26927
- });
26807
+ init_utils2();
26808
+ init_query_builder2();
26809
+ _a134 = entityKind;
26810
+ PgInsertBuilder = class {
26811
+ constructor(table4, session, dialect4, withList, overridingSystemValue_) {
26812
+ __publicField(this, "authToken");
26813
+ this.table = table4;
26928
26814
  this.session = session;
26929
26815
  this.dialect = dialect4;
26930
- this.config = { view: view4 };
26816
+ this.withList = withList;
26817
+ this.overridingSystemValue_ = overridingSystemValue_;
26931
26818
  }
26932
- concurrently() {
26933
- if (this.config.withNoData !== void 0) {
26934
- throw new Error("Cannot use concurrently and withNoData together");
26935
- }
26936
- this.config.concurrently = true;
26819
+ /** @internal */
26820
+ setToken(token) {
26821
+ this.authToken = token;
26937
26822
  return this;
26938
26823
  }
26939
- withNoData() {
26940
- if (this.config.concurrently !== void 0) {
26824
+ overridingSystemValue() {
26825
+ this.overridingSystemValue_ = true;
26826
+ return this;
26827
+ }
26828
+ values(values) {
26829
+ values = Array.isArray(values) ? values : [values];
26830
+ if (values.length === 0) {
26831
+ throw new Error("values() must be called with at least one value");
26832
+ }
26833
+ const mappedValues = values.map((entry) => {
26834
+ const result = {};
26835
+ const cols = this.table[Table2.Symbol.Columns];
26836
+ for (const colKey of Object.keys(entry)) {
26837
+ const colValue = entry[colKey];
26838
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26839
+ }
26840
+ return result;
26841
+ });
26842
+ return new PgInsertBase(
26843
+ this.table,
26844
+ mappedValues,
26845
+ this.session,
26846
+ this.dialect,
26847
+ this.withList,
26848
+ false,
26849
+ this.overridingSystemValue_
26850
+ ).setToken(this.authToken);
26851
+ }
26852
+ select(selectQuery) {
26853
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26854
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26855
+ throw new Error(
26856
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26857
+ );
26858
+ }
26859
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26860
+ }
26861
+ };
26862
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26863
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26864
+ constructor(table4, values, session, dialect4, withList, select, overridingSystemValue_) {
26865
+ super();
26866
+ __publicField(this, "config");
26867
+ __publicField(this, "authToken");
26868
+ __publicField(this, "execute", (placeholderValues) => {
26869
+ return tracer.startActiveSpan("drizzle.operation", () => {
26870
+ return this._prepare().execute(placeholderValues, this.authToken);
26871
+ });
26872
+ });
26873
+ this.session = session;
26874
+ this.dialect = dialect4;
26875
+ this.config = { table: table4, values, withList, select, overridingSystemValue_ };
26876
+ }
26877
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26878
+ this.config.returning = orderSelectedFields(fields);
26879
+ return this;
26880
+ }
26881
+ /**
26882
+ * Adds an `on conflict do nothing` clause to the query.
26883
+ *
26884
+ * Calling this method simply avoids inserting a row as its alternative action.
26885
+ *
26886
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26887
+ *
26888
+ * @param config The `target` and `where` clauses.
26889
+ *
26890
+ * @example
26891
+ * ```ts
26892
+ * // Insert one row and cancel the insert if there's a conflict
26893
+ * await db.insert(cars)
26894
+ * .values({ id: 1, brand: 'BMW' })
26895
+ * .onConflictDoNothing();
26896
+ *
26897
+ * // Explicitly specify conflict target
26898
+ * await db.insert(cars)
26899
+ * .values({ id: 1, brand: 'BMW' })
26900
+ * .onConflictDoNothing({ target: cars.id });
26901
+ * ```
26902
+ */
26903
+ onConflictDoNothing(config = {}) {
26904
+ if (config.target === void 0) {
26905
+ this.config.onConflict = sql`do nothing`;
26906
+ } else {
26907
+ let targetColumn = "";
26908
+ 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));
26909
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26910
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26911
+ }
26912
+ return this;
26913
+ }
26914
+ /**
26915
+ * Adds an `on conflict do update` clause to the query.
26916
+ *
26917
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26918
+ *
26919
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26920
+ *
26921
+ * @param config The `target`, `set` and `where` clauses.
26922
+ *
26923
+ * @example
26924
+ * ```ts
26925
+ * // Update the row if there's a conflict
26926
+ * await db.insert(cars)
26927
+ * .values({ id: 1, brand: 'BMW' })
26928
+ * .onConflictDoUpdate({
26929
+ * target: cars.id,
26930
+ * set: { brand: 'Porsche' }
26931
+ * });
26932
+ *
26933
+ * // Upsert with 'where' clause
26934
+ * await db.insert(cars)
26935
+ * .values({ id: 1, brand: 'BMW' })
26936
+ * .onConflictDoUpdate({
26937
+ * target: cars.id,
26938
+ * set: { brand: 'newBMW' },
26939
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26940
+ * });
26941
+ * ```
26942
+ */
26943
+ onConflictDoUpdate(config) {
26944
+ if (config.where && (config.targetWhere || config.setWhere)) {
26945
+ throw new Error(
26946
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26947
+ );
26948
+ }
26949
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26950
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26951
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26952
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26953
+ let targetColumn = "";
26954
+ 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));
26955
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26956
+ return this;
26957
+ }
26958
+ /** @internal */
26959
+ getSQL() {
26960
+ return this.dialect.buildInsertQuery(this.config);
26961
+ }
26962
+ toSQL() {
26963
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26964
+ return rest;
26965
+ }
26966
+ /** @internal */
26967
+ _prepare(name2) {
26968
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26969
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26970
+ });
26971
+ }
26972
+ prepare(name2) {
26973
+ return this._prepare(name2);
26974
+ }
26975
+ /** @internal */
26976
+ setToken(token) {
26977
+ this.authToken = token;
26978
+ return this;
26979
+ }
26980
+ $dynamic() {
26981
+ return this;
26982
+ }
26983
+ };
26984
+ __publicField(PgInsertBase, _a135, "PgInsert");
26985
+ }
26986
+ });
26987
+
26988
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26989
+ var _a136, _b102, PgRefreshMaterializedView;
26990
+ var init_refresh_materialized_view = __esm({
26991
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26992
+ "use strict";
26993
+ init_entity();
26994
+ init_query_promise();
26995
+ init_tracing();
26996
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26997
+ constructor(view4, session, dialect4) {
26998
+ super();
26999
+ __publicField(this, "config");
27000
+ __publicField(this, "authToken");
27001
+ __publicField(this, "execute", (placeholderValues) => {
27002
+ return tracer.startActiveSpan("drizzle.operation", () => {
27003
+ return this._prepare().execute(placeholderValues, this.authToken);
27004
+ });
27005
+ });
27006
+ this.session = session;
27007
+ this.dialect = dialect4;
27008
+ this.config = { view: view4 };
27009
+ }
27010
+ concurrently() {
27011
+ if (this.config.withNoData !== void 0) {
27012
+ throw new Error("Cannot use concurrently and withNoData together");
27013
+ }
27014
+ this.config.concurrently = true;
27015
+ return this;
27016
+ }
27017
+ withNoData() {
27018
+ if (this.config.concurrently !== void 0) {
26941
27019
  throw new Error("Cannot use concurrently and withNoData together");
26942
27020
  }
26943
27021
  this.config.withNoData = true;
@@ -26960,6 +27038,11 @@ var init_refresh_materialized_view = __esm({
26960
27038
  prepare(name2) {
26961
27039
  return this._prepare(name2);
26962
27040
  }
27041
+ /** @internal */
27042
+ setToken(token) {
27043
+ this.authToken = token;
27044
+ return this;
27045
+ }
26963
27046
  };
26964
27047
  __publicField(PgRefreshMaterializedView, _a136, "PgRefreshMaterializedView");
26965
27048
  }
@@ -26978,17 +27061,27 @@ var init_update = __esm({
26978
27061
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26979
27062
  "use strict";
26980
27063
  init_entity();
27064
+ init_table2();
26981
27065
  init_query_promise();
27066
+ init_selection_proxy();
27067
+ init_sql();
27068
+ init_subquery();
26982
27069
  init_table();
26983
27070
  init_utils2();
27071
+ init_view_common();
26984
27072
  _a137 = entityKind;
26985
27073
  PgUpdateBuilder = class {
26986
27074
  constructor(table4, session, dialect4, withList) {
27075
+ __publicField(this, "authToken");
26987
27076
  this.table = table4;
26988
27077
  this.session = session;
26989
27078
  this.dialect = dialect4;
26990
27079
  this.withList = withList;
26991
27080
  }
27081
+ setToken(token) {
27082
+ this.authToken = token;
27083
+ return this;
27084
+ }
26992
27085
  set(values) {
26993
27086
  return new PgUpdateBase(
26994
27087
  this.table,
@@ -26996,7 +27089,7 @@ var init_update = __esm({
26996
27089
  this.session,
26997
27090
  this.dialect,
26998
27091
  this.withList
26999
- );
27092
+ ).setToken(this.authToken);
27000
27093
  }
27001
27094
  };
27002
27095
  __publicField(PgUpdateBuilder, _a137, "PgUpdateBuilder");
@@ -27004,12 +27097,86 @@ var init_update = __esm({
27004
27097
  constructor(table4, set, session, dialect4, withList) {
27005
27098
  super();
27006
27099
  __publicField(this, "config");
27100
+ __publicField(this, "tableName");
27101
+ __publicField(this, "joinsNotNullableMap");
27102
+ __publicField(this, "leftJoin", this.createJoin("left"));
27103
+ __publicField(this, "rightJoin", this.createJoin("right"));
27104
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27105
+ __publicField(this, "fullJoin", this.createJoin("full"));
27106
+ __publicField(this, "authToken");
27007
27107
  __publicField(this, "execute", (placeholderValues) => {
27008
- return this._prepare().execute(placeholderValues);
27108
+ return this._prepare().execute(placeholderValues, this.authToken);
27009
27109
  });
27010
27110
  this.session = session;
27011
27111
  this.dialect = dialect4;
27012
- this.config = { set, table: table4, withList };
27112
+ this.config = { set, table: table4, withList, joins: [] };
27113
+ this.tableName = getTableLikeName(table4);
27114
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27115
+ }
27116
+ from(source) {
27117
+ const tableName = getTableLikeName(source);
27118
+ if (typeof tableName === "string") {
27119
+ this.joinsNotNullableMap[tableName] = true;
27120
+ }
27121
+ this.config.from = source;
27122
+ return this;
27123
+ }
27124
+ getTableLikeFields(table4) {
27125
+ if (is(table4, PgTable)) {
27126
+ return table4[Table2.Symbol.Columns];
27127
+ } else if (is(table4, Subquery)) {
27128
+ return table4._.selectedFields;
27129
+ }
27130
+ return table4[ViewBaseConfig].selectedFields;
27131
+ }
27132
+ createJoin(joinType) {
27133
+ return (table4, on) => {
27134
+ const tableName = getTableLikeName(table4);
27135
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27136
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27137
+ }
27138
+ if (typeof on === "function") {
27139
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27140
+ on = on(
27141
+ new Proxy(
27142
+ this.config.table[Table2.Symbol.Columns],
27143
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27144
+ ),
27145
+ from && new Proxy(
27146
+ from,
27147
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27148
+ )
27149
+ );
27150
+ }
27151
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27152
+ if (typeof tableName === "string") {
27153
+ switch (joinType) {
27154
+ case "left": {
27155
+ this.joinsNotNullableMap[tableName] = false;
27156
+ break;
27157
+ }
27158
+ case "right": {
27159
+ this.joinsNotNullableMap = Object.fromEntries(
27160
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27161
+ );
27162
+ this.joinsNotNullableMap[tableName] = true;
27163
+ break;
27164
+ }
27165
+ case "inner": {
27166
+ this.joinsNotNullableMap[tableName] = true;
27167
+ break;
27168
+ }
27169
+ case "full": {
27170
+ this.joinsNotNullableMap = Object.fromEntries(
27171
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27172
+ );
27173
+ this.joinsNotNullableMap[tableName] = false;
27174
+ break;
27175
+ }
27176
+ }
27177
+ }
27178
+ return this;
27179
+ };
27013
27180
  }
27014
27181
  /**
27015
27182
  * Adds a 'where' clause to the query.
@@ -27048,7 +27215,24 @@ var init_update = __esm({
27048
27215
  this.config.where = where;
27049
27216
  return this;
27050
27217
  }
27051
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27218
+ returning(fields) {
27219
+ if (!fields) {
27220
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27221
+ if (this.config.from) {
27222
+ const tableName = getTableLikeName(this.config.from);
27223
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27224
+ const fromFields = this.getTableLikeFields(this.config.from);
27225
+ fields[tableName] = fromFields;
27226
+ }
27227
+ for (const join of this.config.joins) {
27228
+ const tableName2 = getTableLikeName(join.table);
27229
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27230
+ const fromFields = this.getTableLikeFields(join.table);
27231
+ fields[tableName2] = fromFields;
27232
+ }
27233
+ }
27234
+ }
27235
+ }
27052
27236
  this.config.returning = orderSelectedFields(fields);
27053
27237
  return this;
27054
27238
  }
@@ -27062,11 +27246,18 @@ var init_update = __esm({
27062
27246
  }
27063
27247
  /** @internal */
27064
27248
  _prepare(name2) {
27065
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27249
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27250
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27251
+ return query;
27066
27252
  }
27067
27253
  prepare(name2) {
27068
27254
  return this._prepare(name2);
27069
27255
  }
27256
+ /** @internal */
27257
+ setToken(token) {
27258
+ this.authToken = token;
27259
+ return this;
27260
+ }
27070
27261
  $dynamic() {
27071
27262
  return this;
27072
27263
  }
@@ -27100,6 +27291,7 @@ var init_count = __esm({
27100
27291
  constructor(params) {
27101
27292
  super(_PgCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
27102
27293
  __publicField(this, "sql");
27294
+ __publicField(this, "token");
27103
27295
  __publicField(this, _a139, "PgCountBuilder");
27104
27296
  __publicField(this, "session");
27105
27297
  this.params = params;
@@ -27116,8 +27308,13 @@ var init_count = __esm({
27116
27308
  static buildCount(source, filters) {
27117
27309
  return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters};`;
27118
27310
  }
27311
+ /** @intrnal */
27312
+ setToken(token) {
27313
+ this.token = token;
27314
+ return this;
27315
+ }
27119
27316
  then(onfulfilled, onrejected) {
27120
- return Promise.resolve(this.session.count(this.sql)).then(
27317
+ return Promise.resolve(this.session.count(this.sql, this.token)).then(
27121
27318
  onfulfilled,
27122
27319
  onrejected
27123
27320
  );
@@ -27194,6 +27391,7 @@ var init_query = __esm({
27194
27391
  PgRelationalQuery = class extends (_b105 = QueryPromise, _a141 = entityKind, _b105) {
27195
27392
  constructor(fullSchema, schema4, tableNamesMap, table4, tableConfig, dialect4, session, config, mode) {
27196
27393
  super();
27394
+ __publicField(this, "authToken");
27197
27395
  this.fullSchema = fullSchema;
27198
27396
  this.schema = schema4;
27199
27397
  this.tableNamesMap = tableNamesMap;
@@ -27251,9 +27449,14 @@ var init_query = __esm({
27251
27449
  toSQL() {
27252
27450
  return this._toSQL().builtQuery;
27253
27451
  }
27452
+ /** @internal */
27453
+ setToken(token) {
27454
+ this.authToken = token;
27455
+ return this;
27456
+ }
27254
27457
  execute() {
27255
27458
  return tracer.startActiveSpan("drizzle.operation", () => {
27256
- return this._prepare().execute();
27459
+ return this._prepare().execute(void 0, this.authToken);
27257
27460
  });
27258
27461
  }
27259
27462
  };
@@ -27316,6 +27519,7 @@ var init_db = __esm({
27316
27519
  PgDatabase = class {
27317
27520
  constructor(dialect4, session, schema4) {
27318
27521
  __publicField(this, "query");
27522
+ __publicField(this, "authToken");
27319
27523
  this.dialect = dialect4;
27320
27524
  this.session = session;
27321
27525
  this._ = schema4 ? {
@@ -27571,7 +27775,7 @@ var init_db = __esm({
27571
27775
  false
27572
27776
  );
27573
27777
  return new PgRaw(
27574
- () => prepared.execute(),
27778
+ () => prepared.execute(void 0, this.authToken),
27575
27779
  sequel,
27576
27780
  builtQuery,
27577
27781
  (result) => prepared.mapResult(result, true)
@@ -28116,6 +28320,7 @@ var init_session = __esm({
28116
28320
  _a159 = entityKind;
28117
28321
  PgPreparedQuery = class {
28118
28322
  constructor(query) {
28323
+ __publicField(this, "authToken");
28119
28324
  /** @internal */
28120
28325
  __publicField(this, "joinsNotNullableMap");
28121
28326
  this.query = query;
@@ -28126,6 +28331,11 @@ var init_session = __esm({
28126
28331
  mapResult(response, _isFromBatch) {
28127
28332
  return response;
28128
28333
  }
28334
+ /** @internal */
28335
+ setToken(token) {
28336
+ this.authToken = token;
28337
+ return this;
28338
+ }
28129
28339
  };
28130
28340
  __publicField(PgPreparedQuery, _a159, "PgPreparedQuery");
28131
28341
  _a160 = entityKind;
@@ -28133,7 +28343,8 @@ var init_session = __esm({
28133
28343
  constructor(dialect4) {
28134
28344
  this.dialect = dialect4;
28135
28345
  }
28136
- execute(query) {
28346
+ /** @internal */
28347
+ execute(query, token) {
28137
28348
  return tracer.startActiveSpan("drizzle.operation", () => {
28138
28349
  const prepared = tracer.startActiveSpan("drizzle.prepareQuery", () => {
28139
28350
  return this.prepareQuery(
@@ -28143,7 +28354,7 @@ var init_session = __esm({
28143
28354
  false
28144
28355
  );
28145
28356
  });
28146
- return prepared.execute();
28357
+ return prepared.setToken(token).execute(void 0, token);
28147
28358
  });
28148
28359
  }
28149
28360
  all(query) {
@@ -28154,8 +28365,9 @@ var init_session = __esm({
28154
28365
  false
28155
28366
  ).all();
28156
28367
  }
28157
- async count(sql2) {
28158
- const res = await this.execute(sql2);
28368
+ /** @internal */
28369
+ async count(sql2, token) {
28370
+ const res = await this.execute(sql2, token);
28159
28371
  return Number(
28160
28372
  res[0]["count"]
28161
28373
  );
@@ -30812,200 +31024,32 @@ var init_delete2 = __esm({
30812
31024
  prepare() {
30813
31025
  return this._prepare(false);
30814
31026
  }
30815
- async execute(placeholderValues) {
30816
- return this._prepare().execute(placeholderValues);
30817
- }
30818
- $dynamic() {
30819
- return this;
30820
- }
30821
- };
30822
- __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30823
- }
30824
- });
30825
-
30826
- // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
30827
- var _a197, SQLiteInsertBuilder, _a198, _b142, SQLiteInsertBase;
30828
- var init_insert2 = __esm({
30829
- "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
30830
- "use strict";
30831
- init_entity();
30832
- init_query_promise();
30833
- init_sql();
30834
- init_table3();
30835
- init_table();
30836
- init_utils2();
30837
- _a197 = entityKind;
30838
- SQLiteInsertBuilder = class {
30839
- constructor(table4, session, dialect4, withList) {
30840
- this.table = table4;
30841
- this.session = session;
30842
- this.dialect = dialect4;
30843
- this.withList = withList;
30844
- }
30845
- values(values) {
30846
- values = Array.isArray(values) ? values : [values];
30847
- if (values.length === 0) {
30848
- throw new Error("values() must be called with at least one value");
30849
- }
30850
- const mappedValues = values.map((entry) => {
30851
- const result = {};
30852
- const cols = this.table[Table2.Symbol.Columns];
30853
- for (const colKey of Object.keys(entry)) {
30854
- const colValue = entry[colKey];
30855
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
30856
- }
30857
- return result;
30858
- });
30859
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
30860
- }
30861
- };
30862
- __publicField(SQLiteInsertBuilder, _a197, "SQLiteInsertBuilder");
30863
- SQLiteInsertBase = class extends (_b142 = QueryPromise, _a198 = entityKind, _b142) {
30864
- constructor(table4, values, session, dialect4, withList) {
30865
- super();
30866
- /** @internal */
30867
- __publicField(this, "config");
30868
- __publicField(this, "run", (placeholderValues) => {
30869
- return this._prepare().run(placeholderValues);
30870
- });
30871
- __publicField(this, "all", (placeholderValues) => {
30872
- return this._prepare().all(placeholderValues);
30873
- });
30874
- __publicField(this, "get", (placeholderValues) => {
30875
- return this._prepare().get(placeholderValues);
30876
- });
30877
- __publicField(this, "values", (placeholderValues) => {
30878
- return this._prepare().values(placeholderValues);
30879
- });
30880
- this.session = session;
30881
- this.dialect = dialect4;
30882
- this.config = { table: table4, values, withList };
30883
- }
30884
- returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
30885
- this.config.returning = orderSelectedFields(fields);
30886
- return this;
30887
- }
30888
- /**
30889
- * Adds an `on conflict do nothing` clause to the query.
30890
- *
30891
- * Calling this method simply avoids inserting a row as its alternative action.
30892
- *
30893
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
30894
- *
30895
- * @param config The `target` and `where` clauses.
30896
- *
30897
- * @example
30898
- * ```ts
30899
- * // Insert one row and cancel the insert if there's a conflict
30900
- * await db.insert(cars)
30901
- * .values({ id: 1, brand: 'BMW' })
30902
- * .onConflictDoNothing();
30903
- *
30904
- * // Explicitly specify conflict target
30905
- * await db.insert(cars)
30906
- * .values({ id: 1, brand: 'BMW' })
30907
- * .onConflictDoNothing({ target: cars.id });
30908
- * ```
30909
- */
30910
- onConflictDoNothing(config = {}) {
30911
- if (config.target === void 0) {
30912
- this.config.onConflict = sql`do nothing`;
30913
- } else {
30914
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30915
- const whereSql = config.where ? sql` where ${config.where}` : sql``;
30916
- this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
30917
- }
30918
- return this;
30919
- }
30920
- /**
30921
- * Adds an `on conflict do update` clause to the query.
30922
- *
30923
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
30924
- *
30925
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
30926
- *
30927
- * @param config The `target`, `set` and `where` clauses.
30928
- *
30929
- * @example
30930
- * ```ts
30931
- * // Update the row if there's a conflict
30932
- * await db.insert(cars)
30933
- * .values({ id: 1, brand: 'BMW' })
30934
- * .onConflictDoUpdate({
30935
- * target: cars.id,
30936
- * set: { brand: 'Porsche' }
30937
- * });
30938
- *
30939
- * // Upsert with 'where' clause
30940
- * await db.insert(cars)
30941
- * .values({ id: 1, brand: 'BMW' })
30942
- * .onConflictDoUpdate({
30943
- * target: cars.id,
30944
- * set: { brand: 'newBMW' },
30945
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
30946
- * });
30947
- * ```
30948
- */
30949
- onConflictDoUpdate(config) {
30950
- if (config.where && (config.targetWhere || config.setWhere)) {
30951
- throw new Error(
30952
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
30953
- );
30954
- }
30955
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
30956
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
30957
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
30958
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30959
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
30960
- this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
30961
- return this;
30962
- }
30963
- /** @internal */
30964
- getSQL() {
30965
- return this.dialect.buildInsertQuery(this.config);
30966
- }
30967
- toSQL() {
30968
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
30969
- return rest;
30970
- }
30971
- /** @internal */
30972
- _prepare(isOneTimeQuery = true) {
30973
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
30974
- this.dialect.sqlToQuery(this.getSQL()),
30975
- this.config.returning,
30976
- this.config.returning ? "all" : "run",
30977
- true
30978
- );
30979
- }
30980
- prepare() {
30981
- return this._prepare(false);
30982
- }
30983
- async execute() {
30984
- return this.config.returning ? this.all() : this.run();
30985
- }
31027
+ async execute(placeholderValues) {
31028
+ return this._prepare().execute(placeholderValues);
31029
+ }
30986
31030
  $dynamic() {
30987
31031
  return this;
30988
31032
  }
30989
31033
  };
30990
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
31034
+ __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30991
31035
  }
30992
31036
  });
30993
31037
 
30994
31038
  // ../drizzle-orm/dist/sqlite-core/view-base.js
30995
- var _a199, _b143, SQLiteViewBase;
31039
+ var _a197, _b142, SQLiteViewBase;
30996
31040
  var init_view_base2 = __esm({
30997
31041
  "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30998
31042
  "use strict";
30999
31043
  init_entity();
31000
31044
  init_sql();
31001
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31045
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
31002
31046
  };
31003
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
31047
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31004
31048
  }
31005
31049
  });
31006
31050
 
31007
31051
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31008
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
31052
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31009
31053
  var init_dialect2 = __esm({
31010
31054
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31011
31055
  "use strict";
@@ -31024,7 +31068,7 @@ var init_dialect2 = __esm({
31024
31068
  init_utils2();
31025
31069
  init_view_common();
31026
31070
  init_view_base2();
31027
- _a200 = entityKind;
31071
+ _a198 = entityKind;
31028
31072
  SQLiteDialect = class {
31029
31073
  constructor(config) {
31030
31074
  /** @internal */
@@ -31077,14 +31121,16 @@ var init_dialect2 = __esm({
31077
31121
  return [res];
31078
31122
  }));
31079
31123
  }
31080
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31124
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31081
31125
  const withSql = this.buildWithCTE(withList);
31082
31126
  const setSql = this.buildUpdateSet(table4, set);
31127
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31128
+ const joinsSql = this.buildJoins(joins);
31083
31129
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31084
31130
  const whereSql = where ? sql` where ${where}` : void 0;
31085
31131
  const orderBySql = this.buildOrderBy(orderBy);
31086
31132
  const limitSql = this.buildLimit(limit);
31087
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31133
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31088
31134
  }
31089
31135
  /**
31090
31136
  * Builds selection SQL with provided fields/expressions
@@ -31137,6 +31183,37 @@ var init_dialect2 = __esm({
31137
31183
  });
31138
31184
  return sql.join(chunks);
31139
31185
  }
31186
+ buildJoins(joins) {
31187
+ if (!joins || joins.length === 0) {
31188
+ return void 0;
31189
+ }
31190
+ const joinsArray = [];
31191
+ if (joins) {
31192
+ for (const [index4, joinMeta] of joins.entries()) {
31193
+ if (index4 === 0) {
31194
+ joinsArray.push(sql` `);
31195
+ }
31196
+ const table4 = joinMeta.table;
31197
+ if (is(table4, SQLiteTable)) {
31198
+ const tableName = table4[SQLiteTable.Symbol.Name];
31199
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31200
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31201
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31202
+ joinsArray.push(
31203
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31204
+ );
31205
+ } else {
31206
+ joinsArray.push(
31207
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31208
+ );
31209
+ }
31210
+ if (index4 < joins.length - 1) {
31211
+ joinsArray.push(sql` `);
31212
+ }
31213
+ }
31214
+ }
31215
+ return sql.join(joinsArray);
31216
+ }
31140
31217
  buildLimit(limit) {
31141
31218
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31142
31219
  }
@@ -31152,6 +31229,12 @@ var init_dialect2 = __esm({
31152
31229
  }
31153
31230
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31154
31231
  }
31232
+ buildFromTable(table4) {
31233
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31234
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31235
+ }
31236
+ return table4;
31237
+ }
31155
31238
  buildSelectQuery({
31156
31239
  withList,
31157
31240
  fields,
@@ -31182,38 +31265,8 @@ var init_dialect2 = __esm({
31182
31265
  const withSql = this.buildWithCTE(withList);
31183
31266
  const distinctSql = distinct ? sql` distinct` : void 0;
31184
31267
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31185
- const tableSql = (() => {
31186
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31187
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31188
- }
31189
- return table4;
31190
- })();
31191
- const joinsArray = [];
31192
- if (joins) {
31193
- for (const [index4, joinMeta] of joins.entries()) {
31194
- if (index4 === 0) {
31195
- joinsArray.push(sql` `);
31196
- }
31197
- const table22 = joinMeta.table;
31198
- if (is(table22, SQLiteTable)) {
31199
- const tableName = table22[SQLiteTable.Symbol.Name];
31200
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31201
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31202
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31203
- joinsArray.push(
31204
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31205
- );
31206
- } else {
31207
- joinsArray.push(
31208
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31209
- );
31210
- }
31211
- if (index4 < joins.length - 1) {
31212
- joinsArray.push(sql` `);
31213
- }
31214
- }
31215
- }
31216
- const joinsSql = sql.join(joinsArray);
31268
+ const tableSql = this.buildFromTable(table4);
31269
+ const joinsSql = this.buildJoins(joins);
31217
31270
  const whereSql = where ? sql` where ${where}` : void 0;
31218
31271
  const havingSql = having ? sql` having ${having}` : void 0;
31219
31272
  const groupByList = [];
@@ -31279,45 +31332,56 @@ var init_dialect2 = __esm({
31279
31332
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31280
31333
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31281
31334
  }
31282
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31335
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31283
31336
  const valuesSqlList = [];
31284
31337
  const columns = table4[Table2.Symbol.Columns];
31285
31338
  const colEntries = Object.entries(columns).filter(
31286
31339
  ([_2, col]) => !col.shouldDisableInsert()
31287
31340
  );
31288
31341
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
31289
- for (const [valueIndex, value] of values.entries()) {
31290
- const valueList = [];
31291
- for (const [fieldName, col] of colEntries) {
31292
- const colValue = value[fieldName];
31293
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31294
- let defaultValue;
31295
- if (col.default !== null && col.default !== void 0) {
31296
- defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31297
- } else if (col.defaultFn !== void 0) {
31298
- const defaultFnResult = col.defaultFn();
31299
- defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31300
- } else if (!col.default && col.onUpdateFn !== void 0) {
31301
- const onUpdateFnResult = col.onUpdateFn();
31302
- defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31342
+ if (select) {
31343
+ const select2 = valuesOrSelect;
31344
+ if (is(select2, SQL)) {
31345
+ valuesSqlList.push(select2);
31346
+ } else {
31347
+ valuesSqlList.push(select2.getSQL());
31348
+ }
31349
+ } else {
31350
+ const values = valuesOrSelect;
31351
+ valuesSqlList.push(sql.raw("values "));
31352
+ for (const [valueIndex, value] of values.entries()) {
31353
+ const valueList = [];
31354
+ for (const [fieldName, col] of colEntries) {
31355
+ const colValue = value[fieldName];
31356
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31357
+ let defaultValue;
31358
+ if (col.default !== null && col.default !== void 0) {
31359
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31360
+ } else if (col.defaultFn !== void 0) {
31361
+ const defaultFnResult = col.defaultFn();
31362
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31363
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31364
+ const onUpdateFnResult = col.onUpdateFn();
31365
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31366
+ } else {
31367
+ defaultValue = sql`null`;
31368
+ }
31369
+ valueList.push(defaultValue);
31303
31370
  } else {
31304
- defaultValue = sql`null`;
31371
+ valueList.push(colValue);
31305
31372
  }
31306
- valueList.push(defaultValue);
31307
- } else {
31308
- valueList.push(colValue);
31309
31373
  }
31310
- }
31311
- valuesSqlList.push(valueList);
31312
- if (valueIndex < values.length - 1) {
31313
- valuesSqlList.push(sql`, `);
31374
+ valuesSqlList.push(valueList);
31375
+ if (valueIndex < values.length - 1) {
31376
+ valuesSqlList.push(sql`, `);
31377
+ }
31314
31378
  }
31315
31379
  }
31316
31380
  const withSql = this.buildWithCTE(withList);
31317
31381
  const valuesSql = sql.join(valuesSqlList);
31318
31382
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31319
31383
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31320
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31384
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31321
31385
  }
31322
31386
  sqlToQuery(sql2, invokeSource) {
31323
31387
  return sql2.toQuery({
@@ -31546,8 +31610,8 @@ var init_dialect2 = __esm({
31546
31610
  };
31547
31611
  }
31548
31612
  };
31549
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31550
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31613
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31614
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31551
31615
  migrate(migrations, session, config) {
31552
31616
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31553
31617
  const migrationTableCreate = sql`
@@ -31581,8 +31645,8 @@ var init_dialect2 = __esm({
31581
31645
  }
31582
31646
  }
31583
31647
  };
31584
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31585
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31648
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31649
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31586
31650
  async migrate(migrations, session, config) {
31587
31651
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31588
31652
  const migrationTableCreate = sql`
@@ -31611,7 +31675,7 @@ var init_dialect2 = __esm({
31611
31675
  });
31612
31676
  }
31613
31677
  };
31614
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31678
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31615
31679
  }
31616
31680
  });
31617
31681
 
@@ -31633,7 +31697,7 @@ function createSetOperator2(type, isAll) {
31633
31697
  return leftSelect.addSetOperators(setOperators);
31634
31698
  };
31635
31699
  }
31636
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31700
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31637
31701
  var init_select3 = __esm({
31638
31702
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31639
31703
  "use strict";
@@ -31647,7 +31711,7 @@ var init_select3 = __esm({
31647
31711
  init_utils2();
31648
31712
  init_view_common();
31649
31713
  init_view_base2();
31650
- _a203 = entityKind;
31714
+ _a201 = entityKind;
31651
31715
  SQLiteSelectBuilder = class {
31652
31716
  constructor(config) {
31653
31717
  __publicField(this, "fields");
@@ -31688,8 +31752,8 @@ var init_select3 = __esm({
31688
31752
  });
31689
31753
  }
31690
31754
  };
31691
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31692
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31755
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31756
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31693
31757
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31694
31758
  super();
31695
31759
  __publicField(this, "_");
@@ -32194,8 +32258,8 @@ var init_select3 = __esm({
32194
32258
  return this;
32195
32259
  }
32196
32260
  };
32197
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32198
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32261
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32262
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32199
32263
  constructor() {
32200
32264
  super(...arguments);
32201
32265
  __publicField(this, "run", (placeholderValues) => {
@@ -32233,7 +32297,7 @@ var init_select3 = __esm({
32233
32297
  return this.all();
32234
32298
  }
32235
32299
  };
32236
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32300
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32237
32301
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32238
32302
  getSQLiteSetOperators = () => ({
32239
32303
  union: union2,
@@ -32249,7 +32313,7 @@ var init_select3 = __esm({
32249
32313
  });
32250
32314
 
32251
32315
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32252
- var _a206, QueryBuilder2;
32316
+ var _a204, QueryBuilder2;
32253
32317
  var init_query_builder3 = __esm({
32254
32318
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32255
32319
  "use strict";
@@ -32258,7 +32322,7 @@ var init_query_builder3 = __esm({
32258
32322
  init_dialect2();
32259
32323
  init_subquery();
32260
32324
  init_select3();
32261
- _a206 = entityKind;
32325
+ _a204 = entityKind;
32262
32326
  QueryBuilder2 = class {
32263
32327
  constructor(dialect4) {
32264
32328
  __publicField(this, "dialect");
@@ -32320,7 +32384,185 @@ var init_query_builder3 = __esm({
32320
32384
  return this.dialect;
32321
32385
  }
32322
32386
  };
32323
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32387
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32388
+ }
32389
+ });
32390
+
32391
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32392
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32393
+ var init_insert2 = __esm({
32394
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32395
+ "use strict";
32396
+ init_entity();
32397
+ init_query_promise();
32398
+ init_sql();
32399
+ init_table3();
32400
+ init_table();
32401
+ init_utils2();
32402
+ init_query_builder3();
32403
+ _a205 = entityKind;
32404
+ SQLiteInsertBuilder = class {
32405
+ constructor(table4, session, dialect4, withList) {
32406
+ this.table = table4;
32407
+ this.session = session;
32408
+ this.dialect = dialect4;
32409
+ this.withList = withList;
32410
+ }
32411
+ values(values) {
32412
+ values = Array.isArray(values) ? values : [values];
32413
+ if (values.length === 0) {
32414
+ throw new Error("values() must be called with at least one value");
32415
+ }
32416
+ const mappedValues = values.map((entry) => {
32417
+ const result = {};
32418
+ const cols = this.table[Table2.Symbol.Columns];
32419
+ for (const colKey of Object.keys(entry)) {
32420
+ const colValue = entry[colKey];
32421
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32422
+ }
32423
+ return result;
32424
+ });
32425
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32426
+ }
32427
+ select(selectQuery) {
32428
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32429
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32430
+ throw new Error(
32431
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32432
+ );
32433
+ }
32434
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32435
+ }
32436
+ };
32437
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32438
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32439
+ constructor(table4, values, session, dialect4, withList, select) {
32440
+ super();
32441
+ /** @internal */
32442
+ __publicField(this, "config");
32443
+ __publicField(this, "run", (placeholderValues) => {
32444
+ return this._prepare().run(placeholderValues);
32445
+ });
32446
+ __publicField(this, "all", (placeholderValues) => {
32447
+ return this._prepare().all(placeholderValues);
32448
+ });
32449
+ __publicField(this, "get", (placeholderValues) => {
32450
+ return this._prepare().get(placeholderValues);
32451
+ });
32452
+ __publicField(this, "values", (placeholderValues) => {
32453
+ return this._prepare().values(placeholderValues);
32454
+ });
32455
+ this.session = session;
32456
+ this.dialect = dialect4;
32457
+ this.config = { table: table4, values, withList, select };
32458
+ }
32459
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32460
+ this.config.returning = orderSelectedFields(fields);
32461
+ return this;
32462
+ }
32463
+ /**
32464
+ * Adds an `on conflict do nothing` clause to the query.
32465
+ *
32466
+ * Calling this method simply avoids inserting a row as its alternative action.
32467
+ *
32468
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32469
+ *
32470
+ * @param config The `target` and `where` clauses.
32471
+ *
32472
+ * @example
32473
+ * ```ts
32474
+ * // Insert one row and cancel the insert if there's a conflict
32475
+ * await db.insert(cars)
32476
+ * .values({ id: 1, brand: 'BMW' })
32477
+ * .onConflictDoNothing();
32478
+ *
32479
+ * // Explicitly specify conflict target
32480
+ * await db.insert(cars)
32481
+ * .values({ id: 1, brand: 'BMW' })
32482
+ * .onConflictDoNothing({ target: cars.id });
32483
+ * ```
32484
+ */
32485
+ onConflictDoNothing(config = {}) {
32486
+ if (config.target === void 0) {
32487
+ this.config.onConflict = sql`do nothing`;
32488
+ } else {
32489
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32490
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32491
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32492
+ }
32493
+ return this;
32494
+ }
32495
+ /**
32496
+ * Adds an `on conflict do update` clause to the query.
32497
+ *
32498
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32499
+ *
32500
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32501
+ *
32502
+ * @param config The `target`, `set` and `where` clauses.
32503
+ *
32504
+ * @example
32505
+ * ```ts
32506
+ * // Update the row if there's a conflict
32507
+ * await db.insert(cars)
32508
+ * .values({ id: 1, brand: 'BMW' })
32509
+ * .onConflictDoUpdate({
32510
+ * target: cars.id,
32511
+ * set: { brand: 'Porsche' }
32512
+ * });
32513
+ *
32514
+ * // Upsert with 'where' clause
32515
+ * await db.insert(cars)
32516
+ * .values({ id: 1, brand: 'BMW' })
32517
+ * .onConflictDoUpdate({
32518
+ * target: cars.id,
32519
+ * set: { brand: 'newBMW' },
32520
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32521
+ * });
32522
+ * ```
32523
+ */
32524
+ onConflictDoUpdate(config) {
32525
+ if (config.where && (config.targetWhere || config.setWhere)) {
32526
+ throw new Error(
32527
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32528
+ );
32529
+ }
32530
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32531
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32532
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32533
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32534
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32535
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32536
+ return this;
32537
+ }
32538
+ /** @internal */
32539
+ getSQL() {
32540
+ return this.dialect.buildInsertQuery(this.config);
32541
+ }
32542
+ toSQL() {
32543
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32544
+ return rest;
32545
+ }
32546
+ /** @internal */
32547
+ _prepare(isOneTimeQuery = true) {
32548
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32549
+ this.dialect.sqlToQuery(this.getSQL()),
32550
+ this.config.returning,
32551
+ this.config.returning ? "all" : "run",
32552
+ true
32553
+ );
32554
+ }
32555
+ prepare() {
32556
+ return this._prepare(false);
32557
+ }
32558
+ async execute() {
32559
+ return this.config.returning ? this.all() : this.run();
32560
+ }
32561
+ $dynamic() {
32562
+ return this;
32563
+ }
32564
+ };
32565
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32324
32566
  }
32325
32567
  });
32326
32568
 
@@ -32340,8 +32582,11 @@ var init_update2 = __esm({
32340
32582
  init_query_promise();
32341
32583
  init_selection_proxy();
32342
32584
  init_table3();
32585
+ init_subquery();
32343
32586
  init_table();
32344
32587
  init_utils2();
32588
+ init_view_common();
32589
+ init_view_base2();
32345
32590
  _a207 = entityKind;
32346
32591
  SQLiteUpdateBuilder = class {
32347
32592
  constructor(table4, session, dialect4, withList) {
@@ -32366,6 +32611,10 @@ var init_update2 = __esm({
32366
32611
  super();
32367
32612
  /** @internal */
32368
32613
  __publicField(this, "config");
32614
+ __publicField(this, "leftJoin", this.createJoin("left"));
32615
+ __publicField(this, "rightJoin", this.createJoin("right"));
32616
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32617
+ __publicField(this, "fullJoin", this.createJoin("full"));
32369
32618
  __publicField(this, "run", (placeholderValues) => {
32370
32619
  return this._prepare().run(placeholderValues);
32371
32620
  });
@@ -32380,7 +32629,34 @@ var init_update2 = __esm({
32380
32629
  });
32381
32630
  this.session = session;
32382
32631
  this.dialect = dialect4;
32383
- this.config = { set, table: table4, withList };
32632
+ this.config = { set, table: table4, withList, joins: [] };
32633
+ }
32634
+ from(source) {
32635
+ this.config.from = source;
32636
+ return this;
32637
+ }
32638
+ createJoin(joinType) {
32639
+ return (table4, on) => {
32640
+ const tableName = getTableLikeName(table4);
32641
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32642
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32643
+ }
32644
+ if (typeof on === "function") {
32645
+ 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;
32646
+ on = on(
32647
+ new Proxy(
32648
+ this.config.table[Table2.Symbol.Columns],
32649
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32650
+ ),
32651
+ from && new Proxy(
32652
+ from,
32653
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32654
+ )
32655
+ );
32656
+ }
32657
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32658
+ return this;
32659
+ };
32384
32660
  }
32385
32661
  /**
32386
32662
  * Adds a 'where' clause to the query.
@@ -35678,131 +35954,6 @@ var init_delete3 = __esm({
35678
35954
  }
35679
35955
  });
35680
35956
 
35681
- // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
35682
- var _a299, MySqlInsertBuilder, _a300, _b222, MySqlInsertBase;
35683
- var init_insert3 = __esm({
35684
- "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
35685
- "use strict";
35686
- init_entity();
35687
- init_query_promise();
35688
- init_sql();
35689
- init_table();
35690
- init_utils2();
35691
- _a299 = entityKind;
35692
- MySqlInsertBuilder = class {
35693
- constructor(table4, session, dialect4) {
35694
- __publicField(this, "shouldIgnore", false);
35695
- this.table = table4;
35696
- this.session = session;
35697
- this.dialect = dialect4;
35698
- }
35699
- ignore() {
35700
- this.shouldIgnore = true;
35701
- return this;
35702
- }
35703
- values(values) {
35704
- values = Array.isArray(values) ? values : [values];
35705
- if (values.length === 0) {
35706
- throw new Error("values() must be called with at least one value");
35707
- }
35708
- const mappedValues = values.map((entry) => {
35709
- const result = {};
35710
- const cols = this.table[Table2.Symbol.Columns];
35711
- for (const colKey of Object.keys(entry)) {
35712
- const colValue = entry[colKey];
35713
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
35714
- }
35715
- return result;
35716
- });
35717
- return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
35718
- }
35719
- };
35720
- __publicField(MySqlInsertBuilder, _a299, "MySqlInsertBuilder");
35721
- MySqlInsertBase = class extends (_b222 = QueryPromise, _a300 = entityKind, _b222) {
35722
- constructor(table4, values, ignore, session, dialect4) {
35723
- super();
35724
- __publicField(this, "config");
35725
- __publicField(this, "execute", (placeholderValues) => {
35726
- return this.prepare().execute(placeholderValues);
35727
- });
35728
- __publicField(this, "createIterator", () => {
35729
- const self2 = this;
35730
- return async function* (placeholderValues) {
35731
- yield* self2.prepare().iterator(placeholderValues);
35732
- };
35733
- });
35734
- __publicField(this, "iterator", this.createIterator());
35735
- this.session = session;
35736
- this.dialect = dialect4;
35737
- this.config = { table: table4, values, ignore };
35738
- }
35739
- /**
35740
- * Adds an `on duplicate key update` clause to the query.
35741
- *
35742
- * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35743
- *
35744
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35745
- *
35746
- * @param config The `set` clause
35747
- *
35748
- * @example
35749
- * ```ts
35750
- * await db.insert(cars)
35751
- * .values({ id: 1, brand: 'BMW'})
35752
- * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
35753
- * ```
35754
- *
35755
- * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
35756
- *
35757
- * ```ts
35758
- * import { sql } from 'drizzle-orm';
35759
- *
35760
- * await db.insert(cars)
35761
- * .values({ id: 1, brand: 'BMW' })
35762
- * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
35763
- * ```
35764
- */
35765
- onDuplicateKeyUpdate(config) {
35766
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
35767
- this.config.onConflict = sql`update ${setSql}`;
35768
- return this;
35769
- }
35770
- $returningId() {
35771
- const returning = [];
35772
- for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
35773
- if (value.primary) {
35774
- returning.push({ field: value, path: [key] });
35775
- }
35776
- }
35777
- this.config.returning = returning;
35778
- return this;
35779
- }
35780
- /** @internal */
35781
- getSQL() {
35782
- return this.dialect.buildInsertQuery(this.config).sql;
35783
- }
35784
- toSQL() {
35785
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
35786
- return rest;
35787
- }
35788
- prepare() {
35789
- const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
35790
- return this.session.prepareQuery(
35791
- this.dialect.sqlToQuery(sql2),
35792
- void 0,
35793
- void 0,
35794
- generatedIds,
35795
- this.config.returning
35796
- );
35797
- }
35798
- $dynamic() {
35799
- return this;
35800
- }
35801
- };
35802
- __publicField(MySqlInsertBase, _a300, "MySqlInsert");
35803
- }
35804
- });
35805
-
35806
35957
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35807
35958
  function getMySqlColumnBuilders() {
35808
35959
  return {
@@ -35883,7 +36034,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35883
36034
  }
35884
36035
  return table4;
35885
36036
  }
35886
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
36037
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35887
36038
  var init_table4 = __esm({
35888
36039
  "../drizzle-orm/dist/mysql-core/table.js"() {
35889
36040
  "use strict";
@@ -35891,15 +36042,15 @@ var init_table4 = __esm({
35891
36042
  init_table();
35892
36043
  init_all3();
35893
36044
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35894
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
36045
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35895
36046
  constructor() {
35896
36047
  super(...arguments);
35897
36048
  /** @internal */
35898
36049
  __publicField(this, _c9);
35899
36050
  /** @internal */
35900
- __publicField(this, _b223, []);
36051
+ __publicField(this, _b222, []);
35901
36052
  /** @internal */
35902
- __publicField(this, _a301);
36053
+ __publicField(this, _a299);
35903
36054
  }
35904
36055
  };
35905
36056
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35914,20 +36065,20 @@ var init_table4 = __esm({
35914
36065
  });
35915
36066
 
35916
36067
  // ../drizzle-orm/dist/mysql-core/view-base.js
35917
- var _a302, _b224, MySqlViewBase;
36068
+ var _a300, _b223, MySqlViewBase;
35918
36069
  var init_view_base3 = __esm({
35919
36070
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35920
36071
  "use strict";
35921
36072
  init_entity();
35922
36073
  init_sql();
35923
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
36074
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35924
36075
  };
35925
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
36076
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35926
36077
  }
35927
36078
  });
35928
36079
 
35929
36080
  // ../drizzle-orm/dist/mysql-core/dialect.js
35930
- var _a303, MySqlDialect;
36081
+ var _a301, MySqlDialect;
35931
36082
  var init_dialect3 = __esm({
35932
36083
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35933
36084
  "use strict";
@@ -35946,7 +36097,7 @@ var init_dialect3 = __esm({
35946
36097
  init_common4();
35947
36098
  init_table4();
35948
36099
  init_view_base3();
35949
- _a303 = entityKind;
36100
+ _a301 = entityKind;
35950
36101
  MySqlDialect = class {
35951
36102
  constructor(config) {
35952
36103
  /** @internal */
@@ -36229,7 +36380,7 @@ var init_dialect3 = __esm({
36229
36380
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36230
36381
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36231
36382
  }
36232
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36383
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36233
36384
  const valuesSqlList = [];
36234
36385
  const columns = table4[Table2.Symbol.Columns];
36235
36386
  const colEntries = Object.entries(columns).filter(
@@ -36237,42 +36388,53 @@ var init_dialect3 = __esm({
36237
36388
  );
36238
36389
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36239
36390
  const generatedIdsResponse = [];
36240
- for (const [valueIndex, value] of values.entries()) {
36241
- const generatedIds = {};
36242
- const valueList = [];
36243
- for (const [fieldName, col] of colEntries) {
36244
- const colValue = value[fieldName];
36245
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36246
- if (col.defaultFn !== void 0) {
36247
- const defaultFnResult = col.defaultFn();
36248
- generatedIds[fieldName] = defaultFnResult;
36249
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36250
- valueList.push(defaultValue);
36251
- } else if (!col.default && col.onUpdateFn !== void 0) {
36252
- const onUpdateFnResult = col.onUpdateFn();
36253
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36254
- valueList.push(newValue);
36391
+ if (select) {
36392
+ const select2 = valuesOrSelect;
36393
+ if (is(select2, SQL)) {
36394
+ valuesSqlList.push(select2);
36395
+ } else {
36396
+ valuesSqlList.push(select2.getSQL());
36397
+ }
36398
+ } else {
36399
+ const values = valuesOrSelect;
36400
+ valuesSqlList.push(sql.raw("values "));
36401
+ for (const [valueIndex, value] of values.entries()) {
36402
+ const generatedIds = {};
36403
+ const valueList = [];
36404
+ for (const [fieldName, col] of colEntries) {
36405
+ const colValue = value[fieldName];
36406
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36407
+ if (col.defaultFn !== void 0) {
36408
+ const defaultFnResult = col.defaultFn();
36409
+ generatedIds[fieldName] = defaultFnResult;
36410
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36411
+ valueList.push(defaultValue);
36412
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36413
+ const onUpdateFnResult = col.onUpdateFn();
36414
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36415
+ valueList.push(newValue);
36416
+ } else {
36417
+ valueList.push(sql`default`);
36418
+ }
36255
36419
  } else {
36256
- valueList.push(sql`default`);
36257
- }
36258
- } else {
36259
- if (col.defaultFn && is(colValue, Param)) {
36260
- generatedIds[fieldName] = colValue.value;
36420
+ if (col.defaultFn && is(colValue, Param)) {
36421
+ generatedIds[fieldName] = colValue.value;
36422
+ }
36423
+ valueList.push(colValue);
36261
36424
  }
36262
- valueList.push(colValue);
36263
36425
  }
36264
- }
36265
- generatedIdsResponse.push(generatedIds);
36266
- valuesSqlList.push(valueList);
36267
- if (valueIndex < values.length - 1) {
36268
- valuesSqlList.push(sql`, `);
36426
+ generatedIdsResponse.push(generatedIds);
36427
+ valuesSqlList.push(valueList);
36428
+ if (valueIndex < values.length - 1) {
36429
+ valuesSqlList.push(sql`, `);
36430
+ }
36269
36431
  }
36270
36432
  }
36271
36433
  const valuesSql = sql.join(valuesSqlList);
36272
36434
  const ignoreSql = ignore ? sql` ignore` : void 0;
36273
36435
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36274
36436
  return {
36275
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36437
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36276
36438
  generatedIds: generatedIdsResponse
36277
36439
  };
36278
36440
  }
@@ -36732,7 +36894,7 @@ var init_dialect3 = __esm({
36732
36894
  };
36733
36895
  }
36734
36896
  };
36735
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36897
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36736
36898
  }
36737
36899
  });
36738
36900
 
@@ -36754,7 +36916,7 @@ function createSetOperator3(type, isAll) {
36754
36916
  return leftSelect.addSetOperators(setOperators);
36755
36917
  };
36756
36918
  }
36757
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36919
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36758
36920
  var init_select4 = __esm({
36759
36921
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36760
36922
  "use strict";
@@ -36769,7 +36931,7 @@ var init_select4 = __esm({
36769
36931
  init_utils2();
36770
36932
  init_view_common();
36771
36933
  init_view_base3();
36772
- _a304 = entityKind;
36934
+ _a302 = entityKind;
36773
36935
  MySqlSelectBuilder = class {
36774
36936
  constructor(config) {
36775
36937
  __publicField(this, "fields");
@@ -36814,8 +36976,8 @@ var init_select4 = __esm({
36814
36976
  );
36815
36977
  }
36816
36978
  };
36817
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36818
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36979
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36980
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36819
36981
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36820
36982
  super();
36821
36983
  __publicField(this, "_");
@@ -37416,8 +37578,8 @@ var init_select4 = __esm({
37416
37578
  return this;
37417
37579
  }
37418
37580
  };
37419
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37420
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37581
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37582
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37421
37583
  constructor() {
37422
37584
  super(...arguments);
37423
37585
  __publicField(this, "execute", (placeholderValues) => {
@@ -37441,7 +37603,7 @@ var init_select4 = __esm({
37441
37603
  return query;
37442
37604
  }
37443
37605
  };
37444
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37606
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37445
37607
  applyMixins(MySqlSelectBase, [QueryPromise]);
37446
37608
  getMySqlSetOperators = () => ({
37447
37609
  union: union3,
@@ -37461,7 +37623,7 @@ var init_select4 = __esm({
37461
37623
  });
37462
37624
 
37463
37625
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37464
- var _a307, QueryBuilder3;
37626
+ var _a305, QueryBuilder3;
37465
37627
  var init_query_builder4 = __esm({
37466
37628
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37467
37629
  "use strict";
@@ -37470,7 +37632,7 @@ var init_query_builder4 = __esm({
37470
37632
  init_selection_proxy();
37471
37633
  init_subquery();
37472
37634
  init_select4();
37473
- _a307 = entityKind;
37635
+ _a305 = entityKind;
37474
37636
  QueryBuilder3 = class {
37475
37637
  constructor(dialect4) {
37476
37638
  __publicField(this, "dialect");
@@ -37532,7 +37694,142 @@ var init_query_builder4 = __esm({
37532
37694
  return this.dialect;
37533
37695
  }
37534
37696
  };
37535
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37697
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37698
+ }
37699
+ });
37700
+
37701
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37702
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37703
+ var init_insert3 = __esm({
37704
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37705
+ "use strict";
37706
+ init_entity();
37707
+ init_query_promise();
37708
+ init_sql();
37709
+ init_table();
37710
+ init_utils2();
37711
+ init_query_builder4();
37712
+ _a306 = entityKind;
37713
+ MySqlInsertBuilder = class {
37714
+ constructor(table4, session, dialect4) {
37715
+ __publicField(this, "shouldIgnore", false);
37716
+ this.table = table4;
37717
+ this.session = session;
37718
+ this.dialect = dialect4;
37719
+ }
37720
+ ignore() {
37721
+ this.shouldIgnore = true;
37722
+ return this;
37723
+ }
37724
+ values(values) {
37725
+ values = Array.isArray(values) ? values : [values];
37726
+ if (values.length === 0) {
37727
+ throw new Error("values() must be called with at least one value");
37728
+ }
37729
+ const mappedValues = values.map((entry) => {
37730
+ const result = {};
37731
+ const cols = this.table[Table2.Symbol.Columns];
37732
+ for (const colKey of Object.keys(entry)) {
37733
+ const colValue = entry[colKey];
37734
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37735
+ }
37736
+ return result;
37737
+ });
37738
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37739
+ }
37740
+ select(selectQuery) {
37741
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37742
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37743
+ throw new Error(
37744
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37745
+ );
37746
+ }
37747
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37748
+ }
37749
+ };
37750
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37751
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37752
+ constructor(table4, values, ignore, session, dialect4, select) {
37753
+ super();
37754
+ __publicField(this, "config");
37755
+ __publicField(this, "execute", (placeholderValues) => {
37756
+ return this.prepare().execute(placeholderValues);
37757
+ });
37758
+ __publicField(this, "createIterator", () => {
37759
+ const self2 = this;
37760
+ return async function* (placeholderValues) {
37761
+ yield* self2.prepare().iterator(placeholderValues);
37762
+ };
37763
+ });
37764
+ __publicField(this, "iterator", this.createIterator());
37765
+ this.session = session;
37766
+ this.dialect = dialect4;
37767
+ this.config = { table: table4, values, select, ignore };
37768
+ }
37769
+ /**
37770
+ * Adds an `on duplicate key update` clause to the query.
37771
+ *
37772
+ * 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.
37773
+ *
37774
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37775
+ *
37776
+ * @param config The `set` clause
37777
+ *
37778
+ * @example
37779
+ * ```ts
37780
+ * await db.insert(cars)
37781
+ * .values({ id: 1, brand: 'BMW'})
37782
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37783
+ * ```
37784
+ *
37785
+ * 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:
37786
+ *
37787
+ * ```ts
37788
+ * import { sql } from 'drizzle-orm';
37789
+ *
37790
+ * await db.insert(cars)
37791
+ * .values({ id: 1, brand: 'BMW' })
37792
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37793
+ * ```
37794
+ */
37795
+ onDuplicateKeyUpdate(config) {
37796
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37797
+ this.config.onConflict = sql`update ${setSql}`;
37798
+ return this;
37799
+ }
37800
+ $returningId() {
37801
+ const returning = [];
37802
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37803
+ if (value.primary) {
37804
+ returning.push({ field: value, path: [key] });
37805
+ }
37806
+ }
37807
+ this.config.returning = returning;
37808
+ return this;
37809
+ }
37810
+ /** @internal */
37811
+ getSQL() {
37812
+ return this.dialect.buildInsertQuery(this.config).sql;
37813
+ }
37814
+ toSQL() {
37815
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37816
+ return rest;
37817
+ }
37818
+ prepare() {
37819
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37820
+ return this.session.prepareQuery(
37821
+ this.dialect.sqlToQuery(sql2),
37822
+ void 0,
37823
+ void 0,
37824
+ generatedIds,
37825
+ this.config.returning
37826
+ );
37827
+ }
37828
+ $dynamic() {
37829
+ return this;
37830
+ }
37831
+ };
37832
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37536
37833
  }
37537
37834
  });
37538
37835