update.d.cts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import type { GetColumnData } from "../../column.cjs";
  2. import { entityKind } from "../../entity.cjs";
  3. import type { SelectResultFields } from "../../query-builders/select.types.cjs";
  4. import { QueryPromise } from "../../query-promise.cjs";
  5. import type { RunnableQuery } from "../../runnable-query.cjs";
  6. import type { Placeholder, Query, SQL, SQLWrapper } from "../../sql/sql.cjs";
  7. import type { SQLiteDialect } from "../dialect.cjs";
  8. import type { SQLitePreparedQuery, SQLiteSession } from "../session.cjs";
  9. import { SQLiteTable } from "../table.cjs";
  10. import { Subquery } from "../../subquery.cjs";
  11. import { type DrizzleTypeError, type UpdateSet, type ValueOrArray } from "../../utils.cjs";
  12. import type { SQLiteColumn } from "../columns/common.cjs";
  13. import { SQLiteViewBase } from "../view-base.cjs";
  14. import type { SelectedFields, SelectedFieldsOrdered, SQLiteSelectJoinConfig } from "./select.types.cjs";
  15. export interface SQLiteUpdateConfig {
  16. where?: SQL | undefined;
  17. limit?: number | Placeholder;
  18. orderBy?: (SQLiteColumn | SQL | SQL.Aliased)[];
  19. set: UpdateSet;
  20. table: SQLiteTable;
  21. from?: SQLiteTable | Subquery | SQLiteViewBase | SQL;
  22. joins: SQLiteSelectJoinConfig[];
  23. returning?: SelectedFieldsOrdered;
  24. withList?: Subquery[];
  25. }
  26. export type SQLiteUpdateSetSource<TTable extends SQLiteTable> = {
  27. [Key in keyof TTable['$inferInsert']]?: GetColumnData<TTable['_']['columns'][Key], 'query'> | SQL | SQLiteColumn | undefined;
  28. } & {};
  29. export declare class SQLiteUpdateBuilder<TTable extends SQLiteTable, TResultType extends 'sync' | 'async', TRunResult> {
  30. protected table: TTable;
  31. protected session: SQLiteSession<any, any, any, any>;
  32. protected dialect: SQLiteDialect;
  33. private withList?;
  34. static readonly [entityKind]: string;
  35. readonly _: {
  36. readonly table: TTable;
  37. };
  38. constructor(table: TTable, session: SQLiteSession<any, any, any, any>, dialect: SQLiteDialect, withList?: Subquery[] | undefined);
  39. set(values: SQLiteUpdateSetSource<TTable>): SQLiteUpdateWithout<SQLiteUpdateBase<TTable, TResultType, TRunResult>, false, 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin'>;
  40. }
  41. export type SQLiteUpdateWithout<T extends AnySQLiteUpdate, TDynamic extends boolean, K extends keyof T & string> = TDynamic extends true ? T : Omit<SQLiteUpdateBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['from'], T['_']['returning'], TDynamic, T['_']['excludedMethods'] | K>, T['_']['excludedMethods'] | K>;
  42. export type SQLiteUpdateWithJoins<T extends AnySQLiteUpdate, TDynamic extends boolean, TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL> = TDynamic extends true ? T : Omit<SQLiteUpdateBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], TFrom, T['_']['returning'], TDynamic, Exclude<T['_']['excludedMethods'] | 'from', 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin'>>, Exclude<T['_']['excludedMethods'] | 'from', 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin'>>;
  43. export type SQLiteUpdateReturningAll<T extends AnySQLiteUpdate, TDynamic extends boolean> = SQLiteUpdateWithout<SQLiteUpdateBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['from'], T['_']['table']['$inferSelect'], TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  44. export type SQLiteUpdateReturning<T extends AnySQLiteUpdate, TDynamic extends boolean, TSelectedFields extends SelectedFields> = SQLiteUpdateWithout<SQLiteUpdateBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['from'], SelectResultFields<TSelectedFields>, TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  45. export type SQLiteUpdateExecute<T extends AnySQLiteUpdate> = T['_']['returning'] extends undefined ? T['_']['runResult'] : T['_']['returning'][];
  46. export type SQLiteUpdatePrepare<T extends AnySQLiteUpdate> = SQLitePreparedQuery<{
  47. type: T['_']['resultType'];
  48. run: T['_']['runResult'];
  49. all: T['_']['returning'] extends undefined ? DrizzleTypeError<'.all() cannot be used without .returning()'> : T['_']['returning'][];
  50. get: T['_']['returning'] extends undefined ? DrizzleTypeError<'.get() cannot be used without .returning()'> : T['_']['returning'];
  51. values: T['_']['returning'] extends undefined ? DrizzleTypeError<'.values() cannot be used without .returning()'> : any[][];
  52. execute: SQLiteUpdateExecute<T>;
  53. }>;
  54. export type SQLiteUpdateJoinFn<T extends AnySQLiteUpdate> = <TJoinedTable extends SQLiteTable | Subquery | SQLiteViewBase | SQL>(table: TJoinedTable, on: ((updateTable: T['_']['table']['_']['columns'], from: T['_']['from'] extends SQLiteTable ? T['_']['from']['_']['columns'] : T['_']['from'] extends Subquery | SQLiteViewBase ? T['_']['from']['_']['selectedFields'] : never) => SQL | undefined) | SQL | undefined) => T;
  55. export type SQLiteUpdateDynamic<T extends AnySQLiteUpdate> = SQLiteUpdate<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['returning']>;
  56. export type SQLiteUpdate<TTable extends SQLiteTable = SQLiteTable, TResultType extends 'sync' | 'async' = 'sync' | 'async', TRunResult = any, TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL | undefined = undefined, TReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined> = SQLiteUpdateBase<TTable, TResultType, TRunResult, TFrom, TReturning, true, never>;
  57. export type AnySQLiteUpdate = SQLiteUpdateBase<any, any, any, any, any, any, any>;
  58. export interface SQLiteUpdateBase<TTable extends SQLiteTable = SQLiteTable, TResultType extends 'sync' | 'async' = 'sync' | 'async', TRunResult = unknown, TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL | undefined = undefined, TReturning = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends SQLWrapper, QueryPromise<TReturning extends undefined ? TRunResult : TReturning[]> {
  59. readonly _: {
  60. readonly dialect: 'sqlite';
  61. readonly table: TTable;
  62. readonly resultType: TResultType;
  63. readonly runResult: TRunResult;
  64. readonly from: TFrom;
  65. readonly returning: TReturning;
  66. readonly dynamic: TDynamic;
  67. readonly excludedMethods: TExcludedMethods;
  68. readonly result: TReturning extends undefined ? TRunResult : TReturning[];
  69. };
  70. }
  71. export declare class SQLiteUpdateBase<TTable extends SQLiteTable = SQLiteTable, TResultType extends 'sync' | 'async' = 'sync' | 'async', TRunResult = unknown, TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL | undefined = undefined, TReturning = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise<TReturning extends undefined ? TRunResult : TReturning[]> implements RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], 'sqlite'>, SQLWrapper {
  72. private session;
  73. private dialect;
  74. static readonly [entityKind]: string;
  75. constructor(table: TTable, set: UpdateSet, session: SQLiteSession<any, any, any, any>, dialect: SQLiteDialect, withList?: Subquery[]);
  76. from<TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL>(source: TFrom): SQLiteUpdateWithJoins<this, TDynamic, TFrom>;
  77. private createJoin;
  78. leftJoin: SQLiteUpdateJoinFn<this>;
  79. rightJoin: SQLiteUpdateJoinFn<this>;
  80. innerJoin: SQLiteUpdateJoinFn<this>;
  81. fullJoin: SQLiteUpdateJoinFn<this>;
  82. /**
  83. * Adds a 'where' clause to the query.
  84. *
  85. * Calling this method will update only those rows that fulfill a specified condition.
  86. *
  87. * See docs: {@link https://orm.drizzle.team/docs/update}
  88. *
  89. * @param where the 'where' clause.
  90. *
  91. * @example
  92. * You can use conditional operators and `sql function` to filter the rows to be updated.
  93. *
  94. * ```ts
  95. * // Update all cars with green color
  96. * db.update(cars).set({ color: 'red' })
  97. * .where(eq(cars.color, 'green'));
  98. * // or
  99. * db.update(cars).set({ color: 'red' })
  100. * .where(sql`${cars.color} = 'green'`)
  101. * ```
  102. *
  103. * You can logically combine conditional operators with `and()` and `or()` operators:
  104. *
  105. * ```ts
  106. * // Update all BMW cars with a green color
  107. * db.update(cars).set({ color: 'red' })
  108. * .where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  109. *
  110. * // Update all cars with the green or blue color
  111. * db.update(cars).set({ color: 'red' })
  112. * .where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  113. * ```
  114. */
  115. where(where: SQL | undefined): SQLiteUpdateWithout<this, TDynamic, 'where'>;
  116. orderBy(builder: (updateTable: TTable) => ValueOrArray<SQLiteColumn | SQL | SQL.Aliased>): SQLiteUpdateWithout<this, TDynamic, 'orderBy'>;
  117. orderBy(...columns: (SQLiteColumn | SQL | SQL.Aliased)[]): SQLiteUpdateWithout<this, TDynamic, 'orderBy'>;
  118. limit(limit: number | Placeholder): SQLiteUpdateWithout<this, TDynamic, 'limit'>;
  119. /**
  120. * Adds a `returning` clause to the query.
  121. *
  122. * Calling this method will return the specified fields of the updated rows. If no fields are specified, all fields will be returned.
  123. *
  124. * See docs: {@link https://orm.drizzle.team/docs/update#update-with-returning}
  125. *
  126. * @example
  127. * ```ts
  128. * // Update all cars with the green color and return all fields
  129. * const updatedCars: Car[] = await db.update(cars)
  130. * .set({ color: 'red' })
  131. * .where(eq(cars.color, 'green'))
  132. * .returning();
  133. *
  134. * // Update all cars with the green color and return only their id and brand fields
  135. * const updatedCarsIdsAndBrands: { id: number, brand: string }[] = await db.update(cars)
  136. * .set({ color: 'red' })
  137. * .where(eq(cars.color, 'green'))
  138. * .returning({ id: cars.id, brand: cars.brand });
  139. * ```
  140. */
  141. returning(): SQLiteUpdateReturningAll<this, TDynamic>;
  142. returning<TSelectedFields extends SelectedFields>(fields: TSelectedFields): SQLiteUpdateReturning<this, TDynamic, TSelectedFields>;
  143. toSQL(): Query;
  144. prepare(): SQLiteUpdatePrepare<this>;
  145. run: ReturnType<this['prepare']>['run'];
  146. all: ReturnType<this['prepare']>['all'];
  147. get: ReturnType<this['prepare']>['get'];
  148. values: ReturnType<this['prepare']>['values'];
  149. execute(): Promise<SQLiteUpdateExecute<this>>;
  150. $dynamic(): SQLiteUpdateDynamic<this>;
  151. }