drizzle-kit 0.28.0 → 0.28.1-375ed28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. package/README.md +7 -7
  2. package/api.js +1032 -710
  3. package/api.mjs +1032 -710
  4. package/bin.cjs +644 -49
  5. package/package.json +1 -1
package/api.mjs CHANGED
@@ -21126,7 +21126,7 @@ var version;
21126
21126
  var init_version = __esm({
21127
21127
  "../drizzle-orm/dist/version.js"() {
21128
21128
  "use strict";
21129
- version = "0.36.1";
21129
+ version = "0.36.4";
21130
21130
  }
21131
21131
  });
21132
21132
 
@@ -21412,7 +21412,11 @@ var init_sql = __esm({
21412
21412
  if (_config.invokeSource === "indexes") {
21413
21413
  return { sql: escapeName(columnName), params: [] };
21414
21414
  }
21415
- return { sql: escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName), params: [] };
21415
+ const schemaName = chunk.table[Table2.Symbol.Schema];
21416
+ return {
21417
+ sql: chunk.table[IsAlias] || schemaName === void 0 ? escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName) : escapeName(schemaName) + "." + escapeName(chunk.table[Table2.Symbol.Name]) + "." + escapeName(columnName),
21418
+ params: []
21419
+ };
21416
21420
  }
21417
21421
  if (is(chunk, View3)) {
21418
21422
  const schemaName = chunk[ViewBaseConfig].schema;
@@ -22145,7 +22149,7 @@ function haveSameKeys(left, right) {
22145
22149
  }
22146
22150
  function mapUpdateSet(table4, values) {
22147
22151
  const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
22148
- if (is(value, SQL)) {
22152
+ if (is(value, SQL) || is(value, Column2)) {
22149
22153
  return [key, value];
22150
22154
  } else {
22151
22155
  return [key, new Param(value, table4[Table2.Symbol.Columns][key])];
@@ -24584,9 +24588,10 @@ var init_delete = __esm({
24584
24588
  constructor(table4, session, dialect4, withList) {
24585
24589
  super();
24586
24590
  __publicField(this, "config");
24591
+ __publicField(this, "authToken");
24587
24592
  __publicField(this, "execute", (placeholderValues) => {
24588
24593
  return tracer.startActiveSpan("drizzle.operation", () => {
24589
- return this._prepare().execute(placeholderValues);
24594
+ return this._prepare().execute(placeholderValues, this.authToken);
24590
24595
  });
24591
24596
  });
24592
24597
  this.session = session;
@@ -24647,167 +24652,16 @@ var init_delete = __esm({
24647
24652
  prepare(name2) {
24648
24653
  return this._prepare(name2);
24649
24654
  }
24650
- $dynamic() {
24651
- return this;
24652
- }
24653
- };
24654
- __publicField(PgDeleteBase, _a124, "PgDelete");
24655
- }
24656
- });
24657
-
24658
- // ../drizzle-orm/dist/pg-core/query-builders/insert.js
24659
- var _a125, PgInsertBuilder, _a126, _b98, PgInsertBase;
24660
- var init_insert = __esm({
24661
- "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
24662
- "use strict";
24663
- init_entity();
24664
- init_query_promise();
24665
- init_sql();
24666
- init_table();
24667
- init_tracing();
24668
- init_utils2();
24669
- _a125 = entityKind;
24670
- PgInsertBuilder = class {
24671
- constructor(table4, session, dialect4, withList) {
24672
- this.table = table4;
24673
- this.session = session;
24674
- this.dialect = dialect4;
24675
- this.withList = withList;
24676
- }
24677
- values(values) {
24678
- values = Array.isArray(values) ? values : [values];
24679
- if (values.length === 0) {
24680
- throw new Error("values() must be called with at least one value");
24681
- }
24682
- const mappedValues = values.map((entry) => {
24683
- const result = {};
24684
- const cols = this.table[Table2.Symbol.Columns];
24685
- for (const colKey of Object.keys(entry)) {
24686
- const colValue = entry[colKey];
24687
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
24688
- }
24689
- return result;
24690
- });
24691
- return new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
24692
- }
24693
- };
24694
- __publicField(PgInsertBuilder, _a125, "PgInsertBuilder");
24695
- PgInsertBase = class extends (_b98 = QueryPromise, _a126 = entityKind, _b98) {
24696
- constructor(table4, values, session, dialect4, withList) {
24697
- super();
24698
- __publicField(this, "config");
24699
- __publicField(this, "execute", (placeholderValues) => {
24700
- return tracer.startActiveSpan("drizzle.operation", () => {
24701
- return this._prepare().execute(placeholderValues);
24702
- });
24703
- });
24704
- this.session = session;
24705
- this.dialect = dialect4;
24706
- this.config = { table: table4, values, withList };
24707
- }
24708
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
24709
- this.config.returning = orderSelectedFields(fields);
24710
- return this;
24711
- }
24712
- /**
24713
- * Adds an `on conflict do nothing` clause to the query.
24714
- *
24715
- * Calling this method simply avoids inserting a row as its alternative action.
24716
- *
24717
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
24718
- *
24719
- * @param config The `target` and `where` clauses.
24720
- *
24721
- * @example
24722
- * ```ts
24723
- * // Insert one row and cancel the insert if there's a conflict
24724
- * await db.insert(cars)
24725
- * .values({ id: 1, brand: 'BMW' })
24726
- * .onConflictDoNothing();
24727
- *
24728
- * // Explicitly specify conflict target
24729
- * await db.insert(cars)
24730
- * .values({ id: 1, brand: 'BMW' })
24731
- * .onConflictDoNothing({ target: cars.id });
24732
- * ```
24733
- */
24734
- onConflictDoNothing(config = {}) {
24735
- if (config.target === void 0) {
24736
- this.config.onConflict = sql`do nothing`;
24737
- } else {
24738
- let targetColumn = "";
24739
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24740
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24741
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
24742
- }
24743
- return this;
24744
- }
24745
- /**
24746
- * Adds an `on conflict do update` clause to the query.
24747
- *
24748
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
24749
- *
24750
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
24751
- *
24752
- * @param config The `target`, `set` and `where` clauses.
24753
- *
24754
- * @example
24755
- * ```ts
24756
- * // Update the row if there's a conflict
24757
- * await db.insert(cars)
24758
- * .values({ id: 1, brand: 'BMW' })
24759
- * .onConflictDoUpdate({
24760
- * target: cars.id,
24761
- * set: { brand: 'Porsche' }
24762
- * });
24763
- *
24764
- * // Upsert with 'where' clause
24765
- * await db.insert(cars)
24766
- * .values({ id: 1, brand: 'BMW' })
24767
- * .onConflictDoUpdate({
24768
- * target: cars.id,
24769
- * set: { brand: 'newBMW' },
24770
- * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
24771
- * });
24772
- * ```
24773
- */
24774
- onConflictDoUpdate(config) {
24775
- if (config.where && (config.targetWhere || config.setWhere)) {
24776
- throw new Error(
24777
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
24778
- );
24779
- }
24780
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
24781
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
24782
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
24783
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
24784
- let targetColumn = "";
24785
- targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
24786
- this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
24787
- return this;
24788
- }
24789
- /** @internal */
24790
- getSQL() {
24791
- return this.dialect.buildInsertQuery(this.config);
24792
- }
24793
- toSQL() {
24794
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
24795
- return rest;
24796
- }
24797
24655
  /** @internal */
24798
- _prepare(name2) {
24799
- return tracer.startActiveSpan("drizzle.prepareQuery", () => {
24800
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
24801
- });
24802
- }
24803
- prepare(name2) {
24804
- return this._prepare(name2);
24656
+ setToken(token) {
24657
+ this.authToken = token;
24658
+ return this;
24805
24659
  }
24806
24660
  $dynamic() {
24807
24661
  return this;
24808
24662
  }
24809
24663
  };
24810
- __publicField(PgInsertBase, _a126, "PgInsert");
24664
+ __publicField(PgDeleteBase, _a124, "PgDelete");
24811
24665
  }
24812
24666
  });
24813
24667
 
@@ -24826,13 +24680,13 @@ function toCamelCase(input) {
24826
24680
  function noopCase(input) {
24827
24681
  return input;
24828
24682
  }
24829
- var _a127, CasingCache;
24683
+ var _a125, CasingCache;
24830
24684
  var init_casing = __esm({
24831
24685
  "../drizzle-orm/dist/casing.js"() {
24832
24686
  "use strict";
24833
24687
  init_entity();
24834
24688
  init_table();
24835
- _a127 = entityKind;
24689
+ _a125 = entityKind;
24836
24690
  CasingCache = class {
24837
24691
  constructor(casing2) {
24838
24692
  /** @internal */
@@ -24869,25 +24723,25 @@ var init_casing = __esm({
24869
24723
  this.cachedTables = {};
24870
24724
  }
24871
24725
  };
24872
- __publicField(CasingCache, _a127, "CasingCache");
24726
+ __publicField(CasingCache, _a125, "CasingCache");
24873
24727
  }
24874
24728
  });
24875
24729
 
24876
24730
  // ../drizzle-orm/dist/pg-core/view-base.js
24877
- var _a128, _b99, PgViewBase;
24731
+ var _a126, _b98, PgViewBase;
24878
24732
  var init_view_base = __esm({
24879
24733
  "../drizzle-orm/dist/pg-core/view-base.js"() {
24880
24734
  "use strict";
24881
24735
  init_entity();
24882
24736
  init_sql();
24883
- PgViewBase = class extends (_b99 = View3, _a128 = entityKind, _b99) {
24737
+ PgViewBase = class extends (_b98 = View3, _a126 = entityKind, _b98) {
24884
24738
  };
24885
- __publicField(PgViewBase, _a128, "PgViewBase");
24739
+ __publicField(PgViewBase, _a126, "PgViewBase");
24886
24740
  }
24887
24741
  });
24888
24742
 
24889
24743
  // ../drizzle-orm/dist/pg-core/dialect.js
24890
- var _a129, PgDialect;
24744
+ var _a127, PgDialect;
24891
24745
  var init_dialect = __esm({
24892
24746
  "../drizzle-orm/dist/pg-core/dialect.js"() {
24893
24747
  "use strict";
@@ -24906,7 +24760,7 @@ var init_dialect = __esm({
24906
24760
  init_utils2();
24907
24761
  init_view_common();
24908
24762
  init_view_base();
24909
- _a129 = entityKind;
24763
+ _a127 = entityKind;
24910
24764
  PgDialect = class {
24911
24765
  constructor(config) {
24912
24766
  /** @internal */
@@ -24986,12 +24840,19 @@ var init_dialect = __esm({
24986
24840
  return [res];
24987
24841
  }));
24988
24842
  }
24989
- buildUpdateQuery({ table: table4, set, where, returning, withList }) {
24843
+ buildUpdateQuery({ table: table4, set, where, returning, withList, from, joins }) {
24990
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)}`}`;
24991
24850
  const setSql = this.buildUpdateSet(table4, set);
24992
- 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;
24993
24854
  const whereSql = where ? sql` where ${where}` : void 0;
24994
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}`;
24855
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
24995
24856
  }
24996
24857
  /**
24997
24858
  * Builds selection SQL with provided fields/expressions
@@ -25043,6 +24904,54 @@ var init_dialect = __esm({
25043
24904
  });
25044
24905
  return sql.join(chunks);
25045
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
+ }
25046
24955
  buildSelectQuery({
25047
24956
  withList,
25048
24957
  fields,
@@ -25077,51 +24986,8 @@ var init_dialect = __esm({
25077
24986
  distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
25078
24987
  }
25079
24988
  const selection = this.buildSelection(fieldsList, { isSingleTable });
25080
- const tableSql = (() => {
25081
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
25082
- let fullName = sql`${sql.identifier(table4[Table2.Symbol.OriginalName])}`;
25083
- if (table4[Table2.Symbol.Schema]) {
25084
- fullName = sql`${sql.identifier(table4[Table2.Symbol.Schema])}.${fullName}`;
25085
- }
25086
- return sql`${fullName} ${sql.identifier(table4[Table2.Symbol.Name])}`;
25087
- }
25088
- return table4;
25089
- })();
25090
- const joinsArray = [];
25091
- if (joins) {
25092
- for (const [index4, joinMeta] of joins.entries()) {
25093
- if (index4 === 0) {
25094
- joinsArray.push(sql` `);
25095
- }
25096
- const table22 = joinMeta.table;
25097
- const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
25098
- if (is(table22, PgTable)) {
25099
- const tableName = table22[PgTable.Symbol.Name];
25100
- const tableSchema = table22[PgTable.Symbol.Schema];
25101
- const origTableName = table22[PgTable.Symbol.OriginalName];
25102
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
25103
- joinsArray.push(
25104
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25105
- );
25106
- } else if (is(table22, View3)) {
25107
- const viewName = table22[ViewBaseConfig].name;
25108
- const viewSchema = table22[ViewBaseConfig].schema;
25109
- const origViewName = table22[ViewBaseConfig].originalName;
25110
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
25111
- joinsArray.push(
25112
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
25113
- );
25114
- } else {
25115
- joinsArray.push(
25116
- sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table22} on ${joinMeta.on}`
25117
- );
25118
- }
25119
- if (index4 < joins.length - 1) {
25120
- joinsArray.push(sql` `);
25121
- }
25122
- }
25123
- }
25124
- const joinsSql = sql.join(joinsArray);
24989
+ const tableSql = this.buildFromTable(table4);
24990
+ const joinsSql = this.buildJoins(joins);
25125
24991
  const whereSql = where ? sql` where ${where}` : void 0;
25126
24992
  const havingSql = having ? sql` having ${having}` : void 0;
25127
24993
  let orderBySql;
@@ -25202,43 +25068,55 @@ var init_dialect = __esm({
25202
25068
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
25203
25069
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
25204
25070
  }
25205
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
25071
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }) {
25206
25072
  const valuesSqlList = [];
25207
25073
  const columns = table4[Table2.Symbol.Columns];
25208
25074
  const colEntries = Object.entries(columns).filter(([_2, col]) => !col.shouldDisableInsert());
25209
25075
  const insertOrder = colEntries.map(
25210
25076
  ([, column4]) => sql.identifier(this.casing.getColumnCasing(column4))
25211
25077
  );
25212
- for (const [valueIndex, value] of values.entries()) {
25213
- const valueList = [];
25214
- for (const [fieldName, col] of colEntries) {
25215
- const colValue = value[fieldName];
25216
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
25217
- if (col.defaultFn !== void 0) {
25218
- const defaultFnResult = col.defaultFn();
25219
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
25220
- valueList.push(defaultValue);
25221
- } else if (!col.default && col.onUpdateFn !== void 0) {
25222
- const onUpdateFnResult = col.onUpdateFn();
25223
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
25224
- valueList.push(newValue);
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
+ }
25225
25104
  } else {
25226
- valueList.push(sql`default`);
25105
+ valueList.push(colValue);
25227
25106
  }
25228
- } else {
25229
- valueList.push(colValue);
25230
25107
  }
25231
- }
25232
- valuesSqlList.push(valueList);
25233
- if (valueIndex < values.length - 1) {
25234
- valuesSqlList.push(sql`, `);
25108
+ valuesSqlList.push(valueList);
25109
+ if (valueIndex < values.length - 1) {
25110
+ valuesSqlList.push(sql`, `);
25111
+ }
25235
25112
  }
25236
25113
  }
25237
25114
  const withSql = this.buildWithCTE(withList);
25238
25115
  const valuesSql = sql.join(valuesSqlList);
25239
25116
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
25240
25117
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
25241
- 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}`;
25242
25120
  }
25243
25121
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25244
25122
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25974,12 +25852,12 @@ var init_dialect = __esm({
25974
25852
  };
25975
25853
  }
25976
25854
  };
25977
- __publicField(PgDialect, _a129, "PgDialect");
25855
+ __publicField(PgDialect, _a127, "PgDialect");
25978
25856
  }
25979
25857
  });
25980
25858
 
25981
25859
  // ../drizzle-orm/dist/selection-proxy.js
25982
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25860
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25983
25861
  var init_selection_proxy = __esm({
25984
25862
  "../drizzle-orm/dist/selection-proxy.js"() {
25985
25863
  "use strict";
@@ -25989,7 +25867,7 @@ var init_selection_proxy = __esm({
25989
25867
  init_sql();
25990
25868
  init_subquery();
25991
25869
  init_view_common();
25992
- _a130 = entityKind;
25870
+ _a128 = entityKind;
25993
25871
  _SelectionProxyHandler = class _SelectionProxyHandler {
25994
25872
  constructor(config) {
25995
25873
  __publicField(this, "config");
@@ -26055,25 +25933,25 @@ var init_selection_proxy = __esm({
26055
25933
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26056
25934
  }
26057
25935
  };
26058
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25936
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26059
25937
  SelectionProxyHandler = _SelectionProxyHandler;
26060
25938
  }
26061
25939
  });
26062
25940
 
26063
25941
  // ../drizzle-orm/dist/query-builders/query-builder.js
26064
- var _a131, TypedQueryBuilder;
25942
+ var _a129, TypedQueryBuilder;
26065
25943
  var init_query_builder = __esm({
26066
25944
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26067
25945
  "use strict";
26068
25946
  init_entity();
26069
- _a131 = entityKind;
25947
+ _a129 = entityKind;
26070
25948
  TypedQueryBuilder = class {
26071
25949
  /** @internal */
26072
25950
  getSelectedFields() {
26073
25951
  return this._.selectedFields;
26074
25952
  }
26075
25953
  };
26076
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25954
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26077
25955
  }
26078
25956
  });
26079
25957
 
@@ -26095,7 +25973,7 @@ function createSetOperator(type, isAll) {
26095
25973
  return leftSelect.addSetOperators(setOperators);
26096
25974
  };
26097
25975
  }
26098
- 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;
26099
25977
  var init_select2 = __esm({
26100
25978
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26101
25979
  "use strict";
@@ -26111,7 +25989,7 @@ var init_select2 = __esm({
26111
25989
  init_utils2();
26112
25990
  init_utils2();
26113
25991
  init_view_common();
26114
- _a132 = entityKind;
25992
+ _a130 = entityKind;
26115
25993
  PgSelectBuilder = class {
26116
25994
  constructor(config) {
26117
25995
  __publicField(this, "fields");
@@ -26119,6 +25997,7 @@ var init_select2 = __esm({
26119
25997
  __publicField(this, "dialect");
26120
25998
  __publicField(this, "withList", []);
26121
25999
  __publicField(this, "distinct");
26000
+ __publicField(this, "authToken");
26122
26001
  this.fields = config.fields;
26123
26002
  this.session = config.session;
26124
26003
  this.dialect = config.dialect;
@@ -26127,6 +26006,11 @@ var init_select2 = __esm({
26127
26006
  }
26128
26007
  this.distinct = config.distinct;
26129
26008
  }
26009
+ /** @internal */
26010
+ setToken(token) {
26011
+ this.authToken = token;
26012
+ return this;
26013
+ }
26130
26014
  /**
26131
26015
  * Specify the table, subquery, or other target that you're
26132
26016
  * building a select query against.
@@ -26149,7 +26033,7 @@ var init_select2 = __esm({
26149
26033
  } else {
26150
26034
  fields = getTableColumns(source);
26151
26035
  }
26152
- return new PgSelectBase({
26036
+ return this.authToken === void 0 ? new PgSelectBase({
26153
26037
  table: source,
26154
26038
  fields,
26155
26039
  isPartialSelect,
@@ -26157,11 +26041,19 @@ var init_select2 = __esm({
26157
26041
  dialect: this.dialect,
26158
26042
  withList: this.withList,
26159
26043
  distinct: this.distinct
26160
- });
26044
+ }) : new PgSelectBase({
26045
+ table: source,
26046
+ fields,
26047
+ isPartialSelect,
26048
+ session: this.session,
26049
+ dialect: this.dialect,
26050
+ withList: this.withList,
26051
+ distinct: this.distinct
26052
+ }).setToken(this.authToken);
26161
26053
  }
26162
26054
  };
26163
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26164
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26055
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26056
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26165
26057
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26166
26058
  super();
26167
26059
  __publicField(this, "_");
@@ -26761,19 +26653,20 @@ var init_select2 = __esm({
26761
26653
  return this;
26762
26654
  }
26763
26655
  };
26764
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26765
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26656
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26657
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26766
26658
  constructor() {
26767
26659
  super(...arguments);
26660
+ __publicField(this, "authToken");
26768
26661
  __publicField(this, "execute", (placeholderValues) => {
26769
26662
  return tracer.startActiveSpan("drizzle.operation", () => {
26770
- return this._prepare().execute(placeholderValues);
26663
+ return this._prepare().execute(placeholderValues, this.authToken);
26771
26664
  });
26772
26665
  });
26773
26666
  }
26774
26667
  /** @internal */
26775
26668
  _prepare(name2) {
26776
- const { session, config, dialect: dialect4, joinsNotNullableMap } = this;
26669
+ const { session, config, dialect: dialect4, joinsNotNullableMap, authToken } = this;
26777
26670
  if (!session) {
26778
26671
  throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
26779
26672
  }
@@ -26781,7 +26674,7 @@ var init_select2 = __esm({
26781
26674
  const fieldsList = orderSelectedFields(config.fields);
26782
26675
  const query = session.prepareQuery(dialect4.sqlToQuery(this.getSQL()), fieldsList, name2, true);
26783
26676
  query.joinsNotNullableMap = joinsNotNullableMap;
26784
- return query;
26677
+ return authToken === void 0 ? query : query.setToken(authToken);
26785
26678
  });
26786
26679
  }
26787
26680
  /**
@@ -26794,8 +26687,13 @@ var init_select2 = __esm({
26794
26687
  prepare(name2) {
26795
26688
  return this._prepare(name2);
26796
26689
  }
26690
+ /** @internal */
26691
+ setToken(token) {
26692
+ this.authToken = token;
26693
+ return this;
26694
+ }
26797
26695
  };
26798
- __publicField(PgSelectBase, _a134, "PgSelect");
26696
+ __publicField(PgSelectBase, _a132, "PgSelect");
26799
26697
  applyMixins(PgSelectBase, [QueryPromise]);
26800
26698
  getPgSetOperators = () => ({
26801
26699
  union,
@@ -26815,7 +26713,7 @@ var init_select2 = __esm({
26815
26713
  });
26816
26714
 
26817
26715
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26818
- var _a135, QueryBuilder;
26716
+ var _a133, QueryBuilder;
26819
26717
  var init_query_builder2 = __esm({
26820
26718
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26821
26719
  "use strict";
@@ -26824,7 +26722,7 @@ var init_query_builder2 = __esm({
26824
26722
  init_selection_proxy();
26825
26723
  init_subquery();
26826
26724
  init_select2();
26827
- _a135 = entityKind;
26725
+ _a133 = entityKind;
26828
26726
  QueryBuilder = class {
26829
26727
  constructor(dialect4) {
26830
26728
  __publicField(this, "dialect");
@@ -26905,27 +26803,227 @@ var init_query_builder2 = __esm({
26905
26803
  return this.dialect;
26906
26804
  }
26907
26805
  };
26908
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26806
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26909
26807
  }
26910
26808
  });
26911
26809
 
26912
- // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26913
- var _a136, _b102, PgRefreshMaterializedView;
26914
- var init_refresh_materialized_view = __esm({
26915
- "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26810
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26811
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26812
+ var init_insert = __esm({
26813
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26916
26814
  "use strict";
26917
26815
  init_entity();
26918
26816
  init_query_promise();
26817
+ init_sql();
26818
+ init_table();
26919
26819
  init_tracing();
26920
- PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26921
- constructor(view4, session, dialect4) {
26922
- super();
26923
- __publicField(this, "config");
26924
- __publicField(this, "execute", (placeholderValues) => {
26925
- return tracer.startActiveSpan("drizzle.operation", () => {
26926
- return this._prepare().execute(placeholderValues);
26927
- });
26928
- });
26820
+ init_utils2();
26821
+ init_query_builder2();
26822
+ _a134 = entityKind;
26823
+ PgInsertBuilder = class {
26824
+ constructor(table4, session, dialect4, withList, overridingSystemValue_) {
26825
+ __publicField(this, "authToken");
26826
+ this.table = table4;
26827
+ this.session = session;
26828
+ this.dialect = dialect4;
26829
+ this.withList = withList;
26830
+ this.overridingSystemValue_ = overridingSystemValue_;
26831
+ }
26832
+ /** @internal */
26833
+ setToken(token) {
26834
+ this.authToken = token;
26835
+ return this;
26836
+ }
26837
+ overridingSystemValue() {
26838
+ this.overridingSystemValue_ = true;
26839
+ return this;
26840
+ }
26841
+ values(values) {
26842
+ values = Array.isArray(values) ? values : [values];
26843
+ if (values.length === 0) {
26844
+ throw new Error("values() must be called with at least one value");
26845
+ }
26846
+ const mappedValues = values.map((entry) => {
26847
+ const result = {};
26848
+ const cols = this.table[Table2.Symbol.Columns];
26849
+ for (const colKey of Object.keys(entry)) {
26850
+ const colValue = entry[colKey];
26851
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26852
+ }
26853
+ return result;
26854
+ });
26855
+ return this.authToken === void 0 ? new PgInsertBase(
26856
+ this.table,
26857
+ mappedValues,
26858
+ this.session,
26859
+ this.dialect,
26860
+ this.withList,
26861
+ false,
26862
+ this.overridingSystemValue_
26863
+ ) : new PgInsertBase(
26864
+ this.table,
26865
+ mappedValues,
26866
+ this.session,
26867
+ this.dialect,
26868
+ this.withList,
26869
+ false,
26870
+ this.overridingSystemValue_
26871
+ ).setToken(this.authToken);
26872
+ }
26873
+ select(selectQuery) {
26874
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26875
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26876
+ throw new Error(
26877
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26878
+ );
26879
+ }
26880
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26881
+ }
26882
+ };
26883
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26884
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26885
+ constructor(table4, values, session, dialect4, withList, select, overridingSystemValue_) {
26886
+ super();
26887
+ __publicField(this, "config");
26888
+ __publicField(this, "authToken");
26889
+ __publicField(this, "execute", (placeholderValues) => {
26890
+ return tracer.startActiveSpan("drizzle.operation", () => {
26891
+ return this._prepare().execute(placeholderValues, this.authToken);
26892
+ });
26893
+ });
26894
+ this.session = session;
26895
+ this.dialect = dialect4;
26896
+ this.config = { table: table4, values, withList, select, overridingSystemValue_ };
26897
+ }
26898
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26899
+ this.config.returning = orderSelectedFields(fields);
26900
+ return this;
26901
+ }
26902
+ /**
26903
+ * Adds an `on conflict do nothing` clause to the query.
26904
+ *
26905
+ * Calling this method simply avoids inserting a row as its alternative action.
26906
+ *
26907
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26908
+ *
26909
+ * @param config The `target` and `where` clauses.
26910
+ *
26911
+ * @example
26912
+ * ```ts
26913
+ * // Insert one row and cancel the insert if there's a conflict
26914
+ * await db.insert(cars)
26915
+ * .values({ id: 1, brand: 'BMW' })
26916
+ * .onConflictDoNothing();
26917
+ *
26918
+ * // Explicitly specify conflict target
26919
+ * await db.insert(cars)
26920
+ * .values({ id: 1, brand: 'BMW' })
26921
+ * .onConflictDoNothing({ target: cars.id });
26922
+ * ```
26923
+ */
26924
+ onConflictDoNothing(config = {}) {
26925
+ if (config.target === void 0) {
26926
+ this.config.onConflict = sql`do nothing`;
26927
+ } else {
26928
+ let targetColumn = "";
26929
+ 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));
26930
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26931
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26932
+ }
26933
+ return this;
26934
+ }
26935
+ /**
26936
+ * Adds an `on conflict do update` clause to the query.
26937
+ *
26938
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26939
+ *
26940
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26941
+ *
26942
+ * @param config The `target`, `set` and `where` clauses.
26943
+ *
26944
+ * @example
26945
+ * ```ts
26946
+ * // Update the row if there's a conflict
26947
+ * await db.insert(cars)
26948
+ * .values({ id: 1, brand: 'BMW' })
26949
+ * .onConflictDoUpdate({
26950
+ * target: cars.id,
26951
+ * set: { brand: 'Porsche' }
26952
+ * });
26953
+ *
26954
+ * // Upsert with 'where' clause
26955
+ * await db.insert(cars)
26956
+ * .values({ id: 1, brand: 'BMW' })
26957
+ * .onConflictDoUpdate({
26958
+ * target: cars.id,
26959
+ * set: { brand: 'newBMW' },
26960
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26961
+ * });
26962
+ * ```
26963
+ */
26964
+ onConflictDoUpdate(config) {
26965
+ if (config.where && (config.targetWhere || config.setWhere)) {
26966
+ throw new Error(
26967
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26968
+ );
26969
+ }
26970
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26971
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26972
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26973
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26974
+ let targetColumn = "";
26975
+ 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));
26976
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26977
+ return this;
26978
+ }
26979
+ /** @internal */
26980
+ getSQL() {
26981
+ return this.dialect.buildInsertQuery(this.config);
26982
+ }
26983
+ toSQL() {
26984
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26985
+ return rest;
26986
+ }
26987
+ /** @internal */
26988
+ _prepare(name2) {
26989
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26990
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26991
+ });
26992
+ }
26993
+ prepare(name2) {
26994
+ return this._prepare(name2);
26995
+ }
26996
+ /** @internal */
26997
+ setToken(token) {
26998
+ this.authToken = token;
26999
+ return this;
27000
+ }
27001
+ $dynamic() {
27002
+ return this;
27003
+ }
27004
+ };
27005
+ __publicField(PgInsertBase, _a135, "PgInsert");
27006
+ }
27007
+ });
27008
+
27009
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
27010
+ var _a136, _b102, PgRefreshMaterializedView;
27011
+ var init_refresh_materialized_view = __esm({
27012
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
27013
+ "use strict";
27014
+ init_entity();
27015
+ init_query_promise();
27016
+ init_tracing();
27017
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
27018
+ constructor(view4, session, dialect4) {
27019
+ super();
27020
+ __publicField(this, "config");
27021
+ __publicField(this, "authToken");
27022
+ __publicField(this, "execute", (placeholderValues) => {
27023
+ return tracer.startActiveSpan("drizzle.operation", () => {
27024
+ return this._prepare().execute(placeholderValues, this.authToken);
27025
+ });
27026
+ });
26929
27027
  this.session = session;
26930
27028
  this.dialect = dialect4;
26931
27029
  this.config = { view: view4 };
@@ -26961,6 +27059,11 @@ var init_refresh_materialized_view = __esm({
26961
27059
  prepare(name2) {
26962
27060
  return this._prepare(name2);
26963
27061
  }
27062
+ /** @internal */
27063
+ setToken(token) {
27064
+ this.authToken = token;
27065
+ return this;
27066
+ }
26964
27067
  };
26965
27068
  __publicField(PgRefreshMaterializedView, _a136, "PgRefreshMaterializedView");
26966
27069
  }
@@ -26979,25 +27082,41 @@ var init_update = __esm({
26979
27082
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26980
27083
  "use strict";
26981
27084
  init_entity();
27085
+ init_table2();
26982
27086
  init_query_promise();
27087
+ init_selection_proxy();
27088
+ init_sql();
27089
+ init_subquery();
26983
27090
  init_table();
26984
27091
  init_utils2();
27092
+ init_view_common();
26985
27093
  _a137 = entityKind;
26986
27094
  PgUpdateBuilder = class {
26987
27095
  constructor(table4, session, dialect4, withList) {
27096
+ __publicField(this, "authToken");
26988
27097
  this.table = table4;
26989
27098
  this.session = session;
26990
27099
  this.dialect = dialect4;
26991
27100
  this.withList = withList;
26992
27101
  }
27102
+ setToken(token) {
27103
+ this.authToken = token;
27104
+ return this;
27105
+ }
26993
27106
  set(values) {
26994
- return new PgUpdateBase(
27107
+ return this.authToken === void 0 ? new PgUpdateBase(
26995
27108
  this.table,
26996
27109
  mapUpdateSet(this.table, values),
26997
27110
  this.session,
26998
27111
  this.dialect,
26999
27112
  this.withList
27000
- );
27113
+ ) : new PgUpdateBase(
27114
+ this.table,
27115
+ mapUpdateSet(this.table, values),
27116
+ this.session,
27117
+ this.dialect,
27118
+ this.withList
27119
+ ).setToken(this.authToken);
27001
27120
  }
27002
27121
  };
27003
27122
  __publicField(PgUpdateBuilder, _a137, "PgUpdateBuilder");
@@ -27005,12 +27124,86 @@ var init_update = __esm({
27005
27124
  constructor(table4, set, session, dialect4, withList) {
27006
27125
  super();
27007
27126
  __publicField(this, "config");
27127
+ __publicField(this, "tableName");
27128
+ __publicField(this, "joinsNotNullableMap");
27129
+ __publicField(this, "leftJoin", this.createJoin("left"));
27130
+ __publicField(this, "rightJoin", this.createJoin("right"));
27131
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27132
+ __publicField(this, "fullJoin", this.createJoin("full"));
27133
+ __publicField(this, "authToken");
27008
27134
  __publicField(this, "execute", (placeholderValues) => {
27009
- return this._prepare().execute(placeholderValues);
27135
+ return this._prepare().execute(placeholderValues, this.authToken);
27010
27136
  });
27011
27137
  this.session = session;
27012
27138
  this.dialect = dialect4;
27013
- this.config = { set, table: table4, withList };
27139
+ this.config = { set, table: table4, withList, joins: [] };
27140
+ this.tableName = getTableLikeName(table4);
27141
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27142
+ }
27143
+ from(source) {
27144
+ const tableName = getTableLikeName(source);
27145
+ if (typeof tableName === "string") {
27146
+ this.joinsNotNullableMap[tableName] = true;
27147
+ }
27148
+ this.config.from = source;
27149
+ return this;
27150
+ }
27151
+ getTableLikeFields(table4) {
27152
+ if (is(table4, PgTable)) {
27153
+ return table4[Table2.Symbol.Columns];
27154
+ } else if (is(table4, Subquery)) {
27155
+ return table4._.selectedFields;
27156
+ }
27157
+ return table4[ViewBaseConfig].selectedFields;
27158
+ }
27159
+ createJoin(joinType) {
27160
+ return (table4, on) => {
27161
+ const tableName = getTableLikeName(table4);
27162
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27163
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27164
+ }
27165
+ if (typeof on === "function") {
27166
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27167
+ on = on(
27168
+ new Proxy(
27169
+ this.config.table[Table2.Symbol.Columns],
27170
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27171
+ ),
27172
+ from && new Proxy(
27173
+ from,
27174
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27175
+ )
27176
+ );
27177
+ }
27178
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27179
+ if (typeof tableName === "string") {
27180
+ switch (joinType) {
27181
+ case "left": {
27182
+ this.joinsNotNullableMap[tableName] = false;
27183
+ break;
27184
+ }
27185
+ case "right": {
27186
+ this.joinsNotNullableMap = Object.fromEntries(
27187
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27188
+ );
27189
+ this.joinsNotNullableMap[tableName] = true;
27190
+ break;
27191
+ }
27192
+ case "inner": {
27193
+ this.joinsNotNullableMap[tableName] = true;
27194
+ break;
27195
+ }
27196
+ case "full": {
27197
+ this.joinsNotNullableMap = Object.fromEntries(
27198
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27199
+ );
27200
+ this.joinsNotNullableMap[tableName] = false;
27201
+ break;
27202
+ }
27203
+ }
27204
+ }
27205
+ return this;
27206
+ };
27014
27207
  }
27015
27208
  /**
27016
27209
  * Adds a 'where' clause to the query.
@@ -27049,7 +27242,24 @@ var init_update = __esm({
27049
27242
  this.config.where = where;
27050
27243
  return this;
27051
27244
  }
27052
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27245
+ returning(fields) {
27246
+ if (!fields) {
27247
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27248
+ if (this.config.from) {
27249
+ const tableName = getTableLikeName(this.config.from);
27250
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27251
+ const fromFields = this.getTableLikeFields(this.config.from);
27252
+ fields[tableName] = fromFields;
27253
+ }
27254
+ for (const join of this.config.joins) {
27255
+ const tableName2 = getTableLikeName(join.table);
27256
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27257
+ const fromFields = this.getTableLikeFields(join.table);
27258
+ fields[tableName2] = fromFields;
27259
+ }
27260
+ }
27261
+ }
27262
+ }
27053
27263
  this.config.returning = orderSelectedFields(fields);
27054
27264
  return this;
27055
27265
  }
@@ -27063,11 +27273,18 @@ var init_update = __esm({
27063
27273
  }
27064
27274
  /** @internal */
27065
27275
  _prepare(name2) {
27066
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27276
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27277
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27278
+ return query;
27067
27279
  }
27068
27280
  prepare(name2) {
27069
27281
  return this._prepare(name2);
27070
27282
  }
27283
+ /** @internal */
27284
+ setToken(token) {
27285
+ this.authToken = token;
27286
+ return this;
27287
+ }
27071
27288
  $dynamic() {
27072
27289
  return this;
27073
27290
  }
@@ -27101,6 +27318,7 @@ var init_count = __esm({
27101
27318
  constructor(params) {
27102
27319
  super(_PgCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
27103
27320
  __publicField(this, "sql");
27321
+ __publicField(this, "token");
27104
27322
  __publicField(this, _a139, "PgCountBuilder");
27105
27323
  __publicField(this, "session");
27106
27324
  this.params = params;
@@ -27117,8 +27335,12 @@ var init_count = __esm({
27117
27335
  static buildCount(source, filters) {
27118
27336
  return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters};`;
27119
27337
  }
27338
+ /** @intrnal */
27339
+ setToken(token) {
27340
+ this.token = token;
27341
+ }
27120
27342
  then(onfulfilled, onrejected) {
27121
- return Promise.resolve(this.session.count(this.sql)).then(
27343
+ return Promise.resolve(this.session.count(this.sql, this.token)).then(
27122
27344
  onfulfilled,
27123
27345
  onrejected
27124
27346
  );
@@ -27195,6 +27417,7 @@ var init_query = __esm({
27195
27417
  PgRelationalQuery = class extends (_b105 = QueryPromise, _a141 = entityKind, _b105) {
27196
27418
  constructor(fullSchema, schema4, tableNamesMap, table4, tableConfig, dialect4, session, config, mode) {
27197
27419
  super();
27420
+ __publicField(this, "authToken");
27198
27421
  this.fullSchema = fullSchema;
27199
27422
  this.schema = schema4;
27200
27423
  this.tableNamesMap = tableNamesMap;
@@ -27252,9 +27475,14 @@ var init_query = __esm({
27252
27475
  toSQL() {
27253
27476
  return this._toSQL().builtQuery;
27254
27477
  }
27478
+ /** @internal */
27479
+ setToken(token) {
27480
+ this.authToken = token;
27481
+ return this;
27482
+ }
27255
27483
  execute() {
27256
27484
  return tracer.startActiveSpan("drizzle.operation", () => {
27257
- return this._prepare().execute();
27485
+ return this._prepare().execute(void 0, this.authToken);
27258
27486
  });
27259
27487
  }
27260
27488
  };
@@ -27317,6 +27545,7 @@ var init_db = __esm({
27317
27545
  PgDatabase = class {
27318
27546
  constructor(dialect4, session, schema4) {
27319
27547
  __publicField(this, "query");
27548
+ __publicField(this, "authToken");
27320
27549
  this.dialect = dialect4;
27321
27550
  this.session = session;
27322
27551
  this._ = schema4 ? {
@@ -27572,7 +27801,7 @@ var init_db = __esm({
27572
27801
  false
27573
27802
  );
27574
27803
  return new PgRaw(
27575
- () => prepared.execute(),
27804
+ () => prepared.execute(void 0, this.authToken),
27576
27805
  sequel,
27577
27806
  builtQuery,
27578
27807
  (result) => prepared.mapResult(result, true)
@@ -28117,6 +28346,7 @@ var init_session = __esm({
28117
28346
  _a159 = entityKind;
28118
28347
  PgPreparedQuery = class {
28119
28348
  constructor(query) {
28349
+ __publicField(this, "authToken");
28120
28350
  /** @internal */
28121
28351
  __publicField(this, "joinsNotNullableMap");
28122
28352
  this.query = query;
@@ -28127,6 +28357,11 @@ var init_session = __esm({
28127
28357
  mapResult(response, _isFromBatch) {
28128
28358
  return response;
28129
28359
  }
28360
+ /** @internal */
28361
+ setToken(token) {
28362
+ this.authToken = token;
28363
+ return this;
28364
+ }
28130
28365
  };
28131
28366
  __publicField(PgPreparedQuery, _a159, "PgPreparedQuery");
28132
28367
  _a160 = entityKind;
@@ -28134,7 +28369,8 @@ var init_session = __esm({
28134
28369
  constructor(dialect4) {
28135
28370
  this.dialect = dialect4;
28136
28371
  }
28137
- execute(query) {
28372
+ /** @internal */
28373
+ execute(query, token) {
28138
28374
  return tracer.startActiveSpan("drizzle.operation", () => {
28139
28375
  const prepared = tracer.startActiveSpan("drizzle.prepareQuery", () => {
28140
28376
  return this.prepareQuery(
@@ -28144,7 +28380,7 @@ var init_session = __esm({
28144
28380
  false
28145
28381
  );
28146
28382
  });
28147
- return prepared.execute();
28383
+ return prepared.setToken(token).execute(void 0, token);
28148
28384
  });
28149
28385
  }
28150
28386
  all(query) {
@@ -28155,8 +28391,9 @@ var init_session = __esm({
28155
28391
  false
28156
28392
  ).all();
28157
28393
  }
28158
- async count(sql2) {
28159
- const res = await this.execute(sql2);
28394
+ /** @internal */
28395
+ async count(sql2, token) {
28396
+ const res = await this.execute(sql2, token);
28160
28397
  return Number(
28161
28398
  res[0]["count"]
28162
28399
  );
@@ -30790,180 +31027,12 @@ var init_delete2 = __esm({
30790
31027
  return this;
30791
31028
  }
30792
31029
  returning(fields = this.table[SQLiteTable.Symbol.Columns]) {
30793
- this.config.returning = orderSelectedFields(fields);
30794
- return this;
30795
- }
30796
- /** @internal */
30797
- getSQL() {
30798
- return this.dialect.buildDeleteQuery(this.config);
30799
- }
30800
- toSQL() {
30801
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
30802
- return rest;
30803
- }
30804
- /** @internal */
30805
- _prepare(isOneTimeQuery = true) {
30806
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
30807
- this.dialect.sqlToQuery(this.getSQL()),
30808
- this.config.returning,
30809
- this.config.returning ? "all" : "run",
30810
- true
30811
- );
30812
- }
30813
- prepare() {
30814
- return this._prepare(false);
30815
- }
30816
- async execute(placeholderValues) {
30817
- return this._prepare().execute(placeholderValues);
30818
- }
30819
- $dynamic() {
30820
- return this;
30821
- }
30822
- };
30823
- __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30824
- }
30825
- });
30826
-
30827
- // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
30828
- var _a197, SQLiteInsertBuilder, _a198, _b142, SQLiteInsertBase;
30829
- var init_insert2 = __esm({
30830
- "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
30831
- "use strict";
30832
- init_entity();
30833
- init_query_promise();
30834
- init_sql();
30835
- init_table3();
30836
- init_table();
30837
- init_utils2();
30838
- _a197 = entityKind;
30839
- SQLiteInsertBuilder = class {
30840
- constructor(table4, session, dialect4, withList) {
30841
- this.table = table4;
30842
- this.session = session;
30843
- this.dialect = dialect4;
30844
- this.withList = withList;
30845
- }
30846
- values(values) {
30847
- values = Array.isArray(values) ? values : [values];
30848
- if (values.length === 0) {
30849
- throw new Error("values() must be called with at least one value");
30850
- }
30851
- const mappedValues = values.map((entry) => {
30852
- const result = {};
30853
- const cols = this.table[Table2.Symbol.Columns];
30854
- for (const colKey of Object.keys(entry)) {
30855
- const colValue = entry[colKey];
30856
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
30857
- }
30858
- return result;
30859
- });
30860
- return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
30861
- }
30862
- };
30863
- __publicField(SQLiteInsertBuilder, _a197, "SQLiteInsertBuilder");
30864
- SQLiteInsertBase = class extends (_b142 = QueryPromise, _a198 = entityKind, _b142) {
30865
- constructor(table4, values, session, dialect4, withList) {
30866
- super();
30867
- /** @internal */
30868
- __publicField(this, "config");
30869
- __publicField(this, "run", (placeholderValues) => {
30870
- return this._prepare().run(placeholderValues);
30871
- });
30872
- __publicField(this, "all", (placeholderValues) => {
30873
- return this._prepare().all(placeholderValues);
30874
- });
30875
- __publicField(this, "get", (placeholderValues) => {
30876
- return this._prepare().get(placeholderValues);
30877
- });
30878
- __publicField(this, "values", (placeholderValues) => {
30879
- return this._prepare().values(placeholderValues);
30880
- });
30881
- this.session = session;
30882
- this.dialect = dialect4;
30883
- this.config = { table: table4, values, withList };
30884
- }
30885
- returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
30886
- this.config.returning = orderSelectedFields(fields);
30887
- return this;
30888
- }
30889
- /**
30890
- * Adds an `on conflict do nothing` clause to the query.
30891
- *
30892
- * Calling this method simply avoids inserting a row as its alternative action.
30893
- *
30894
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
30895
- *
30896
- * @param config The `target` and `where` clauses.
30897
- *
30898
- * @example
30899
- * ```ts
30900
- * // Insert one row and cancel the insert if there's a conflict
30901
- * await db.insert(cars)
30902
- * .values({ id: 1, brand: 'BMW' })
30903
- * .onConflictDoNothing();
30904
- *
30905
- * // Explicitly specify conflict target
30906
- * await db.insert(cars)
30907
- * .values({ id: 1, brand: 'BMW' })
30908
- * .onConflictDoNothing({ target: cars.id });
30909
- * ```
30910
- */
30911
- onConflictDoNothing(config = {}) {
30912
- if (config.target === void 0) {
30913
- this.config.onConflict = sql`do nothing`;
30914
- } else {
30915
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30916
- const whereSql = config.where ? sql` where ${config.where}` : sql``;
30917
- this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
30918
- }
30919
- return this;
30920
- }
30921
- /**
30922
- * Adds an `on conflict do update` clause to the query.
30923
- *
30924
- * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
30925
- *
30926
- * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
30927
- *
30928
- * @param config The `target`, `set` and `where` clauses.
30929
- *
30930
- * @example
30931
- * ```ts
30932
- * // Update the row if there's a conflict
30933
- * await db.insert(cars)
30934
- * .values({ id: 1, brand: 'BMW' })
30935
- * .onConflictDoUpdate({
30936
- * target: cars.id,
30937
- * set: { brand: 'Porsche' }
30938
- * });
30939
- *
30940
- * // Upsert with 'where' clause
30941
- * await db.insert(cars)
30942
- * .values({ id: 1, brand: 'BMW' })
30943
- * .onConflictDoUpdate({
30944
- * target: cars.id,
30945
- * set: { brand: 'newBMW' },
30946
- * where: sql`${cars.createdAt} > '2023-01-01'::date`,
30947
- * });
30948
- * ```
30949
- */
30950
- onConflictDoUpdate(config) {
30951
- if (config.where && (config.targetWhere || config.setWhere)) {
30952
- throw new Error(
30953
- 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
30954
- );
30955
- }
30956
- const whereSql = config.where ? sql` where ${config.where}` : void 0;
30957
- const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
30958
- const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
30959
- const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
30960
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
30961
- this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
31030
+ this.config.returning = orderSelectedFields(fields);
30962
31031
  return this;
30963
31032
  }
30964
31033
  /** @internal */
30965
31034
  getSQL() {
30966
- return this.dialect.buildInsertQuery(this.config);
31035
+ return this.dialect.buildDeleteQuery(this.config);
30967
31036
  }
30968
31037
  toSQL() {
30969
31038
  const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
@@ -30981,32 +31050,32 @@ var init_insert2 = __esm({
30981
31050
  prepare() {
30982
31051
  return this._prepare(false);
30983
31052
  }
30984
- async execute() {
30985
- return this.config.returning ? this.all() : this.run();
31053
+ async execute(placeholderValues) {
31054
+ return this._prepare().execute(placeholderValues);
30986
31055
  }
30987
31056
  $dynamic() {
30988
31057
  return this;
30989
31058
  }
30990
31059
  };
30991
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
31060
+ __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30992
31061
  }
30993
31062
  });
30994
31063
 
30995
31064
  // ../drizzle-orm/dist/sqlite-core/view-base.js
30996
- var _a199, _b143, SQLiteViewBase;
31065
+ var _a197, _b142, SQLiteViewBase;
30997
31066
  var init_view_base2 = __esm({
30998
31067
  "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30999
31068
  "use strict";
31000
31069
  init_entity();
31001
31070
  init_sql();
31002
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31071
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
31003
31072
  };
31004
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
31073
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31005
31074
  }
31006
31075
  });
31007
31076
 
31008
31077
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31009
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
31078
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31010
31079
  var init_dialect2 = __esm({
31011
31080
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31012
31081
  "use strict";
@@ -31025,7 +31094,7 @@ var init_dialect2 = __esm({
31025
31094
  init_utils2();
31026
31095
  init_view_common();
31027
31096
  init_view_base2();
31028
- _a200 = entityKind;
31097
+ _a198 = entityKind;
31029
31098
  SQLiteDialect = class {
31030
31099
  constructor(config) {
31031
31100
  /** @internal */
@@ -31078,14 +31147,16 @@ var init_dialect2 = __esm({
31078
31147
  return [res];
31079
31148
  }));
31080
31149
  }
31081
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31150
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31082
31151
  const withSql = this.buildWithCTE(withList);
31083
31152
  const setSql = this.buildUpdateSet(table4, set);
31153
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31154
+ const joinsSql = this.buildJoins(joins);
31084
31155
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31085
31156
  const whereSql = where ? sql` where ${where}` : void 0;
31086
31157
  const orderBySql = this.buildOrderBy(orderBy);
31087
31158
  const limitSql = this.buildLimit(limit);
31088
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31159
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31089
31160
  }
31090
31161
  /**
31091
31162
  * Builds selection SQL with provided fields/expressions
@@ -31138,6 +31209,37 @@ var init_dialect2 = __esm({
31138
31209
  });
31139
31210
  return sql.join(chunks);
31140
31211
  }
31212
+ buildJoins(joins) {
31213
+ if (!joins || joins.length === 0) {
31214
+ return void 0;
31215
+ }
31216
+ const joinsArray = [];
31217
+ if (joins) {
31218
+ for (const [index4, joinMeta] of joins.entries()) {
31219
+ if (index4 === 0) {
31220
+ joinsArray.push(sql` `);
31221
+ }
31222
+ const table4 = joinMeta.table;
31223
+ if (is(table4, SQLiteTable)) {
31224
+ const tableName = table4[SQLiteTable.Symbol.Name];
31225
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31226
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31227
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31228
+ joinsArray.push(
31229
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31230
+ );
31231
+ } else {
31232
+ joinsArray.push(
31233
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31234
+ );
31235
+ }
31236
+ if (index4 < joins.length - 1) {
31237
+ joinsArray.push(sql` `);
31238
+ }
31239
+ }
31240
+ }
31241
+ return sql.join(joinsArray);
31242
+ }
31141
31243
  buildLimit(limit) {
31142
31244
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31143
31245
  }
@@ -31153,6 +31255,12 @@ var init_dialect2 = __esm({
31153
31255
  }
31154
31256
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31155
31257
  }
31258
+ buildFromTable(table4) {
31259
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31260
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31261
+ }
31262
+ return table4;
31263
+ }
31156
31264
  buildSelectQuery({
31157
31265
  withList,
31158
31266
  fields,
@@ -31183,38 +31291,8 @@ var init_dialect2 = __esm({
31183
31291
  const withSql = this.buildWithCTE(withList);
31184
31292
  const distinctSql = distinct ? sql` distinct` : void 0;
31185
31293
  const selection = this.buildSelection(fieldsList, { isSingleTable });
31186
- const tableSql = (() => {
31187
- if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31188
- return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31189
- }
31190
- return table4;
31191
- })();
31192
- const joinsArray = [];
31193
- if (joins) {
31194
- for (const [index4, joinMeta] of joins.entries()) {
31195
- if (index4 === 0) {
31196
- joinsArray.push(sql` `);
31197
- }
31198
- const table22 = joinMeta.table;
31199
- if (is(table22, SQLiteTable)) {
31200
- const tableName = table22[SQLiteTable.Symbol.Name];
31201
- const tableSchema = table22[SQLiteTable.Symbol.Schema];
31202
- const origTableName = table22[SQLiteTable.Symbol.OriginalName];
31203
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31204
- joinsArray.push(
31205
- sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31206
- );
31207
- } else {
31208
- joinsArray.push(
31209
- sql`${sql.raw(joinMeta.joinType)} join ${table22} on ${joinMeta.on}`
31210
- );
31211
- }
31212
- if (index4 < joins.length - 1) {
31213
- joinsArray.push(sql` `);
31214
- }
31215
- }
31216
- }
31217
- const joinsSql = sql.join(joinsArray);
31294
+ const tableSql = this.buildFromTable(table4);
31295
+ const joinsSql = this.buildJoins(joins);
31218
31296
  const whereSql = where ? sql` where ${where}` : void 0;
31219
31297
  const havingSql = having ? sql` having ${having}` : void 0;
31220
31298
  const groupByList = [];
@@ -31280,45 +31358,56 @@ var init_dialect2 = __esm({
31280
31358
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31281
31359
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31282
31360
  }
31283
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31361
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31284
31362
  const valuesSqlList = [];
31285
31363
  const columns = table4[Table2.Symbol.Columns];
31286
31364
  const colEntries = Object.entries(columns).filter(
31287
31365
  ([_2, col]) => !col.shouldDisableInsert()
31288
31366
  );
31289
31367
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
31290
- for (const [valueIndex, value] of values.entries()) {
31291
- const valueList = [];
31292
- for (const [fieldName, col] of colEntries) {
31293
- const colValue = value[fieldName];
31294
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31295
- let defaultValue;
31296
- if (col.default !== null && col.default !== void 0) {
31297
- defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31298
- } else if (col.defaultFn !== void 0) {
31299
- const defaultFnResult = col.defaultFn();
31300
- defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31301
- } else if (!col.default && col.onUpdateFn !== void 0) {
31302
- const onUpdateFnResult = col.onUpdateFn();
31303
- defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31368
+ if (select) {
31369
+ const select2 = valuesOrSelect;
31370
+ if (is(select2, SQL)) {
31371
+ valuesSqlList.push(select2);
31372
+ } else {
31373
+ valuesSqlList.push(select2.getSQL());
31374
+ }
31375
+ } else {
31376
+ const values = valuesOrSelect;
31377
+ valuesSqlList.push(sql.raw("values "));
31378
+ for (const [valueIndex, value] of values.entries()) {
31379
+ const valueList = [];
31380
+ for (const [fieldName, col] of colEntries) {
31381
+ const colValue = value[fieldName];
31382
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31383
+ let defaultValue;
31384
+ if (col.default !== null && col.default !== void 0) {
31385
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31386
+ } else if (col.defaultFn !== void 0) {
31387
+ const defaultFnResult = col.defaultFn();
31388
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31389
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31390
+ const onUpdateFnResult = col.onUpdateFn();
31391
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31392
+ } else {
31393
+ defaultValue = sql`null`;
31394
+ }
31395
+ valueList.push(defaultValue);
31304
31396
  } else {
31305
- defaultValue = sql`null`;
31397
+ valueList.push(colValue);
31306
31398
  }
31307
- valueList.push(defaultValue);
31308
- } else {
31309
- valueList.push(colValue);
31310
31399
  }
31311
- }
31312
- valuesSqlList.push(valueList);
31313
- if (valueIndex < values.length - 1) {
31314
- valuesSqlList.push(sql`, `);
31400
+ valuesSqlList.push(valueList);
31401
+ if (valueIndex < values.length - 1) {
31402
+ valuesSqlList.push(sql`, `);
31403
+ }
31315
31404
  }
31316
31405
  }
31317
31406
  const withSql = this.buildWithCTE(withList);
31318
31407
  const valuesSql = sql.join(valuesSqlList);
31319
31408
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31320
31409
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31321
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31410
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31322
31411
  }
31323
31412
  sqlToQuery(sql2, invokeSource) {
31324
31413
  return sql2.toQuery({
@@ -31547,8 +31636,8 @@ var init_dialect2 = __esm({
31547
31636
  };
31548
31637
  }
31549
31638
  };
31550
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31551
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31639
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31640
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31552
31641
  migrate(migrations, session, config) {
31553
31642
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31554
31643
  const migrationTableCreate = sql`
@@ -31582,8 +31671,8 @@ var init_dialect2 = __esm({
31582
31671
  }
31583
31672
  }
31584
31673
  };
31585
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31586
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31674
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31675
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31587
31676
  async migrate(migrations, session, config) {
31588
31677
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31589
31678
  const migrationTableCreate = sql`
@@ -31612,7 +31701,7 @@ var init_dialect2 = __esm({
31612
31701
  });
31613
31702
  }
31614
31703
  };
31615
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31704
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31616
31705
  }
31617
31706
  });
31618
31707
 
@@ -31634,7 +31723,7 @@ function createSetOperator2(type, isAll) {
31634
31723
  return leftSelect.addSetOperators(setOperators);
31635
31724
  };
31636
31725
  }
31637
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31726
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31638
31727
  var init_select3 = __esm({
31639
31728
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31640
31729
  "use strict";
@@ -31648,7 +31737,7 @@ var init_select3 = __esm({
31648
31737
  init_utils2();
31649
31738
  init_view_common();
31650
31739
  init_view_base2();
31651
- _a203 = entityKind;
31740
+ _a201 = entityKind;
31652
31741
  SQLiteSelectBuilder = class {
31653
31742
  constructor(config) {
31654
31743
  __publicField(this, "fields");
@@ -31689,8 +31778,8 @@ var init_select3 = __esm({
31689
31778
  });
31690
31779
  }
31691
31780
  };
31692
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31693
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31781
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31782
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31694
31783
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31695
31784
  super();
31696
31785
  __publicField(this, "_");
@@ -32195,8 +32284,8 @@ var init_select3 = __esm({
32195
32284
  return this;
32196
32285
  }
32197
32286
  };
32198
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32199
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32287
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32288
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32200
32289
  constructor() {
32201
32290
  super(...arguments);
32202
32291
  __publicField(this, "run", (placeholderValues) => {
@@ -32234,7 +32323,7 @@ var init_select3 = __esm({
32234
32323
  return this.all();
32235
32324
  }
32236
32325
  };
32237
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32326
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32238
32327
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32239
32328
  getSQLiteSetOperators = () => ({
32240
32329
  union: union2,
@@ -32250,7 +32339,7 @@ var init_select3 = __esm({
32250
32339
  });
32251
32340
 
32252
32341
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32253
- var _a206, QueryBuilder2;
32342
+ var _a204, QueryBuilder2;
32254
32343
  var init_query_builder3 = __esm({
32255
32344
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32256
32345
  "use strict";
@@ -32259,7 +32348,7 @@ var init_query_builder3 = __esm({
32259
32348
  init_dialect2();
32260
32349
  init_subquery();
32261
32350
  init_select3();
32262
- _a206 = entityKind;
32351
+ _a204 = entityKind;
32263
32352
  QueryBuilder2 = class {
32264
32353
  constructor(dialect4) {
32265
32354
  __publicField(this, "dialect");
@@ -32321,7 +32410,185 @@ var init_query_builder3 = __esm({
32321
32410
  return this.dialect;
32322
32411
  }
32323
32412
  };
32324
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32413
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32414
+ }
32415
+ });
32416
+
32417
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32418
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32419
+ var init_insert2 = __esm({
32420
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32421
+ "use strict";
32422
+ init_entity();
32423
+ init_query_promise();
32424
+ init_sql();
32425
+ init_table3();
32426
+ init_table();
32427
+ init_utils2();
32428
+ init_query_builder3();
32429
+ _a205 = entityKind;
32430
+ SQLiteInsertBuilder = class {
32431
+ constructor(table4, session, dialect4, withList) {
32432
+ this.table = table4;
32433
+ this.session = session;
32434
+ this.dialect = dialect4;
32435
+ this.withList = withList;
32436
+ }
32437
+ values(values) {
32438
+ values = Array.isArray(values) ? values : [values];
32439
+ if (values.length === 0) {
32440
+ throw new Error("values() must be called with at least one value");
32441
+ }
32442
+ const mappedValues = values.map((entry) => {
32443
+ const result = {};
32444
+ const cols = this.table[Table2.Symbol.Columns];
32445
+ for (const colKey of Object.keys(entry)) {
32446
+ const colValue = entry[colKey];
32447
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32448
+ }
32449
+ return result;
32450
+ });
32451
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32452
+ }
32453
+ select(selectQuery) {
32454
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32455
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32456
+ throw new Error(
32457
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32458
+ );
32459
+ }
32460
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32461
+ }
32462
+ };
32463
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32464
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32465
+ constructor(table4, values, session, dialect4, withList, select) {
32466
+ super();
32467
+ /** @internal */
32468
+ __publicField(this, "config");
32469
+ __publicField(this, "run", (placeholderValues) => {
32470
+ return this._prepare().run(placeholderValues);
32471
+ });
32472
+ __publicField(this, "all", (placeholderValues) => {
32473
+ return this._prepare().all(placeholderValues);
32474
+ });
32475
+ __publicField(this, "get", (placeholderValues) => {
32476
+ return this._prepare().get(placeholderValues);
32477
+ });
32478
+ __publicField(this, "values", (placeholderValues) => {
32479
+ return this._prepare().values(placeholderValues);
32480
+ });
32481
+ this.session = session;
32482
+ this.dialect = dialect4;
32483
+ this.config = { table: table4, values, withList, select };
32484
+ }
32485
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32486
+ this.config.returning = orderSelectedFields(fields);
32487
+ return this;
32488
+ }
32489
+ /**
32490
+ * Adds an `on conflict do nothing` clause to the query.
32491
+ *
32492
+ * Calling this method simply avoids inserting a row as its alternative action.
32493
+ *
32494
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32495
+ *
32496
+ * @param config The `target` and `where` clauses.
32497
+ *
32498
+ * @example
32499
+ * ```ts
32500
+ * // Insert one row and cancel the insert if there's a conflict
32501
+ * await db.insert(cars)
32502
+ * .values({ id: 1, brand: 'BMW' })
32503
+ * .onConflictDoNothing();
32504
+ *
32505
+ * // Explicitly specify conflict target
32506
+ * await db.insert(cars)
32507
+ * .values({ id: 1, brand: 'BMW' })
32508
+ * .onConflictDoNothing({ target: cars.id });
32509
+ * ```
32510
+ */
32511
+ onConflictDoNothing(config = {}) {
32512
+ if (config.target === void 0) {
32513
+ this.config.onConflict = sql`do nothing`;
32514
+ } else {
32515
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32516
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32517
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32518
+ }
32519
+ return this;
32520
+ }
32521
+ /**
32522
+ * Adds an `on conflict do update` clause to the query.
32523
+ *
32524
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32525
+ *
32526
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32527
+ *
32528
+ * @param config The `target`, `set` and `where` clauses.
32529
+ *
32530
+ * @example
32531
+ * ```ts
32532
+ * // Update the row if there's a conflict
32533
+ * await db.insert(cars)
32534
+ * .values({ id: 1, brand: 'BMW' })
32535
+ * .onConflictDoUpdate({
32536
+ * target: cars.id,
32537
+ * set: { brand: 'Porsche' }
32538
+ * });
32539
+ *
32540
+ * // Upsert with 'where' clause
32541
+ * await db.insert(cars)
32542
+ * .values({ id: 1, brand: 'BMW' })
32543
+ * .onConflictDoUpdate({
32544
+ * target: cars.id,
32545
+ * set: { brand: 'newBMW' },
32546
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32547
+ * });
32548
+ * ```
32549
+ */
32550
+ onConflictDoUpdate(config) {
32551
+ if (config.where && (config.targetWhere || config.setWhere)) {
32552
+ throw new Error(
32553
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32554
+ );
32555
+ }
32556
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32557
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32558
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32559
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32560
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32561
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32562
+ return this;
32563
+ }
32564
+ /** @internal */
32565
+ getSQL() {
32566
+ return this.dialect.buildInsertQuery(this.config);
32567
+ }
32568
+ toSQL() {
32569
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32570
+ return rest;
32571
+ }
32572
+ /** @internal */
32573
+ _prepare(isOneTimeQuery = true) {
32574
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32575
+ this.dialect.sqlToQuery(this.getSQL()),
32576
+ this.config.returning,
32577
+ this.config.returning ? "all" : "run",
32578
+ true
32579
+ );
32580
+ }
32581
+ prepare() {
32582
+ return this._prepare(false);
32583
+ }
32584
+ async execute() {
32585
+ return this.config.returning ? this.all() : this.run();
32586
+ }
32587
+ $dynamic() {
32588
+ return this;
32589
+ }
32590
+ };
32591
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32325
32592
  }
32326
32593
  });
32327
32594
 
@@ -32341,8 +32608,11 @@ var init_update2 = __esm({
32341
32608
  init_query_promise();
32342
32609
  init_selection_proxy();
32343
32610
  init_table3();
32611
+ init_subquery();
32344
32612
  init_table();
32345
32613
  init_utils2();
32614
+ init_view_common();
32615
+ init_view_base2();
32346
32616
  _a207 = entityKind;
32347
32617
  SQLiteUpdateBuilder = class {
32348
32618
  constructor(table4, session, dialect4, withList) {
@@ -32367,6 +32637,10 @@ var init_update2 = __esm({
32367
32637
  super();
32368
32638
  /** @internal */
32369
32639
  __publicField(this, "config");
32640
+ __publicField(this, "leftJoin", this.createJoin("left"));
32641
+ __publicField(this, "rightJoin", this.createJoin("right"));
32642
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32643
+ __publicField(this, "fullJoin", this.createJoin("full"));
32370
32644
  __publicField(this, "run", (placeholderValues) => {
32371
32645
  return this._prepare().run(placeholderValues);
32372
32646
  });
@@ -32381,7 +32655,34 @@ var init_update2 = __esm({
32381
32655
  });
32382
32656
  this.session = session;
32383
32657
  this.dialect = dialect4;
32384
- this.config = { set, table: table4, withList };
32658
+ this.config = { set, table: table4, withList, joins: [] };
32659
+ }
32660
+ from(source) {
32661
+ this.config.from = source;
32662
+ return this;
32663
+ }
32664
+ createJoin(joinType) {
32665
+ return (table4, on) => {
32666
+ const tableName = getTableLikeName(table4);
32667
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32668
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32669
+ }
32670
+ if (typeof on === "function") {
32671
+ 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;
32672
+ on = on(
32673
+ new Proxy(
32674
+ this.config.table[Table2.Symbol.Columns],
32675
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32676
+ ),
32677
+ from && new Proxy(
32678
+ from,
32679
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32680
+ )
32681
+ );
32682
+ }
32683
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32684
+ return this;
32685
+ };
32385
32686
  }
32386
32687
  /**
32387
32688
  * Adds a 'where' clause to the query.
@@ -35679,131 +35980,6 @@ var init_delete3 = __esm({
35679
35980
  }
35680
35981
  });
35681
35982
 
35682
- // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
35683
- var _a299, MySqlInsertBuilder, _a300, _b222, MySqlInsertBase;
35684
- var init_insert3 = __esm({
35685
- "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
35686
- "use strict";
35687
- init_entity();
35688
- init_query_promise();
35689
- init_sql();
35690
- init_table();
35691
- init_utils2();
35692
- _a299 = entityKind;
35693
- MySqlInsertBuilder = class {
35694
- constructor(table4, session, dialect4) {
35695
- __publicField(this, "shouldIgnore", false);
35696
- this.table = table4;
35697
- this.session = session;
35698
- this.dialect = dialect4;
35699
- }
35700
- ignore() {
35701
- this.shouldIgnore = true;
35702
- return this;
35703
- }
35704
- values(values) {
35705
- values = Array.isArray(values) ? values : [values];
35706
- if (values.length === 0) {
35707
- throw new Error("values() must be called with at least one value");
35708
- }
35709
- const mappedValues = values.map((entry) => {
35710
- const result = {};
35711
- const cols = this.table[Table2.Symbol.Columns];
35712
- for (const colKey of Object.keys(entry)) {
35713
- const colValue = entry[colKey];
35714
- result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
35715
- }
35716
- return result;
35717
- });
35718
- return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
35719
- }
35720
- };
35721
- __publicField(MySqlInsertBuilder, _a299, "MySqlInsertBuilder");
35722
- MySqlInsertBase = class extends (_b222 = QueryPromise, _a300 = entityKind, _b222) {
35723
- constructor(table4, values, ignore, session, dialect4) {
35724
- super();
35725
- __publicField(this, "config");
35726
- __publicField(this, "execute", (placeholderValues) => {
35727
- return this.prepare().execute(placeholderValues);
35728
- });
35729
- __publicField(this, "createIterator", () => {
35730
- const self2 = this;
35731
- return async function* (placeholderValues) {
35732
- yield* self2.prepare().iterator(placeholderValues);
35733
- };
35734
- });
35735
- __publicField(this, "iterator", this.createIterator());
35736
- this.session = session;
35737
- this.dialect = dialect4;
35738
- this.config = { table: table4, values, ignore };
35739
- }
35740
- /**
35741
- * Adds an `on duplicate key update` clause to the query.
35742
- *
35743
- * Calling this method will update update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
35744
- *
35745
- * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
35746
- *
35747
- * @param config The `set` clause
35748
- *
35749
- * @example
35750
- * ```ts
35751
- * await db.insert(cars)
35752
- * .values({ id: 1, brand: 'BMW'})
35753
- * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
35754
- * ```
35755
- *
35756
- * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
35757
- *
35758
- * ```ts
35759
- * import { sql } from 'drizzle-orm';
35760
- *
35761
- * await db.insert(cars)
35762
- * .values({ id: 1, brand: 'BMW' })
35763
- * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
35764
- * ```
35765
- */
35766
- onDuplicateKeyUpdate(config) {
35767
- const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
35768
- this.config.onConflict = sql`update ${setSql}`;
35769
- return this;
35770
- }
35771
- $returningId() {
35772
- const returning = [];
35773
- for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
35774
- if (value.primary) {
35775
- returning.push({ field: value, path: [key] });
35776
- }
35777
- }
35778
- this.config.returning = orderSelectedFields(this.config.table[Table2.Symbol.Columns]);
35779
- return this;
35780
- }
35781
- /** @internal */
35782
- getSQL() {
35783
- return this.dialect.buildInsertQuery(this.config).sql;
35784
- }
35785
- toSQL() {
35786
- const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
35787
- return rest;
35788
- }
35789
- prepare() {
35790
- const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
35791
- return this.session.prepareQuery(
35792
- this.dialect.sqlToQuery(sql2),
35793
- void 0,
35794
- void 0,
35795
- generatedIds,
35796
- this.config.returning
35797
- );
35798
- }
35799
- $dynamic() {
35800
- return this;
35801
- }
35802
- };
35803
- __publicField(MySqlInsertBase, _a300, "MySqlInsert");
35804
- }
35805
- });
35806
-
35807
35983
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35808
35984
  function getMySqlColumnBuilders() {
35809
35985
  return {
@@ -35884,7 +36060,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35884
36060
  }
35885
36061
  return table4;
35886
36062
  }
35887
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
36063
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35888
36064
  var init_table4 = __esm({
35889
36065
  "../drizzle-orm/dist/mysql-core/table.js"() {
35890
36066
  "use strict";
@@ -35892,15 +36068,15 @@ var init_table4 = __esm({
35892
36068
  init_table();
35893
36069
  init_all3();
35894
36070
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35895
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
36071
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35896
36072
  constructor() {
35897
36073
  super(...arguments);
35898
36074
  /** @internal */
35899
36075
  __publicField(this, _c9);
35900
36076
  /** @internal */
35901
- __publicField(this, _b223, []);
36077
+ __publicField(this, _b222, []);
35902
36078
  /** @internal */
35903
- __publicField(this, _a301);
36079
+ __publicField(this, _a299);
35904
36080
  }
35905
36081
  };
35906
36082
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35915,20 +36091,20 @@ var init_table4 = __esm({
35915
36091
  });
35916
36092
 
35917
36093
  // ../drizzle-orm/dist/mysql-core/view-base.js
35918
- var _a302, _b224, MySqlViewBase;
36094
+ var _a300, _b223, MySqlViewBase;
35919
36095
  var init_view_base3 = __esm({
35920
36096
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35921
36097
  "use strict";
35922
36098
  init_entity();
35923
36099
  init_sql();
35924
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
36100
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35925
36101
  };
35926
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
36102
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35927
36103
  }
35928
36104
  });
35929
36105
 
35930
36106
  // ../drizzle-orm/dist/mysql-core/dialect.js
35931
- var _a303, MySqlDialect;
36107
+ var _a301, MySqlDialect;
35932
36108
  var init_dialect3 = __esm({
35933
36109
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35934
36110
  "use strict";
@@ -35947,7 +36123,7 @@ var init_dialect3 = __esm({
35947
36123
  init_common4();
35948
36124
  init_table4();
35949
36125
  init_view_base3();
35950
- _a303 = entityKind;
36126
+ _a301 = entityKind;
35951
36127
  MySqlDialect = class {
35952
36128
  constructor(config) {
35953
36129
  /** @internal */
@@ -36230,7 +36406,7 @@ var init_dialect3 = __esm({
36230
36406
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36231
36407
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36232
36408
  }
36233
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36409
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36234
36410
  const valuesSqlList = [];
36235
36411
  const columns = table4[Table2.Symbol.Columns];
36236
36412
  const colEntries = Object.entries(columns).filter(
@@ -36238,42 +36414,53 @@ var init_dialect3 = __esm({
36238
36414
  );
36239
36415
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36240
36416
  const generatedIdsResponse = [];
36241
- for (const [valueIndex, value] of values.entries()) {
36242
- const generatedIds = {};
36243
- const valueList = [];
36244
- for (const [fieldName, col] of colEntries) {
36245
- const colValue = value[fieldName];
36246
- if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36247
- if (col.defaultFn !== void 0) {
36248
- const defaultFnResult = col.defaultFn();
36249
- generatedIds[fieldName] = defaultFnResult;
36250
- const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36251
- valueList.push(defaultValue);
36252
- } else if (!col.default && col.onUpdateFn !== void 0) {
36253
- const onUpdateFnResult = col.onUpdateFn();
36254
- const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36255
- valueList.push(newValue);
36417
+ if (select) {
36418
+ const select2 = valuesOrSelect;
36419
+ if (is(select2, SQL)) {
36420
+ valuesSqlList.push(select2);
36421
+ } else {
36422
+ valuesSqlList.push(select2.getSQL());
36423
+ }
36424
+ } else {
36425
+ const values = valuesOrSelect;
36426
+ valuesSqlList.push(sql.raw("values "));
36427
+ for (const [valueIndex, value] of values.entries()) {
36428
+ const generatedIds = {};
36429
+ const valueList = [];
36430
+ for (const [fieldName, col] of colEntries) {
36431
+ const colValue = value[fieldName];
36432
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36433
+ if (col.defaultFn !== void 0) {
36434
+ const defaultFnResult = col.defaultFn();
36435
+ generatedIds[fieldName] = defaultFnResult;
36436
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36437
+ valueList.push(defaultValue);
36438
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36439
+ const onUpdateFnResult = col.onUpdateFn();
36440
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36441
+ valueList.push(newValue);
36442
+ } else {
36443
+ valueList.push(sql`default`);
36444
+ }
36256
36445
  } else {
36257
- valueList.push(sql`default`);
36258
- }
36259
- } else {
36260
- if (col.defaultFn && is(colValue, Param)) {
36261
- generatedIds[fieldName] = colValue.value;
36446
+ if (col.defaultFn && is(colValue, Param)) {
36447
+ generatedIds[fieldName] = colValue.value;
36448
+ }
36449
+ valueList.push(colValue);
36262
36450
  }
36263
- valueList.push(colValue);
36264
36451
  }
36265
- }
36266
- generatedIdsResponse.push(generatedIds);
36267
- valuesSqlList.push(valueList);
36268
- if (valueIndex < values.length - 1) {
36269
- valuesSqlList.push(sql`, `);
36452
+ generatedIdsResponse.push(generatedIds);
36453
+ valuesSqlList.push(valueList);
36454
+ if (valueIndex < values.length - 1) {
36455
+ valuesSqlList.push(sql`, `);
36456
+ }
36270
36457
  }
36271
36458
  }
36272
36459
  const valuesSql = sql.join(valuesSqlList);
36273
36460
  const ignoreSql = ignore ? sql` ignore` : void 0;
36274
36461
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36275
36462
  return {
36276
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36463
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36277
36464
  generatedIds: generatedIdsResponse
36278
36465
  };
36279
36466
  }
@@ -36733,7 +36920,7 @@ var init_dialect3 = __esm({
36733
36920
  };
36734
36921
  }
36735
36922
  };
36736
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36923
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36737
36924
  }
36738
36925
  });
36739
36926
 
@@ -36755,7 +36942,7 @@ function createSetOperator3(type, isAll) {
36755
36942
  return leftSelect.addSetOperators(setOperators);
36756
36943
  };
36757
36944
  }
36758
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36945
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36759
36946
  var init_select4 = __esm({
36760
36947
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36761
36948
  "use strict";
@@ -36770,7 +36957,7 @@ var init_select4 = __esm({
36770
36957
  init_utils2();
36771
36958
  init_view_common();
36772
36959
  init_view_base3();
36773
- _a304 = entityKind;
36960
+ _a302 = entityKind;
36774
36961
  MySqlSelectBuilder = class {
36775
36962
  constructor(config) {
36776
36963
  __publicField(this, "fields");
@@ -36815,8 +37002,8 @@ var init_select4 = __esm({
36815
37002
  );
36816
37003
  }
36817
37004
  };
36818
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36819
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
37005
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
37006
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36820
37007
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36821
37008
  super();
36822
37009
  __publicField(this, "_");
@@ -37417,8 +37604,8 @@ var init_select4 = __esm({
37417
37604
  return this;
37418
37605
  }
37419
37606
  };
37420
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37421
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37607
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37608
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37422
37609
  constructor() {
37423
37610
  super(...arguments);
37424
37611
  __publicField(this, "execute", (placeholderValues) => {
@@ -37442,7 +37629,7 @@ var init_select4 = __esm({
37442
37629
  return query;
37443
37630
  }
37444
37631
  };
37445
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37632
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37446
37633
  applyMixins(MySqlSelectBase, [QueryPromise]);
37447
37634
  getMySqlSetOperators = () => ({
37448
37635
  union: union3,
@@ -37462,7 +37649,7 @@ var init_select4 = __esm({
37462
37649
  });
37463
37650
 
37464
37651
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37465
- var _a307, QueryBuilder3;
37652
+ var _a305, QueryBuilder3;
37466
37653
  var init_query_builder4 = __esm({
37467
37654
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37468
37655
  "use strict";
@@ -37471,7 +37658,7 @@ var init_query_builder4 = __esm({
37471
37658
  init_selection_proxy();
37472
37659
  init_subquery();
37473
37660
  init_select4();
37474
- _a307 = entityKind;
37661
+ _a305 = entityKind;
37475
37662
  QueryBuilder3 = class {
37476
37663
  constructor(dialect4) {
37477
37664
  __publicField(this, "dialect");
@@ -37533,7 +37720,142 @@ var init_query_builder4 = __esm({
37533
37720
  return this.dialect;
37534
37721
  }
37535
37722
  };
37536
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37723
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37724
+ }
37725
+ });
37726
+
37727
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37728
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37729
+ var init_insert3 = __esm({
37730
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37731
+ "use strict";
37732
+ init_entity();
37733
+ init_query_promise();
37734
+ init_sql();
37735
+ init_table();
37736
+ init_utils2();
37737
+ init_query_builder4();
37738
+ _a306 = entityKind;
37739
+ MySqlInsertBuilder = class {
37740
+ constructor(table4, session, dialect4) {
37741
+ __publicField(this, "shouldIgnore", false);
37742
+ this.table = table4;
37743
+ this.session = session;
37744
+ this.dialect = dialect4;
37745
+ }
37746
+ ignore() {
37747
+ this.shouldIgnore = true;
37748
+ return this;
37749
+ }
37750
+ values(values) {
37751
+ values = Array.isArray(values) ? values : [values];
37752
+ if (values.length === 0) {
37753
+ throw new Error("values() must be called with at least one value");
37754
+ }
37755
+ const mappedValues = values.map((entry) => {
37756
+ const result = {};
37757
+ const cols = this.table[Table2.Symbol.Columns];
37758
+ for (const colKey of Object.keys(entry)) {
37759
+ const colValue = entry[colKey];
37760
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37761
+ }
37762
+ return result;
37763
+ });
37764
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37765
+ }
37766
+ select(selectQuery) {
37767
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37768
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37769
+ throw new Error(
37770
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37771
+ );
37772
+ }
37773
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37774
+ }
37775
+ };
37776
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37777
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37778
+ constructor(table4, values, ignore, session, dialect4, select) {
37779
+ super();
37780
+ __publicField(this, "config");
37781
+ __publicField(this, "execute", (placeholderValues) => {
37782
+ return this.prepare().execute(placeholderValues);
37783
+ });
37784
+ __publicField(this, "createIterator", () => {
37785
+ const self2 = this;
37786
+ return async function* (placeholderValues) {
37787
+ yield* self2.prepare().iterator(placeholderValues);
37788
+ };
37789
+ });
37790
+ __publicField(this, "iterator", this.createIterator());
37791
+ this.session = session;
37792
+ this.dialect = dialect4;
37793
+ this.config = { table: table4, values, select, ignore };
37794
+ }
37795
+ /**
37796
+ * Adds an `on duplicate key update` clause to the query.
37797
+ *
37798
+ * 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.
37799
+ *
37800
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37801
+ *
37802
+ * @param config The `set` clause
37803
+ *
37804
+ * @example
37805
+ * ```ts
37806
+ * await db.insert(cars)
37807
+ * .values({ id: 1, brand: 'BMW'})
37808
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37809
+ * ```
37810
+ *
37811
+ * 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:
37812
+ *
37813
+ * ```ts
37814
+ * import { sql } from 'drizzle-orm';
37815
+ *
37816
+ * await db.insert(cars)
37817
+ * .values({ id: 1, brand: 'BMW' })
37818
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37819
+ * ```
37820
+ */
37821
+ onDuplicateKeyUpdate(config) {
37822
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37823
+ this.config.onConflict = sql`update ${setSql}`;
37824
+ return this;
37825
+ }
37826
+ $returningId() {
37827
+ const returning = [];
37828
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37829
+ if (value.primary) {
37830
+ returning.push({ field: value, path: [key] });
37831
+ }
37832
+ }
37833
+ this.config.returning = returning;
37834
+ return this;
37835
+ }
37836
+ /** @internal */
37837
+ getSQL() {
37838
+ return this.dialect.buildInsertQuery(this.config).sql;
37839
+ }
37840
+ toSQL() {
37841
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37842
+ return rest;
37843
+ }
37844
+ prepare() {
37845
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37846
+ return this.session.prepareQuery(
37847
+ this.dialect.sqlToQuery(sql2),
37848
+ void 0,
37849
+ void 0,
37850
+ generatedIds,
37851
+ this.config.returning
37852
+ );
37853
+ }
37854
+ $dynamic() {
37855
+ return this;
37856
+ }
37857
+ };
37858
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37537
37859
  }
37538
37860
  });
37539
37861