drizzle-kit 0.28.0-cc4f208 → 0.28.1-08d2486

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/api.js +1011 -714
  2. package/api.mjs +1011 -714
  3. package/bin.cjs +673 -78
  4. package/package.json +1 -1
package/api.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.4";
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,55 @@ 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, overridingSystemValue_ }) {
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
+ const overridingSql = overridingSystemValue_ === true ? sql`overriding system value ` : void 0;
25119
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${overridingSql}${valuesSql}${onConflictSql}${returningSql}`;
25246
25120
  }
25247
25121
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25248
25122
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25978,12 +25852,12 @@ var init_dialect = __esm({
25978
25852
  };
25979
25853
  }
25980
25854
  };
25981
- __publicField(PgDialect, _a129, "PgDialect");
25855
+ __publicField(PgDialect, _a127, "PgDialect");
25982
25856
  }
25983
25857
  });
25984
25858
 
25985
25859
  // ../drizzle-orm/dist/selection-proxy.js
25986
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25860
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25987
25861
  var init_selection_proxy = __esm({
25988
25862
  "../drizzle-orm/dist/selection-proxy.js"() {
25989
25863
  "use strict";
@@ -25993,7 +25867,7 @@ var init_selection_proxy = __esm({
25993
25867
  init_sql();
25994
25868
  init_subquery();
25995
25869
  init_view_common();
25996
- _a130 = entityKind;
25870
+ _a128 = entityKind;
25997
25871
  _SelectionProxyHandler = class _SelectionProxyHandler {
25998
25872
  constructor(config) {
25999
25873
  __publicField(this, "config");
@@ -26059,25 +25933,25 @@ var init_selection_proxy = __esm({
26059
25933
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26060
25934
  }
26061
25935
  };
26062
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25936
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26063
25937
  SelectionProxyHandler = _SelectionProxyHandler;
26064
25938
  }
26065
25939
  });
26066
25940
 
26067
25941
  // ../drizzle-orm/dist/query-builders/query-builder.js
26068
- var _a131, TypedQueryBuilder;
25942
+ var _a129, TypedQueryBuilder;
26069
25943
  var init_query_builder = __esm({
26070
25944
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26071
25945
  "use strict";
26072
25946
  init_entity();
26073
- _a131 = entityKind;
25947
+ _a129 = entityKind;
26074
25948
  TypedQueryBuilder = class {
26075
25949
  /** @internal */
26076
25950
  getSelectedFields() {
26077
25951
  return this._.selectedFields;
26078
25952
  }
26079
25953
  };
26080
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25954
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26081
25955
  }
26082
25956
  });
26083
25957
 
@@ -26099,7 +25973,7 @@ function createSetOperator(type, isAll) {
26099
25973
  return leftSelect.addSetOperators(setOperators);
26100
25974
  };
26101
25975
  }
26102
- var _a132, PgSelectBuilder, _a133, _b100, PgSelectQueryBuilderBase, _a134, _b101, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
25976
+ var _a130, PgSelectBuilder, _a131, _b99, PgSelectQueryBuilderBase, _a132, _b100, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
26103
25977
  var init_select2 = __esm({
26104
25978
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26105
25979
  "use strict";
@@ -26115,7 +25989,7 @@ var init_select2 = __esm({
26115
25989
  init_utils2();
26116
25990
  init_utils2();
26117
25991
  init_view_common();
26118
- _a132 = entityKind;
25992
+ _a130 = entityKind;
26119
25993
  PgSelectBuilder = class {
26120
25994
  constructor(config) {
26121
25995
  __publicField(this, "fields");
@@ -26123,6 +25997,7 @@ var init_select2 = __esm({
26123
25997
  __publicField(this, "dialect");
26124
25998
  __publicField(this, "withList", []);
26125
25999
  __publicField(this, "distinct");
26000
+ __publicField(this, "authToken");
26126
26001
  this.fields = config.fields;
26127
26002
  this.session = config.session;
26128
26003
  this.dialect = config.dialect;
@@ -26131,6 +26006,11 @@ var init_select2 = __esm({
26131
26006
  }
26132
26007
  this.distinct = config.distinct;
26133
26008
  }
26009
+ /** @internal */
26010
+ setToken(token) {
26011
+ this.authToken = token;
26012
+ return this;
26013
+ }
26134
26014
  /**
26135
26015
  * Specify the table, subquery, or other target that you're
26136
26016
  * building a select query against.
@@ -26161,11 +26041,11 @@ var init_select2 = __esm({
26161
26041
  dialect: this.dialect,
26162
26042
  withList: this.withList,
26163
26043
  distinct: this.distinct
26164
- });
26044
+ }).setToken(this.authToken);
26165
26045
  }
26166
26046
  };
26167
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26168
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26047
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26048
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26169
26049
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26170
26050
  super();
26171
26051
  __publicField(this, "_");
@@ -26765,19 +26645,20 @@ var init_select2 = __esm({
26765
26645
  return this;
26766
26646
  }
26767
26647
  };
26768
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26769
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26648
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26649
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26770
26650
  constructor() {
26771
26651
  super(...arguments);
26652
+ __publicField(this, "authToken");
26772
26653
  __publicField(this, "execute", (placeholderValues) => {
26773
26654
  return tracer.startActiveSpan("drizzle.operation", () => {
26774
- return this._prepare().execute(placeholderValues);
26655
+ return this._prepare().execute(placeholderValues, this.authToken);
26775
26656
  });
26776
26657
  });
26777
26658
  }
26778
26659
  /** @internal */
26779
26660
  _prepare(name2) {
26780
- const { session, config, dialect: dialect4, joinsNotNullableMap } = this;
26661
+ const { session, config, dialect: dialect4, joinsNotNullableMap, authToken } = this;
26781
26662
  if (!session) {
26782
26663
  throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
26783
26664
  }
@@ -26785,7 +26666,7 @@ var init_select2 = __esm({
26785
26666
  const fieldsList = orderSelectedFields(config.fields);
26786
26667
  const query = session.prepareQuery(dialect4.sqlToQuery(this.getSQL()), fieldsList, name2, true);
26787
26668
  query.joinsNotNullableMap = joinsNotNullableMap;
26788
- return query;
26669
+ return query.setToken(authToken);
26789
26670
  });
26790
26671
  }
26791
26672
  /**
@@ -26798,8 +26679,13 @@ var init_select2 = __esm({
26798
26679
  prepare(name2) {
26799
26680
  return this._prepare(name2);
26800
26681
  }
26682
+ /** @internal */
26683
+ setToken(token) {
26684
+ this.authToken = token;
26685
+ return this;
26686
+ }
26801
26687
  };
26802
- __publicField(PgSelectBase, _a134, "PgSelect");
26688
+ __publicField(PgSelectBase, _a132, "PgSelect");
26803
26689
  applyMixins(PgSelectBase, [QueryPromise]);
26804
26690
  getPgSetOperators = () => ({
26805
26691
  union,
@@ -26819,7 +26705,7 @@ var init_select2 = __esm({
26819
26705
  });
26820
26706
 
26821
26707
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26822
- var _a135, QueryBuilder;
26708
+ var _a133, QueryBuilder;
26823
26709
  var init_query_builder2 = __esm({
26824
26710
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26825
26711
  "use strict";
@@ -26828,7 +26714,7 @@ var init_query_builder2 = __esm({
26828
26714
  init_selection_proxy();
26829
26715
  init_subquery();
26830
26716
  init_select2();
26831
- _a135 = entityKind;
26717
+ _a133 = entityKind;
26832
26718
  QueryBuilder = class {
26833
26719
  constructor(dialect4) {
26834
26720
  __publicField(this, "dialect");
@@ -26909,40 +26795,232 @@ var init_query_builder2 = __esm({
26909
26795
  return this.dialect;
26910
26796
  }
26911
26797
  };
26912
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26798
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26913
26799
  }
26914
26800
  });
26915
26801
 
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"() {
26802
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26803
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26804
+ var init_insert = __esm({
26805
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26920
26806
  "use strict";
26921
26807
  init_entity();
26922
26808
  init_query_promise();
26809
+ init_sql();
26810
+ init_table();
26923
26811
  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
- });
26812
+ init_utils2();
26813
+ init_query_builder2();
26814
+ _a134 = entityKind;
26815
+ PgInsertBuilder = class {
26816
+ constructor(table4, session, dialect4, withList, overridingSystemValue_) {
26817
+ __publicField(this, "authToken");
26818
+ this.table = table4;
26933
26819
  this.session = session;
26934
26820
  this.dialect = dialect4;
26935
- this.config = { view: view4 };
26821
+ this.withList = withList;
26822
+ this.overridingSystemValue_ = overridingSystemValue_;
26936
26823
  }
26937
- concurrently() {
26938
- if (this.config.withNoData !== void 0) {
26939
- throw new Error("Cannot use concurrently and withNoData together");
26940
- }
26941
- this.config.concurrently = true;
26824
+ /** @internal */
26825
+ setToken(token) {
26826
+ this.authToken = token;
26942
26827
  return this;
26943
26828
  }
26944
- withNoData() {
26945
- if (this.config.concurrently !== void 0) {
26829
+ overridingSystemValue() {
26830
+ this.overridingSystemValue_ = true;
26831
+ return this;
26832
+ }
26833
+ values(values) {
26834
+ values = Array.isArray(values) ? values : [values];
26835
+ if (values.length === 0) {
26836
+ throw new Error("values() must be called with at least one value");
26837
+ }
26838
+ const mappedValues = values.map((entry) => {
26839
+ const result = {};
26840
+ const cols = this.table[Table2.Symbol.Columns];
26841
+ for (const colKey of Object.keys(entry)) {
26842
+ const colValue = entry[colKey];
26843
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26844
+ }
26845
+ return result;
26846
+ });
26847
+ return new PgInsertBase(
26848
+ this.table,
26849
+ mappedValues,
26850
+ this.session,
26851
+ this.dialect,
26852
+ this.withList,
26853
+ false,
26854
+ this.overridingSystemValue_
26855
+ ).setToken(this.authToken);
26856
+ }
26857
+ select(selectQuery) {
26858
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26859
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26860
+ throw new Error(
26861
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26862
+ );
26863
+ }
26864
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26865
+ }
26866
+ };
26867
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26868
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26869
+ constructor(table4, values, session, dialect4, withList, select, overridingSystemValue_) {
26870
+ super();
26871
+ __publicField(this, "config");
26872
+ __publicField(this, "authToken");
26873
+ __publicField(this, "execute", (placeholderValues) => {
26874
+ return tracer.startActiveSpan("drizzle.operation", () => {
26875
+ return this._prepare().execute(placeholderValues, this.authToken);
26876
+ });
26877
+ });
26878
+ this.session = session;
26879
+ this.dialect = dialect4;
26880
+ this.config = { table: table4, values, withList, select, overridingSystemValue_ };
26881
+ }
26882
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26883
+ this.config.returning = orderSelectedFields(fields);
26884
+ return this;
26885
+ }
26886
+ /**
26887
+ * Adds an `on conflict do nothing` clause to the query.
26888
+ *
26889
+ * Calling this method simply avoids inserting a row as its alternative action.
26890
+ *
26891
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26892
+ *
26893
+ * @param config The `target` and `where` clauses.
26894
+ *
26895
+ * @example
26896
+ * ```ts
26897
+ * // Insert one row and cancel the insert if there's a conflict
26898
+ * await db.insert(cars)
26899
+ * .values({ id: 1, brand: 'BMW' })
26900
+ * .onConflictDoNothing();
26901
+ *
26902
+ * // Explicitly specify conflict target
26903
+ * await db.insert(cars)
26904
+ * .values({ id: 1, brand: 'BMW' })
26905
+ * .onConflictDoNothing({ target: cars.id });
26906
+ * ```
26907
+ */
26908
+ onConflictDoNothing(config = {}) {
26909
+ if (config.target === void 0) {
26910
+ this.config.onConflict = sql`do nothing`;
26911
+ } else {
26912
+ let targetColumn = "";
26913
+ 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));
26914
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26915
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26916
+ }
26917
+ return this;
26918
+ }
26919
+ /**
26920
+ * Adds an `on conflict do update` clause to the query.
26921
+ *
26922
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26923
+ *
26924
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26925
+ *
26926
+ * @param config The `target`, `set` and `where` clauses.
26927
+ *
26928
+ * @example
26929
+ * ```ts
26930
+ * // Update the row if there's a conflict
26931
+ * await db.insert(cars)
26932
+ * .values({ id: 1, brand: 'BMW' })
26933
+ * .onConflictDoUpdate({
26934
+ * target: cars.id,
26935
+ * set: { brand: 'Porsche' }
26936
+ * });
26937
+ *
26938
+ * // Upsert with 'where' clause
26939
+ * await db.insert(cars)
26940
+ * .values({ id: 1, brand: 'BMW' })
26941
+ * .onConflictDoUpdate({
26942
+ * target: cars.id,
26943
+ * set: { brand: 'newBMW' },
26944
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26945
+ * });
26946
+ * ```
26947
+ */
26948
+ onConflictDoUpdate(config) {
26949
+ if (config.where && (config.targetWhere || config.setWhere)) {
26950
+ throw new Error(
26951
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26952
+ );
26953
+ }
26954
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26955
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26956
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26957
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26958
+ let targetColumn = "";
26959
+ 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));
26960
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26961
+ return this;
26962
+ }
26963
+ /** @internal */
26964
+ getSQL() {
26965
+ return this.dialect.buildInsertQuery(this.config);
26966
+ }
26967
+ toSQL() {
26968
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26969
+ return rest;
26970
+ }
26971
+ /** @internal */
26972
+ _prepare(name2) {
26973
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26974
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26975
+ });
26976
+ }
26977
+ prepare(name2) {
26978
+ return this._prepare(name2);
26979
+ }
26980
+ /** @internal */
26981
+ setToken(token) {
26982
+ this.authToken = token;
26983
+ return this;
26984
+ }
26985
+ $dynamic() {
26986
+ return this;
26987
+ }
26988
+ };
26989
+ __publicField(PgInsertBase, _a135, "PgInsert");
26990
+ }
26991
+ });
26992
+
26993
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26994
+ var _a136, _b102, PgRefreshMaterializedView;
26995
+ var init_refresh_materialized_view = __esm({
26996
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26997
+ "use strict";
26998
+ init_entity();
26999
+ init_query_promise();
27000
+ init_tracing();
27001
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
27002
+ constructor(view4, session, dialect4) {
27003
+ super();
27004
+ __publicField(this, "config");
27005
+ __publicField(this, "authToken");
27006
+ __publicField(this, "execute", (placeholderValues) => {
27007
+ return tracer.startActiveSpan("drizzle.operation", () => {
27008
+ return this._prepare().execute(placeholderValues, this.authToken);
27009
+ });
27010
+ });
27011
+ this.session = session;
27012
+ this.dialect = dialect4;
27013
+ this.config = { view: view4 };
27014
+ }
27015
+ concurrently() {
27016
+ if (this.config.withNoData !== void 0) {
27017
+ throw new Error("Cannot use concurrently and withNoData together");
27018
+ }
27019
+ this.config.concurrently = true;
27020
+ return this;
27021
+ }
27022
+ withNoData() {
27023
+ if (this.config.concurrently !== void 0) {
26946
27024
  throw new Error("Cannot use concurrently and withNoData together");
26947
27025
  }
26948
27026
  this.config.withNoData = true;
@@ -26965,6 +27043,11 @@ var init_refresh_materialized_view = __esm({
26965
27043
  prepare(name2) {
26966
27044
  return this._prepare(name2);
26967
27045
  }
27046
+ /** @internal */
27047
+ setToken(token) {
27048
+ this.authToken = token;
27049
+ return this;
27050
+ }
26968
27051
  };
26969
27052
  __publicField(PgRefreshMaterializedView, _a136, "PgRefreshMaterializedView");
26970
27053
  }
@@ -26983,17 +27066,27 @@ var init_update = __esm({
26983
27066
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26984
27067
  "use strict";
26985
27068
  init_entity();
27069
+ init_table2();
26986
27070
  init_query_promise();
27071
+ init_selection_proxy();
27072
+ init_sql();
27073
+ init_subquery();
26987
27074
  init_table();
26988
27075
  init_utils2();
27076
+ init_view_common();
26989
27077
  _a137 = entityKind;
26990
27078
  PgUpdateBuilder = class {
26991
27079
  constructor(table4, session, dialect4, withList) {
27080
+ __publicField(this, "authToken");
26992
27081
  this.table = table4;
26993
27082
  this.session = session;
26994
27083
  this.dialect = dialect4;
26995
27084
  this.withList = withList;
26996
27085
  }
27086
+ setToken(token) {
27087
+ this.authToken = token;
27088
+ return this;
27089
+ }
26997
27090
  set(values) {
26998
27091
  return new PgUpdateBase(
26999
27092
  this.table,
@@ -27001,7 +27094,7 @@ var init_update = __esm({
27001
27094
  this.session,
27002
27095
  this.dialect,
27003
27096
  this.withList
27004
- );
27097
+ ).setToken(this.authToken);
27005
27098
  }
27006
27099
  };
27007
27100
  __publicField(PgUpdateBuilder, _a137, "PgUpdateBuilder");
@@ -27009,12 +27102,86 @@ var init_update = __esm({
27009
27102
  constructor(table4, set, session, dialect4, withList) {
27010
27103
  super();
27011
27104
  __publicField(this, "config");
27105
+ __publicField(this, "tableName");
27106
+ __publicField(this, "joinsNotNullableMap");
27107
+ __publicField(this, "leftJoin", this.createJoin("left"));
27108
+ __publicField(this, "rightJoin", this.createJoin("right"));
27109
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27110
+ __publicField(this, "fullJoin", this.createJoin("full"));
27111
+ __publicField(this, "authToken");
27012
27112
  __publicField(this, "execute", (placeholderValues) => {
27013
- return this._prepare().execute(placeholderValues);
27113
+ return this._prepare().execute(placeholderValues, this.authToken);
27014
27114
  });
27015
27115
  this.session = session;
27016
27116
  this.dialect = dialect4;
27017
- this.config = { set, table: table4, withList };
27117
+ this.config = { set, table: table4, withList, joins: [] };
27118
+ this.tableName = getTableLikeName(table4);
27119
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27120
+ }
27121
+ from(source) {
27122
+ const tableName = getTableLikeName(source);
27123
+ if (typeof tableName === "string") {
27124
+ this.joinsNotNullableMap[tableName] = true;
27125
+ }
27126
+ this.config.from = source;
27127
+ return this;
27128
+ }
27129
+ getTableLikeFields(table4) {
27130
+ if (is(table4, PgTable)) {
27131
+ return table4[Table2.Symbol.Columns];
27132
+ } else if (is(table4, Subquery)) {
27133
+ return table4._.selectedFields;
27134
+ }
27135
+ return table4[ViewBaseConfig].selectedFields;
27136
+ }
27137
+ createJoin(joinType) {
27138
+ return (table4, on) => {
27139
+ const tableName = getTableLikeName(table4);
27140
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27141
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27142
+ }
27143
+ if (typeof on === "function") {
27144
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27145
+ on = on(
27146
+ new Proxy(
27147
+ this.config.table[Table2.Symbol.Columns],
27148
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27149
+ ),
27150
+ from && new Proxy(
27151
+ from,
27152
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27153
+ )
27154
+ );
27155
+ }
27156
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27157
+ if (typeof tableName === "string") {
27158
+ switch (joinType) {
27159
+ case "left": {
27160
+ this.joinsNotNullableMap[tableName] = false;
27161
+ break;
27162
+ }
27163
+ case "right": {
27164
+ this.joinsNotNullableMap = Object.fromEntries(
27165
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27166
+ );
27167
+ this.joinsNotNullableMap[tableName] = true;
27168
+ break;
27169
+ }
27170
+ case "inner": {
27171
+ this.joinsNotNullableMap[tableName] = true;
27172
+ break;
27173
+ }
27174
+ case "full": {
27175
+ this.joinsNotNullableMap = Object.fromEntries(
27176
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27177
+ );
27178
+ this.joinsNotNullableMap[tableName] = false;
27179
+ break;
27180
+ }
27181
+ }
27182
+ }
27183
+ return this;
27184
+ };
27018
27185
  }
27019
27186
  /**
27020
27187
  * Adds a 'where' clause to the query.
@@ -27053,7 +27220,24 @@ var init_update = __esm({
27053
27220
  this.config.where = where;
27054
27221
  return this;
27055
27222
  }
27056
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27223
+ returning(fields) {
27224
+ if (!fields) {
27225
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27226
+ if (this.config.from) {
27227
+ const tableName = getTableLikeName(this.config.from);
27228
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27229
+ const fromFields = this.getTableLikeFields(this.config.from);
27230
+ fields[tableName] = fromFields;
27231
+ }
27232
+ for (const join of this.config.joins) {
27233
+ const tableName2 = getTableLikeName(join.table);
27234
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27235
+ const fromFields = this.getTableLikeFields(join.table);
27236
+ fields[tableName2] = fromFields;
27237
+ }
27238
+ }
27239
+ }
27240
+ }
27057
27241
  this.config.returning = orderSelectedFields(fields);
27058
27242
  return this;
27059
27243
  }
@@ -27067,11 +27251,18 @@ var init_update = __esm({
27067
27251
  }
27068
27252
  /** @internal */
27069
27253
  _prepare(name2) {
27070
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27254
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27255
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27256
+ return query;
27071
27257
  }
27072
27258
  prepare(name2) {
27073
27259
  return this._prepare(name2);
27074
27260
  }
27261
+ /** @internal */
27262
+ setToken(token) {
27263
+ this.authToken = token;
27264
+ return this;
27265
+ }
27075
27266
  $dynamic() {
27076
27267
  return this;
27077
27268
  }
@@ -27105,6 +27296,7 @@ var init_count = __esm({
27105
27296
  constructor(params) {
27106
27297
  super(_PgCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
27107
27298
  __publicField(this, "sql");
27299
+ __publicField(this, "token");
27108
27300
  __publicField(this, _a139, "PgCountBuilder");
27109
27301
  __publicField(this, "session");
27110
27302
  this.params = params;
@@ -27121,8 +27313,13 @@ var init_count = __esm({
27121
27313
  static buildCount(source, filters) {
27122
27314
  return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters};`;
27123
27315
  }
27316
+ /** @intrnal */
27317
+ setToken(token) {
27318
+ this.token = token;
27319
+ return this;
27320
+ }
27124
27321
  then(onfulfilled, onrejected) {
27125
- return Promise.resolve(this.session.count(this.sql)).then(
27322
+ return Promise.resolve(this.session.count(this.sql, this.token)).then(
27126
27323
  onfulfilled,
27127
27324
  onrejected
27128
27325
  );
@@ -27199,6 +27396,7 @@ var init_query = __esm({
27199
27396
  PgRelationalQuery = class extends (_b105 = QueryPromise, _a141 = entityKind, _b105) {
27200
27397
  constructor(fullSchema, schema4, tableNamesMap, table4, tableConfig, dialect4, session, config, mode) {
27201
27398
  super();
27399
+ __publicField(this, "authToken");
27202
27400
  this.fullSchema = fullSchema;
27203
27401
  this.schema = schema4;
27204
27402
  this.tableNamesMap = tableNamesMap;
@@ -27256,9 +27454,14 @@ var init_query = __esm({
27256
27454
  toSQL() {
27257
27455
  return this._toSQL().builtQuery;
27258
27456
  }
27457
+ /** @internal */
27458
+ setToken(token) {
27459
+ this.authToken = token;
27460
+ return this;
27461
+ }
27259
27462
  execute() {
27260
27463
  return tracer.startActiveSpan("drizzle.operation", () => {
27261
- return this._prepare().execute();
27464
+ return this._prepare().execute(void 0, this.authToken);
27262
27465
  });
27263
27466
  }
27264
27467
  };
@@ -27321,6 +27524,7 @@ var init_db = __esm({
27321
27524
  PgDatabase = class {
27322
27525
  constructor(dialect4, session, schema4) {
27323
27526
  __publicField(this, "query");
27527
+ __publicField(this, "authToken");
27324
27528
  this.dialect = dialect4;
27325
27529
  this.session = session;
27326
27530
  this._ = schema4 ? {
@@ -27576,7 +27780,7 @@ var init_db = __esm({
27576
27780
  false
27577
27781
  );
27578
27782
  return new PgRaw(
27579
- () => prepared.execute(),
27783
+ () => prepared.execute(void 0, this.authToken),
27580
27784
  sequel,
27581
27785
  builtQuery,
27582
27786
  (result) => prepared.mapResult(result, true)
@@ -28121,6 +28325,7 @@ var init_session = __esm({
28121
28325
  _a159 = entityKind;
28122
28326
  PgPreparedQuery = class {
28123
28327
  constructor(query) {
28328
+ __publicField(this, "authToken");
28124
28329
  /** @internal */
28125
28330
  __publicField(this, "joinsNotNullableMap");
28126
28331
  this.query = query;
@@ -28131,6 +28336,11 @@ var init_session = __esm({
28131
28336
  mapResult(response, _isFromBatch) {
28132
28337
  return response;
28133
28338
  }
28339
+ /** @internal */
28340
+ setToken(token) {
28341
+ this.authToken = token;
28342
+ return this;
28343
+ }
28134
28344
  };
28135
28345
  __publicField(PgPreparedQuery, _a159, "PgPreparedQuery");
28136
28346
  _a160 = entityKind;
@@ -28138,7 +28348,8 @@ var init_session = __esm({
28138
28348
  constructor(dialect4) {
28139
28349
  this.dialect = dialect4;
28140
28350
  }
28141
- execute(query) {
28351
+ /** @internal */
28352
+ execute(query, token) {
28142
28353
  return tracer.startActiveSpan("drizzle.operation", () => {
28143
28354
  const prepared = tracer.startActiveSpan("drizzle.prepareQuery", () => {
28144
28355
  return this.prepareQuery(
@@ -28148,7 +28359,7 @@ var init_session = __esm({
28148
28359
  false
28149
28360
  );
28150
28361
  });
28151
- return prepared.execute();
28362
+ return prepared.setToken(token).execute(void 0, token);
28152
28363
  });
28153
28364
  }
28154
28365
  all(query) {
@@ -28159,8 +28370,9 @@ var init_session = __esm({
28159
28370
  false
28160
28371
  ).all();
28161
28372
  }
28162
- async count(sql2) {
28163
- const res = await this.execute(sql2);
28373
+ /** @internal */
28374
+ async count(sql2, token) {
28375
+ const res = await this.execute(sql2, token);
28164
28376
  return Number(
28165
28377
  res[0]["count"]
28166
28378
  );
@@ -30817,200 +31029,32 @@ var init_delete2 = __esm({
30817
31029
  prepare() {
30818
31030
  return this._prepare(false);
30819
31031
  }
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
- }
31032
+ async execute(placeholderValues) {
31033
+ return this._prepare().execute(placeholderValues);
31034
+ }
30991
31035
  $dynamic() {
30992
31036
  return this;
30993
31037
  }
30994
31038
  };
30995
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
31039
+ __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30996
31040
  }
30997
31041
  });
30998
31042
 
30999
31043
  // ../drizzle-orm/dist/sqlite-core/view-base.js
31000
- var _a199, _b143, SQLiteViewBase;
31044
+ var _a197, _b142, SQLiteViewBase;
31001
31045
  var init_view_base2 = __esm({
31002
31046
  "../drizzle-orm/dist/sqlite-core/view-base.js"() {
31003
31047
  "use strict";
31004
31048
  init_entity();
31005
31049
  init_sql();
31006
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31050
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
31007
31051
  };
31008
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
31052
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31009
31053
  }
31010
31054
  });
31011
31055
 
31012
31056
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31013
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
31057
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31014
31058
  var init_dialect2 = __esm({
31015
31059
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31016
31060
  "use strict";
@@ -31029,7 +31073,7 @@ var init_dialect2 = __esm({
31029
31073
  init_utils2();
31030
31074
  init_view_common();
31031
31075
  init_view_base2();
31032
- _a200 = entityKind;
31076
+ _a198 = entityKind;
31033
31077
  SQLiteDialect = class {
31034
31078
  constructor(config) {
31035
31079
  /** @internal */
@@ -31082,14 +31126,16 @@ var init_dialect2 = __esm({
31082
31126
  return [res];
31083
31127
  }));
31084
31128
  }
31085
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31129
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31086
31130
  const withSql = this.buildWithCTE(withList);
31087
31131
  const setSql = this.buildUpdateSet(table4, set);
31132
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31133
+ const joinsSql = this.buildJoins(joins);
31088
31134
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31089
31135
  const whereSql = where ? sql` where ${where}` : void 0;
31090
31136
  const orderBySql = this.buildOrderBy(orderBy);
31091
31137
  const limitSql = this.buildLimit(limit);
31092
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31138
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31093
31139
  }
31094
31140
  /**
31095
31141
  * Builds selection SQL with provided fields/expressions
@@ -31142,6 +31188,37 @@ var init_dialect2 = __esm({
31142
31188
  });
31143
31189
  return sql.join(chunks);
31144
31190
  }
31191
+ buildJoins(joins) {
31192
+ if (!joins || joins.length === 0) {
31193
+ return void 0;
31194
+ }
31195
+ const joinsArray = [];
31196
+ if (joins) {
31197
+ for (const [index4, joinMeta] of joins.entries()) {
31198
+ if (index4 === 0) {
31199
+ joinsArray.push(sql` `);
31200
+ }
31201
+ const table4 = joinMeta.table;
31202
+ if (is(table4, SQLiteTable)) {
31203
+ const tableName = table4[SQLiteTable.Symbol.Name];
31204
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31205
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31206
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31207
+ joinsArray.push(
31208
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31209
+ );
31210
+ } else {
31211
+ joinsArray.push(
31212
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31213
+ );
31214
+ }
31215
+ if (index4 < joins.length - 1) {
31216
+ joinsArray.push(sql` `);
31217
+ }
31218
+ }
31219
+ }
31220
+ return sql.join(joinsArray);
31221
+ }
31145
31222
  buildLimit(limit) {
31146
31223
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31147
31224
  }
@@ -31157,6 +31234,12 @@ var init_dialect2 = __esm({
31157
31234
  }
31158
31235
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31159
31236
  }
31237
+ buildFromTable(table4) {
31238
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31239
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31240
+ }
31241
+ return table4;
31242
+ }
31160
31243
  buildSelectQuery({
31161
31244
  withList,
31162
31245
  fields,
@@ -31187,38 +31270,8 @@ var init_dialect2 = __esm({
31187
31270
  const withSql = this.buildWithCTE(withList);
31188
31271
  const distinctSql = distinct ? sql` distinct` : void 0;
31189
31272
  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);
31273
+ const tableSql = this.buildFromTable(table4);
31274
+ const joinsSql = this.buildJoins(joins);
31222
31275
  const whereSql = where ? sql` where ${where}` : void 0;
31223
31276
  const havingSql = having ? sql` having ${having}` : void 0;
31224
31277
  const groupByList = [];
@@ -31284,45 +31337,56 @@ var init_dialect2 = __esm({
31284
31337
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31285
31338
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31286
31339
  }
31287
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31340
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31288
31341
  const valuesSqlList = [];
31289
31342
  const columns = table4[Table2.Symbol.Columns];
31290
31343
  const colEntries = Object.entries(columns).filter(
31291
31344
  ([_2, col]) => !col.shouldDisableInsert()
31292
31345
  );
31293
31346
  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);
31347
+ if (select) {
31348
+ const select2 = valuesOrSelect;
31349
+ if (is(select2, SQL)) {
31350
+ valuesSqlList.push(select2);
31351
+ } else {
31352
+ valuesSqlList.push(select2.getSQL());
31353
+ }
31354
+ } else {
31355
+ const values = valuesOrSelect;
31356
+ valuesSqlList.push(sql.raw("values "));
31357
+ for (const [valueIndex, value] of values.entries()) {
31358
+ const valueList = [];
31359
+ for (const [fieldName, col] of colEntries) {
31360
+ const colValue = value[fieldName];
31361
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31362
+ let defaultValue;
31363
+ if (col.default !== null && col.default !== void 0) {
31364
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31365
+ } else if (col.defaultFn !== void 0) {
31366
+ const defaultFnResult = col.defaultFn();
31367
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31368
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31369
+ const onUpdateFnResult = col.onUpdateFn();
31370
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31371
+ } else {
31372
+ defaultValue = sql`null`;
31373
+ }
31374
+ valueList.push(defaultValue);
31308
31375
  } else {
31309
- defaultValue = sql`null`;
31376
+ valueList.push(colValue);
31310
31377
  }
31311
- valueList.push(defaultValue);
31312
- } else {
31313
- valueList.push(colValue);
31314
31378
  }
31315
- }
31316
- valuesSqlList.push(valueList);
31317
- if (valueIndex < values.length - 1) {
31318
- valuesSqlList.push(sql`, `);
31379
+ valuesSqlList.push(valueList);
31380
+ if (valueIndex < values.length - 1) {
31381
+ valuesSqlList.push(sql`, `);
31382
+ }
31319
31383
  }
31320
31384
  }
31321
31385
  const withSql = this.buildWithCTE(withList);
31322
31386
  const valuesSql = sql.join(valuesSqlList);
31323
31387
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31324
31388
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31325
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31389
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31326
31390
  }
31327
31391
  sqlToQuery(sql2, invokeSource) {
31328
31392
  return sql2.toQuery({
@@ -31551,8 +31615,8 @@ var init_dialect2 = __esm({
31551
31615
  };
31552
31616
  }
31553
31617
  };
31554
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31555
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31618
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31619
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31556
31620
  migrate(migrations, session, config) {
31557
31621
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31558
31622
  const migrationTableCreate = sql`
@@ -31586,8 +31650,8 @@ var init_dialect2 = __esm({
31586
31650
  }
31587
31651
  }
31588
31652
  };
31589
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31590
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31653
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31654
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31591
31655
  async migrate(migrations, session, config) {
31592
31656
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31593
31657
  const migrationTableCreate = sql`
@@ -31616,7 +31680,7 @@ var init_dialect2 = __esm({
31616
31680
  });
31617
31681
  }
31618
31682
  };
31619
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31683
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31620
31684
  }
31621
31685
  });
31622
31686
 
@@ -31638,7 +31702,7 @@ function createSetOperator2(type, isAll) {
31638
31702
  return leftSelect.addSetOperators(setOperators);
31639
31703
  };
31640
31704
  }
31641
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31705
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31642
31706
  var init_select3 = __esm({
31643
31707
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31644
31708
  "use strict";
@@ -31652,7 +31716,7 @@ var init_select3 = __esm({
31652
31716
  init_utils2();
31653
31717
  init_view_common();
31654
31718
  init_view_base2();
31655
- _a203 = entityKind;
31719
+ _a201 = entityKind;
31656
31720
  SQLiteSelectBuilder = class {
31657
31721
  constructor(config) {
31658
31722
  __publicField(this, "fields");
@@ -31693,8 +31757,8 @@ var init_select3 = __esm({
31693
31757
  });
31694
31758
  }
31695
31759
  };
31696
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31697
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31760
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31761
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31698
31762
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31699
31763
  super();
31700
31764
  __publicField(this, "_");
@@ -32199,8 +32263,8 @@ var init_select3 = __esm({
32199
32263
  return this;
32200
32264
  }
32201
32265
  };
32202
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32203
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32266
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32267
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32204
32268
  constructor() {
32205
32269
  super(...arguments);
32206
32270
  __publicField(this, "run", (placeholderValues) => {
@@ -32238,7 +32302,7 @@ var init_select3 = __esm({
32238
32302
  return this.all();
32239
32303
  }
32240
32304
  };
32241
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32305
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32242
32306
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32243
32307
  getSQLiteSetOperators = () => ({
32244
32308
  union: union2,
@@ -32254,7 +32318,7 @@ var init_select3 = __esm({
32254
32318
  });
32255
32319
 
32256
32320
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32257
- var _a206, QueryBuilder2;
32321
+ var _a204, QueryBuilder2;
32258
32322
  var init_query_builder3 = __esm({
32259
32323
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32260
32324
  "use strict";
@@ -32263,7 +32327,7 @@ var init_query_builder3 = __esm({
32263
32327
  init_dialect2();
32264
32328
  init_subquery();
32265
32329
  init_select3();
32266
- _a206 = entityKind;
32330
+ _a204 = entityKind;
32267
32331
  QueryBuilder2 = class {
32268
32332
  constructor(dialect4) {
32269
32333
  __publicField(this, "dialect");
@@ -32325,7 +32389,185 @@ var init_query_builder3 = __esm({
32325
32389
  return this.dialect;
32326
32390
  }
32327
32391
  };
32328
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32392
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32393
+ }
32394
+ });
32395
+
32396
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32397
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32398
+ var init_insert2 = __esm({
32399
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32400
+ "use strict";
32401
+ init_entity();
32402
+ init_query_promise();
32403
+ init_sql();
32404
+ init_table3();
32405
+ init_table();
32406
+ init_utils2();
32407
+ init_query_builder3();
32408
+ _a205 = entityKind;
32409
+ SQLiteInsertBuilder = class {
32410
+ constructor(table4, session, dialect4, withList) {
32411
+ this.table = table4;
32412
+ this.session = session;
32413
+ this.dialect = dialect4;
32414
+ this.withList = withList;
32415
+ }
32416
+ values(values) {
32417
+ values = Array.isArray(values) ? values : [values];
32418
+ if (values.length === 0) {
32419
+ throw new Error("values() must be called with at least one value");
32420
+ }
32421
+ const mappedValues = values.map((entry) => {
32422
+ const result = {};
32423
+ const cols = this.table[Table2.Symbol.Columns];
32424
+ for (const colKey of Object.keys(entry)) {
32425
+ const colValue = entry[colKey];
32426
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32427
+ }
32428
+ return result;
32429
+ });
32430
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32431
+ }
32432
+ select(selectQuery) {
32433
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32434
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32435
+ throw new Error(
32436
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32437
+ );
32438
+ }
32439
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32440
+ }
32441
+ };
32442
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32443
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32444
+ constructor(table4, values, session, dialect4, withList, select) {
32445
+ super();
32446
+ /** @internal */
32447
+ __publicField(this, "config");
32448
+ __publicField(this, "run", (placeholderValues) => {
32449
+ return this._prepare().run(placeholderValues);
32450
+ });
32451
+ __publicField(this, "all", (placeholderValues) => {
32452
+ return this._prepare().all(placeholderValues);
32453
+ });
32454
+ __publicField(this, "get", (placeholderValues) => {
32455
+ return this._prepare().get(placeholderValues);
32456
+ });
32457
+ __publicField(this, "values", (placeholderValues) => {
32458
+ return this._prepare().values(placeholderValues);
32459
+ });
32460
+ this.session = session;
32461
+ this.dialect = dialect4;
32462
+ this.config = { table: table4, values, withList, select };
32463
+ }
32464
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32465
+ this.config.returning = orderSelectedFields(fields);
32466
+ return this;
32467
+ }
32468
+ /**
32469
+ * Adds an `on conflict do nothing` clause to the query.
32470
+ *
32471
+ * Calling this method simply avoids inserting a row as its alternative action.
32472
+ *
32473
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32474
+ *
32475
+ * @param config The `target` and `where` clauses.
32476
+ *
32477
+ * @example
32478
+ * ```ts
32479
+ * // Insert one row and cancel the insert if there's a conflict
32480
+ * await db.insert(cars)
32481
+ * .values({ id: 1, brand: 'BMW' })
32482
+ * .onConflictDoNothing();
32483
+ *
32484
+ * // Explicitly specify conflict target
32485
+ * await db.insert(cars)
32486
+ * .values({ id: 1, brand: 'BMW' })
32487
+ * .onConflictDoNothing({ target: cars.id });
32488
+ * ```
32489
+ */
32490
+ onConflictDoNothing(config = {}) {
32491
+ if (config.target === void 0) {
32492
+ this.config.onConflict = sql`do nothing`;
32493
+ } else {
32494
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32495
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32496
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32497
+ }
32498
+ return this;
32499
+ }
32500
+ /**
32501
+ * Adds an `on conflict do update` clause to the query.
32502
+ *
32503
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32504
+ *
32505
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32506
+ *
32507
+ * @param config The `target`, `set` and `where` clauses.
32508
+ *
32509
+ * @example
32510
+ * ```ts
32511
+ * // Update the row if there's a conflict
32512
+ * await db.insert(cars)
32513
+ * .values({ id: 1, brand: 'BMW' })
32514
+ * .onConflictDoUpdate({
32515
+ * target: cars.id,
32516
+ * set: { brand: 'Porsche' }
32517
+ * });
32518
+ *
32519
+ * // Upsert with 'where' clause
32520
+ * await db.insert(cars)
32521
+ * .values({ id: 1, brand: 'BMW' })
32522
+ * .onConflictDoUpdate({
32523
+ * target: cars.id,
32524
+ * set: { brand: 'newBMW' },
32525
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32526
+ * });
32527
+ * ```
32528
+ */
32529
+ onConflictDoUpdate(config) {
32530
+ if (config.where && (config.targetWhere || config.setWhere)) {
32531
+ throw new Error(
32532
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32533
+ );
32534
+ }
32535
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32536
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32537
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32538
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32539
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32540
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32541
+ return this;
32542
+ }
32543
+ /** @internal */
32544
+ getSQL() {
32545
+ return this.dialect.buildInsertQuery(this.config);
32546
+ }
32547
+ toSQL() {
32548
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32549
+ return rest;
32550
+ }
32551
+ /** @internal */
32552
+ _prepare(isOneTimeQuery = true) {
32553
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32554
+ this.dialect.sqlToQuery(this.getSQL()),
32555
+ this.config.returning,
32556
+ this.config.returning ? "all" : "run",
32557
+ true
32558
+ );
32559
+ }
32560
+ prepare() {
32561
+ return this._prepare(false);
32562
+ }
32563
+ async execute() {
32564
+ return this.config.returning ? this.all() : this.run();
32565
+ }
32566
+ $dynamic() {
32567
+ return this;
32568
+ }
32569
+ };
32570
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32329
32571
  }
32330
32572
  });
32331
32573
 
@@ -32345,8 +32587,11 @@ var init_update2 = __esm({
32345
32587
  init_query_promise();
32346
32588
  init_selection_proxy();
32347
32589
  init_table3();
32590
+ init_subquery();
32348
32591
  init_table();
32349
32592
  init_utils2();
32593
+ init_view_common();
32594
+ init_view_base2();
32350
32595
  _a207 = entityKind;
32351
32596
  SQLiteUpdateBuilder = class {
32352
32597
  constructor(table4, session, dialect4, withList) {
@@ -32371,6 +32616,10 @@ var init_update2 = __esm({
32371
32616
  super();
32372
32617
  /** @internal */
32373
32618
  __publicField(this, "config");
32619
+ __publicField(this, "leftJoin", this.createJoin("left"));
32620
+ __publicField(this, "rightJoin", this.createJoin("right"));
32621
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32622
+ __publicField(this, "fullJoin", this.createJoin("full"));
32374
32623
  __publicField(this, "run", (placeholderValues) => {
32375
32624
  return this._prepare().run(placeholderValues);
32376
32625
  });
@@ -32385,7 +32634,34 @@ var init_update2 = __esm({
32385
32634
  });
32386
32635
  this.session = session;
32387
32636
  this.dialect = dialect4;
32388
- this.config = { set, table: table4, withList };
32637
+ this.config = { set, table: table4, withList, joins: [] };
32638
+ }
32639
+ from(source) {
32640
+ this.config.from = source;
32641
+ return this;
32642
+ }
32643
+ createJoin(joinType) {
32644
+ return (table4, on) => {
32645
+ const tableName = getTableLikeName(table4);
32646
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32647
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32648
+ }
32649
+ if (typeof on === "function") {
32650
+ 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;
32651
+ on = on(
32652
+ new Proxy(
32653
+ this.config.table[Table2.Symbol.Columns],
32654
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32655
+ ),
32656
+ from && new Proxy(
32657
+ from,
32658
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32659
+ )
32660
+ );
32661
+ }
32662
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32663
+ return this;
32664
+ };
32389
32665
  }
32390
32666
  /**
32391
32667
  * Adds a 'where' clause to the query.
@@ -35683,131 +35959,6 @@ var init_delete3 = __esm({
35683
35959
  }
35684
35960
  });
35685
35961
 
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
35962
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35812
35963
  function getMySqlColumnBuilders() {
35813
35964
  return {
@@ -35888,7 +36039,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35888
36039
  }
35889
36040
  return table4;
35890
36041
  }
35891
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
36042
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35892
36043
  var init_table4 = __esm({
35893
36044
  "../drizzle-orm/dist/mysql-core/table.js"() {
35894
36045
  "use strict";
@@ -35896,15 +36047,15 @@ var init_table4 = __esm({
35896
36047
  init_table();
35897
36048
  init_all3();
35898
36049
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35899
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
36050
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35900
36051
  constructor() {
35901
36052
  super(...arguments);
35902
36053
  /** @internal */
35903
36054
  __publicField(this, _c9);
35904
36055
  /** @internal */
35905
- __publicField(this, _b223, []);
36056
+ __publicField(this, _b222, []);
35906
36057
  /** @internal */
35907
- __publicField(this, _a301);
36058
+ __publicField(this, _a299);
35908
36059
  }
35909
36060
  };
35910
36061
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35919,20 +36070,20 @@ var init_table4 = __esm({
35919
36070
  });
35920
36071
 
35921
36072
  // ../drizzle-orm/dist/mysql-core/view-base.js
35922
- var _a302, _b224, MySqlViewBase;
36073
+ var _a300, _b223, MySqlViewBase;
35923
36074
  var init_view_base3 = __esm({
35924
36075
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35925
36076
  "use strict";
35926
36077
  init_entity();
35927
36078
  init_sql();
35928
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
36079
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35929
36080
  };
35930
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
36081
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35931
36082
  }
35932
36083
  });
35933
36084
 
35934
36085
  // ../drizzle-orm/dist/mysql-core/dialect.js
35935
- var _a303, MySqlDialect;
36086
+ var _a301, MySqlDialect;
35936
36087
  var init_dialect3 = __esm({
35937
36088
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35938
36089
  "use strict";
@@ -35951,7 +36102,7 @@ var init_dialect3 = __esm({
35951
36102
  init_common4();
35952
36103
  init_table4();
35953
36104
  init_view_base3();
35954
- _a303 = entityKind;
36105
+ _a301 = entityKind;
35955
36106
  MySqlDialect = class {
35956
36107
  constructor(config) {
35957
36108
  /** @internal */
@@ -36234,7 +36385,7 @@ var init_dialect3 = __esm({
36234
36385
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36235
36386
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36236
36387
  }
36237
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36388
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36238
36389
  const valuesSqlList = [];
36239
36390
  const columns = table4[Table2.Symbol.Columns];
36240
36391
  const colEntries = Object.entries(columns).filter(
@@ -36242,42 +36393,53 @@ var init_dialect3 = __esm({
36242
36393
  );
36243
36394
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36244
36395
  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);
36396
+ if (select) {
36397
+ const select2 = valuesOrSelect;
36398
+ if (is(select2, SQL)) {
36399
+ valuesSqlList.push(select2);
36400
+ } else {
36401
+ valuesSqlList.push(select2.getSQL());
36402
+ }
36403
+ } else {
36404
+ const values = valuesOrSelect;
36405
+ valuesSqlList.push(sql.raw("values "));
36406
+ for (const [valueIndex, value] of values.entries()) {
36407
+ const generatedIds = {};
36408
+ const valueList = [];
36409
+ for (const [fieldName, col] of colEntries) {
36410
+ const colValue = value[fieldName];
36411
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36412
+ if (col.defaultFn !== void 0) {
36413
+ const defaultFnResult = col.defaultFn();
36414
+ generatedIds[fieldName] = defaultFnResult;
36415
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36416
+ valueList.push(defaultValue);
36417
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36418
+ const onUpdateFnResult = col.onUpdateFn();
36419
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36420
+ valueList.push(newValue);
36421
+ } else {
36422
+ valueList.push(sql`default`);
36423
+ }
36260
36424
  } else {
36261
- valueList.push(sql`default`);
36262
- }
36263
- } else {
36264
- if (col.defaultFn && is(colValue, Param)) {
36265
- generatedIds[fieldName] = colValue.value;
36425
+ if (col.defaultFn && is(colValue, Param)) {
36426
+ generatedIds[fieldName] = colValue.value;
36427
+ }
36428
+ valueList.push(colValue);
36266
36429
  }
36267
- valueList.push(colValue);
36268
36430
  }
36269
- }
36270
- generatedIdsResponse.push(generatedIds);
36271
- valuesSqlList.push(valueList);
36272
- if (valueIndex < values.length - 1) {
36273
- valuesSqlList.push(sql`, `);
36431
+ generatedIdsResponse.push(generatedIds);
36432
+ valuesSqlList.push(valueList);
36433
+ if (valueIndex < values.length - 1) {
36434
+ valuesSqlList.push(sql`, `);
36435
+ }
36274
36436
  }
36275
36437
  }
36276
36438
  const valuesSql = sql.join(valuesSqlList);
36277
36439
  const ignoreSql = ignore ? sql` ignore` : void 0;
36278
36440
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36279
36441
  return {
36280
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36442
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36281
36443
  generatedIds: generatedIdsResponse
36282
36444
  };
36283
36445
  }
@@ -36737,7 +36899,7 @@ var init_dialect3 = __esm({
36737
36899
  };
36738
36900
  }
36739
36901
  };
36740
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36902
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36741
36903
  }
36742
36904
  });
36743
36905
 
@@ -36759,7 +36921,7 @@ function createSetOperator3(type, isAll) {
36759
36921
  return leftSelect.addSetOperators(setOperators);
36760
36922
  };
36761
36923
  }
36762
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36924
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36763
36925
  var init_select4 = __esm({
36764
36926
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36765
36927
  "use strict";
@@ -36774,7 +36936,7 @@ var init_select4 = __esm({
36774
36936
  init_utils2();
36775
36937
  init_view_common();
36776
36938
  init_view_base3();
36777
- _a304 = entityKind;
36939
+ _a302 = entityKind;
36778
36940
  MySqlSelectBuilder = class {
36779
36941
  constructor(config) {
36780
36942
  __publicField(this, "fields");
@@ -36819,8 +36981,8 @@ var init_select4 = __esm({
36819
36981
  );
36820
36982
  }
36821
36983
  };
36822
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36823
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36984
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36985
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36824
36986
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36825
36987
  super();
36826
36988
  __publicField(this, "_");
@@ -37421,8 +37583,8 @@ var init_select4 = __esm({
37421
37583
  return this;
37422
37584
  }
37423
37585
  };
37424
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37425
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37586
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37587
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37426
37588
  constructor() {
37427
37589
  super(...arguments);
37428
37590
  __publicField(this, "execute", (placeholderValues) => {
@@ -37446,7 +37608,7 @@ var init_select4 = __esm({
37446
37608
  return query;
37447
37609
  }
37448
37610
  };
37449
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37611
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37450
37612
  applyMixins(MySqlSelectBase, [QueryPromise]);
37451
37613
  getMySqlSetOperators = () => ({
37452
37614
  union: union3,
@@ -37466,7 +37628,7 @@ var init_select4 = __esm({
37466
37628
  });
37467
37629
 
37468
37630
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37469
- var _a307, QueryBuilder3;
37631
+ var _a305, QueryBuilder3;
37470
37632
  var init_query_builder4 = __esm({
37471
37633
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37472
37634
  "use strict";
@@ -37475,7 +37637,7 @@ var init_query_builder4 = __esm({
37475
37637
  init_selection_proxy();
37476
37638
  init_subquery();
37477
37639
  init_select4();
37478
- _a307 = entityKind;
37640
+ _a305 = entityKind;
37479
37641
  QueryBuilder3 = class {
37480
37642
  constructor(dialect4) {
37481
37643
  __publicField(this, "dialect");
@@ -37537,7 +37699,142 @@ var init_query_builder4 = __esm({
37537
37699
  return this.dialect;
37538
37700
  }
37539
37701
  };
37540
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37702
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37703
+ }
37704
+ });
37705
+
37706
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37707
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37708
+ var init_insert3 = __esm({
37709
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37710
+ "use strict";
37711
+ init_entity();
37712
+ init_query_promise();
37713
+ init_sql();
37714
+ init_table();
37715
+ init_utils2();
37716
+ init_query_builder4();
37717
+ _a306 = entityKind;
37718
+ MySqlInsertBuilder = class {
37719
+ constructor(table4, session, dialect4) {
37720
+ __publicField(this, "shouldIgnore", false);
37721
+ this.table = table4;
37722
+ this.session = session;
37723
+ this.dialect = dialect4;
37724
+ }
37725
+ ignore() {
37726
+ this.shouldIgnore = true;
37727
+ return this;
37728
+ }
37729
+ values(values) {
37730
+ values = Array.isArray(values) ? values : [values];
37731
+ if (values.length === 0) {
37732
+ throw new Error("values() must be called with at least one value");
37733
+ }
37734
+ const mappedValues = values.map((entry) => {
37735
+ const result = {};
37736
+ const cols = this.table[Table2.Symbol.Columns];
37737
+ for (const colKey of Object.keys(entry)) {
37738
+ const colValue = entry[colKey];
37739
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37740
+ }
37741
+ return result;
37742
+ });
37743
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37744
+ }
37745
+ select(selectQuery) {
37746
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37747
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37748
+ throw new Error(
37749
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37750
+ );
37751
+ }
37752
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37753
+ }
37754
+ };
37755
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37756
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37757
+ constructor(table4, values, ignore, session, dialect4, select) {
37758
+ super();
37759
+ __publicField(this, "config");
37760
+ __publicField(this, "execute", (placeholderValues) => {
37761
+ return this.prepare().execute(placeholderValues);
37762
+ });
37763
+ __publicField(this, "createIterator", () => {
37764
+ const self2 = this;
37765
+ return async function* (placeholderValues) {
37766
+ yield* self2.prepare().iterator(placeholderValues);
37767
+ };
37768
+ });
37769
+ __publicField(this, "iterator", this.createIterator());
37770
+ this.session = session;
37771
+ this.dialect = dialect4;
37772
+ this.config = { table: table4, values, select, ignore };
37773
+ }
37774
+ /**
37775
+ * Adds an `on duplicate key update` clause to the query.
37776
+ *
37777
+ * 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.
37778
+ *
37779
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37780
+ *
37781
+ * @param config The `set` clause
37782
+ *
37783
+ * @example
37784
+ * ```ts
37785
+ * await db.insert(cars)
37786
+ * .values({ id: 1, brand: 'BMW'})
37787
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37788
+ * ```
37789
+ *
37790
+ * 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:
37791
+ *
37792
+ * ```ts
37793
+ * import { sql } from 'drizzle-orm';
37794
+ *
37795
+ * await db.insert(cars)
37796
+ * .values({ id: 1, brand: 'BMW' })
37797
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37798
+ * ```
37799
+ */
37800
+ onDuplicateKeyUpdate(config) {
37801
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37802
+ this.config.onConflict = sql`update ${setSql}`;
37803
+ return this;
37804
+ }
37805
+ $returningId() {
37806
+ const returning = [];
37807
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37808
+ if (value.primary) {
37809
+ returning.push({ field: value, path: [key] });
37810
+ }
37811
+ }
37812
+ this.config.returning = returning;
37813
+ return this;
37814
+ }
37815
+ /** @internal */
37816
+ getSQL() {
37817
+ return this.dialect.buildInsertQuery(this.config).sql;
37818
+ }
37819
+ toSQL() {
37820
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37821
+ return rest;
37822
+ }
37823
+ prepare() {
37824
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37825
+ return this.session.prepareQuery(
37826
+ this.dialect.sqlToQuery(sql2),
37827
+ void 0,
37828
+ void 0,
37829
+ generatedIds,
37830
+ this.config.returning
37831
+ );
37832
+ }
37833
+ $dynamic() {
37834
+ return this;
37835
+ }
37836
+ };
37837
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37541
37838
  }
37542
37839
  });
37543
37840