drizzle-kit 0.28.0-cc4f208 → 0.28.1-442f74d

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 +1007 -709
  2. package/api.mjs +1007 -709
  3. package/bin.cjs +1 -1
  4. package/package.json +1 -1
package/api.mjs CHANGED
@@ -21126,7 +21126,7 @@ var version;
21126
21126
  var init_version = __esm({
21127
21127
  "../drizzle-orm/dist/version.js"() {
21128
21128
  "use strict";
21129
- version = "0.36.1";
21129
+ version = "0.36.3";
21130
21130
  }
21131
21131
  });
21132
21132
 
@@ -22149,7 +22149,7 @@ function haveSameKeys(left, right) {
22149
22149
  }
22150
22150
  function mapUpdateSet(table4, values) {
22151
22151
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22152
- if (is(value, SQL)) {
22152
+ if (is(value, SQL) || is(value, Column2)) {
22153
22153
  return [key, value];
22154
22154
  } else {
22155
22155
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24588,9 +24588,10 @@ var init_delete = __esm({
24588
24588
  constructor(table4, session, dialect4, withList) {
24589
24589
  super();
24590
24590
  __publicField(this, "config");
24591
+ __publicField(this, "authToken");
24591
24592
  __publicField(this, "execute", (placeholderValues) => {
24592
24593
  return tracer.startActiveSpan("drizzle.operation", () => {
24593
- return this._prepare().execute(placeholderValues);
24594
+ return this._prepare().execute(placeholderValues, this.authToken);
24594
24595
  });
24595
24596
  });
24596
24597
  this.session = session;
@@ -24651,167 +24652,16 @@ var init_delete = __esm({
24651
24652
  prepare(name2) {
24652
24653
  return this._prepare(name2);
24653
24654
  }
24654
- $dynamic() {
24655
- return this;
24656
- }
24657
- };
24658
- __publicField(PgDeleteBase, _a124, "PgDelete");
24659
- }
24660
- });
24661
-
24662
- // ../drizzle-orm/dist/pg-core/query-builders/insert.js
24663
- var _a125, PgInsertBuilder, _a126, _b98, PgInsertBase;
24664
- var init_insert = __esm({
24665
- "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
24666
- "use strict";
24667
- init_entity();
24668
- init_query_promise();
24669
- init_sql();
24670
- init_table();
24671
- init_tracing();
24672
- init_utils2();
24673
- _a125 = entityKind;
24674
- PgInsertBuilder = class {
24675
- constructor(table4, session, dialect4, withList) {
24676
- this.table = table4;
24677
- this.session = session;
24678
- this.dialect = dialect4;
24679
- this.withList = withList;
24680
- }
24681
- values(values) {
24682
- values = Array.isArray(values) ? values : [values];
24683
- if (values.length === 0) {
24684
- throw new Error("values() must be called with at least one value");
24685
- }
24686
- const mappedValues = values.map((entry) => {
24687
- const result = {};
24688
- const cols = this.table[Table2.Symbol.Columns];
24689
- for (const colKey of Object.keys(entry)) {
24690
- const colValue = entry[colKey];
24691
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
24692
- }
24693
- return result;
24694
- });
24695
- return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
24696
- }
24697
- };
24698
- __publicField(PgInsertBuilder, _a125, "PgInsertBuilder");
24699
- PgInsertBase = class extends (_b98 = QueryPromise, _a126 = entityKind, _b98) {
24700
- constructor(table4, values, session, dialect4, withList) {
24701
- super();
24702
- __publicField(this, "config");
24703
- __publicField(this, "execute", (placeholderValues) => {
24704
- return tracer.startActiveSpan("drizzle.operation", () => {
24705
- return this._prepare().execute(placeholderValues);
24706
- });
24707
- });
24708
- this.session = session;
24709
- this.dialect = dialect4;
24710
- this.config = { table: table4, values, withList };
24711
- }
24712
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
24713
- this.config.returning = orderSelectedFields(fields);
24714
- return this;
24715
- }
24716
- /**
24717
- * Adds an `on conflict do nothing` clause to the query.
24718
- *
24719
- * Calling this method simply avoids inserting a row as its alternative action.
24720
- *
24721
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
24722
- *
24723
- * @param config The `target` and `where` clauses.
24724
- *
24725
- * @example
24726
- * ```ts
24727
- * // Insert one row and cancel the insert if there's a conflict
24728
- * await db.insert(cars)
24729
- * .values({ id: 1, brand: 'BMW' })
24730
- * .onConflictDoNothing();
24731
- *
24732
- * // Explicitly specify conflict target
24733
- * await db.insert(cars)
24734
- * .values({ id: 1, brand: 'BMW' })
24735
- * .onConflictDoNothing({ target: cars.id });
24736
- * ```
24737
- */
24738
- onConflictDoNothing(config = {}) {
24739
- if (config.target === void 0) {
24740
- this.config.onConflict = sql`do nothing`;
24741
- } else {
24742
- let targetColumn = "";
24743
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24744
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24745
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
24746
- }
24747
- return this;
24748
- }
24749
- /**
24750
- * Adds an `on conflict do update` clause to the query.
24751
- *
24752
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
24753
- *
24754
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
24755
- *
24756
- * @param config The `target`, `set` and `where` clauses.
24757
- *
24758
- * @example
24759
- * ```ts
24760
- * // Update the row if there's a conflict
24761
- * await db.insert(cars)
24762
- * .values({ id: 1, brand: 'BMW' })
24763
- * .onConflictDoUpdate({
24764
- * target: cars.id,
24765
- * set: { brand: 'Porsche' }
24766
- * });
24767
- *
24768
- * // Upsert with 'where' clause
24769
- * await db.insert(cars)
24770
- * .values({ id: 1, brand: 'BMW' })
24771
- * .onConflictDoUpdate({
24772
- * target: cars.id,
24773
- * set: { brand: 'newBMW' },
24774
- * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
24775
- * });
24776
- * ```
24777
- */
24778
- onConflictDoUpdate(config) {
24779
- if (config.where && (config.targetWhere || config.setWhere)) {
24780
- throw new Error(
24781
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
24782
- );
24783
- }
24784
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24785
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
24786
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
24787
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
24788
- let targetColumn = "";
24789
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24790
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
24791
- return this;
24792
- }
24793
- /** @internal */
24794
- getSQL() {
24795
- return this.dialect.buildInsertQuery(this.config);
24796
- }
24797
- toSQL() {
24798
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
24799
- return rest;
24800
- }
24801
24655
  /** @internal */
24802
- _prepare(name2) {
24803
- return tracer.startActiveSpan("drizzle.prepareQuery", () => {
24804
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
24805
- });
24806
- }
24807
- prepare(name2) {
24808
- return this._prepare(name2);
24656
+ setToken(token) {
24657
+ this.authToken = token;
24658
+ return this;
24809
24659
  }
24810
24660
  $dynamic() {
24811
24661
  return this;
24812
24662
  }
24813
24663
  };
24814
- __publicField(PgInsertBase, _a126, "PgInsert");
24664
+ __publicField(PgDeleteBase, _a124, "PgDelete");
24815
24665
  }
24816
24666
  });
24817
24667
 
@@ -24830,13 +24680,13 @@ function toCamelCase(input) {
24830
24680
  function noopCase(input) {
24831
24681
  return input;
24832
24682
  }
24833
- var _a127, CasingCache;
24683
+ var _a125, CasingCache;
24834
24684
  var init_casing = __esm({
24835
24685
  "../drizzle-orm/dist/casing.js"() {
24836
24686
  "use strict";
24837
24687
  init_entity();
24838
24688
  init_table();
24839
- _a127 = entityKind;
24689
+ _a125 = entityKind;
24840
24690
  CasingCache = class {
24841
24691
  constructor(casing2) {
24842
24692
  /** @internal */
@@ -24873,25 +24723,25 @@ var init_casing = __esm({
24873
24723
  this.cachedTables = {};
24874
24724
  }
24875
24725
  };
24876
- __publicField(CasingCache, _a127, "CasingCache");
24726
+ __publicField(CasingCache, _a125, "CasingCache");
24877
24727
  }
24878
24728
  });
24879
24729
 
24880
24730
  // ../drizzle-orm/dist/pg-core/view-base.js
24881
- var _a128, _b99, PgViewBase;
24731
+ var _a126, _b98, PgViewBase;
24882
24732
  var init_view_base = __esm({
24883
24733
  "../drizzle-orm/dist/pg-core/view-base.js"() {
24884
24734
  "use strict";
24885
24735
  init_entity();
24886
24736
  init_sql();
24887
- PgViewBase = class extends (_b99 = View3, _a128 = entityKind, _b99) {
24737
+ PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
24888
24738
  };
24889
- __publicField(PgViewBase, _a128, "PgViewBase");
24739
+ __publicField(PgViewBase, _a126, "PgViewBase");
24890
24740
  }
24891
24741
  });
24892
24742
 
24893
24743
  // ../drizzle-orm/dist/pg-core/dialect.js
24894
- var _a129, PgDialect;
24744
+ var _a127, PgDialect;
24895
24745
  var init_dialect = __esm({
24896
24746
  "../drizzle-orm/dist/pg-core/dialect.js"() {
24897
24747
  "use strict";
@@ -24910,7 +24760,7 @@ var init_dialect = __esm({
24910
24760
  init_utils2();
24911
24761
  init_view_common();
24912
24762
  init_view_base();
24913
- _a129 = entityKind;
24763
+ _a127 = entityKind;
24914
24764
  PgDialect = class {
24915
24765
  constructor(config) {
24916
24766
  /** @internal */
@@ -24990,12 +24840,19 @@ var init_dialect = __esm({
24990
24840
  return [res];
24991
24841
  }));
24992
24842
  }
24993
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24843
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24994
24844
  const withSql = this.buildWithCTE(withList);
24845
+ const tableName = table4[PgTable.Symbol.Name];
24846
+ const tableSchema = table4[PgTable.Symbol.Schema];
24847
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24848
+ const alias = tableName === origTableName ? void 0 : tableName;
24849
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
24995
24850
  const setSql = this.buildUpdateSet(table4, set);
24996
- const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
24851
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
24852
+ const joinsSql = this.buildJoins(joins);
24853
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
24997
24854
  const whereSql = where ? sql` where ${where}` : void 0;
24998
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
24855
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24999
24856
  }
25000
24857
  /**
25001
24858
  * Builds selection SQL with provided fields/expressions
@@ -25047,6 +24904,54 @@ var init_dialect = __esm({
25047
24904
  });
25048
24905
  return sql.join(chunks);
25049
24906
  }
24907
+ buildJoins(joins) {
24908
+ if (!joins || joins.length === 0) {
24909
+ return void 0;
24910
+ }
24911
+ const joinsArray = [];
24912
+ for (const [index4, joinMeta] of joins.entries()) {
24913
+ if (index4 === 0) {
24914
+ joinsArray.push(sql` `);
24915
+ }
24916
+ const table4 = joinMeta.table;
24917
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
24918
+ if (is(table4, PgTable)) {
24919
+ const tableName = table4[PgTable.Symbol.Name];
24920
+ const tableSchema = table4[PgTable.Symbol.Schema];
24921
+ const origTableName = table4[PgTable.Symbol.OriginalName];
24922
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
24923
+ joinsArray.push(
24924
+ 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}`
24925
+ );
24926
+ } else if (is(table4, View3)) {
24927
+ const viewName = table4[ViewBaseConfig].name;
24928
+ const viewSchema = table4[ViewBaseConfig].schema;
24929
+ const origViewName = table4[ViewBaseConfig].originalName;
24930
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
24931
+ joinsArray.push(
24932
+ 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}`
24933
+ );
24934
+ } else {
24935
+ joinsArray.push(
24936
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table4} on ${joinMeta.on}`
24937
+ );
24938
+ }
24939
+ if (index4 < joins.length - 1) {
24940
+ joinsArray.push(sql` `);
24941
+ }
24942
+ }
24943
+ return sql.join(joinsArray);
24944
+ }
24945
+ buildFromTable(table4) {
24946
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
24947
+ let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
24948
+ if (table4[Table2.Symbol.Schema]) {
24949
+ fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
24950
+ }
24951
+ return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
24952
+ }
24953
+ return table4;
24954
+ }
25050
24955
  buildSelectQuery({
25051
24956
  withList,
25052
24957
  fields,
@@ -25081,51 +24986,8 @@ var init_dialect = __esm({
25081
24986
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25082
24987
  }
25083
24988
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25084
- const tableSql = (() => {
25085
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25086
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25087
- if (table4[Table2.Symbol.Schema]) {
25088
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25089
- }
25090
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25091
- }
25092
- return table4;
25093
- })();
25094
- const joinsArray = [];
25095
- if (joins) {
25096
- for (const [index4, joinMeta] of joins.entries()) {
25097
- if (index4 === 0) {
25098
- joinsArray.push(sql` `);
25099
- }
25100
- const table22 = joinMeta.table;
25101
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25102
- if (is(table22, PgTable)) {
25103
- const tableName = table22[PgTable.Symbol.Name];
25104
- const tableSchema = table22[PgTable.Symbol.Schema];
25105
- const origTableName = table22[PgTable.Symbol.OriginalName];
25106
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25107
- joinsArray.push(
25108
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25109
- );
25110
- } else if (is(table22, View3)) {
25111
- const viewName = table22[ViewBaseConfig].name;
25112
- const viewSchema = table22[ViewBaseConfig].schema;
25113
- const origViewName = table22[ViewBaseConfig].originalName;
25114
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25115
- joinsArray.push(
25116
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25117
- );
25118
- } else {
25119
- joinsArray.push(
25120
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25121
- );
25122
- }
25123
- if (index4 < joins.length - 1) {
25124
- joinsArray.push(sql` `);
25125
- }
25126
- }
25127
- }
25128
- const joinsSql = sql.join(joinsArray);
24989
+ const tableSql = this.buildFromTable(table4);
24990
+ const joinsSql = this.buildJoins(joins);
25129
24991
  const whereSql = where ? sql` where ${where}` : void 0;
25130
24992
  const havingSql = having ? sql` having ${having}` : void 0;
25131
24993
  let orderBySql;
@@ -25206,43 +25068,54 @@ var init_dialect = __esm({
25206
25068
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
25207
25069
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
25208
25070
  }
25209
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
25071
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
25210
25072
  const valuesSqlList = [];
25211
25073
  const columns = table4[Table2.Symbol.Columns];
25212
25074
  const colEntries = Object.entries(columns).filter(([_2, col]) => !col.shouldDisableInsert());
25213
25075
  const insertOrder = colEntries.map(
25214
25076
  ([, column4]) => sql.identifier(this.casing.getColumnCasing(column4))
25215
25077
  );
25216
- for (const [valueIndex, value] of values.entries()) {
25217
- const valueList = [];
25218
- for (const [fieldName, col] of colEntries) {
25219
- const colValue = value[fieldName];
25220
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25221
- if (col.defaultFn !== void 0) {
25222
- const defaultFnResult = col.defaultFn();
25223
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25224
- valueList.push(defaultValue);
25225
- } else if (!col.default && col.onUpdateFn !== void 0) {
25226
- const onUpdateFnResult = col.onUpdateFn();
25227
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25228
- valueList.push(newValue);
25078
+ if (select) {
25079
+ const select2 = valuesOrSelect;
25080
+ if (is(select2, SQL)) {
25081
+ valuesSqlList.push(select2);
25082
+ } else {
25083
+ valuesSqlList.push(select2.getSQL());
25084
+ }
25085
+ } else {
25086
+ const values = valuesOrSelect;
25087
+ valuesSqlList.push(sql.raw("values "));
25088
+ for (const [valueIndex, value] of values.entries()) {
25089
+ const valueList = [];
25090
+ for (const [fieldName, col] of colEntries) {
25091
+ const colValue = value[fieldName];
25092
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25093
+ if (col.defaultFn !== void 0) {
25094
+ const defaultFnResult = col.defaultFn();
25095
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25096
+ valueList.push(defaultValue);
25097
+ } else if (!col.default && col.onUpdateFn !== void 0) {
25098
+ const onUpdateFnResult = col.onUpdateFn();
25099
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25100
+ valueList.push(newValue);
25101
+ } else {
25102
+ valueList.push(sql`default`);
25103
+ }
25229
25104
  } else {
25230
- valueList.push(sql`default`);
25105
+ valueList.push(colValue);
25231
25106
  }
25232
- } else {
25233
- valueList.push(colValue);
25234
25107
  }
25235
- }
25236
- valuesSqlList.push(valueList);
25237
- if (valueIndex < values.length - 1) {
25238
- valuesSqlList.push(sql`, `);
25108
+ valuesSqlList.push(valueList);
25109
+ if (valueIndex < values.length - 1) {
25110
+ valuesSqlList.push(sql`, `);
25111
+ }
25239
25112
  }
25240
25113
  }
25241
25114
  const withSql = this.buildWithCTE(withList);
25242
25115
  const valuesSql = sql.join(valuesSqlList);
25243
25116
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25244
25117
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
25245
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
25118
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
25246
25119
  }
25247
25120
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25248
25121
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25978,12 +25851,12 @@ var init_dialect = __esm({
25978
25851
  };
25979
25852
  }
25980
25853
  };
25981
- __publicField(PgDialect, _a129, "PgDialect");
25854
+ __publicField(PgDialect, _a127, "PgDialect");
25982
25855
  }
25983
25856
  });
25984
25857
 
25985
25858
  // ../drizzle-orm/dist/selection-proxy.js
25986
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25859
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25987
25860
  var init_selection_proxy = __esm({
25988
25861
  "../drizzle-orm/dist/selection-proxy.js"() {
25989
25862
  "use strict";
@@ -25993,7 +25866,7 @@ var init_selection_proxy = __esm({
25993
25866
  init_sql();
25994
25867
  init_subquery();
25995
25868
  init_view_common();
25996
- _a130 = entityKind;
25869
+ _a128 = entityKind;
25997
25870
  _SelectionProxyHandler = class _SelectionProxyHandler {
25998
25871
  constructor(config) {
25999
25872
  __publicField(this, "config");
@@ -26059,25 +25932,25 @@ var init_selection_proxy = __esm({
26059
25932
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26060
25933
  }
26061
25934
  };
26062
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25935
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26063
25936
  SelectionProxyHandler = _SelectionProxyHandler;
26064
25937
  }
26065
25938
  });
26066
25939
 
26067
25940
  // ../drizzle-orm/dist/query-builders/query-builder.js
26068
- var _a131, TypedQueryBuilder;
25941
+ var _a129, TypedQueryBuilder;
26069
25942
  var init_query_builder = __esm({
26070
25943
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26071
25944
  "use strict";
26072
25945
  init_entity();
26073
- _a131 = entityKind;
25946
+ _a129 = entityKind;
26074
25947
  TypedQueryBuilder = class {
26075
25948
  /** @internal */
26076
25949
  getSelectedFields() {
26077
25950
  return this._.selectedFields;
26078
25951
  }
26079
25952
  };
26080
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25953
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26081
25954
  }
26082
25955
  });
26083
25956
 
@@ -26099,7 +25972,7 @@ function createSetOperator(type, isAll) {
26099
25972
  return leftSelect.addSetOperators(setOperators);
26100
25973
  };
26101
25974
  }
26102
- var _a132, PgSelectBuilder, _a133, _b100, PgSelectQueryBuilderBase, _a134, _b101, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
25975
+ var _a130, PgSelectBuilder, _a131, _b99, PgSelectQueryBuilderBase, _a132, _b100, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
26103
25976
  var init_select2 = __esm({
26104
25977
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26105
25978
  "use strict";
@@ -26115,7 +25988,7 @@ var init_select2 = __esm({
26115
25988
  init_utils2();
26116
25989
  init_utils2();
26117
25990
  init_view_common();
26118
- _a132 = entityKind;
25991
+ _a130 = entityKind;
26119
25992
  PgSelectBuilder = class {
26120
25993
  constructor(config) {
26121
25994
  __publicField(this, "fields");
@@ -26123,6 +25996,7 @@ var init_select2 = __esm({
26123
25996
  __publicField(this, "dialect");
26124
25997
  __publicField(this, "withList", []);
26125
25998
  __publicField(this, "distinct");
25999
+ __publicField(this, "authToken");
26126
26000
  this.fields = config.fields;
26127
26001
  this.session = config.session;
26128
26002
  this.dialect = config.dialect;
@@ -26131,6 +26005,11 @@ var init_select2 = __esm({
26131
26005
  }
26132
26006
  this.distinct = config.distinct;
26133
26007
  }
26008
+ /** @internal */
26009
+ setToken(token) {
26010
+ this.authToken = token;
26011
+ return this;
26012
+ }
26134
26013
  /**
26135
26014
  * Specify the table, subquery, or other target that you're
26136
26015
  * building a select query against.
@@ -26153,7 +26032,7 @@ var init_select2 = __esm({
26153
26032
  } else {
26154
26033
  fields = getTableColumns(source);
26155
26034
  }
26156
- return new PgSelectBase({
26035
+ return this.authToken === void 0 ? new PgSelectBase({
26157
26036
  table: source,
26158
26037
  fields,
26159
26038
  isPartialSelect,
@@ -26161,11 +26040,19 @@ var init_select2 = __esm({
26161
26040
  dialect: this.dialect,
26162
26041
  withList: this.withList,
26163
26042
  distinct: this.distinct
26164
- });
26043
+ }) : new PgSelectBase({
26044
+ table: source,
26045
+ fields,
26046
+ isPartialSelect,
26047
+ session: this.session,
26048
+ dialect: this.dialect,
26049
+ withList: this.withList,
26050
+ distinct: this.distinct
26051
+ }).setToken(this.authToken);
26165
26052
  }
26166
26053
  };
26167
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26168
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26054
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26055
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26169
26056
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26170
26057
  super();
26171
26058
  __publicField(this, "_");
@@ -26765,19 +26652,20 @@ var init_select2 = __esm({
26765
26652
  return this;
26766
26653
  }
26767
26654
  };
26768
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26769
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26655
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26656
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26770
26657
  constructor() {
26771
26658
  super(...arguments);
26659
+ __publicField(this, "authToken");
26772
26660
  __publicField(this, "execute", (placeholderValues) => {
26773
26661
  return tracer.startActiveSpan("drizzle.operation", () => {
26774
- return this._prepare().execute(placeholderValues);
26662
+ return this._prepare().execute(placeholderValues, this.authToken);
26775
26663
  });
26776
26664
  });
26777
26665
  }
26778
26666
  /** @internal */
26779
26667
  _prepare(name2) {
26780
- const { session, config, dialect: dialect4, joinsNotNullableMap } = this;
26668
+ const { session, config, dialect: dialect4, joinsNotNullableMap, authToken } = this;
26781
26669
  if (!session) {
26782
26670
  throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
26783
26671
  }
@@ -26785,7 +26673,7 @@ var init_select2 = __esm({
26785
26673
  const fieldsList = orderSelectedFields(config.fields);
26786
26674
  const query = session.prepareQuery(dialect4.sqlToQuery(this.getSQL()), fieldsList, name2, true);
26787
26675
  query.joinsNotNullableMap = joinsNotNullableMap;
26788
- return query;
26676
+ return authToken === void 0 ? query : query.setToken(authToken);
26789
26677
  });
26790
26678
  }
26791
26679
  /**
@@ -26798,8 +26686,13 @@ var init_select2 = __esm({
26798
26686
  prepare(name2) {
26799
26687
  return this._prepare(name2);
26800
26688
  }
26689
+ /** @internal */
26690
+ setToken(token) {
26691
+ this.authToken = token;
26692
+ return this;
26693
+ }
26801
26694
  };
26802
- __publicField(PgSelectBase, _a134, "PgSelect");
26695
+ __publicField(PgSelectBase, _a132, "PgSelect");
26803
26696
  applyMixins(PgSelectBase, [QueryPromise]);
26804
26697
  getPgSetOperators = () => ({
26805
26698
  union,
@@ -26819,7 +26712,7 @@ var init_select2 = __esm({
26819
26712
  });
26820
26713
 
26821
26714
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26822
- var _a135, QueryBuilder;
26715
+ var _a133, QueryBuilder;
26823
26716
  var init_query_builder2 = __esm({
26824
26717
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26825
26718
  "use strict";
@@ -26828,7 +26721,7 @@ var init_query_builder2 = __esm({
26828
26721
  init_selection_proxy();
26829
26722
  init_subquery();
26830
26723
  init_select2();
26831
- _a135 = entityKind;
26724
+ _a133 = entityKind;
26832
26725
  QueryBuilder = class {
26833
26726
  constructor(dialect4) {
26834
26727
  __publicField(this, "dialect");
@@ -26909,30 +26802,211 @@ var init_query_builder2 = __esm({
26909
26802
  return this.dialect;
26910
26803
  }
26911
26804
  };
26912
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26805
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26913
26806
  }
26914
26807
  });
26915
26808
 
26916
- // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26917
- var _a136, _b102, PgRefreshMaterializedView;
26918
- var init_refresh_materialized_view = __esm({
26919
- "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26809
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26810
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26811
+ var init_insert = __esm({
26812
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26920
26813
  "use strict";
26921
26814
  init_entity();
26922
26815
  init_query_promise();
26816
+ init_sql();
26817
+ init_table();
26923
26818
  init_tracing();
26924
- PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26925
- constructor(view4, session, dialect4) {
26926
- super();
26927
- __publicField(this, "config");
26928
- __publicField(this, "execute", (placeholderValues) => {
26929
- return tracer.startActiveSpan("drizzle.operation", () => {
26930
- return this._prepare().execute(placeholderValues);
26931
- });
26932
- });
26819
+ init_utils2();
26820
+ init_query_builder2();
26821
+ _a134 = entityKind;
26822
+ PgInsertBuilder = class {
26823
+ constructor(table4, session, dialect4, withList) {
26824
+ __publicField(this, "authToken");
26825
+ this.table = table4;
26933
26826
  this.session = session;
26934
26827
  this.dialect = dialect4;
26935
- this.config = { view: view4 };
26828
+ this.withList = withList;
26829
+ }
26830
+ /** @internal */
26831
+ setToken(token) {
26832
+ this.authToken = token;
26833
+ return this;
26834
+ }
26835
+ values(values) {
26836
+ values = Array.isArray(values) ? values : [values];
26837
+ if (values.length === 0) {
26838
+ throw new Error("values() must be called with at least one value");
26839
+ }
26840
+ const mappedValues = values.map((entry) => {
26841
+ const result = {};
26842
+ const cols = this.table[Table2.Symbol.Columns];
26843
+ for (const colKey of Object.keys(entry)) {
26844
+ const colValue = entry[colKey];
26845
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26846
+ }
26847
+ return result;
26848
+ });
26849
+ return this.authToken === void 0 ? new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList) : new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList).setToken(
26850
+ this.authToken
26851
+ );
26852
+ }
26853
+ select(selectQuery) {
26854
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26855
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26856
+ throw new Error(
26857
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26858
+ );
26859
+ }
26860
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26861
+ }
26862
+ };
26863
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26864
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26865
+ constructor(table4, values, session, dialect4, withList, select) {
26866
+ super();
26867
+ __publicField(this, "config");
26868
+ __publicField(this, "authToken");
26869
+ __publicField(this, "execute", (placeholderValues) => {
26870
+ return tracer.startActiveSpan("drizzle.operation", () => {
26871
+ return this._prepare().execute(placeholderValues, this.authToken);
26872
+ });
26873
+ });
26874
+ this.session = session;
26875
+ this.dialect = dialect4;
26876
+ this.config = { table: table4, values, withList, select };
26877
+ }
26878
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26879
+ this.config.returning = orderSelectedFields(fields);
26880
+ return this;
26881
+ }
26882
+ /**
26883
+ * Adds an `on conflict do nothing` clause to the query.
26884
+ *
26885
+ * Calling this method simply avoids inserting a row as its alternative action.
26886
+ *
26887
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26888
+ *
26889
+ * @param config The `target` and `where` clauses.
26890
+ *
26891
+ * @example
26892
+ * ```ts
26893
+ * // Insert one row and cancel the insert if there's a conflict
26894
+ * await db.insert(cars)
26895
+ * .values({ id: 1, brand: 'BMW' })
26896
+ * .onConflictDoNothing();
26897
+ *
26898
+ * // Explicitly specify conflict target
26899
+ * await db.insert(cars)
26900
+ * .values({ id: 1, brand: 'BMW' })
26901
+ * .onConflictDoNothing({ target: cars.id });
26902
+ * ```
26903
+ */
26904
+ onConflictDoNothing(config = {}) {
26905
+ if (config.target === void 0) {
26906
+ this.config.onConflict = sql`do nothing`;
26907
+ } else {
26908
+ let targetColumn = "";
26909
+ 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));
26910
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26911
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26912
+ }
26913
+ return this;
26914
+ }
26915
+ /**
26916
+ * Adds an `on conflict do update` clause to the query.
26917
+ *
26918
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26919
+ *
26920
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26921
+ *
26922
+ * @param config The `target`, `set` and `where` clauses.
26923
+ *
26924
+ * @example
26925
+ * ```ts
26926
+ * // Update the row if there's a conflict
26927
+ * await db.insert(cars)
26928
+ * .values({ id: 1, brand: 'BMW' })
26929
+ * .onConflictDoUpdate({
26930
+ * target: cars.id,
26931
+ * set: { brand: 'Porsche' }
26932
+ * });
26933
+ *
26934
+ * // Upsert with 'where' clause
26935
+ * await db.insert(cars)
26936
+ * .values({ id: 1, brand: 'BMW' })
26937
+ * .onConflictDoUpdate({
26938
+ * target: cars.id,
26939
+ * set: { brand: 'newBMW' },
26940
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26941
+ * });
26942
+ * ```
26943
+ */
26944
+ onConflictDoUpdate(config) {
26945
+ if (config.where && (config.targetWhere || config.setWhere)) {
26946
+ throw new Error(
26947
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26948
+ );
26949
+ }
26950
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26951
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26952
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26953
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26954
+ let targetColumn = "";
26955
+ 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));
26956
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26957
+ return this;
26958
+ }
26959
+ /** @internal */
26960
+ getSQL() {
26961
+ return this.dialect.buildInsertQuery(this.config);
26962
+ }
26963
+ toSQL() {
26964
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26965
+ return rest;
26966
+ }
26967
+ /** @internal */
26968
+ _prepare(name2) {
26969
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26970
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26971
+ });
26972
+ }
26973
+ prepare(name2) {
26974
+ return this._prepare(name2);
26975
+ }
26976
+ /** @internal */
26977
+ setToken(token) {
26978
+ this.authToken = token;
26979
+ return this;
26980
+ }
26981
+ $dynamic() {
26982
+ return this;
26983
+ }
26984
+ };
26985
+ __publicField(PgInsertBase, _a135, "PgInsert");
26986
+ }
26987
+ });
26988
+
26989
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26990
+ var _a136, _b102, PgRefreshMaterializedView;
26991
+ var init_refresh_materialized_view = __esm({
26992
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26993
+ "use strict";
26994
+ init_entity();
26995
+ init_query_promise();
26996
+ init_tracing();
26997
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26998
+ constructor(view4, session, dialect4) {
26999
+ super();
27000
+ __publicField(this, "config");
27001
+ __publicField(this, "authToken");
27002
+ __publicField(this, "execute", (placeholderValues) => {
27003
+ return tracer.startActiveSpan("drizzle.operation", () => {
27004
+ return this._prepare().execute(placeholderValues, this.authToken);
27005
+ });
27006
+ });
27007
+ this.session = session;
27008
+ this.dialect = dialect4;
27009
+ this.config = { view: view4 };
26936
27010
  }
26937
27011
  concurrently() {
26938
27012
  if (this.config.withNoData !== void 0) {
@@ -26965,6 +27039,11 @@ var init_refresh_materialized_view = __esm({
26965
27039
  prepare(name2) {
26966
27040
  return this._prepare(name2);
26967
27041
  }
27042
+ /** @internal */
27043
+ setToken(token) {
27044
+ this.authToken = token;
27045
+ return this;
27046
+ }
26968
27047
  };
26969
27048
  __publicField(PgRefreshMaterializedView, _a136, "PgRefreshMaterializedView");
26970
27049
  }
@@ -26983,25 +27062,41 @@ var init_update = __esm({
26983
27062
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26984
27063
  "use strict";
26985
27064
  init_entity();
27065
+ init_table2();
26986
27066
  init_query_promise();
27067
+ init_selection_proxy();
27068
+ init_sql();
27069
+ init_subquery();
26987
27070
  init_table();
26988
27071
  init_utils2();
27072
+ init_view_common();
26989
27073
  _a137 = entityKind;
26990
27074
  PgUpdateBuilder = class {
26991
27075
  constructor(table4, session, dialect4, withList) {
27076
+ __publicField(this, "authToken");
26992
27077
  this.table = table4;
26993
27078
  this.session = session;
26994
27079
  this.dialect = dialect4;
26995
27080
  this.withList = withList;
26996
27081
  }
27082
+ setToken(token) {
27083
+ this.authToken = token;
27084
+ return this;
27085
+ }
26997
27086
  set(values) {
26998
- return new PgUpdateBase(
27087
+ return this.authToken === void 0 ? new PgUpdateBase(
26999
27088
  this.table,
27000
27089
  mapUpdateSet(this.table, values),
27001
27090
  this.session,
27002
27091
  this.dialect,
27003
27092
  this.withList
27004
- );
27093
+ ) : new PgUpdateBase(
27094
+ this.table,
27095
+ mapUpdateSet(this.table, values),
27096
+ this.session,
27097
+ this.dialect,
27098
+ this.withList
27099
+ ).setToken(this.authToken);
27005
27100
  }
27006
27101
  };
27007
27102
  __publicField(PgUpdateBuilder, _a137, "PgUpdateBuilder");
@@ -27009,12 +27104,86 @@ var init_update = __esm({
27009
27104
  constructor(table4, set, session, dialect4, withList) {
27010
27105
  super();
27011
27106
  __publicField(this, "config");
27107
+ __publicField(this, "tableName");
27108
+ __publicField(this, "joinsNotNullableMap");
27109
+ __publicField(this, "leftJoin", this.createJoin("left"));
27110
+ __publicField(this, "rightJoin", this.createJoin("right"));
27111
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27112
+ __publicField(this, "fullJoin", this.createJoin("full"));
27113
+ __publicField(this, "authToken");
27012
27114
  __publicField(this, "execute", (placeholderValues) => {
27013
- return this._prepare().execute(placeholderValues);
27115
+ return this._prepare().execute(placeholderValues, this.authToken);
27014
27116
  });
27015
27117
  this.session = session;
27016
27118
  this.dialect = dialect4;
27017
- this.config = { set, table: table4, withList };
27119
+ this.config = { set, table: table4, withList, joins: [] };
27120
+ this.tableName = getTableLikeName(table4);
27121
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27122
+ }
27123
+ from(source) {
27124
+ const tableName = getTableLikeName(source);
27125
+ if (typeof tableName === "string") {
27126
+ this.joinsNotNullableMap[tableName] = true;
27127
+ }
27128
+ this.config.from = source;
27129
+ return this;
27130
+ }
27131
+ getTableLikeFields(table4) {
27132
+ if (is(table4, PgTable)) {
27133
+ return table4[Table2.Symbol.Columns];
27134
+ } else if (is(table4, Subquery)) {
27135
+ return table4._.selectedFields;
27136
+ }
27137
+ return table4[ViewBaseConfig].selectedFields;
27138
+ }
27139
+ createJoin(joinType) {
27140
+ return (table4, on) => {
27141
+ const tableName = getTableLikeName(table4);
27142
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27143
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27144
+ }
27145
+ if (typeof on === "function") {
27146
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27147
+ on = on(
27148
+ new Proxy(
27149
+ this.config.table[Table2.Symbol.Columns],
27150
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27151
+ ),
27152
+ from && new Proxy(
27153
+ from,
27154
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27155
+ )
27156
+ );
27157
+ }
27158
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27159
+ if (typeof tableName === "string") {
27160
+ switch (joinType) {
27161
+ case "left": {
27162
+ this.joinsNotNullableMap[tableName] = false;
27163
+ break;
27164
+ }
27165
+ case "right": {
27166
+ this.joinsNotNullableMap = Object.fromEntries(
27167
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27168
+ );
27169
+ this.joinsNotNullableMap[tableName] = true;
27170
+ break;
27171
+ }
27172
+ case "inner": {
27173
+ this.joinsNotNullableMap[tableName] = true;
27174
+ break;
27175
+ }
27176
+ case "full": {
27177
+ this.joinsNotNullableMap = Object.fromEntries(
27178
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27179
+ );
27180
+ this.joinsNotNullableMap[tableName] = false;
27181
+ break;
27182
+ }
27183
+ }
27184
+ }
27185
+ return this;
27186
+ };
27018
27187
  }
27019
27188
  /**
27020
27189
  * Adds a 'where' clause to the query.
@@ -27053,7 +27222,24 @@ var init_update = __esm({
27053
27222
  this.config.where = where;
27054
27223
  return this;
27055
27224
  }
27056
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27225
+ returning(fields) {
27226
+ if (!fields) {
27227
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27228
+ if (this.config.from) {
27229
+ const tableName = getTableLikeName(this.config.from);
27230
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27231
+ const fromFields = this.getTableLikeFields(this.config.from);
27232
+ fields[tableName] = fromFields;
27233
+ }
27234
+ for (const join of this.config.joins) {
27235
+ const tableName2 = getTableLikeName(join.table);
27236
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27237
+ const fromFields = this.getTableLikeFields(join.table);
27238
+ fields[tableName2] = fromFields;
27239
+ }
27240
+ }
27241
+ }
27242
+ }
27057
27243
  this.config.returning = orderSelectedFields(fields);
27058
27244
  return this;
27059
27245
  }
@@ -27067,11 +27253,18 @@ var init_update = __esm({
27067
27253
  }
27068
27254
  /** @internal */
27069
27255
  _prepare(name2) {
27070
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27256
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27257
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27258
+ return query;
27071
27259
  }
27072
27260
  prepare(name2) {
27073
27261
  return this._prepare(name2);
27074
27262
  }
27263
+ /** @internal */
27264
+ setToken(token) {
27265
+ this.authToken = token;
27266
+ return this;
27267
+ }
27075
27268
  $dynamic() {
27076
27269
  return this;
27077
27270
  }
@@ -27105,6 +27298,7 @@ var init_count = __esm({
27105
27298
  constructor(params) {
27106
27299
  super(_PgCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
27107
27300
  __publicField(this, "sql");
27301
+ __publicField(this, "token");
27108
27302
  __publicField(this, _a139, "PgCountBuilder");
27109
27303
  __publicField(this, "session");
27110
27304
  this.params = params;
@@ -27121,8 +27315,12 @@ var init_count = __esm({
27121
27315
  static buildCount(source, filters) {
27122
27316
  return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters};`;
27123
27317
  }
27318
+ /** @intrnal */
27319
+ setToken(token) {
27320
+ this.token = token;
27321
+ }
27124
27322
  then(onfulfilled, onrejected) {
27125
- return Promise.resolve(this.session.count(this.sql)).then(
27323
+ return Promise.resolve(this.session.count(this.sql, this.token)).then(
27126
27324
  onfulfilled,
27127
27325
  onrejected
27128
27326
  );
@@ -27199,6 +27397,7 @@ var init_query = __esm({
27199
27397
  PgRelationalQuery = class extends (_b105 = QueryPromise, _a141 = entityKind, _b105) {
27200
27398
  constructor(fullSchema, schema4, tableNamesMap, table4, tableConfig, dialect4, session, config, mode) {
27201
27399
  super();
27400
+ __publicField(this, "authToken");
27202
27401
  this.fullSchema = fullSchema;
27203
27402
  this.schema = schema4;
27204
27403
  this.tableNamesMap = tableNamesMap;
@@ -27256,9 +27455,14 @@ var init_query = __esm({
27256
27455
  toSQL() {
27257
27456
  return this._toSQL().builtQuery;
27258
27457
  }
27458
+ /** @internal */
27459
+ setToken(token) {
27460
+ this.authToken = token;
27461
+ return this;
27462
+ }
27259
27463
  execute() {
27260
27464
  return tracer.startActiveSpan("drizzle.operation", () => {
27261
- return this._prepare().execute();
27465
+ return this._prepare().execute(void 0, this.authToken);
27262
27466
  });
27263
27467
  }
27264
27468
  };
@@ -27321,6 +27525,7 @@ var init_db = __esm({
27321
27525
  PgDatabase = class {
27322
27526
  constructor(dialect4, session, schema4) {
27323
27527
  __publicField(this, "query");
27528
+ __publicField(this, "authToken");
27324
27529
  this.dialect = dialect4;
27325
27530
  this.session = session;
27326
27531
  this._ = schema4 ? {
@@ -27576,7 +27781,7 @@ var init_db = __esm({
27576
27781
  false
27577
27782
  );
27578
27783
  return new PgRaw(
27579
- () => prepared.execute(),
27784
+ () => prepared.execute(void 0, this.authToken),
27580
27785
  sequel,
27581
27786
  builtQuery,
27582
27787
  (result) => prepared.mapResult(result, true)
@@ -28121,6 +28326,7 @@ var init_session = __esm({
28121
28326
  _a159 = entityKind;
28122
28327
  PgPreparedQuery = class {
28123
28328
  constructor(query) {
28329
+ __publicField(this, "authToken");
28124
28330
  /** @internal */
28125
28331
  __publicField(this, "joinsNotNullableMap");
28126
28332
  this.query = query;
@@ -28131,6 +28337,11 @@ var init_session = __esm({
28131
28337
  mapResult(response, _isFromBatch) {
28132
28338
  return response;
28133
28339
  }
28340
+ /** @internal */
28341
+ setToken(token) {
28342
+ this.authToken = token;
28343
+ return this;
28344
+ }
28134
28345
  };
28135
28346
  __publicField(PgPreparedQuery, _a159, "PgPreparedQuery");
28136
28347
  _a160 = entityKind;
@@ -28138,7 +28349,8 @@ var init_session = __esm({
28138
28349
  constructor(dialect4) {
28139
28350
  this.dialect = dialect4;
28140
28351
  }
28141
- execute(query) {
28352
+ /** @internal */
28353
+ execute(query, token) {
28142
28354
  return tracer.startActiveSpan("drizzle.operation", () => {
28143
28355
  const prepared = tracer.startActiveSpan("drizzle.prepareQuery", () => {
28144
28356
  return this.prepareQuery(
@@ -28148,7 +28360,7 @@ var init_session = __esm({
28148
28360
  false
28149
28361
  );
28150
28362
  });
28151
- return prepared.execute();
28363
+ return prepared.setToken(token).execute(void 0, token);
28152
28364
  });
28153
28365
  }
28154
28366
  all(query) {
@@ -28159,8 +28371,9 @@ var init_session = __esm({
28159
28371
  false
28160
28372
  ).all();
28161
28373
  }
28162
- async count(sql2) {
28163
- const res = await this.execute(sql2);
28374
+ /** @internal */
28375
+ async count(sql2, token) {
28376
+ const res = await this.execute(sql2, token);
28164
28377
  return Number(
28165
28378
  res[0]["count"]
28166
28379
  );
@@ -30817,200 +31030,32 @@ var init_delete2 = __esm({
30817
31030
  prepare() {
30818
31031
  return this._prepare(false);
30819
31032
  }
30820
- async execute(placeholderValues) {
30821
- return this._prepare().execute(placeholderValues);
30822
- }
30823
- $dynamic() {
30824
- return this;
30825
- }
30826
- };
30827
- __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30828
- }
30829
- });
30830
-
30831
- // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
30832
- var _a197, SQLiteInsertBuilder, _a198, _b142, SQLiteInsertBase;
30833
- var init_insert2 = __esm({
30834
- "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
30835
- "use strict";
30836
- init_entity();
30837
- init_query_promise();
30838
- init_sql();
30839
- init_table3();
30840
- init_table();
30841
- init_utils2();
30842
- _a197 = entityKind;
30843
- SQLiteInsertBuilder = class {
30844
- constructor(table4, session, dialect4, withList) {
30845
- this.table = table4;
30846
- this.session = session;
30847
- this.dialect = dialect4;
30848
- this.withList = withList;
30849
- }
30850
- values(values) {
30851
- values = Array.isArray(values) ? values : [values];
30852
- if (values.length === 0) {
30853
- throw new Error("values() must be called with at least one value");
30854
- }
30855
- const mappedValues = values.map((entry) => {
30856
- const result = {};
30857
- const cols = this.table[Table2.Symbol.Columns];
30858
- for (const colKey of Object.keys(entry)) {
30859
- const colValue = entry[colKey];
30860
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
30861
- }
30862
- return result;
30863
- });
30864
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
30865
- }
30866
- };
30867
- __publicField(SQLiteInsertBuilder, _a197, "SQLiteInsertBuilder");
30868
- SQLiteInsertBase = class extends (_b142 = QueryPromise, _a198 = entityKind, _b142) {
30869
- constructor(table4, values, session, dialect4, withList) {
30870
- super();
30871
- /** @internal */
30872
- __publicField(this, "config");
30873
- __publicField(this, "run", (placeholderValues) => {
30874
- return this._prepare().run(placeholderValues);
30875
- });
30876
- __publicField(this, "all", (placeholderValues) => {
30877
- return this._prepare().all(placeholderValues);
30878
- });
30879
- __publicField(this, "get", (placeholderValues) => {
30880
- return this._prepare().get(placeholderValues);
30881
- });
30882
- __publicField(this, "values", (placeholderValues) => {
30883
- return this._prepare().values(placeholderValues);
30884
- });
30885
- this.session = session;
30886
- this.dialect = dialect4;
30887
- this.config = { table: table4, values, withList };
30888
- }
30889
- returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
30890
- this.config.returning = orderSelectedFields(fields);
30891
- return this;
30892
- }
30893
- /**
30894
- * Adds an `on conflict do nothing` clause to the query.
30895
- *
30896
- * Calling this method simply avoids inserting a row as its alternative action.
30897
- *
30898
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
30899
- *
30900
- * @param config The `target` and `where` clauses.
30901
- *
30902
- * @example
30903
- * ```ts
30904
- * // Insert one row and cancel the insert if there's a conflict
30905
- * await db.insert(cars)
30906
- * .values({ id: 1, brand: 'BMW' })
30907
- * .onConflictDoNothing();
30908
- *
30909
- * // Explicitly specify conflict target
30910
- * await db.insert(cars)
30911
- * .values({ id: 1, brand: 'BMW' })
30912
- * .onConflictDoNothing({ target: cars.id });
30913
- * ```
30914
- */
30915
- onConflictDoNothing(config = {}) {
30916
- if (config.target === void 0) {
30917
- this.config.onConflict = sql`do nothing`;
30918
- } else {
30919
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30920
- const whereSql = config.where ? sql` where ${config.where}` : sql``;
30921
- this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
30922
- }
30923
- return this;
30924
- }
30925
- /**
30926
- * Adds an `on conflict do update` clause to the query.
30927
- *
30928
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
30929
- *
30930
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
30931
- *
30932
- * @param config The `target`, `set` and `where` clauses.
30933
- *
30934
- * @example
30935
- * ```ts
30936
- * // Update the row if there's a conflict
30937
- * await db.insert(cars)
30938
- * .values({ id: 1, brand: 'BMW' })
30939
- * .onConflictDoUpdate({
30940
- * target: cars.id,
30941
- * set: { brand: 'Porsche' }
30942
- * });
30943
- *
30944
- * // Upsert with 'where' clause
30945
- * await db.insert(cars)
30946
- * .values({ id: 1, brand: 'BMW' })
30947
- * .onConflictDoUpdate({
30948
- * target: cars.id,
30949
- * set: { brand: 'newBMW' },
30950
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
30951
- * });
30952
- * ```
30953
- */
30954
- onConflictDoUpdate(config) {
30955
- if (config.where && (config.targetWhere || config.setWhere)) {
30956
- throw new Error(
30957
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
30958
- );
30959
- }
30960
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
30961
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
30962
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
30963
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30964
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
30965
- this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
30966
- return this;
30967
- }
30968
- /** @internal */
30969
- getSQL() {
30970
- return this.dialect.buildInsertQuery(this.config);
30971
- }
30972
- toSQL() {
30973
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
30974
- return rest;
30975
- }
30976
- /** @internal */
30977
- _prepare(isOneTimeQuery = true) {
30978
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
30979
- this.dialect.sqlToQuery(this.getSQL()),
30980
- this.config.returning,
30981
- this.config.returning ? "all" : "run",
30982
- true
30983
- );
30984
- }
30985
- prepare() {
30986
- return this._prepare(false);
30987
- }
30988
- async execute() {
30989
- return this.config.returning ? this.all() : this.run();
30990
- }
31033
+ async execute(placeholderValues) {
31034
+ return this._prepare().execute(placeholderValues);
31035
+ }
30991
31036
  $dynamic() {
30992
31037
  return this;
30993
31038
  }
30994
31039
  };
30995
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
31040
+ __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30996
31041
  }
30997
31042
  });
30998
31043
 
30999
31044
  // ../drizzle-orm/dist/sqlite-core/view-base.js
31000
- var _a199, _b143, SQLiteViewBase;
31045
+ var _a197, _b142, SQLiteViewBase;
31001
31046
  var init_view_base2 = __esm({
31002
31047
  "../drizzle-orm/dist/sqlite-core/view-base.js"() {
31003
31048
  "use strict";
31004
31049
  init_entity();
31005
31050
  init_sql();
31006
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31051
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
31007
31052
  };
31008
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
31053
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31009
31054
  }
31010
31055
  });
31011
31056
 
31012
31057
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31013
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
31058
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31014
31059
  var init_dialect2 = __esm({
31015
31060
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31016
31061
  "use strict";
@@ -31029,7 +31074,7 @@ var init_dialect2 = __esm({
31029
31074
  init_utils2();
31030
31075
  init_view_common();
31031
31076
  init_view_base2();
31032
- _a200 = entityKind;
31077
+ _a198 = entityKind;
31033
31078
  SQLiteDialect = class {
31034
31079
  constructor(config) {
31035
31080
  /** @internal */
@@ -31082,14 +31127,16 @@ var init_dialect2 = __esm({
31082
31127
  return [res];
31083
31128
  }));
31084
31129
  }
31085
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31130
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31086
31131
  const withSql = this.buildWithCTE(withList);
31087
31132
  const setSql = this.buildUpdateSet(table4, set);
31133
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31134
+ const joinsSql = this.buildJoins(joins);
31088
31135
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31089
31136
  const whereSql = where ? sql` where ${where}` : void 0;
31090
31137
  const orderBySql = this.buildOrderBy(orderBy);
31091
31138
  const limitSql = this.buildLimit(limit);
31092
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31139
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31093
31140
  }
31094
31141
  /**
31095
31142
  * Builds selection SQL with provided fields/expressions
@@ -31142,6 +31189,37 @@ var init_dialect2 = __esm({
31142
31189
  });
31143
31190
  return sql.join(chunks);
31144
31191
  }
31192
+ buildJoins(joins) {
31193
+ if (!joins || joins.length === 0) {
31194
+ return void 0;
31195
+ }
31196
+ const joinsArray = [];
31197
+ if (joins) {
31198
+ for (const [index4, joinMeta] of joins.entries()) {
31199
+ if (index4 === 0) {
31200
+ joinsArray.push(sql` `);
31201
+ }
31202
+ const table4 = joinMeta.table;
31203
+ if (is(table4, SQLiteTable)) {
31204
+ const tableName = table4[SQLiteTable.Symbol.Name];
31205
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31206
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31207
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31208
+ joinsArray.push(
31209
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31210
+ );
31211
+ } else {
31212
+ joinsArray.push(
31213
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31214
+ );
31215
+ }
31216
+ if (index4 < joins.length - 1) {
31217
+ joinsArray.push(sql` `);
31218
+ }
31219
+ }
31220
+ }
31221
+ return sql.join(joinsArray);
31222
+ }
31145
31223
  buildLimit(limit) {
31146
31224
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31147
31225
  }
@@ -31157,6 +31235,12 @@ var init_dialect2 = __esm({
31157
31235
  }
31158
31236
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31159
31237
  }
31238
+ buildFromTable(table4) {
31239
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31240
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31241
+ }
31242
+ return table4;
31243
+ }
31160
31244
  buildSelectQuery({
31161
31245
  withList,
31162
31246
  fields,
@@ -31187,38 +31271,8 @@ var init_dialect2 = __esm({
31187
31271
  const withSql = this.buildWithCTE(withList);
31188
31272
  const distinctSql = distinct ? sql` distinct` : void 0;
31189
31273
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31190
- const tableSql = (() => {
31191
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31192
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31193
- }
31194
- return table4;
31195
- })();
31196
- const joinsArray = [];
31197
- if (joins) {
31198
- for (const [index4, joinMeta] of joins.entries()) {
31199
- if (index4 === 0) {
31200
- joinsArray.push(sql` `);
31201
- }
31202
- const table22 = joinMeta.table;
31203
- if (is(table22, SQLiteTable)) {
31204
- const tableName = table22[SQLiteTable.Symbol.Name];
31205
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31206
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31207
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31208
- joinsArray.push(
31209
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31210
- );
31211
- } else {
31212
- joinsArray.push(
31213
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31214
- );
31215
- }
31216
- if (index4 < joins.length - 1) {
31217
- joinsArray.push(sql` `);
31218
- }
31219
- }
31220
- }
31221
- const joinsSql = sql.join(joinsArray);
31274
+ const tableSql = this.buildFromTable(table4);
31275
+ const joinsSql = this.buildJoins(joins);
31222
31276
  const whereSql = where ? sql` where ${where}` : void 0;
31223
31277
  const havingSql = having ? sql` having ${having}` : void 0;
31224
31278
  const groupByList = [];
@@ -31284,45 +31338,56 @@ var init_dialect2 = __esm({
31284
31338
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31285
31339
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31286
31340
  }
31287
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31341
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31288
31342
  const valuesSqlList = [];
31289
31343
  const columns = table4[Table2.Symbol.Columns];
31290
31344
  const colEntries = Object.entries(columns).filter(
31291
31345
  ([_2, col]) => !col.shouldDisableInsert()
31292
31346
  );
31293
31347
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
31294
- for (const [valueIndex, value] of values.entries()) {
31295
- const valueList = [];
31296
- for (const [fieldName, col] of colEntries) {
31297
- const colValue = value[fieldName];
31298
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31299
- let defaultValue;
31300
- if (col.default !== null && col.default !== void 0) {
31301
- defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31302
- } else if (col.defaultFn !== void 0) {
31303
- const defaultFnResult = col.defaultFn();
31304
- defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31305
- } else if (!col.default && col.onUpdateFn !== void 0) {
31306
- const onUpdateFnResult = col.onUpdateFn();
31307
- defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31348
+ if (select) {
31349
+ const select2 = valuesOrSelect;
31350
+ if (is(select2, SQL)) {
31351
+ valuesSqlList.push(select2);
31352
+ } else {
31353
+ valuesSqlList.push(select2.getSQL());
31354
+ }
31355
+ } else {
31356
+ const values = valuesOrSelect;
31357
+ valuesSqlList.push(sql.raw("values "));
31358
+ for (const [valueIndex, value] of values.entries()) {
31359
+ const valueList = [];
31360
+ for (const [fieldName, col] of colEntries) {
31361
+ const colValue = value[fieldName];
31362
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31363
+ let defaultValue;
31364
+ if (col.default !== null && col.default !== void 0) {
31365
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31366
+ } else if (col.defaultFn !== void 0) {
31367
+ const defaultFnResult = col.defaultFn();
31368
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31369
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31370
+ const onUpdateFnResult = col.onUpdateFn();
31371
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31372
+ } else {
31373
+ defaultValue = sql`null`;
31374
+ }
31375
+ valueList.push(defaultValue);
31308
31376
  } else {
31309
- defaultValue = sql`null`;
31377
+ valueList.push(colValue);
31310
31378
  }
31311
- valueList.push(defaultValue);
31312
- } else {
31313
- valueList.push(colValue);
31314
31379
  }
31315
- }
31316
- valuesSqlList.push(valueList);
31317
- if (valueIndex < values.length - 1) {
31318
- valuesSqlList.push(sql`, `);
31380
+ valuesSqlList.push(valueList);
31381
+ if (valueIndex < values.length - 1) {
31382
+ valuesSqlList.push(sql`, `);
31383
+ }
31319
31384
  }
31320
31385
  }
31321
31386
  const withSql = this.buildWithCTE(withList);
31322
31387
  const valuesSql = sql.join(valuesSqlList);
31323
31388
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31324
31389
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31325
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31390
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31326
31391
  }
31327
31392
  sqlToQuery(sql2, invokeSource) {
31328
31393
  return sql2.toQuery({
@@ -31551,8 +31616,8 @@ var init_dialect2 = __esm({
31551
31616
  };
31552
31617
  }
31553
31618
  };
31554
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31555
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31619
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31620
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31556
31621
  migrate(migrations, session, config) {
31557
31622
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31558
31623
  const migrationTableCreate = sql`
@@ -31586,8 +31651,8 @@ var init_dialect2 = __esm({
31586
31651
  }
31587
31652
  }
31588
31653
  };
31589
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31590
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31654
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31655
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31591
31656
  async migrate(migrations, session, config) {
31592
31657
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31593
31658
  const migrationTableCreate = sql`
@@ -31616,7 +31681,7 @@ var init_dialect2 = __esm({
31616
31681
  });
31617
31682
  }
31618
31683
  };
31619
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31684
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31620
31685
  }
31621
31686
  });
31622
31687
 
@@ -31638,7 +31703,7 @@ function createSetOperator2(type, isAll) {
31638
31703
  return leftSelect.addSetOperators(setOperators);
31639
31704
  };
31640
31705
  }
31641
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31706
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31642
31707
  var init_select3 = __esm({
31643
31708
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31644
31709
  "use strict";
@@ -31652,7 +31717,7 @@ var init_select3 = __esm({
31652
31717
  init_utils2();
31653
31718
  init_view_common();
31654
31719
  init_view_base2();
31655
- _a203 = entityKind;
31720
+ _a201 = entityKind;
31656
31721
  SQLiteSelectBuilder = class {
31657
31722
  constructor(config) {
31658
31723
  __publicField(this, "fields");
@@ -31693,8 +31758,8 @@ var init_select3 = __esm({
31693
31758
  });
31694
31759
  }
31695
31760
  };
31696
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31697
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31761
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31762
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31698
31763
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31699
31764
  super();
31700
31765
  __publicField(this, "_");
@@ -32199,8 +32264,8 @@ var init_select3 = __esm({
32199
32264
  return this;
32200
32265
  }
32201
32266
  };
32202
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32203
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32267
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32268
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32204
32269
  constructor() {
32205
32270
  super(...arguments);
32206
32271
  __publicField(this, "run", (placeholderValues) => {
@@ -32238,7 +32303,7 @@ var init_select3 = __esm({
32238
32303
  return this.all();
32239
32304
  }
32240
32305
  };
32241
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32306
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32242
32307
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32243
32308
  getSQLiteSetOperators = () => ({
32244
32309
  union: union2,
@@ -32254,7 +32319,7 @@ var init_select3 = __esm({
32254
32319
  });
32255
32320
 
32256
32321
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32257
- var _a206, QueryBuilder2;
32322
+ var _a204, QueryBuilder2;
32258
32323
  var init_query_builder3 = __esm({
32259
32324
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32260
32325
  "use strict";
@@ -32263,7 +32328,7 @@ var init_query_builder3 = __esm({
32263
32328
  init_dialect2();
32264
32329
  init_subquery();
32265
32330
  init_select3();
32266
- _a206 = entityKind;
32331
+ _a204 = entityKind;
32267
32332
  QueryBuilder2 = class {
32268
32333
  constructor(dialect4) {
32269
32334
  __publicField(this, "dialect");
@@ -32325,7 +32390,185 @@ var init_query_builder3 = __esm({
32325
32390
  return this.dialect;
32326
32391
  }
32327
32392
  };
32328
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32393
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32394
+ }
32395
+ });
32396
+
32397
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32398
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32399
+ var init_insert2 = __esm({
32400
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32401
+ "use strict";
32402
+ init_entity();
32403
+ init_query_promise();
32404
+ init_sql();
32405
+ init_table3();
32406
+ init_table();
32407
+ init_utils2();
32408
+ init_query_builder3();
32409
+ _a205 = entityKind;
32410
+ SQLiteInsertBuilder = class {
32411
+ constructor(table4, session, dialect4, withList) {
32412
+ this.table = table4;
32413
+ this.session = session;
32414
+ this.dialect = dialect4;
32415
+ this.withList = withList;
32416
+ }
32417
+ values(values) {
32418
+ values = Array.isArray(values) ? values : [values];
32419
+ if (values.length === 0) {
32420
+ throw new Error("values() must be called with at least one value");
32421
+ }
32422
+ const mappedValues = values.map((entry) => {
32423
+ const result = {};
32424
+ const cols = this.table[Table2.Symbol.Columns];
32425
+ for (const colKey of Object.keys(entry)) {
32426
+ const colValue = entry[colKey];
32427
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32428
+ }
32429
+ return result;
32430
+ });
32431
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32432
+ }
32433
+ select(selectQuery) {
32434
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32435
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32436
+ throw new Error(
32437
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32438
+ );
32439
+ }
32440
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32441
+ }
32442
+ };
32443
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32444
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32445
+ constructor(table4, values, session, dialect4, withList, select) {
32446
+ super();
32447
+ /** @internal */
32448
+ __publicField(this, "config");
32449
+ __publicField(this, "run", (placeholderValues) => {
32450
+ return this._prepare().run(placeholderValues);
32451
+ });
32452
+ __publicField(this, "all", (placeholderValues) => {
32453
+ return this._prepare().all(placeholderValues);
32454
+ });
32455
+ __publicField(this, "get", (placeholderValues) => {
32456
+ return this._prepare().get(placeholderValues);
32457
+ });
32458
+ __publicField(this, "values", (placeholderValues) => {
32459
+ return this._prepare().values(placeholderValues);
32460
+ });
32461
+ this.session = session;
32462
+ this.dialect = dialect4;
32463
+ this.config = { table: table4, values, withList, select };
32464
+ }
32465
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32466
+ this.config.returning = orderSelectedFields(fields);
32467
+ return this;
32468
+ }
32469
+ /**
32470
+ * Adds an `on conflict do nothing` clause to the query.
32471
+ *
32472
+ * Calling this method simply avoids inserting a row as its alternative action.
32473
+ *
32474
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32475
+ *
32476
+ * @param config The `target` and `where` clauses.
32477
+ *
32478
+ * @example
32479
+ * ```ts
32480
+ * // Insert one row and cancel the insert if there's a conflict
32481
+ * await db.insert(cars)
32482
+ * .values({ id: 1, brand: 'BMW' })
32483
+ * .onConflictDoNothing();
32484
+ *
32485
+ * // Explicitly specify conflict target
32486
+ * await db.insert(cars)
32487
+ * .values({ id: 1, brand: 'BMW' })
32488
+ * .onConflictDoNothing({ target: cars.id });
32489
+ * ```
32490
+ */
32491
+ onConflictDoNothing(config = {}) {
32492
+ if (config.target === void 0) {
32493
+ this.config.onConflict = sql`do nothing`;
32494
+ } else {
32495
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32496
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32497
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32498
+ }
32499
+ return this;
32500
+ }
32501
+ /**
32502
+ * Adds an `on conflict do update` clause to the query.
32503
+ *
32504
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32505
+ *
32506
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32507
+ *
32508
+ * @param config The `target`, `set` and `where` clauses.
32509
+ *
32510
+ * @example
32511
+ * ```ts
32512
+ * // Update the row if there's a conflict
32513
+ * await db.insert(cars)
32514
+ * .values({ id: 1, brand: 'BMW' })
32515
+ * .onConflictDoUpdate({
32516
+ * target: cars.id,
32517
+ * set: { brand: 'Porsche' }
32518
+ * });
32519
+ *
32520
+ * // Upsert with 'where' clause
32521
+ * await db.insert(cars)
32522
+ * .values({ id: 1, brand: 'BMW' })
32523
+ * .onConflictDoUpdate({
32524
+ * target: cars.id,
32525
+ * set: { brand: 'newBMW' },
32526
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32527
+ * });
32528
+ * ```
32529
+ */
32530
+ onConflictDoUpdate(config) {
32531
+ if (config.where && (config.targetWhere || config.setWhere)) {
32532
+ throw new Error(
32533
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32534
+ );
32535
+ }
32536
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32537
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32538
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32539
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32540
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32541
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32542
+ return this;
32543
+ }
32544
+ /** @internal */
32545
+ getSQL() {
32546
+ return this.dialect.buildInsertQuery(this.config);
32547
+ }
32548
+ toSQL() {
32549
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32550
+ return rest;
32551
+ }
32552
+ /** @internal */
32553
+ _prepare(isOneTimeQuery = true) {
32554
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32555
+ this.dialect.sqlToQuery(this.getSQL()),
32556
+ this.config.returning,
32557
+ this.config.returning ? "all" : "run",
32558
+ true
32559
+ );
32560
+ }
32561
+ prepare() {
32562
+ return this._prepare(false);
32563
+ }
32564
+ async execute() {
32565
+ return this.config.returning ? this.all() : this.run();
32566
+ }
32567
+ $dynamic() {
32568
+ return this;
32569
+ }
32570
+ };
32571
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32329
32572
  }
32330
32573
  });
32331
32574
 
@@ -32345,8 +32588,11 @@ var init_update2 = __esm({
32345
32588
  init_query_promise();
32346
32589
  init_selection_proxy();
32347
32590
  init_table3();
32591
+ init_subquery();
32348
32592
  init_table();
32349
32593
  init_utils2();
32594
+ init_view_common();
32595
+ init_view_base2();
32350
32596
  _a207 = entityKind;
32351
32597
  SQLiteUpdateBuilder = class {
32352
32598
  constructor(table4, session, dialect4, withList) {
@@ -32371,6 +32617,10 @@ var init_update2 = __esm({
32371
32617
  super();
32372
32618
  /** @internal */
32373
32619
  __publicField(this, "config");
32620
+ __publicField(this, "leftJoin", this.createJoin("left"));
32621
+ __publicField(this, "rightJoin", this.createJoin("right"));
32622
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32623
+ __publicField(this, "fullJoin", this.createJoin("full"));
32374
32624
  __publicField(this, "run", (placeholderValues) => {
32375
32625
  return this._prepare().run(placeholderValues);
32376
32626
  });
@@ -32385,7 +32635,34 @@ var init_update2 = __esm({
32385
32635
  });
32386
32636
  this.session = session;
32387
32637
  this.dialect = dialect4;
32388
- this.config = { set, table: table4, withList };
32638
+ this.config = { set, table: table4, withList, joins: [] };
32639
+ }
32640
+ from(source) {
32641
+ this.config.from = source;
32642
+ return this;
32643
+ }
32644
+ createJoin(joinType) {
32645
+ return (table4, on) => {
32646
+ const tableName = getTableLikeName(table4);
32647
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32648
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32649
+ }
32650
+ if (typeof on === "function") {
32651
+ 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;
32652
+ on = on(
32653
+ new Proxy(
32654
+ this.config.table[Table2.Symbol.Columns],
32655
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32656
+ ),
32657
+ from && new Proxy(
32658
+ from,
32659
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32660
+ )
32661
+ );
32662
+ }
32663
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32664
+ return this;
32665
+ };
32389
32666
  }
32390
32667
  /**
32391
32668
  * Adds a 'where' clause to the query.
@@ -35683,131 +35960,6 @@ var init_delete3 = __esm({
35683
35960
  }
35684
35961
  });
35685
35962
 
35686
- // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
35687
- var _a299, MySqlInsertBuilder, _a300, _b222, MySqlInsertBase;
35688
- var init_insert3 = __esm({
35689
- "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
35690
- "use strict";
35691
- init_entity();
35692
- init_query_promise();
35693
- init_sql();
35694
- init_table();
35695
- init_utils2();
35696
- _a299 = entityKind;
35697
- MySqlInsertBuilder = class {
35698
- constructor(table4, session, dialect4) {
35699
- __publicField(this, "shouldIgnore", false);
35700
- this.table = table4;
35701
- this.session = session;
35702
- this.dialect = dialect4;
35703
- }
35704
- ignore() {
35705
- this.shouldIgnore = true;
35706
- return this;
35707
- }
35708
- values(values) {
35709
- values = Array.isArray(values) ? values : [values];
35710
- if (values.length === 0) {
35711
- throw new Error("values() must be called with at least one value");
35712
- }
35713
- const mappedValues = values.map((entry) => {
35714
- const result = {};
35715
- const cols = this.table[Table2.Symbol.Columns];
35716
- for (const colKey of Object.keys(entry)) {
35717
- const colValue = entry[colKey];
35718
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
35719
- }
35720
- return result;
35721
- });
35722
- return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
35723
- }
35724
- };
35725
- __publicField(MySqlInsertBuilder, _a299, "MySqlInsertBuilder");
35726
- MySqlInsertBase = class extends (_b222 = QueryPromise, _a300 = entityKind, _b222) {
35727
- constructor(table4, values, ignore, session, dialect4) {
35728
- super();
35729
- __publicField(this, "config");
35730
- __publicField(this, "execute", (placeholderValues) => {
35731
- return this.prepare().execute(placeholderValues);
35732
- });
35733
- __publicField(this, "createIterator", () => {
35734
- const self2 = this;
35735
- return async function* (placeholderValues) {
35736
- yield* self2.prepare().iterator(placeholderValues);
35737
- };
35738
- });
35739
- __publicField(this, "iterator", this.createIterator());
35740
- this.session = session;
35741
- this.dialect = dialect4;
35742
- this.config = { table: table4, values, ignore };
35743
- }
35744
- /**
35745
- * Adds an `on duplicate key update` clause to the query.
35746
- *
35747
- * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35748
- *
35749
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35750
- *
35751
- * @param config The `set` clause
35752
- *
35753
- * @example
35754
- * ```ts
35755
- * await db.insert(cars)
35756
- * .values({ id: 1, brand: 'BMW'})
35757
- * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
35758
- * ```
35759
- *
35760
- * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
35761
- *
35762
- * ```ts
35763
- * import { sql } from 'drizzle-orm';
35764
- *
35765
- * await db.insert(cars)
35766
- * .values({ id: 1, brand: 'BMW' })
35767
- * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
35768
- * ```
35769
- */
35770
- onDuplicateKeyUpdate(config) {
35771
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
35772
- this.config.onConflict = sql`update ${setSql}`;
35773
- return this;
35774
- }
35775
- $returningId() {
35776
- const returning = [];
35777
- for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
35778
- if (value.primary) {
35779
- returning.push({ field: value, path: [key] });
35780
- }
35781
- }
35782
- this.config.returning = returning;
35783
- return this;
35784
- }
35785
- /** @internal */
35786
- getSQL() {
35787
- return this.dialect.buildInsertQuery(this.config).sql;
35788
- }
35789
- toSQL() {
35790
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
35791
- return rest;
35792
- }
35793
- prepare() {
35794
- const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
35795
- return this.session.prepareQuery(
35796
- this.dialect.sqlToQuery(sql2),
35797
- void 0,
35798
- void 0,
35799
- generatedIds,
35800
- this.config.returning
35801
- );
35802
- }
35803
- $dynamic() {
35804
- return this;
35805
- }
35806
- };
35807
- __publicField(MySqlInsertBase, _a300, "MySqlInsert");
35808
- }
35809
- });
35810
-
35811
35963
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35812
35964
  function getMySqlColumnBuilders() {
35813
35965
  return {
@@ -35888,7 +36040,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35888
36040
  }
35889
36041
  return table4;
35890
36042
  }
35891
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
36043
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35892
36044
  var init_table4 = __esm({
35893
36045
  "../drizzle-orm/dist/mysql-core/table.js"() {
35894
36046
  "use strict";
@@ -35896,15 +36048,15 @@ var init_table4 = __esm({
35896
36048
  init_table();
35897
36049
  init_all3();
35898
36050
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35899
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
36051
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35900
36052
  constructor() {
35901
36053
  super(...arguments);
35902
36054
  /** @internal */
35903
36055
  __publicField(this, _c9);
35904
36056
  /** @internal */
35905
- __publicField(this, _b223, []);
36057
+ __publicField(this, _b222, []);
35906
36058
  /** @internal */
35907
- __publicField(this, _a301);
36059
+ __publicField(this, _a299);
35908
36060
  }
35909
36061
  };
35910
36062
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35919,20 +36071,20 @@ var init_table4 = __esm({
35919
36071
  });
35920
36072
 
35921
36073
  // ../drizzle-orm/dist/mysql-core/view-base.js
35922
- var _a302, _b224, MySqlViewBase;
36074
+ var _a300, _b223, MySqlViewBase;
35923
36075
  var init_view_base3 = __esm({
35924
36076
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35925
36077
  "use strict";
35926
36078
  init_entity();
35927
36079
  init_sql();
35928
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
36080
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35929
36081
  };
35930
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
36082
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35931
36083
  }
35932
36084
  });
35933
36085
 
35934
36086
  // ../drizzle-orm/dist/mysql-core/dialect.js
35935
- var _a303, MySqlDialect;
36087
+ var _a301, MySqlDialect;
35936
36088
  var init_dialect3 = __esm({
35937
36089
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35938
36090
  "use strict";
@@ -35951,7 +36103,7 @@ var init_dialect3 = __esm({
35951
36103
  init_common4();
35952
36104
  init_table4();
35953
36105
  init_view_base3();
35954
- _a303 = entityKind;
36106
+ _a301 = entityKind;
35955
36107
  MySqlDialect = class {
35956
36108
  constructor(config) {
35957
36109
  /** @internal */
@@ -36234,7 +36386,7 @@ var init_dialect3 = __esm({
36234
36386
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36235
36387
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36236
36388
  }
36237
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36389
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36238
36390
  const valuesSqlList = [];
36239
36391
  const columns = table4[Table2.Symbol.Columns];
36240
36392
  const colEntries = Object.entries(columns).filter(
@@ -36242,42 +36394,53 @@ var init_dialect3 = __esm({
36242
36394
  );
36243
36395
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36244
36396
  const generatedIdsResponse = [];
36245
- for (const [valueIndex, value] of values.entries()) {
36246
- const generatedIds = {};
36247
- const valueList = [];
36248
- for (const [fieldName, col] of colEntries) {
36249
- const colValue = value[fieldName];
36250
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36251
- if (col.defaultFn !== void 0) {
36252
- const defaultFnResult = col.defaultFn();
36253
- generatedIds[fieldName] = defaultFnResult;
36254
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36255
- valueList.push(defaultValue);
36256
- } else if (!col.default && col.onUpdateFn !== void 0) {
36257
- const onUpdateFnResult = col.onUpdateFn();
36258
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36259
- valueList.push(newValue);
36397
+ if (select) {
36398
+ const select2 = valuesOrSelect;
36399
+ if (is(select2, SQL)) {
36400
+ valuesSqlList.push(select2);
36401
+ } else {
36402
+ valuesSqlList.push(select2.getSQL());
36403
+ }
36404
+ } else {
36405
+ const values = valuesOrSelect;
36406
+ valuesSqlList.push(sql.raw("values "));
36407
+ for (const [valueIndex, value] of values.entries()) {
36408
+ const generatedIds = {};
36409
+ const valueList = [];
36410
+ for (const [fieldName, col] of colEntries) {
36411
+ const colValue = value[fieldName];
36412
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36413
+ if (col.defaultFn !== void 0) {
36414
+ const defaultFnResult = col.defaultFn();
36415
+ generatedIds[fieldName] = defaultFnResult;
36416
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36417
+ valueList.push(defaultValue);
36418
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36419
+ const onUpdateFnResult = col.onUpdateFn();
36420
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36421
+ valueList.push(newValue);
36422
+ } else {
36423
+ valueList.push(sql`default`);
36424
+ }
36260
36425
  } else {
36261
- valueList.push(sql`default`);
36262
- }
36263
- } else {
36264
- if (col.defaultFn && is(colValue, Param)) {
36265
- generatedIds[fieldName] = colValue.value;
36426
+ if (col.defaultFn && is(colValue, Param)) {
36427
+ generatedIds[fieldName] = colValue.value;
36428
+ }
36429
+ valueList.push(colValue);
36266
36430
  }
36267
- valueList.push(colValue);
36268
36431
  }
36269
- }
36270
- generatedIdsResponse.push(generatedIds);
36271
- valuesSqlList.push(valueList);
36272
- if (valueIndex < values.length - 1) {
36273
- valuesSqlList.push(sql`, `);
36432
+ generatedIdsResponse.push(generatedIds);
36433
+ valuesSqlList.push(valueList);
36434
+ if (valueIndex < values.length - 1) {
36435
+ valuesSqlList.push(sql`, `);
36436
+ }
36274
36437
  }
36275
36438
  }
36276
36439
  const valuesSql = sql.join(valuesSqlList);
36277
36440
  const ignoreSql = ignore ? sql` ignore` : void 0;
36278
36441
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36279
36442
  return {
36280
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36443
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36281
36444
  generatedIds: generatedIdsResponse
36282
36445
  };
36283
36446
  }
@@ -36737,7 +36900,7 @@ var init_dialect3 = __esm({
36737
36900
  };
36738
36901
  }
36739
36902
  };
36740
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36903
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36741
36904
  }
36742
36905
  });
36743
36906
 
@@ -36759,7 +36922,7 @@ function createSetOperator3(type, isAll) {
36759
36922
  return leftSelect.addSetOperators(setOperators);
36760
36923
  };
36761
36924
  }
36762
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36925
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36763
36926
  var init_select4 = __esm({
36764
36927
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36765
36928
  "use strict";
@@ -36774,7 +36937,7 @@ var init_select4 = __esm({
36774
36937
  init_utils2();
36775
36938
  init_view_common();
36776
36939
  init_view_base3();
36777
- _a304 = entityKind;
36940
+ _a302 = entityKind;
36778
36941
  MySqlSelectBuilder = class {
36779
36942
  constructor(config) {
36780
36943
  __publicField(this, "fields");
@@ -36819,8 +36982,8 @@ var init_select4 = __esm({
36819
36982
  );
36820
36983
  }
36821
36984
  };
36822
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36823
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36985
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36986
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36824
36987
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36825
36988
  super();
36826
36989
  __publicField(this, "_");
@@ -37421,8 +37584,8 @@ var init_select4 = __esm({
37421
37584
  return this;
37422
37585
  }
37423
37586
  };
37424
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37425
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37587
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37588
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37426
37589
  constructor() {
37427
37590
  super(...arguments);
37428
37591
  __publicField(this, "execute", (placeholderValues) => {
@@ -37446,7 +37609,7 @@ var init_select4 = __esm({
37446
37609
  return query;
37447
37610
  }
37448
37611
  };
37449
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37612
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37450
37613
  applyMixins(MySqlSelectBase, [QueryPromise]);
37451
37614
  getMySqlSetOperators = () => ({
37452
37615
  union: union3,
@@ -37466,7 +37629,7 @@ var init_select4 = __esm({
37466
37629
  });
37467
37630
 
37468
37631
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37469
- var _a307, QueryBuilder3;
37632
+ var _a305, QueryBuilder3;
37470
37633
  var init_query_builder4 = __esm({
37471
37634
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37472
37635
  "use strict";
@@ -37475,7 +37638,7 @@ var init_query_builder4 = __esm({
37475
37638
  init_selection_proxy();
37476
37639
  init_subquery();
37477
37640
  init_select4();
37478
- _a307 = entityKind;
37641
+ _a305 = entityKind;
37479
37642
  QueryBuilder3 = class {
37480
37643
  constructor(dialect4) {
37481
37644
  __publicField(this, "dialect");
@@ -37537,7 +37700,142 @@ var init_query_builder4 = __esm({
37537
37700
  return this.dialect;
37538
37701
  }
37539
37702
  };
37540
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37703
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37704
+ }
37705
+ });
37706
+
37707
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37708
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37709
+ var init_insert3 = __esm({
37710
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37711
+ "use strict";
37712
+ init_entity();
37713
+ init_query_promise();
37714
+ init_sql();
37715
+ init_table();
37716
+ init_utils2();
37717
+ init_query_builder4();
37718
+ _a306 = entityKind;
37719
+ MySqlInsertBuilder = class {
37720
+ constructor(table4, session, dialect4) {
37721
+ __publicField(this, "shouldIgnore", false);
37722
+ this.table = table4;
37723
+ this.session = session;
37724
+ this.dialect = dialect4;
37725
+ }
37726
+ ignore() {
37727
+ this.shouldIgnore = true;
37728
+ return this;
37729
+ }
37730
+ values(values) {
37731
+ values = Array.isArray(values) ? values : [values];
37732
+ if (values.length === 0) {
37733
+ throw new Error("values() must be called with at least one value");
37734
+ }
37735
+ const mappedValues = values.map((entry) => {
37736
+ const result = {};
37737
+ const cols = this.table[Table2.Symbol.Columns];
37738
+ for (const colKey of Object.keys(entry)) {
37739
+ const colValue = entry[colKey];
37740
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37741
+ }
37742
+ return result;
37743
+ });
37744
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37745
+ }
37746
+ select(selectQuery) {
37747
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37748
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37749
+ throw new Error(
37750
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37751
+ );
37752
+ }
37753
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37754
+ }
37755
+ };
37756
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37757
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37758
+ constructor(table4, values, ignore, session, dialect4, select) {
37759
+ super();
37760
+ __publicField(this, "config");
37761
+ __publicField(this, "execute", (placeholderValues) => {
37762
+ return this.prepare().execute(placeholderValues);
37763
+ });
37764
+ __publicField(this, "createIterator", () => {
37765
+ const self2 = this;
37766
+ return async function* (placeholderValues) {
37767
+ yield* self2.prepare().iterator(placeholderValues);
37768
+ };
37769
+ });
37770
+ __publicField(this, "iterator", this.createIterator());
37771
+ this.session = session;
37772
+ this.dialect = dialect4;
37773
+ this.config = { table: table4, values, select, ignore };
37774
+ }
37775
+ /**
37776
+ * Adds an `on duplicate key update` clause to the query.
37777
+ *
37778
+ * 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.
37779
+ *
37780
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37781
+ *
37782
+ * @param config The `set` clause
37783
+ *
37784
+ * @example
37785
+ * ```ts
37786
+ * await db.insert(cars)
37787
+ * .values({ id: 1, brand: 'BMW'})
37788
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37789
+ * ```
37790
+ *
37791
+ * 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:
37792
+ *
37793
+ * ```ts
37794
+ * import { sql } from 'drizzle-orm';
37795
+ *
37796
+ * await db.insert(cars)
37797
+ * .values({ id: 1, brand: 'BMW' })
37798
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37799
+ * ```
37800
+ */
37801
+ onDuplicateKeyUpdate(config) {
37802
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37803
+ this.config.onConflict = sql`update ${setSql}`;
37804
+ return this;
37805
+ }
37806
+ $returningId() {
37807
+ const returning = [];
37808
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37809
+ if (value.primary) {
37810
+ returning.push({ field: value, path: [key] });
37811
+ }
37812
+ }
37813
+ this.config.returning = returning;
37814
+ return this;
37815
+ }
37816
+ /** @internal */
37817
+ getSQL() {
37818
+ return this.dialect.buildInsertQuery(this.config).sql;
37819
+ }
37820
+ toSQL() {
37821
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37822
+ return rest;
37823
+ }
37824
+ prepare() {
37825
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37826
+ return this.session.prepareQuery(
37827
+ this.dialect.sqlToQuery(sql2),
37828
+ void 0,
37829
+ void 0,
37830
+ generatedIds,
37831
+ this.config.returning
37832
+ );
37833
+ }
37834
+ $dynamic() {
37835
+ return this;
37836
+ }
37837
+ };
37838
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37541
37839
  }
37542
37840
  });
37543
37841