update.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import type { GetColumnData } from "../../column.js";
  2. import { entityKind } from "../../entity.js";
  3. import type { GelDialect } from "../dialect.js";
  4. import type { GelPreparedQuery, GelQueryResultHKT, GelQueryResultKind, GelSession, PreparedQueryConfig } from "../session.js";
  5. import { GelTable } from "../table.js";
  6. import type { AppendToNullabilityMap, AppendToResult, GetSelectTableName, GetSelectTableSelection, JoinNullability, JoinType, SelectMode, SelectResult } from "../../query-builders/select.types.js";
  7. import { QueryPromise } from "../../query-promise.js";
  8. import type { RunnableQuery } from "../../runnable-query.js";
  9. import { type ColumnsSelection, type Query, SQL, type SQLWrapper } from "../../sql/sql.js";
  10. import { Subquery } from "../../subquery.js";
  11. import { Table } from "../../table.js";
  12. import { type Assume, type NeonAuthToken, type UpdateSet } from "../../utils.js";
  13. import type { GelColumn } from "../columns/common.js";
  14. import type { GelViewBase } from "../view-base.js";
  15. import type { GelSelectJoinConfig, SelectedFields, SelectedFieldsOrdered } from "./select.types.js";
  16. export interface GelUpdateConfig {
  17. where?: SQL | undefined;
  18. set: UpdateSet;
  19. table: GelTable;
  20. from?: GelTable | Subquery | GelViewBase | SQL;
  21. joins: GelSelectJoinConfig[];
  22. returning?: SelectedFieldsOrdered;
  23. withList?: Subquery[];
  24. }
  25. export type GelUpdateSetSource<TTable extends GelTable> = {
  26. [Key in keyof TTable['$inferInsert']]?: GetColumnData<TTable['_']['columns'][Key]> | SQL | GelColumn;
  27. } & {};
  28. export declare class GelUpdateBuilder<TTable extends GelTable, TQueryResult extends GelQueryResultHKT> {
  29. private table;
  30. private session;
  31. private dialect;
  32. private withList?;
  33. static readonly [entityKind]: string;
  34. readonly _: {
  35. readonly table: TTable;
  36. };
  37. constructor(table: TTable, session: GelSession, dialect: GelDialect, withList?: Subquery[] | undefined);
  38. private authToken?;
  39. setToken(token: NeonAuthToken): this;
  40. set(values: GelUpdateSetSource<TTable>): GelUpdateWithout<GelUpdateBase<TTable, TQueryResult>, false, 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin'>;
  41. }
  42. export type GelUpdateWithout<T extends AnyGelUpdate, TDynamic extends boolean, K extends keyof T & string> = TDynamic extends true ? T : Omit<GelUpdateBase<T['_']['table'], T['_']['queryResult'], T['_']['from'], T['_']['returning'], T['_']['nullabilityMap'], T['_']['joins'], TDynamic, T['_']['excludedMethods'] | K>, T['_']['excludedMethods'] | K>;
  43. export type GelUpdateWithJoins<T extends AnyGelUpdate, TDynamic extends boolean, TFrom extends GelTable | Subquery | GelViewBase | SQL> = TDynamic extends true ? T : Omit<GelUpdateBase<T['_']['table'], T['_']['queryResult'], TFrom, T['_']['returning'], AppendToNullabilityMap<T['_']['nullabilityMap'], GetSelectTableName<TFrom>, 'inner'>, [
  44. ...T['_']['joins'],
  45. {
  46. name: GetSelectTableName<TFrom>;
  47. joinType: 'inner';
  48. table: TFrom;
  49. }
  50. ], TDynamic, Exclude<T['_']['excludedMethods'] | 'from', 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin'>>, Exclude<T['_']['excludedMethods'] | 'from', 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin'>>;
  51. export type GelUpdateJoinFn<T extends AnyGelUpdate, TDynamic extends boolean, TJoinType extends JoinType> = <TJoinedTable extends GelTable | Subquery | GelViewBase | SQL>(table: TJoinedTable, on: ((updateTable: T['_']['table']['_']['columns'], from: T['_']['from'] extends GelTable ? T['_']['from']['_']['columns'] : T['_']['from'] extends Subquery | GelViewBase ? T['_']['from']['_']['selectedFields'] : never) => SQL | undefined) | SQL | undefined) => GelUpdateJoin<T, TDynamic, TJoinType, TJoinedTable>;
  52. export type GelUpdateJoin<T extends AnyGelUpdate, TDynamic extends boolean, TJoinType extends JoinType, TJoinedTable extends GelTable | Subquery | GelViewBase | SQL> = TDynamic extends true ? T : GelUpdateBase<T['_']['table'], T['_']['queryResult'], T['_']['from'], T['_']['returning'], AppendToNullabilityMap<T['_']['nullabilityMap'], GetSelectTableName<TJoinedTable>, TJoinType>, [
  53. ...T['_']['joins'],
  54. {
  55. name: GetSelectTableName<TJoinedTable>;
  56. joinType: TJoinType;
  57. table: TJoinedTable;
  58. }
  59. ], TDynamic, T['_']['excludedMethods']>;
  60. type Join = {
  61. name: string | undefined;
  62. joinType: JoinType;
  63. table: GelTable | Subquery | GelViewBase | SQL;
  64. };
  65. type AccumulateToResult<T extends AnyGelUpdate, TSelectMode extends SelectMode, TJoins extends Join[], TSelectedFields extends ColumnsSelection> = TJoins extends [infer TJoin extends Join, ...infer TRest extends Join[]] ? AccumulateToResult<T, TSelectMode extends 'partial' ? TSelectMode : 'multiple', TRest, AppendToResult<T['_']['table']['_']['name'], TSelectedFields, TJoin['name'], TJoin['table'] extends Table ? TJoin['table']['_']['columns'] : TJoin['table'] extends Subquery ? Assume<TJoin['table']['_']['selectedFields'], SelectedFields> : never, TSelectMode extends 'partial' ? TSelectMode : 'multiple'>> : TSelectedFields;
  66. export type GelUpdateReturningAll<T extends AnyGelUpdate, TDynamic extends boolean> = GelUpdateWithout<GelUpdateBase<T['_']['table'], T['_']['queryResult'], T['_']['from'], SelectResult<AccumulateToResult<T, 'single', T['_']['joins'], GetSelectTableSelection<T['_']['table']>>, 'partial', T['_']['nullabilityMap']>, T['_']['nullabilityMap'], T['_']['joins'], TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  67. export type GelUpdateReturning<T extends AnyGelUpdate, TDynamic extends boolean, TSelectedFields extends SelectedFields> = GelUpdateWithout<GelUpdateBase<T['_']['table'], T['_']['queryResult'], T['_']['from'], SelectResult<AccumulateToResult<T, 'partial', T['_']['joins'], TSelectedFields>, 'partial', T['_']['nullabilityMap']>, T['_']['nullabilityMap'], T['_']['joins'], TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  68. export type GelUpdatePrepare<T extends AnyGelUpdate> = GelPreparedQuery<PreparedQueryConfig & {
  69. execute: T['_']['returning'] extends undefined ? GelQueryResultKind<T['_']['queryResult'], never> : T['_']['returning'][];
  70. }>;
  71. export type GelUpdateDynamic<T extends AnyGelUpdate> = GelUpdate<T['_']['table'], T['_']['queryResult'], T['_']['from'], T['_']['returning'], T['_']['nullabilityMap']>;
  72. export type GelUpdate<TTable extends GelTable = GelTable, TQueryResult extends GelQueryResultHKT = GelQueryResultHKT, TFrom extends GelTable | Subquery | GelViewBase | SQL | undefined = undefined, TReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined, TNullabilityMap extends Record<string, JoinNullability> = Record<TTable['_']['name'], 'not-null'>, TJoins extends Join[] = []> = GelUpdateBase<TTable, TQueryResult, TFrom, TReturning, TNullabilityMap, TJoins, true, never>;
  73. export type AnyGelUpdate = GelUpdateBase<any, any, any, any, any, any, any, any>;
  74. export interface GelUpdateBase<TTable extends GelTable, TQueryResult extends GelQueryResultHKT, TFrom extends GelTable | Subquery | GelViewBase | SQL | undefined = undefined, TReturning extends Record<string, unknown> | undefined = undefined, TNullabilityMap extends Record<string, JoinNullability> = Record<TTable['_']['name'], 'not-null'>, TJoins extends Join[] = [], TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise<TReturning extends undefined ? GelQueryResultKind<TQueryResult, never> : TReturning[]>, RunnableQuery<TReturning extends undefined ? GelQueryResultKind<TQueryResult, never> : TReturning[], 'gel'>, SQLWrapper {
  75. readonly _: {
  76. readonly dialect: 'gel';
  77. readonly table: TTable;
  78. readonly joins: TJoins;
  79. readonly nullabilityMap: TNullabilityMap;
  80. readonly queryResult: TQueryResult;
  81. readonly from: TFrom;
  82. readonly returning: TReturning;
  83. readonly dynamic: TDynamic;
  84. readonly excludedMethods: TExcludedMethods;
  85. readonly result: TReturning extends undefined ? GelQueryResultKind<TQueryResult, never> : TReturning[];
  86. };
  87. }
  88. export declare class GelUpdateBase<TTable extends GelTable, TQueryResult extends GelQueryResultHKT, TFrom extends GelTable | Subquery | GelViewBase | SQL | undefined = undefined, TReturning extends Record<string, unknown> | undefined = undefined, TNullabilityMap extends Record<string, JoinNullability> = Record<TTable['_']['name'], 'not-null'>, TJoins extends Join[] = [], TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise<TReturning extends undefined ? GelQueryResultKind<TQueryResult, never> : TReturning[]> implements RunnableQuery<TReturning extends undefined ? GelQueryResultKind<TQueryResult, never> : TReturning[], 'gel'>, SQLWrapper {
  89. private session;
  90. private dialect;
  91. static readonly [entityKind]: string;
  92. private config;
  93. private tableName;
  94. private joinsNotNullableMap;
  95. constructor(table: TTable, set: UpdateSet, session: GelSession, dialect: GelDialect, withList?: Subquery[]);
  96. from<TFrom extends GelTable | Subquery | GelViewBase | SQL>(source: TFrom): GelUpdateWithJoins<this, TDynamic, TFrom>;
  97. private getTableLikeFields;
  98. private createJoin;
  99. leftJoin: GelUpdateJoinFn<this, TDynamic, "left">;
  100. rightJoin: GelUpdateJoinFn<this, TDynamic, "right">;
  101. innerJoin: GelUpdateJoinFn<this, TDynamic, "inner">;
  102. fullJoin: GelUpdateJoinFn<this, TDynamic, "full">;
  103. /**
  104. * Adds a 'where' clause to the query.
  105. *
  106. * Calling this method will update only those rows that fulfill a specified condition.
  107. *
  108. * See docs: {@link https://orm.drizzle.team/docs/update}
  109. *
  110. * @param where the 'where' clause.
  111. *
  112. * @example
  113. * You can use conditional operators and `sql function` to filter the rows to be updated.
  114. *
  115. * ```ts
  116. * // Update all cars with green color
  117. * await db.update(cars).set({ color: 'red' })
  118. * .where(eq(cars.color, 'green'));
  119. * // or
  120. * await db.update(cars).set({ color: 'red' })
  121. * .where(sql`${cars.color} = 'green'`)
  122. * ```
  123. *
  124. * You can logically combine conditional operators with `and()` and `or()` operators:
  125. *
  126. * ```ts
  127. * // Update all BMW cars with a green color
  128. * await db.update(cars).set({ color: 'red' })
  129. * .where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  130. *
  131. * // Update all cars with the green or blue color
  132. * await db.update(cars).set({ color: 'red' })
  133. * .where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  134. * ```
  135. */
  136. where(where: SQL | undefined): GelUpdateWithout<this, TDynamic, 'where'>;
  137. /**
  138. * Adds a `returning` clause to the query.
  139. *
  140. * Calling this method will return the specified fields of the updated rows. If no fields are specified, all fields will be returned.
  141. *
  142. * See docs: {@link https://orm.drizzle.team/docs/update#update-with-returning}
  143. *
  144. * @example
  145. * ```ts
  146. * // Update all cars with the green color and return all fields
  147. * const updatedCars: Car[] = await db.update(cars)
  148. * .set({ color: 'red' })
  149. * .where(eq(cars.color, 'green'))
  150. * .returning();
  151. *
  152. * // Update all cars with the green color and return only their id and brand fields
  153. * const updatedCarsIdsAndBrands: { id: number, brand: string }[] = await db.update(cars)
  154. * .set({ color: 'red' })
  155. * .where(eq(cars.color, 'green'))
  156. * .returning({ id: cars.id, brand: cars.brand });
  157. * ```
  158. */
  159. returning(): GelUpdateReturningAll<this, TDynamic>;
  160. returning<TSelectedFields extends SelectedFields>(fields: TSelectedFields): GelUpdateReturning<this, TDynamic, TSelectedFields>;
  161. toSQL(): Query;
  162. prepare(name: string): GelUpdatePrepare<this>;
  163. execute: ReturnType<this['prepare']>['execute'];
  164. $dynamic(): GelUpdateDynamic<this>;
  165. }
  166. export {};