drizzle-kit 0.28.0 → 0.28.1-442f74d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +7 -7
  2. package/api.js +1016 -714
  3. package/api.mjs +1016 -714
  4. package/bin.cjs +8 -8
  5. package/package.json +1 -1
package/api.mjs CHANGED
@@ -21126,7 +21126,7 @@ var version;
21126
21126
  var init_version = __esm({
21127
21127
  "../drizzle-orm/dist/version.js"() {
21128
21128
  "use strict";
21129
- version = "0.36.1";
21129
+ version = "0.36.3";
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,54 @@ 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 }) {
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
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
25242
25119
  }
25243
25120
  buildRefreshMaterializedViewQuery({ view: view4, concurrently, withNoData }) {
25244
25121
  const concurrentlySql = concurrently ? sql` concurrently` : void 0;
@@ -25974,12 +25851,12 @@ var init_dialect = __esm({
25974
25851
  };
25975
25852
  }
25976
25853
  };
25977
- __publicField(PgDialect, _a129, "PgDialect");
25854
+ __publicField(PgDialect, _a127, "PgDialect");
25978
25855
  }
25979
25856
  });
25980
25857
 
25981
25858
  // ../drizzle-orm/dist/selection-proxy.js
25982
- var _a130, _SelectionProxyHandler, SelectionProxyHandler;
25859
+ var _a128, _SelectionProxyHandler, SelectionProxyHandler;
25983
25860
  var init_selection_proxy = __esm({
25984
25861
  "../drizzle-orm/dist/selection-proxy.js"() {
25985
25862
  "use strict";
@@ -25989,7 +25866,7 @@ var init_selection_proxy = __esm({
25989
25866
  init_sql();
25990
25867
  init_subquery();
25991
25868
  init_view_common();
25992
- _a130 = entityKind;
25869
+ _a128 = entityKind;
25993
25870
  _SelectionProxyHandler = class _SelectionProxyHandler {
25994
25871
  constructor(config) {
25995
25872
  __publicField(this, "config");
@@ -26055,25 +25932,25 @@ var init_selection_proxy = __esm({
26055
25932
  return new Proxy(value, new _SelectionProxyHandler(this.config));
26056
25933
  }
26057
25934
  };
26058
- __publicField(_SelectionProxyHandler, _a130, "SelectionProxyHandler");
25935
+ __publicField(_SelectionProxyHandler, _a128, "SelectionProxyHandler");
26059
25936
  SelectionProxyHandler = _SelectionProxyHandler;
26060
25937
  }
26061
25938
  });
26062
25939
 
26063
25940
  // ../drizzle-orm/dist/query-builders/query-builder.js
26064
- var _a131, TypedQueryBuilder;
25941
+ var _a129, TypedQueryBuilder;
26065
25942
  var init_query_builder = __esm({
26066
25943
  "../drizzle-orm/dist/query-builders/query-builder.js"() {
26067
25944
  "use strict";
26068
25945
  init_entity();
26069
- _a131 = entityKind;
25946
+ _a129 = entityKind;
26070
25947
  TypedQueryBuilder = class {
26071
25948
  /** @internal */
26072
25949
  getSelectedFields() {
26073
25950
  return this._.selectedFields;
26074
25951
  }
26075
25952
  };
26076
- __publicField(TypedQueryBuilder, _a131, "TypedQueryBuilder");
25953
+ __publicField(TypedQueryBuilder, _a129, "TypedQueryBuilder");
26077
25954
  }
26078
25955
  });
26079
25956
 
@@ -26095,7 +25972,7 @@ function createSetOperator(type, isAll) {
26095
25972
  return leftSelect.addSetOperators(setOperators);
26096
25973
  };
26097
25974
  }
26098
- var _a132, PgSelectBuilder, _a133, _b100, PgSelectQueryBuilderBase, _a134, _b101, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
25975
+ var _a130, PgSelectBuilder, _a131, _b99, PgSelectQueryBuilderBase, _a132, _b100, PgSelectBase, getPgSetOperators, union, unionAll, intersect, intersectAll, except, exceptAll;
26099
25976
  var init_select2 = __esm({
26100
25977
  "../drizzle-orm/dist/pg-core/query-builders/select.js"() {
26101
25978
  "use strict";
@@ -26111,7 +25988,7 @@ var init_select2 = __esm({
26111
25988
  init_utils2();
26112
25989
  init_utils2();
26113
25990
  init_view_common();
26114
- _a132 = entityKind;
25991
+ _a130 = entityKind;
26115
25992
  PgSelectBuilder = class {
26116
25993
  constructor(config) {
26117
25994
  __publicField(this, "fields");
@@ -26119,6 +25996,7 @@ var init_select2 = __esm({
26119
25996
  __publicField(this, "dialect");
26120
25997
  __publicField(this, "withList", []);
26121
25998
  __publicField(this, "distinct");
25999
+ __publicField(this, "authToken");
26122
26000
  this.fields = config.fields;
26123
26001
  this.session = config.session;
26124
26002
  this.dialect = config.dialect;
@@ -26127,6 +26005,11 @@ var init_select2 = __esm({
26127
26005
  }
26128
26006
  this.distinct = config.distinct;
26129
26007
  }
26008
+ /** @internal */
26009
+ setToken(token) {
26010
+ this.authToken = token;
26011
+ return this;
26012
+ }
26130
26013
  /**
26131
26014
  * Specify the table, subquery, or other target that you're
26132
26015
  * building a select query against.
@@ -26149,7 +26032,7 @@ var init_select2 = __esm({
26149
26032
  } else {
26150
26033
  fields = getTableColumns(source);
26151
26034
  }
26152
- return new PgSelectBase({
26035
+ return this.authToken === void 0 ? new PgSelectBase({
26153
26036
  table: source,
26154
26037
  fields,
26155
26038
  isPartialSelect,
@@ -26157,11 +26040,19 @@ var init_select2 = __esm({
26157
26040
  dialect: this.dialect,
26158
26041
  withList: this.withList,
26159
26042
  distinct: this.distinct
26160
- });
26043
+ }) : new PgSelectBase({
26044
+ table: source,
26045
+ fields,
26046
+ isPartialSelect,
26047
+ session: this.session,
26048
+ dialect: this.dialect,
26049
+ withList: this.withList,
26050
+ distinct: this.distinct
26051
+ }).setToken(this.authToken);
26161
26052
  }
26162
26053
  };
26163
- __publicField(PgSelectBuilder, _a132, "PgSelectBuilder");
26164
- PgSelectQueryBuilderBase = class extends (_b100 = TypedQueryBuilder, _a133 = entityKind, _b100) {
26054
+ __publicField(PgSelectBuilder, _a130, "PgSelectBuilder");
26055
+ PgSelectQueryBuilderBase = class extends (_b99 = TypedQueryBuilder, _a131 = entityKind, _b99) {
26165
26056
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
26166
26057
  super();
26167
26058
  __publicField(this, "_");
@@ -26761,19 +26652,20 @@ var init_select2 = __esm({
26761
26652
  return this;
26762
26653
  }
26763
26654
  };
26764
- __publicField(PgSelectQueryBuilderBase, _a133, "PgSelectQueryBuilder");
26765
- PgSelectBase = class extends (_b101 = PgSelectQueryBuilderBase, _a134 = entityKind, _b101) {
26655
+ __publicField(PgSelectQueryBuilderBase, _a131, "PgSelectQueryBuilder");
26656
+ PgSelectBase = class extends (_b100 = PgSelectQueryBuilderBase, _a132 = entityKind, _b100) {
26766
26657
  constructor() {
26767
26658
  super(...arguments);
26659
+ __publicField(this, "authToken");
26768
26660
  __publicField(this, "execute", (placeholderValues) => {
26769
26661
  return tracer.startActiveSpan("drizzle.operation", () => {
26770
- return this._prepare().execute(placeholderValues);
26662
+ return this._prepare().execute(placeholderValues, this.authToken);
26771
26663
  });
26772
26664
  });
26773
26665
  }
26774
26666
  /** @internal */
26775
26667
  _prepare(name2) {
26776
- const { session, config, dialect: dialect4, joinsNotNullableMap } = this;
26668
+ const { session, config, dialect: dialect4, joinsNotNullableMap, authToken } = this;
26777
26669
  if (!session) {
26778
26670
  throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
26779
26671
  }
@@ -26781,7 +26673,7 @@ var init_select2 = __esm({
26781
26673
  const fieldsList = orderSelectedFields(config.fields);
26782
26674
  const query = session.prepareQuery(dialect4.sqlToQuery(this.getSQL()), fieldsList, name2, true);
26783
26675
  query.joinsNotNullableMap = joinsNotNullableMap;
26784
- return query;
26676
+ return authToken === void 0 ? query : query.setToken(authToken);
26785
26677
  });
26786
26678
  }
26787
26679
  /**
@@ -26794,8 +26686,13 @@ var init_select2 = __esm({
26794
26686
  prepare(name2) {
26795
26687
  return this._prepare(name2);
26796
26688
  }
26689
+ /** @internal */
26690
+ setToken(token) {
26691
+ this.authToken = token;
26692
+ return this;
26693
+ }
26797
26694
  };
26798
- __publicField(PgSelectBase, _a134, "PgSelect");
26695
+ __publicField(PgSelectBase, _a132, "PgSelect");
26799
26696
  applyMixins(PgSelectBase, [QueryPromise]);
26800
26697
  getPgSetOperators = () => ({
26801
26698
  union,
@@ -26815,7 +26712,7 @@ var init_select2 = __esm({
26815
26712
  });
26816
26713
 
26817
26714
  // ../drizzle-orm/dist/pg-core/query-builders/query-builder.js
26818
- var _a135, QueryBuilder;
26715
+ var _a133, QueryBuilder;
26819
26716
  var init_query_builder2 = __esm({
26820
26717
  "../drizzle-orm/dist/pg-core/query-builders/query-builder.js"() {
26821
26718
  "use strict";
@@ -26824,7 +26721,7 @@ var init_query_builder2 = __esm({
26824
26721
  init_selection_proxy();
26825
26722
  init_subquery();
26826
26723
  init_select2();
26827
- _a135 = entityKind;
26724
+ _a133 = entityKind;
26828
26725
  QueryBuilder = class {
26829
26726
  constructor(dialect4) {
26830
26727
  __publicField(this, "dialect");
@@ -26905,31 +26802,212 @@ var init_query_builder2 = __esm({
26905
26802
  return this.dialect;
26906
26803
  }
26907
26804
  };
26908
- __publicField(QueryBuilder, _a135, "PgQueryBuilder");
26805
+ __publicField(QueryBuilder, _a133, "PgQueryBuilder");
26909
26806
  }
26910
26807
  });
26911
26808
 
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"() {
26809
+ // ../drizzle-orm/dist/pg-core/query-builders/insert.js
26810
+ var _a134, PgInsertBuilder, _a135, _b101, PgInsertBase;
26811
+ var init_insert = __esm({
26812
+ "../drizzle-orm/dist/pg-core/query-builders/insert.js"() {
26916
26813
  "use strict";
26917
26814
  init_entity();
26918
26815
  init_query_promise();
26816
+ init_sql();
26817
+ init_table();
26919
26818
  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
- });
26929
- this.session = session;
26930
- this.dialect = dialect4;
26931
- this.config = { view: view4 };
26932
- }
26819
+ init_utils2();
26820
+ init_query_builder2();
26821
+ _a134 = entityKind;
26822
+ PgInsertBuilder = class {
26823
+ constructor(table4, session, dialect4, withList) {
26824
+ __publicField(this, "authToken");
26825
+ this.table = table4;
26826
+ this.session = session;
26827
+ this.dialect = dialect4;
26828
+ this.withList = withList;
26829
+ }
26830
+ /** @internal */
26831
+ setToken(token) {
26832
+ this.authToken = token;
26833
+ return this;
26834
+ }
26835
+ values(values) {
26836
+ values = Array.isArray(values) ? values : [values];
26837
+ if (values.length === 0) {
26838
+ throw new Error("values() must be called with at least one value");
26839
+ }
26840
+ const mappedValues = values.map((entry) => {
26841
+ const result = {};
26842
+ const cols = this.table[Table2.Symbol.Columns];
26843
+ for (const colKey of Object.keys(entry)) {
26844
+ const colValue = entry[colKey];
26845
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
26846
+ }
26847
+ return result;
26848
+ });
26849
+ return this.authToken === void 0 ? new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList) : new PgInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList).setToken(
26850
+ this.authToken
26851
+ );
26852
+ }
26853
+ select(selectQuery) {
26854
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
26855
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
26856
+ throw new Error(
26857
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
26858
+ );
26859
+ }
26860
+ return new PgInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
26861
+ }
26862
+ };
26863
+ __publicField(PgInsertBuilder, _a134, "PgInsertBuilder");
26864
+ PgInsertBase = class extends (_b101 = QueryPromise, _a135 = entityKind, _b101) {
26865
+ constructor(table4, values, session, dialect4, withList, select) {
26866
+ super();
26867
+ __publicField(this, "config");
26868
+ __publicField(this, "authToken");
26869
+ __publicField(this, "execute", (placeholderValues) => {
26870
+ return tracer.startActiveSpan("drizzle.operation", () => {
26871
+ return this._prepare().execute(placeholderValues, this.authToken);
26872
+ });
26873
+ });
26874
+ this.session = session;
26875
+ this.dialect = dialect4;
26876
+ this.config = { table: table4, values, withList, select };
26877
+ }
26878
+ returning(fields = this.config.table[Table2.Symbol.Columns]) {
26879
+ this.config.returning = orderSelectedFields(fields);
26880
+ return this;
26881
+ }
26882
+ /**
26883
+ * Adds an `on conflict do nothing` clause to the query.
26884
+ *
26885
+ * Calling this method simply avoids inserting a row as its alternative action.
26886
+ *
26887
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
26888
+ *
26889
+ * @param config The `target` and `where` clauses.
26890
+ *
26891
+ * @example
26892
+ * ```ts
26893
+ * // Insert one row and cancel the insert if there's a conflict
26894
+ * await db.insert(cars)
26895
+ * .values({ id: 1, brand: 'BMW' })
26896
+ * .onConflictDoNothing();
26897
+ *
26898
+ * // Explicitly specify conflict target
26899
+ * await db.insert(cars)
26900
+ * .values({ id: 1, brand: 'BMW' })
26901
+ * .onConflictDoNothing({ target: cars.id });
26902
+ * ```
26903
+ */
26904
+ onConflictDoNothing(config = {}) {
26905
+ if (config.target === void 0) {
26906
+ this.config.onConflict = sql`do nothing`;
26907
+ } else {
26908
+ let targetColumn = "";
26909
+ targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
26910
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26911
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${whereSql} do nothing`;
26912
+ }
26913
+ return this;
26914
+ }
26915
+ /**
26916
+ * Adds an `on conflict do update` clause to the query.
26917
+ *
26918
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
26919
+ *
26920
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
26921
+ *
26922
+ * @param config The `target`, `set` and `where` clauses.
26923
+ *
26924
+ * @example
26925
+ * ```ts
26926
+ * // Update the row if there's a conflict
26927
+ * await db.insert(cars)
26928
+ * .values({ id: 1, brand: 'BMW' })
26929
+ * .onConflictDoUpdate({
26930
+ * target: cars.id,
26931
+ * set: { brand: 'Porsche' }
26932
+ * });
26933
+ *
26934
+ * // Upsert with 'where' clause
26935
+ * await db.insert(cars)
26936
+ * .values({ id: 1, brand: 'BMW' })
26937
+ * .onConflictDoUpdate({
26938
+ * target: cars.id,
26939
+ * set: { brand: 'newBMW' },
26940
+ * targetWhere: sql`${cars.createdAt} > '2023-01-01'::date`,
26941
+ * });
26942
+ * ```
26943
+ */
26944
+ onConflictDoUpdate(config) {
26945
+ if (config.where && (config.targetWhere || config.setWhere)) {
26946
+ throw new Error(
26947
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
26948
+ );
26949
+ }
26950
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
26951
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
26952
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
26953
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
26954
+ let targetColumn = "";
26955
+ targetColumn = Array.isArray(config.target) ? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(",") : this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
26956
+ this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
26957
+ return this;
26958
+ }
26959
+ /** @internal */
26960
+ getSQL() {
26961
+ return this.dialect.buildInsertQuery(this.config);
26962
+ }
26963
+ toSQL() {
26964
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
26965
+ return rest;
26966
+ }
26967
+ /** @internal */
26968
+ _prepare(name2) {
26969
+ return tracer.startActiveSpan("drizzle.prepareQuery", () => {
26970
+ return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
26971
+ });
26972
+ }
26973
+ prepare(name2) {
26974
+ return this._prepare(name2);
26975
+ }
26976
+ /** @internal */
26977
+ setToken(token) {
26978
+ this.authToken = token;
26979
+ return this;
26980
+ }
26981
+ $dynamic() {
26982
+ return this;
26983
+ }
26984
+ };
26985
+ __publicField(PgInsertBase, _a135, "PgInsert");
26986
+ }
26987
+ });
26988
+
26989
+ // ../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js
26990
+ var _a136, _b102, PgRefreshMaterializedView;
26991
+ var init_refresh_materialized_view = __esm({
26992
+ "../drizzle-orm/dist/pg-core/query-builders/refresh-materialized-view.js"() {
26993
+ "use strict";
26994
+ init_entity();
26995
+ init_query_promise();
26996
+ init_tracing();
26997
+ PgRefreshMaterializedView = class extends (_b102 = QueryPromise, _a136 = entityKind, _b102) {
26998
+ constructor(view4, session, dialect4) {
26999
+ super();
27000
+ __publicField(this, "config");
27001
+ __publicField(this, "authToken");
27002
+ __publicField(this, "execute", (placeholderValues) => {
27003
+ return tracer.startActiveSpan("drizzle.operation", () => {
27004
+ return this._prepare().execute(placeholderValues, this.authToken);
27005
+ });
27006
+ });
27007
+ this.session = session;
27008
+ this.dialect = dialect4;
27009
+ this.config = { view: view4 };
27010
+ }
26933
27011
  concurrently() {
26934
27012
  if (this.config.withNoData !== void 0) {
26935
27013
  throw new Error("Cannot use concurrently and withNoData together");
@@ -26961,6 +27039,11 @@ var init_refresh_materialized_view = __esm({
26961
27039
  prepare(name2) {
26962
27040
  return this._prepare(name2);
26963
27041
  }
27042
+ /** @internal */
27043
+ setToken(token) {
27044
+ this.authToken = token;
27045
+ return this;
27046
+ }
26964
27047
  };
26965
27048
  __publicField(PgRefreshMaterializedView, _a136, "PgRefreshMaterializedView");
26966
27049
  }
@@ -26979,25 +27062,41 @@ var init_update = __esm({
26979
27062
  "../drizzle-orm/dist/pg-core/query-builders/update.js"() {
26980
27063
  "use strict";
26981
27064
  init_entity();
27065
+ init_table2();
26982
27066
  init_query_promise();
27067
+ init_selection_proxy();
27068
+ init_sql();
27069
+ init_subquery();
26983
27070
  init_table();
26984
27071
  init_utils2();
27072
+ init_view_common();
26985
27073
  _a137 = entityKind;
26986
27074
  PgUpdateBuilder = class {
26987
27075
  constructor(table4, session, dialect4, withList) {
27076
+ __publicField(this, "authToken");
26988
27077
  this.table = table4;
26989
27078
  this.session = session;
26990
27079
  this.dialect = dialect4;
26991
27080
  this.withList = withList;
26992
27081
  }
27082
+ setToken(token) {
27083
+ this.authToken = token;
27084
+ return this;
27085
+ }
26993
27086
  set(values) {
26994
- return new PgUpdateBase(
27087
+ return this.authToken === void 0 ? new PgUpdateBase(
26995
27088
  this.table,
26996
27089
  mapUpdateSet(this.table, values),
26997
27090
  this.session,
26998
27091
  this.dialect,
26999
27092
  this.withList
27000
- );
27093
+ ) : new PgUpdateBase(
27094
+ this.table,
27095
+ mapUpdateSet(this.table, values),
27096
+ this.session,
27097
+ this.dialect,
27098
+ this.withList
27099
+ ).setToken(this.authToken);
27001
27100
  }
27002
27101
  };
27003
27102
  __publicField(PgUpdateBuilder, _a137, "PgUpdateBuilder");
@@ -27005,12 +27104,86 @@ var init_update = __esm({
27005
27104
  constructor(table4, set, session, dialect4, withList) {
27006
27105
  super();
27007
27106
  __publicField(this, "config");
27107
+ __publicField(this, "tableName");
27108
+ __publicField(this, "joinsNotNullableMap");
27109
+ __publicField(this, "leftJoin", this.createJoin("left"));
27110
+ __publicField(this, "rightJoin", this.createJoin("right"));
27111
+ __publicField(this, "innerJoin", this.createJoin("inner"));
27112
+ __publicField(this, "fullJoin", this.createJoin("full"));
27113
+ __publicField(this, "authToken");
27008
27114
  __publicField(this, "execute", (placeholderValues) => {
27009
- return this._prepare().execute(placeholderValues);
27115
+ return this._prepare().execute(placeholderValues, this.authToken);
27010
27116
  });
27011
27117
  this.session = session;
27012
27118
  this.dialect = dialect4;
27013
- this.config = { set, table: table4, withList };
27119
+ this.config = { set, table: table4, withList, joins: [] };
27120
+ this.tableName = getTableLikeName(table4);
27121
+ this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
27122
+ }
27123
+ from(source) {
27124
+ const tableName = getTableLikeName(source);
27125
+ if (typeof tableName === "string") {
27126
+ this.joinsNotNullableMap[tableName] = true;
27127
+ }
27128
+ this.config.from = source;
27129
+ return this;
27130
+ }
27131
+ getTableLikeFields(table4) {
27132
+ if (is(table4, PgTable)) {
27133
+ return table4[Table2.Symbol.Columns];
27134
+ } else if (is(table4, Subquery)) {
27135
+ return table4._.selectedFields;
27136
+ }
27137
+ return table4[ViewBaseConfig].selectedFields;
27138
+ }
27139
+ createJoin(joinType) {
27140
+ return (table4, on) => {
27141
+ const tableName = getTableLikeName(table4);
27142
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
27143
+ throw new Error(`Alias "${tableName}" is already used in this query`);
27144
+ }
27145
+ if (typeof on === "function") {
27146
+ const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
27147
+ on = on(
27148
+ new Proxy(
27149
+ this.config.table[Table2.Symbol.Columns],
27150
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27151
+ ),
27152
+ from && new Proxy(
27153
+ from,
27154
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
27155
+ )
27156
+ );
27157
+ }
27158
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
27159
+ if (typeof tableName === "string") {
27160
+ switch (joinType) {
27161
+ case "left": {
27162
+ this.joinsNotNullableMap[tableName] = false;
27163
+ break;
27164
+ }
27165
+ case "right": {
27166
+ this.joinsNotNullableMap = Object.fromEntries(
27167
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27168
+ );
27169
+ this.joinsNotNullableMap[tableName] = true;
27170
+ break;
27171
+ }
27172
+ case "inner": {
27173
+ this.joinsNotNullableMap[tableName] = true;
27174
+ break;
27175
+ }
27176
+ case "full": {
27177
+ this.joinsNotNullableMap = Object.fromEntries(
27178
+ Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
27179
+ );
27180
+ this.joinsNotNullableMap[tableName] = false;
27181
+ break;
27182
+ }
27183
+ }
27184
+ }
27185
+ return this;
27186
+ };
27014
27187
  }
27015
27188
  /**
27016
27189
  * Adds a 'where' clause to the query.
@@ -27049,7 +27222,24 @@ var init_update = __esm({
27049
27222
  this.config.where = where;
27050
27223
  return this;
27051
27224
  }
27052
- returning(fields = this.config.table[Table2.Symbol.Columns]) {
27225
+ returning(fields) {
27226
+ if (!fields) {
27227
+ fields = Object.assign({}, this.config.table[Table2.Symbol.Columns]);
27228
+ if (this.config.from) {
27229
+ const tableName = getTableLikeName(this.config.from);
27230
+ if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
27231
+ const fromFields = this.getTableLikeFields(this.config.from);
27232
+ fields[tableName] = fromFields;
27233
+ }
27234
+ for (const join of this.config.joins) {
27235
+ const tableName2 = getTableLikeName(join.table);
27236
+ if (typeof tableName2 === "string" && !is(join.table, SQL)) {
27237
+ const fromFields = this.getTableLikeFields(join.table);
27238
+ fields[tableName2] = fromFields;
27239
+ }
27240
+ }
27241
+ }
27242
+ }
27053
27243
  this.config.returning = orderSelectedFields(fields);
27054
27244
  return this;
27055
27245
  }
@@ -27063,11 +27253,18 @@ var init_update = __esm({
27063
27253
  }
27064
27254
  /** @internal */
27065
27255
  _prepare(name2) {
27066
- return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27256
+ const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name2, true);
27257
+ query.joinsNotNullableMap = this.joinsNotNullableMap;
27258
+ return query;
27067
27259
  }
27068
27260
  prepare(name2) {
27069
27261
  return this._prepare(name2);
27070
27262
  }
27263
+ /** @internal */
27264
+ setToken(token) {
27265
+ this.authToken = token;
27266
+ return this;
27267
+ }
27071
27268
  $dynamic() {
27072
27269
  return this;
27073
27270
  }
@@ -27101,6 +27298,7 @@ var init_count = __esm({
27101
27298
  constructor(params) {
27102
27299
  super(_PgCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
27103
27300
  __publicField(this, "sql");
27301
+ __publicField(this, "token");
27104
27302
  __publicField(this, _a139, "PgCountBuilder");
27105
27303
  __publicField(this, "session");
27106
27304
  this.params = params;
@@ -27117,8 +27315,12 @@ var init_count = __esm({
27117
27315
  static buildCount(source, filters) {
27118
27316
  return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters};`;
27119
27317
  }
27318
+ /** @intrnal */
27319
+ setToken(token) {
27320
+ this.token = token;
27321
+ }
27120
27322
  then(onfulfilled, onrejected) {
27121
- return Promise.resolve(this.session.count(this.sql)).then(
27323
+ return Promise.resolve(this.session.count(this.sql, this.token)).then(
27122
27324
  onfulfilled,
27123
27325
  onrejected
27124
27326
  );
@@ -27195,6 +27397,7 @@ var init_query = __esm({
27195
27397
  PgRelationalQuery = class extends (_b105 = QueryPromise, _a141 = entityKind, _b105) {
27196
27398
  constructor(fullSchema, schema4, tableNamesMap, table4, tableConfig, dialect4, session, config, mode) {
27197
27399
  super();
27400
+ __publicField(this, "authToken");
27198
27401
  this.fullSchema = fullSchema;
27199
27402
  this.schema = schema4;
27200
27403
  this.tableNamesMap = tableNamesMap;
@@ -27252,9 +27455,14 @@ var init_query = __esm({
27252
27455
  toSQL() {
27253
27456
  return this._toSQL().builtQuery;
27254
27457
  }
27458
+ /** @internal */
27459
+ setToken(token) {
27460
+ this.authToken = token;
27461
+ return this;
27462
+ }
27255
27463
  execute() {
27256
27464
  return tracer.startActiveSpan("drizzle.operation", () => {
27257
- return this._prepare().execute();
27465
+ return this._prepare().execute(void 0, this.authToken);
27258
27466
  });
27259
27467
  }
27260
27468
  };
@@ -27317,6 +27525,7 @@ var init_db = __esm({
27317
27525
  PgDatabase = class {
27318
27526
  constructor(dialect4, session, schema4) {
27319
27527
  __publicField(this, "query");
27528
+ __publicField(this, "authToken");
27320
27529
  this.dialect = dialect4;
27321
27530
  this.session = session;
27322
27531
  this._ = schema4 ? {
@@ -27572,7 +27781,7 @@ var init_db = __esm({
27572
27781
  false
27573
27782
  );
27574
27783
  return new PgRaw(
27575
- () => prepared.execute(),
27784
+ () => prepared.execute(void 0, this.authToken),
27576
27785
  sequel,
27577
27786
  builtQuery,
27578
27787
  (result) => prepared.mapResult(result, true)
@@ -28117,6 +28326,7 @@ var init_session = __esm({
28117
28326
  _a159 = entityKind;
28118
28327
  PgPreparedQuery = class {
28119
28328
  constructor(query) {
28329
+ __publicField(this, "authToken");
28120
28330
  /** @internal */
28121
28331
  __publicField(this, "joinsNotNullableMap");
28122
28332
  this.query = query;
@@ -28127,6 +28337,11 @@ var init_session = __esm({
28127
28337
  mapResult(response, _isFromBatch) {
28128
28338
  return response;
28129
28339
  }
28340
+ /** @internal */
28341
+ setToken(token) {
28342
+ this.authToken = token;
28343
+ return this;
28344
+ }
28130
28345
  };
28131
28346
  __publicField(PgPreparedQuery, _a159, "PgPreparedQuery");
28132
28347
  _a160 = entityKind;
@@ -28134,7 +28349,8 @@ var init_session = __esm({
28134
28349
  constructor(dialect4) {
28135
28350
  this.dialect = dialect4;
28136
28351
  }
28137
- execute(query) {
28352
+ /** @internal */
28353
+ execute(query, token) {
28138
28354
  return tracer.startActiveSpan("drizzle.operation", () => {
28139
28355
  const prepared = tracer.startActiveSpan("drizzle.prepareQuery", () => {
28140
28356
  return this.prepareQuery(
@@ -28144,7 +28360,7 @@ var init_session = __esm({
28144
28360
  false
28145
28361
  );
28146
28362
  });
28147
- return prepared.execute();
28363
+ return prepared.setToken(token).execute(void 0, token);
28148
28364
  });
28149
28365
  }
28150
28366
  all(query) {
@@ -28155,8 +28371,9 @@ var init_session = __esm({
28155
28371
  false
28156
28372
  ).all();
28157
28373
  }
28158
- async count(sql2) {
28159
- const res = await this.execute(sql2);
28374
+ /** @internal */
28375
+ async count(sql2, token) {
28376
+ const res = await this.execute(sql2, token);
28160
28377
  return Number(
28161
28378
  res[0]["count"]
28162
28379
  );
@@ -30790,180 +31007,12 @@ var init_delete2 = __esm({
30790
31007
  return this;
30791
31008
  }
30792
31009
  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}`;
31010
+ this.config.returning = orderSelectedFields(fields);
30962
31011
  return this;
30963
31012
  }
30964
31013
  /** @internal */
30965
31014
  getSQL() {
30966
- return this.dialect.buildInsertQuery(this.config);
31015
+ return this.dialect.buildDeleteQuery(this.config);
30967
31016
  }
30968
31017
  toSQL() {
30969
31018
  const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
@@ -30981,32 +31030,32 @@ var init_insert2 = __esm({
30981
31030
  prepare() {
30982
31031
  return this._prepare(false);
30983
31032
  }
30984
- async execute() {
30985
- return this.config.returning ? this.all() : this.run();
31033
+ async execute(placeholderValues) {
31034
+ return this._prepare().execute(placeholderValues);
30986
31035
  }
30987
31036
  $dynamic() {
30988
31037
  return this;
30989
31038
  }
30990
31039
  };
30991
- __publicField(SQLiteInsertBase, _a198, "SQLiteInsert");
31040
+ __publicField(SQLiteDeleteBase, _a196, "SQLiteDelete");
30992
31041
  }
30993
31042
  });
30994
31043
 
30995
31044
  // ../drizzle-orm/dist/sqlite-core/view-base.js
30996
- var _a199, _b143, SQLiteViewBase;
31045
+ var _a197, _b142, SQLiteViewBase;
30997
31046
  var init_view_base2 = __esm({
30998
31047
  "../drizzle-orm/dist/sqlite-core/view-base.js"() {
30999
31048
  "use strict";
31000
31049
  init_entity();
31001
31050
  init_sql();
31002
- SQLiteViewBase = class extends (_b143 = View3, _a199 = entityKind, _b143) {
31051
+ SQLiteViewBase = class extends (_b142 = View3, _a197 = entityKind, _b142) {
31003
31052
  };
31004
- __publicField(SQLiteViewBase, _a199, "SQLiteViewBase");
31053
+ __publicField(SQLiteViewBase, _a197, "SQLiteViewBase");
31005
31054
  }
31006
31055
  });
31007
31056
 
31008
31057
  // ../drizzle-orm/dist/sqlite-core/dialect.js
31009
- var _a200, SQLiteDialect, _a201, _b144, SQLiteSyncDialect, _a202, _b145, SQLiteAsyncDialect;
31058
+ var _a198, SQLiteDialect, _a199, _b143, SQLiteSyncDialect, _a200, _b144, SQLiteAsyncDialect;
31010
31059
  var init_dialect2 = __esm({
31011
31060
  "../drizzle-orm/dist/sqlite-core/dialect.js"() {
31012
31061
  "use strict";
@@ -31025,7 +31074,7 @@ var init_dialect2 = __esm({
31025
31074
  init_utils2();
31026
31075
  init_view_common();
31027
31076
  init_view_base2();
31028
- _a200 = entityKind;
31077
+ _a198 = entityKind;
31029
31078
  SQLiteDialect = class {
31030
31079
  constructor(config) {
31031
31080
  /** @internal */
@@ -31078,14 +31127,16 @@ var init_dialect2 = __esm({
31078
31127
  return [res];
31079
31128
  }));
31080
31129
  }
31081
- buildUpdateQuery({ table: table4, set, where, returning, withList, limit, orderBy }) {
31130
+ buildUpdateQuery({ table: table4, set, where, returning, withList, joins, from, limit, orderBy }) {
31082
31131
  const withSql = this.buildWithCTE(withList);
31083
31132
  const setSql = this.buildUpdateSet(table4, set);
31133
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
31134
+ const joinsSql = this.buildJoins(joins);
31084
31135
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31085
31136
  const whereSql = where ? sql` where ${where}` : void 0;
31086
31137
  const orderBySql = this.buildOrderBy(orderBy);
31087
31138
  const limitSql = this.buildLimit(limit);
31088
- return sql`${withSql}update ${table4} set ${setSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31139
+ return sql`${withSql}update ${table4} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}${orderBySql}${limitSql}`;
31089
31140
  }
31090
31141
  /**
31091
31142
  * Builds selection SQL with provided fields/expressions
@@ -31138,6 +31189,37 @@ var init_dialect2 = __esm({
31138
31189
  });
31139
31190
  return sql.join(chunks);
31140
31191
  }
31192
+ buildJoins(joins) {
31193
+ if (!joins || joins.length === 0) {
31194
+ return void 0;
31195
+ }
31196
+ const joinsArray = [];
31197
+ if (joins) {
31198
+ for (const [index4, joinMeta] of joins.entries()) {
31199
+ if (index4 === 0) {
31200
+ joinsArray.push(sql` `);
31201
+ }
31202
+ const table4 = joinMeta.table;
31203
+ if (is(table4, SQLiteTable)) {
31204
+ const tableName = table4[SQLiteTable.Symbol.Name];
31205
+ const tableSchema = table4[SQLiteTable.Symbol.Schema];
31206
+ const origTableName = table4[SQLiteTable.Symbol.OriginalName];
31207
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
31208
+ joinsArray.push(
31209
+ sql`${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
31210
+ );
31211
+ } else {
31212
+ joinsArray.push(
31213
+ sql`${sql.raw(joinMeta.joinType)} join ${table4} on ${joinMeta.on}`
31214
+ );
31215
+ }
31216
+ if (index4 < joins.length - 1) {
31217
+ joinsArray.push(sql` `);
31218
+ }
31219
+ }
31220
+ }
31221
+ return sql.join(joinsArray);
31222
+ }
31141
31223
  buildLimit(limit) {
31142
31224
  return typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
31143
31225
  }
@@ -31153,6 +31235,12 @@ var init_dialect2 = __esm({
31153
31235
  }
31154
31236
  return orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : void 0;
31155
31237
  }
31238
+ buildFromTable(table4) {
31239
+ if (is(table4, Table2) && table4[Table2.Symbol.OriginalName] !== table4[Table2.Symbol.Name]) {
31240
+ return sql`${sql.identifier(table4[Table2.Symbol.OriginalName])} ${sql.identifier(table4[Table2.Symbol.Name])}`;
31241
+ }
31242
+ return table4;
31243
+ }
31156
31244
  buildSelectQuery({
31157
31245
  withList,
31158
31246
  fields,
@@ -31183,38 +31271,8 @@ var init_dialect2 = __esm({
31183
31271
  const withSql = this.buildWithCTE(withList);
31184
31272
  const distinctSql = distinct ? sql` distinct` : void 0;
31185
31273
  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);
31274
+ const tableSql = this.buildFromTable(table4);
31275
+ const joinsSql = this.buildJoins(joins);
31218
31276
  const whereSql = where ? sql` where ${where}` : void 0;
31219
31277
  const havingSql = having ? sql` having ${having}` : void 0;
31220
31278
  const groupByList = [];
@@ -31280,45 +31338,56 @@ var init_dialect2 = __esm({
31280
31338
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
31281
31339
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
31282
31340
  }
31283
- buildInsertQuery({ table: table4, values, onConflict, returning, withList }) {
31341
+ buildInsertQuery({ table: table4, values: valuesOrSelect, onConflict, returning, withList, select }) {
31284
31342
  const valuesSqlList = [];
31285
31343
  const columns = table4[Table2.Symbol.Columns];
31286
31344
  const colEntries = Object.entries(columns).filter(
31287
31345
  ([_2, col]) => !col.shouldDisableInsert()
31288
31346
  );
31289
31347
  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);
31348
+ if (select) {
31349
+ const select2 = valuesOrSelect;
31350
+ if (is(select2, SQL)) {
31351
+ valuesSqlList.push(select2);
31352
+ } else {
31353
+ valuesSqlList.push(select2.getSQL());
31354
+ }
31355
+ } else {
31356
+ const values = valuesOrSelect;
31357
+ valuesSqlList.push(sql.raw("values "));
31358
+ for (const [valueIndex, value] of values.entries()) {
31359
+ const valueList = [];
31360
+ for (const [fieldName, col] of colEntries) {
31361
+ const colValue = value[fieldName];
31362
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
31363
+ let defaultValue;
31364
+ if (col.default !== null && col.default !== void 0) {
31365
+ defaultValue = is(col.default, SQL) ? col.default : sql.param(col.default, col);
31366
+ } else if (col.defaultFn !== void 0) {
31367
+ const defaultFnResult = col.defaultFn();
31368
+ defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
31369
+ } else if (!col.default && col.onUpdateFn !== void 0) {
31370
+ const onUpdateFnResult = col.onUpdateFn();
31371
+ defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
31372
+ } else {
31373
+ defaultValue = sql`null`;
31374
+ }
31375
+ valueList.push(defaultValue);
31304
31376
  } else {
31305
- defaultValue = sql`null`;
31377
+ valueList.push(colValue);
31306
31378
  }
31307
- valueList.push(defaultValue);
31308
- } else {
31309
- valueList.push(colValue);
31310
31379
  }
31311
- }
31312
- valuesSqlList.push(valueList);
31313
- if (valueIndex < values.length - 1) {
31314
- valuesSqlList.push(sql`, `);
31380
+ valuesSqlList.push(valueList);
31381
+ if (valueIndex < values.length - 1) {
31382
+ valuesSqlList.push(sql`, `);
31383
+ }
31315
31384
  }
31316
31385
  }
31317
31386
  const withSql = this.buildWithCTE(withList);
31318
31387
  const valuesSql = sql.join(valuesSqlList);
31319
31388
  const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
31320
31389
  const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
31321
- return sql`${withSql}insert into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
31390
+ return sql`${withSql}insert into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
31322
31391
  }
31323
31392
  sqlToQuery(sql2, invokeSource) {
31324
31393
  return sql2.toQuery({
@@ -31547,8 +31616,8 @@ var init_dialect2 = __esm({
31547
31616
  };
31548
31617
  }
31549
31618
  };
31550
- __publicField(SQLiteDialect, _a200, "SQLiteDialect");
31551
- SQLiteSyncDialect = class extends (_b144 = SQLiteDialect, _a201 = entityKind, _b144) {
31619
+ __publicField(SQLiteDialect, _a198, "SQLiteDialect");
31620
+ SQLiteSyncDialect = class extends (_b143 = SQLiteDialect, _a199 = entityKind, _b143) {
31552
31621
  migrate(migrations, session, config) {
31553
31622
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31554
31623
  const migrationTableCreate = sql`
@@ -31582,8 +31651,8 @@ var init_dialect2 = __esm({
31582
31651
  }
31583
31652
  }
31584
31653
  };
31585
- __publicField(SQLiteSyncDialect, _a201, "SQLiteSyncDialect");
31586
- SQLiteAsyncDialect = class extends (_b145 = SQLiteDialect, _a202 = entityKind, _b145) {
31654
+ __publicField(SQLiteSyncDialect, _a199, "SQLiteSyncDialect");
31655
+ SQLiteAsyncDialect = class extends (_b144 = SQLiteDialect, _a200 = entityKind, _b144) {
31587
31656
  async migrate(migrations, session, config) {
31588
31657
  const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
31589
31658
  const migrationTableCreate = sql`
@@ -31612,7 +31681,7 @@ var init_dialect2 = __esm({
31612
31681
  });
31613
31682
  }
31614
31683
  };
31615
- __publicField(SQLiteAsyncDialect, _a202, "SQLiteAsyncDialect");
31684
+ __publicField(SQLiteAsyncDialect, _a200, "SQLiteAsyncDialect");
31616
31685
  }
31617
31686
  });
31618
31687
 
@@ -31634,7 +31703,7 @@ function createSetOperator2(type, isAll) {
31634
31703
  return leftSelect.addSetOperators(setOperators);
31635
31704
  };
31636
31705
  }
31637
- var _a203, SQLiteSelectBuilder, _a204, _b146, SQLiteSelectQueryBuilderBase, _a205, _b147, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31706
+ var _a201, SQLiteSelectBuilder, _a202, _b145, SQLiteSelectQueryBuilderBase, _a203, _b146, SQLiteSelectBase, getSQLiteSetOperators, union2, unionAll2, intersect2, except2;
31638
31707
  var init_select3 = __esm({
31639
31708
  "../drizzle-orm/dist/sqlite-core/query-builders/select.js"() {
31640
31709
  "use strict";
@@ -31648,7 +31717,7 @@ var init_select3 = __esm({
31648
31717
  init_utils2();
31649
31718
  init_view_common();
31650
31719
  init_view_base2();
31651
- _a203 = entityKind;
31720
+ _a201 = entityKind;
31652
31721
  SQLiteSelectBuilder = class {
31653
31722
  constructor(config) {
31654
31723
  __publicField(this, "fields");
@@ -31689,8 +31758,8 @@ var init_select3 = __esm({
31689
31758
  });
31690
31759
  }
31691
31760
  };
31692
- __publicField(SQLiteSelectBuilder, _a203, "SQLiteSelectBuilder");
31693
- SQLiteSelectQueryBuilderBase = class extends (_b146 = TypedQueryBuilder, _a204 = entityKind, _b146) {
31761
+ __publicField(SQLiteSelectBuilder, _a201, "SQLiteSelectBuilder");
31762
+ SQLiteSelectQueryBuilderBase = class extends (_b145 = TypedQueryBuilder, _a202 = entityKind, _b145) {
31694
31763
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
31695
31764
  super();
31696
31765
  __publicField(this, "_");
@@ -32195,8 +32264,8 @@ var init_select3 = __esm({
32195
32264
  return this;
32196
32265
  }
32197
32266
  };
32198
- __publicField(SQLiteSelectQueryBuilderBase, _a204, "SQLiteSelectQueryBuilder");
32199
- SQLiteSelectBase = class extends (_b147 = SQLiteSelectQueryBuilderBase, _a205 = entityKind, _b147) {
32267
+ __publicField(SQLiteSelectQueryBuilderBase, _a202, "SQLiteSelectQueryBuilder");
32268
+ SQLiteSelectBase = class extends (_b146 = SQLiteSelectQueryBuilderBase, _a203 = entityKind, _b146) {
32200
32269
  constructor() {
32201
32270
  super(...arguments);
32202
32271
  __publicField(this, "run", (placeholderValues) => {
@@ -32234,7 +32303,7 @@ var init_select3 = __esm({
32234
32303
  return this.all();
32235
32304
  }
32236
32305
  };
32237
- __publicField(SQLiteSelectBase, _a205, "SQLiteSelect");
32306
+ __publicField(SQLiteSelectBase, _a203, "SQLiteSelect");
32238
32307
  applyMixins(SQLiteSelectBase, [QueryPromise]);
32239
32308
  getSQLiteSetOperators = () => ({
32240
32309
  union: union2,
@@ -32250,7 +32319,7 @@ var init_select3 = __esm({
32250
32319
  });
32251
32320
 
32252
32321
  // ../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js
32253
- var _a206, QueryBuilder2;
32322
+ var _a204, QueryBuilder2;
32254
32323
  var init_query_builder3 = __esm({
32255
32324
  "../drizzle-orm/dist/sqlite-core/query-builders/query-builder.js"() {
32256
32325
  "use strict";
@@ -32259,7 +32328,7 @@ var init_query_builder3 = __esm({
32259
32328
  init_dialect2();
32260
32329
  init_subquery();
32261
32330
  init_select3();
32262
- _a206 = entityKind;
32331
+ _a204 = entityKind;
32263
32332
  QueryBuilder2 = class {
32264
32333
  constructor(dialect4) {
32265
32334
  __publicField(this, "dialect");
@@ -32321,7 +32390,185 @@ var init_query_builder3 = __esm({
32321
32390
  return this.dialect;
32322
32391
  }
32323
32392
  };
32324
- __publicField(QueryBuilder2, _a206, "SQLiteQueryBuilder");
32393
+ __publicField(QueryBuilder2, _a204, "SQLiteQueryBuilder");
32394
+ }
32395
+ });
32396
+
32397
+ // ../drizzle-orm/dist/sqlite-core/query-builders/insert.js
32398
+ var _a205, SQLiteInsertBuilder, _a206, _b147, SQLiteInsertBase;
32399
+ var init_insert2 = __esm({
32400
+ "../drizzle-orm/dist/sqlite-core/query-builders/insert.js"() {
32401
+ "use strict";
32402
+ init_entity();
32403
+ init_query_promise();
32404
+ init_sql();
32405
+ init_table3();
32406
+ init_table();
32407
+ init_utils2();
32408
+ init_query_builder3();
32409
+ _a205 = entityKind;
32410
+ SQLiteInsertBuilder = class {
32411
+ constructor(table4, session, dialect4, withList) {
32412
+ this.table = table4;
32413
+ this.session = session;
32414
+ this.dialect = dialect4;
32415
+ this.withList = withList;
32416
+ }
32417
+ values(values) {
32418
+ values = Array.isArray(values) ? values : [values];
32419
+ if (values.length === 0) {
32420
+ throw new Error("values() must be called with at least one value");
32421
+ }
32422
+ const mappedValues = values.map((entry) => {
32423
+ const result = {};
32424
+ const cols = this.table[Table2.Symbol.Columns];
32425
+ for (const colKey of Object.keys(entry)) {
32426
+ const colValue = entry[colKey];
32427
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
32428
+ }
32429
+ return result;
32430
+ });
32431
+ return new SQLiteInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList);
32432
+ }
32433
+ select(selectQuery) {
32434
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder2()) : selectQuery;
32435
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
32436
+ throw new Error(
32437
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
32438
+ );
32439
+ }
32440
+ return new SQLiteInsertBase(this.table, select, this.session, this.dialect, this.withList, true);
32441
+ }
32442
+ };
32443
+ __publicField(SQLiteInsertBuilder, _a205, "SQLiteInsertBuilder");
32444
+ SQLiteInsertBase = class extends (_b147 = QueryPromise, _a206 = entityKind, _b147) {
32445
+ constructor(table4, values, session, dialect4, withList, select) {
32446
+ super();
32447
+ /** @internal */
32448
+ __publicField(this, "config");
32449
+ __publicField(this, "run", (placeholderValues) => {
32450
+ return this._prepare().run(placeholderValues);
32451
+ });
32452
+ __publicField(this, "all", (placeholderValues) => {
32453
+ return this._prepare().all(placeholderValues);
32454
+ });
32455
+ __publicField(this, "get", (placeholderValues) => {
32456
+ return this._prepare().get(placeholderValues);
32457
+ });
32458
+ __publicField(this, "values", (placeholderValues) => {
32459
+ return this._prepare().values(placeholderValues);
32460
+ });
32461
+ this.session = session;
32462
+ this.dialect = dialect4;
32463
+ this.config = { table: table4, values, withList, select };
32464
+ }
32465
+ returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
32466
+ this.config.returning = orderSelectedFields(fields);
32467
+ return this;
32468
+ }
32469
+ /**
32470
+ * Adds an `on conflict do nothing` clause to the query.
32471
+ *
32472
+ * Calling this method simply avoids inserting a row as its alternative action.
32473
+ *
32474
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-conflict-do-nothing}
32475
+ *
32476
+ * @param config The `target` and `where` clauses.
32477
+ *
32478
+ * @example
32479
+ * ```ts
32480
+ * // Insert one row and cancel the insert if there's a conflict
32481
+ * await db.insert(cars)
32482
+ * .values({ id: 1, brand: 'BMW' })
32483
+ * .onConflictDoNothing();
32484
+ *
32485
+ * // Explicitly specify conflict target
32486
+ * await db.insert(cars)
32487
+ * .values({ id: 1, brand: 'BMW' })
32488
+ * .onConflictDoNothing({ target: cars.id });
32489
+ * ```
32490
+ */
32491
+ onConflictDoNothing(config = {}) {
32492
+ if (config.target === void 0) {
32493
+ this.config.onConflict = sql`do nothing`;
32494
+ } else {
32495
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32496
+ const whereSql = config.where ? sql` where ${config.where}` : sql``;
32497
+ this.config.onConflict = sql`${targetSql} do nothing${whereSql}`;
32498
+ }
32499
+ return this;
32500
+ }
32501
+ /**
32502
+ * Adds an `on conflict do update` clause to the query.
32503
+ *
32504
+ * Calling this method will update the existing row that conflicts with the row proposed for insertion as its alternative action.
32505
+ *
32506
+ * See docs: {@link https://orm.drizzle.team/docs/insert#upserts-and-conflicts}
32507
+ *
32508
+ * @param config The `target`, `set` and `where` clauses.
32509
+ *
32510
+ * @example
32511
+ * ```ts
32512
+ * // Update the row if there's a conflict
32513
+ * await db.insert(cars)
32514
+ * .values({ id: 1, brand: 'BMW' })
32515
+ * .onConflictDoUpdate({
32516
+ * target: cars.id,
32517
+ * set: { brand: 'Porsche' }
32518
+ * });
32519
+ *
32520
+ * // Upsert with 'where' clause
32521
+ * await db.insert(cars)
32522
+ * .values({ id: 1, brand: 'BMW' })
32523
+ * .onConflictDoUpdate({
32524
+ * target: cars.id,
32525
+ * set: { brand: 'newBMW' },
32526
+ * where: sql`${cars.createdAt} > '2023-01-01'::date`,
32527
+ * });
32528
+ * ```
32529
+ */
32530
+ onConflictDoUpdate(config) {
32531
+ if (config.where && (config.targetWhere || config.setWhere)) {
32532
+ throw new Error(
32533
+ 'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.'
32534
+ );
32535
+ }
32536
+ const whereSql = config.where ? sql` where ${config.where}` : void 0;
32537
+ const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : void 0;
32538
+ const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : void 0;
32539
+ const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
32540
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
32541
+ this.config.onConflict = sql`${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
32542
+ return this;
32543
+ }
32544
+ /** @internal */
32545
+ getSQL() {
32546
+ return this.dialect.buildInsertQuery(this.config);
32547
+ }
32548
+ toSQL() {
32549
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
32550
+ return rest;
32551
+ }
32552
+ /** @internal */
32553
+ _prepare(isOneTimeQuery = true) {
32554
+ return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
32555
+ this.dialect.sqlToQuery(this.getSQL()),
32556
+ this.config.returning,
32557
+ this.config.returning ? "all" : "run",
32558
+ true
32559
+ );
32560
+ }
32561
+ prepare() {
32562
+ return this._prepare(false);
32563
+ }
32564
+ async execute() {
32565
+ return this.config.returning ? this.all() : this.run();
32566
+ }
32567
+ $dynamic() {
32568
+ return this;
32569
+ }
32570
+ };
32571
+ __publicField(SQLiteInsertBase, _a206, "SQLiteInsert");
32325
32572
  }
32326
32573
  });
32327
32574
 
@@ -32341,8 +32588,11 @@ var init_update2 = __esm({
32341
32588
  init_query_promise();
32342
32589
  init_selection_proxy();
32343
32590
  init_table3();
32591
+ init_subquery();
32344
32592
  init_table();
32345
32593
  init_utils2();
32594
+ init_view_common();
32595
+ init_view_base2();
32346
32596
  _a207 = entityKind;
32347
32597
  SQLiteUpdateBuilder = class {
32348
32598
  constructor(table4, session, dialect4, withList) {
@@ -32367,6 +32617,10 @@ var init_update2 = __esm({
32367
32617
  super();
32368
32618
  /** @internal */
32369
32619
  __publicField(this, "config");
32620
+ __publicField(this, "leftJoin", this.createJoin("left"));
32621
+ __publicField(this, "rightJoin", this.createJoin("right"));
32622
+ __publicField(this, "innerJoin", this.createJoin("inner"));
32623
+ __publicField(this, "fullJoin", this.createJoin("full"));
32370
32624
  __publicField(this, "run", (placeholderValues) => {
32371
32625
  return this._prepare().run(placeholderValues);
32372
32626
  });
@@ -32381,7 +32635,34 @@ var init_update2 = __esm({
32381
32635
  });
32382
32636
  this.session = session;
32383
32637
  this.dialect = dialect4;
32384
- this.config = { set, table: table4, withList };
32638
+ this.config = { set, table: table4, withList, joins: [] };
32639
+ }
32640
+ from(source) {
32641
+ this.config.from = source;
32642
+ return this;
32643
+ }
32644
+ createJoin(joinType) {
32645
+ return (table4, on) => {
32646
+ const tableName = getTableLikeName(table4);
32647
+ if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
32648
+ throw new Error(`Alias "${tableName}" is already used in this query`);
32649
+ }
32650
+ if (typeof on === "function") {
32651
+ const from = this.config.from ? is(table4, SQLiteTable) ? table4[Table2.Symbol.Columns] : is(table4, Subquery) ? table4._.selectedFields : is(table4, SQLiteViewBase) ? table4[ViewBaseConfig].selectedFields : void 0 : void 0;
32652
+ on = on(
32653
+ new Proxy(
32654
+ this.config.table[Table2.Symbol.Columns],
32655
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32656
+ ),
32657
+ from && new Proxy(
32658
+ from,
32659
+ new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
32660
+ )
32661
+ );
32662
+ }
32663
+ this.config.joins.push({ on, table: table4, joinType, alias: tableName });
32664
+ return this;
32665
+ };
32385
32666
  }
32386
32667
  /**
32387
32668
  * Adds a 'where' clause to the query.
@@ -35679,131 +35960,6 @@ var init_delete3 = __esm({
35679
35960
  }
35680
35961
  });
35681
35962
 
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
35963
  // ../drizzle-orm/dist/mysql-core/columns/all.js
35808
35964
  function getMySqlColumnBuilders() {
35809
35965
  return {
@@ -35884,7 +36040,7 @@ function mysqlTableWithSchema(name2, columns, extraConfig, schema4, baseName = n
35884
36040
  }
35885
36041
  return table4;
35886
36042
  }
35887
- var InlineForeignKeys3, _a301, _b223, _c9, _d4, _e4, MySqlTable, mysqlTable;
36043
+ var InlineForeignKeys3, _a299, _b222, _c9, _d4, _e4, MySqlTable, mysqlTable;
35888
36044
  var init_table4 = __esm({
35889
36045
  "../drizzle-orm/dist/mysql-core/table.js"() {
35890
36046
  "use strict";
@@ -35892,15 +36048,15 @@ var init_table4 = __esm({
35892
36048
  init_table();
35893
36049
  init_all3();
35894
36050
  InlineForeignKeys3 = Symbol.for("drizzle:MySqlInlineForeignKeys");
35895
- MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b223 = InlineForeignKeys3, _a301 = Table2.Symbol.ExtraConfigBuilder, _e4) {
36051
+ MySqlTable = class extends (_e4 = Table2, _d4 = entityKind, _c9 = Table2.Symbol.Columns, _b222 = InlineForeignKeys3, _a299 = Table2.Symbol.ExtraConfigBuilder, _e4) {
35896
36052
  constructor() {
35897
36053
  super(...arguments);
35898
36054
  /** @internal */
35899
36055
  __publicField(this, _c9);
35900
36056
  /** @internal */
35901
- __publicField(this, _b223, []);
36057
+ __publicField(this, _b222, []);
35902
36058
  /** @internal */
35903
- __publicField(this, _a301);
36059
+ __publicField(this, _a299);
35904
36060
  }
35905
36061
  };
35906
36062
  __publicField(MySqlTable, _d4, "MySqlTable");
@@ -35915,20 +36071,20 @@ var init_table4 = __esm({
35915
36071
  });
35916
36072
 
35917
36073
  // ../drizzle-orm/dist/mysql-core/view-base.js
35918
- var _a302, _b224, MySqlViewBase;
36074
+ var _a300, _b223, MySqlViewBase;
35919
36075
  var init_view_base3 = __esm({
35920
36076
  "../drizzle-orm/dist/mysql-core/view-base.js"() {
35921
36077
  "use strict";
35922
36078
  init_entity();
35923
36079
  init_sql();
35924
- MySqlViewBase = class extends (_b224 = View3, _a302 = entityKind, _b224) {
36080
+ MySqlViewBase = class extends (_b223 = View3, _a300 = entityKind, _b223) {
35925
36081
  };
35926
- __publicField(MySqlViewBase, _a302, "MySqlViewBase");
36082
+ __publicField(MySqlViewBase, _a300, "MySqlViewBase");
35927
36083
  }
35928
36084
  });
35929
36085
 
35930
36086
  // ../drizzle-orm/dist/mysql-core/dialect.js
35931
- var _a303, MySqlDialect;
36087
+ var _a301, MySqlDialect;
35932
36088
  var init_dialect3 = __esm({
35933
36089
  "../drizzle-orm/dist/mysql-core/dialect.js"() {
35934
36090
  "use strict";
@@ -35947,7 +36103,7 @@ var init_dialect3 = __esm({
35947
36103
  init_common4();
35948
36104
  init_table4();
35949
36105
  init_view_base3();
35950
- _a303 = entityKind;
36106
+ _a301 = entityKind;
35951
36107
  MySqlDialect = class {
35952
36108
  constructor(config) {
35953
36109
  /** @internal */
@@ -36230,7 +36386,7 @@ var init_dialect3 = __esm({
36230
36386
  const offsetSql = offset ? sql` offset ${offset}` : void 0;
36231
36387
  return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
36232
36388
  }
36233
- buildInsertQuery({ table: table4, values, ignore, onConflict }) {
36389
+ buildInsertQuery({ table: table4, values: valuesOrSelect, ignore, onConflict, select }) {
36234
36390
  const valuesSqlList = [];
36235
36391
  const columns = table4[Table2.Symbol.Columns];
36236
36392
  const colEntries = Object.entries(columns).filter(
@@ -36238,42 +36394,53 @@ var init_dialect3 = __esm({
36238
36394
  );
36239
36395
  const insertOrder = colEntries.map(([, column4]) => sql.identifier(this.casing.getColumnCasing(column4)));
36240
36396
  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);
36397
+ if (select) {
36398
+ const select2 = valuesOrSelect;
36399
+ if (is(select2, SQL)) {
36400
+ valuesSqlList.push(select2);
36401
+ } else {
36402
+ valuesSqlList.push(select2.getSQL());
36403
+ }
36404
+ } else {
36405
+ const values = valuesOrSelect;
36406
+ valuesSqlList.push(sql.raw("values "));
36407
+ for (const [valueIndex, value] of values.entries()) {
36408
+ const generatedIds = {};
36409
+ const valueList = [];
36410
+ for (const [fieldName, col] of colEntries) {
36411
+ const colValue = value[fieldName];
36412
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
36413
+ if (col.defaultFn !== void 0) {
36414
+ const defaultFnResult = col.defaultFn();
36415
+ generatedIds[fieldName] = defaultFnResult;
36416
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
36417
+ valueList.push(defaultValue);
36418
+ } else if (!col.default && col.onUpdateFn !== void 0) {
36419
+ const onUpdateFnResult = col.onUpdateFn();
36420
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
36421
+ valueList.push(newValue);
36422
+ } else {
36423
+ valueList.push(sql`default`);
36424
+ }
36256
36425
  } else {
36257
- valueList.push(sql`default`);
36258
- }
36259
- } else {
36260
- if (col.defaultFn && is(colValue, Param)) {
36261
- generatedIds[fieldName] = colValue.value;
36426
+ if (col.defaultFn && is(colValue, Param)) {
36427
+ generatedIds[fieldName] = colValue.value;
36428
+ }
36429
+ valueList.push(colValue);
36262
36430
  }
36263
- valueList.push(colValue);
36264
36431
  }
36265
- }
36266
- generatedIdsResponse.push(generatedIds);
36267
- valuesSqlList.push(valueList);
36268
- if (valueIndex < values.length - 1) {
36269
- valuesSqlList.push(sql`, `);
36432
+ generatedIdsResponse.push(generatedIds);
36433
+ valuesSqlList.push(valueList);
36434
+ if (valueIndex < values.length - 1) {
36435
+ valuesSqlList.push(sql`, `);
36436
+ }
36270
36437
  }
36271
36438
  }
36272
36439
  const valuesSql = sql.join(valuesSqlList);
36273
36440
  const ignoreSql = ignore ? sql` ignore` : void 0;
36274
36441
  const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : void 0;
36275
36442
  return {
36276
- sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} values ${valuesSql}${onConflictSql}`,
36443
+ sql: sql`insert${ignoreSql} into ${table4} ${insertOrder} ${valuesSql}${onConflictSql}`,
36277
36444
  generatedIds: generatedIdsResponse
36278
36445
  };
36279
36446
  }
@@ -36733,7 +36900,7 @@ var init_dialect3 = __esm({
36733
36900
  };
36734
36901
  }
36735
36902
  };
36736
- __publicField(MySqlDialect, _a303, "MySqlDialect");
36903
+ __publicField(MySqlDialect, _a301, "MySqlDialect");
36737
36904
  }
36738
36905
  });
36739
36906
 
@@ -36755,7 +36922,7 @@ function createSetOperator3(type, isAll) {
36755
36922
  return leftSelect.addSetOperators(setOperators);
36756
36923
  };
36757
36924
  }
36758
- var _a304, MySqlSelectBuilder, _a305, _b225, MySqlSelectQueryBuilderBase, _a306, _b226, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36925
+ var _a302, MySqlSelectBuilder, _a303, _b224, MySqlSelectQueryBuilderBase, _a304, _b225, MySqlSelectBase, getMySqlSetOperators, union3, unionAll3, intersect3, intersectAll2, except3, exceptAll2;
36759
36926
  var init_select4 = __esm({
36760
36927
  "../drizzle-orm/dist/mysql-core/query-builders/select.js"() {
36761
36928
  "use strict";
@@ -36770,7 +36937,7 @@ var init_select4 = __esm({
36770
36937
  init_utils2();
36771
36938
  init_view_common();
36772
36939
  init_view_base3();
36773
- _a304 = entityKind;
36940
+ _a302 = entityKind;
36774
36941
  MySqlSelectBuilder = class {
36775
36942
  constructor(config) {
36776
36943
  __publicField(this, "fields");
@@ -36815,8 +36982,8 @@ var init_select4 = __esm({
36815
36982
  );
36816
36983
  }
36817
36984
  };
36818
- __publicField(MySqlSelectBuilder, _a304, "MySqlSelectBuilder");
36819
- MySqlSelectQueryBuilderBase = class extends (_b225 = TypedQueryBuilder, _a305 = entityKind, _b225) {
36985
+ __publicField(MySqlSelectBuilder, _a302, "MySqlSelectBuilder");
36986
+ MySqlSelectQueryBuilderBase = class extends (_b224 = TypedQueryBuilder, _a303 = entityKind, _b224) {
36820
36987
  constructor({ table: table4, fields, isPartialSelect, session, dialect: dialect4, withList, distinct }) {
36821
36988
  super();
36822
36989
  __publicField(this, "_");
@@ -37417,8 +37584,8 @@ var init_select4 = __esm({
37417
37584
  return this;
37418
37585
  }
37419
37586
  };
37420
- __publicField(MySqlSelectQueryBuilderBase, _a305, "MySqlSelectQueryBuilder");
37421
- MySqlSelectBase = class extends (_b226 = MySqlSelectQueryBuilderBase, _a306 = entityKind, _b226) {
37587
+ __publicField(MySqlSelectQueryBuilderBase, _a303, "MySqlSelectQueryBuilder");
37588
+ MySqlSelectBase = class extends (_b225 = MySqlSelectQueryBuilderBase, _a304 = entityKind, _b225) {
37422
37589
  constructor() {
37423
37590
  super(...arguments);
37424
37591
  __publicField(this, "execute", (placeholderValues) => {
@@ -37442,7 +37609,7 @@ var init_select4 = __esm({
37442
37609
  return query;
37443
37610
  }
37444
37611
  };
37445
- __publicField(MySqlSelectBase, _a306, "MySqlSelect");
37612
+ __publicField(MySqlSelectBase, _a304, "MySqlSelect");
37446
37613
  applyMixins(MySqlSelectBase, [QueryPromise]);
37447
37614
  getMySqlSetOperators = () => ({
37448
37615
  union: union3,
@@ -37462,7 +37629,7 @@ var init_select4 = __esm({
37462
37629
  });
37463
37630
 
37464
37631
  // ../drizzle-orm/dist/mysql-core/query-builders/query-builder.js
37465
- var _a307, QueryBuilder3;
37632
+ var _a305, QueryBuilder3;
37466
37633
  var init_query_builder4 = __esm({
37467
37634
  "../drizzle-orm/dist/mysql-core/query-builders/query-builder.js"() {
37468
37635
  "use strict";
@@ -37471,7 +37638,7 @@ var init_query_builder4 = __esm({
37471
37638
  init_selection_proxy();
37472
37639
  init_subquery();
37473
37640
  init_select4();
37474
- _a307 = entityKind;
37641
+ _a305 = entityKind;
37475
37642
  QueryBuilder3 = class {
37476
37643
  constructor(dialect4) {
37477
37644
  __publicField(this, "dialect");
@@ -37533,7 +37700,142 @@ var init_query_builder4 = __esm({
37533
37700
  return this.dialect;
37534
37701
  }
37535
37702
  };
37536
- __publicField(QueryBuilder3, _a307, "MySqlQueryBuilder");
37703
+ __publicField(QueryBuilder3, _a305, "MySqlQueryBuilder");
37704
+ }
37705
+ });
37706
+
37707
+ // ../drizzle-orm/dist/mysql-core/query-builders/insert.js
37708
+ var _a306, MySqlInsertBuilder, _a307, _b226, MySqlInsertBase;
37709
+ var init_insert3 = __esm({
37710
+ "../drizzle-orm/dist/mysql-core/query-builders/insert.js"() {
37711
+ "use strict";
37712
+ init_entity();
37713
+ init_query_promise();
37714
+ init_sql();
37715
+ init_table();
37716
+ init_utils2();
37717
+ init_query_builder4();
37718
+ _a306 = entityKind;
37719
+ MySqlInsertBuilder = class {
37720
+ constructor(table4, session, dialect4) {
37721
+ __publicField(this, "shouldIgnore", false);
37722
+ this.table = table4;
37723
+ this.session = session;
37724
+ this.dialect = dialect4;
37725
+ }
37726
+ ignore() {
37727
+ this.shouldIgnore = true;
37728
+ return this;
37729
+ }
37730
+ values(values) {
37731
+ values = Array.isArray(values) ? values : [values];
37732
+ if (values.length === 0) {
37733
+ throw new Error("values() must be called with at least one value");
37734
+ }
37735
+ const mappedValues = values.map((entry) => {
37736
+ const result = {};
37737
+ const cols = this.table[Table2.Symbol.Columns];
37738
+ for (const colKey of Object.keys(entry)) {
37739
+ const colValue = entry[colKey];
37740
+ result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
37741
+ }
37742
+ return result;
37743
+ });
37744
+ return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
37745
+ }
37746
+ select(selectQuery) {
37747
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder3()) : selectQuery;
37748
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
37749
+ throw new Error(
37750
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
37751
+ );
37752
+ }
37753
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
37754
+ }
37755
+ };
37756
+ __publicField(MySqlInsertBuilder, _a306, "MySqlInsertBuilder");
37757
+ MySqlInsertBase = class extends (_b226 = QueryPromise, _a307 = entityKind, _b226) {
37758
+ constructor(table4, values, ignore, session, dialect4, select) {
37759
+ super();
37760
+ __publicField(this, "config");
37761
+ __publicField(this, "execute", (placeholderValues) => {
37762
+ return this.prepare().execute(placeholderValues);
37763
+ });
37764
+ __publicField(this, "createIterator", () => {
37765
+ const self2 = this;
37766
+ return async function* (placeholderValues) {
37767
+ yield* self2.prepare().iterator(placeholderValues);
37768
+ };
37769
+ });
37770
+ __publicField(this, "iterator", this.createIterator());
37771
+ this.session = session;
37772
+ this.dialect = dialect4;
37773
+ this.config = { table: table4, values, select, ignore };
37774
+ }
37775
+ /**
37776
+ * Adds an `on duplicate key update` clause to the query.
37777
+ *
37778
+ * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
37779
+ *
37780
+ * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
37781
+ *
37782
+ * @param config The `set` clause
37783
+ *
37784
+ * @example
37785
+ * ```ts
37786
+ * await db.insert(cars)
37787
+ * .values({ id: 1, brand: 'BMW'})
37788
+ * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
37789
+ * ```
37790
+ *
37791
+ * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
37792
+ *
37793
+ * ```ts
37794
+ * import { sql } from 'drizzle-orm';
37795
+ *
37796
+ * await db.insert(cars)
37797
+ * .values({ id: 1, brand: 'BMW' })
37798
+ * .onDuplicateKeyUpdate({ set: { id: sql`id` } });
37799
+ * ```
37800
+ */
37801
+ onDuplicateKeyUpdate(config) {
37802
+ const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
37803
+ this.config.onConflict = sql`update ${setSql}`;
37804
+ return this;
37805
+ }
37806
+ $returningId() {
37807
+ const returning = [];
37808
+ for (const [key, value] of Object.entries(this.config.table[Table2.Symbol.Columns])) {
37809
+ if (value.primary) {
37810
+ returning.push({ field: value, path: [key] });
37811
+ }
37812
+ }
37813
+ this.config.returning = returning;
37814
+ return this;
37815
+ }
37816
+ /** @internal */
37817
+ getSQL() {
37818
+ return this.dialect.buildInsertQuery(this.config).sql;
37819
+ }
37820
+ toSQL() {
37821
+ const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
37822
+ return rest;
37823
+ }
37824
+ prepare() {
37825
+ const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
37826
+ return this.session.prepareQuery(
37827
+ this.dialect.sqlToQuery(sql2),
37828
+ void 0,
37829
+ void 0,
37830
+ generatedIds,
37831
+ this.config.returning
37832
+ );
37833
+ }
37834
+ $dynamic() {
37835
+ return this;
37836
+ }
37837
+ };
37838
+ __publicField(MySqlInsertBase, _a307, "MySqlInsert");
37537
37839
  }
37538
37840
  });
37539
37841