delete.d.cts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { entityKind } from "../../entity.cjs";
  2. import type { SelectResultFields } from "../../query-builders/select.types.cjs";
  3. import { QueryPromise } from "../../query-promise.cjs";
  4. import type { RunnableQuery } from "../../runnable-query.cjs";
  5. import type { Placeholder, Query, SQL, SQLWrapper } from "../../sql/sql.cjs";
  6. import type { SQLiteDialect } from "../dialect.cjs";
  7. import type { SQLitePreparedQuery, SQLiteSession } from "../session.cjs";
  8. import { SQLiteTable } from "../table.cjs";
  9. import type { Subquery } from "../../subquery.cjs";
  10. import { type DrizzleTypeError, type ValueOrArray } from "../../utils.cjs";
  11. import type { SQLiteColumn } from "../columns/common.cjs";
  12. import type { SelectedFieldsFlat, SelectedFieldsOrdered } from "./select.types.cjs";
  13. export type SQLiteDeleteWithout<T extends AnySQLiteDeleteBase, TDynamic extends boolean, K extends keyof T & string> = TDynamic extends true ? T : Omit<SQLiteDeleteBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['returning'], TDynamic, T['_']['excludedMethods'] | K>, T['_']['excludedMethods'] | K>;
  14. export type SQLiteDelete<TTable extends SQLiteTable = SQLiteTable, TResultType extends 'sync' | 'async' = 'sync' | 'async', TRunResult = unknown, TReturning extends Record<string, unknown> | undefined = undefined> = SQLiteDeleteBase<TTable, TResultType, TRunResult, TReturning, true, never>;
  15. export interface SQLiteDeleteConfig {
  16. where?: SQL | undefined;
  17. limit?: number | Placeholder;
  18. orderBy?: (SQLiteColumn | SQL | SQL.Aliased)[];
  19. table: SQLiteTable;
  20. returning?: SelectedFieldsOrdered;
  21. withList?: Subquery[];
  22. }
  23. export type SQLiteDeleteReturningAll<T extends AnySQLiteDeleteBase, TDynamic extends boolean> = SQLiteDeleteWithout<SQLiteDeleteBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['table']['$inferSelect'], T['_']['dynamic'], T['_']['excludedMethods']>, TDynamic, 'returning'>;
  24. export type SQLiteDeleteReturning<T extends AnySQLiteDeleteBase, TDynamic extends boolean, TSelectedFields extends SelectedFieldsFlat> = SQLiteDeleteWithout<SQLiteDeleteBase<T['_']['table'], T['_']['resultType'], T['_']['runResult'], SelectResultFields<TSelectedFields>, T['_']['dynamic'], T['_']['excludedMethods']>, TDynamic, 'returning'>;
  25. export type SQLiteDeleteExecute<T extends AnySQLiteDeleteBase> = T['_']['returning'] extends undefined ? T['_']['runResult'] : T['_']['returning'][];
  26. export type SQLiteDeletePrepare<T extends AnySQLiteDeleteBase> = SQLitePreparedQuery<{
  27. type: T['_']['resultType'];
  28. run: T['_']['runResult'];
  29. all: T['_']['returning'] extends undefined ? DrizzleTypeError<'.all() cannot be used without .returning()'> : T['_']['returning'][];
  30. get: T['_']['returning'] extends undefined ? DrizzleTypeError<'.get() cannot be used without .returning()'> : T['_']['returning'] | undefined;
  31. values: T['_']['returning'] extends undefined ? DrizzleTypeError<'.values() cannot be used without .returning()'> : any[][];
  32. execute: SQLiteDeleteExecute<T>;
  33. }>;
  34. export type SQLiteDeleteDynamic<T extends AnySQLiteDeleteBase> = SQLiteDelete<T['_']['table'], T['_']['resultType'], T['_']['runResult'], T['_']['returning']>;
  35. export type AnySQLiteDeleteBase = SQLiteDeleteBase<any, any, any, any, any, any>;
  36. export interface SQLiteDeleteBase<TTable extends SQLiteTable, TResultType extends 'sync' | 'async', TRunResult, TReturning extends Record<string, unknown> | undefined = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise<TReturning extends undefined ? TRunResult : TReturning[]>, RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], 'sqlite'>, SQLWrapper {
  37. readonly _: {
  38. dialect: 'sqlite';
  39. readonly table: TTable;
  40. readonly resultType: TResultType;
  41. readonly runResult: TRunResult;
  42. readonly returning: TReturning;
  43. readonly dynamic: TDynamic;
  44. readonly excludedMethods: TExcludedMethods;
  45. readonly result: TReturning extends undefined ? TRunResult : TReturning[];
  46. };
  47. }
  48. export declare class SQLiteDeleteBase<TTable extends SQLiteTable, TResultType extends 'sync' | 'async', TRunResult, TReturning extends Record<string, unknown> | undefined = 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 {
  49. private table;
  50. private session;
  51. private dialect;
  52. static readonly [entityKind]: string;
  53. constructor(table: TTable, session: SQLiteSession<any, any, any, any>, dialect: SQLiteDialect, withList?: Subquery[]);
  54. /**
  55. * Adds a `where` clause to the query.
  56. *
  57. * Calling this method will delete only those rows that fulfill a specified condition.
  58. *
  59. * See docs: {@link https://orm.drizzle.team/docs/delete}
  60. *
  61. * @param where the `where` clause.
  62. *
  63. * @example
  64. * You can use conditional operators and `sql function` to filter the rows to be deleted.
  65. *
  66. * ```ts
  67. * // Delete all cars with green color
  68. * db.delete(cars).where(eq(cars.color, 'green'));
  69. * // or
  70. * db.delete(cars).where(sql`${cars.color} = 'green'`)
  71. * ```
  72. *
  73. * You can logically combine conditional operators with `and()` and `or()` operators:
  74. *
  75. * ```ts
  76. * // Delete all BMW cars with a green color
  77. * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  78. *
  79. * // Delete all cars with the green or blue color
  80. * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  81. * ```
  82. */
  83. where(where: SQL | undefined): SQLiteDeleteWithout<this, TDynamic, 'where'>;
  84. orderBy(builder: (deleteTable: TTable) => ValueOrArray<SQLiteColumn | SQL | SQL.Aliased>): SQLiteDeleteWithout<this, TDynamic, 'orderBy'>;
  85. orderBy(...columns: (SQLiteColumn | SQL | SQL.Aliased)[]): SQLiteDeleteWithout<this, TDynamic, 'orderBy'>;
  86. limit(limit: number | Placeholder): SQLiteDeleteWithout<this, TDynamic, 'limit'>;
  87. /**
  88. * Adds a `returning` clause to the query.
  89. *
  90. * Calling this method will return the specified fields of the deleted rows. If no fields are specified, all fields will be returned.
  91. *
  92. * See docs: {@link https://orm.drizzle.team/docs/delete#delete-with-return}
  93. *
  94. * @example
  95. * ```ts
  96. * // Delete all cars with the green color and return all fields
  97. * const deletedCars: Car[] = await db.delete(cars)
  98. * .where(eq(cars.color, 'green'))
  99. * .returning();
  100. *
  101. * // Delete all cars with the green color and return only their id and brand fields
  102. * const deletedCarsIdsAndBrands: { id: number, brand: string }[] = await db.delete(cars)
  103. * .where(eq(cars.color, 'green'))
  104. * .returning({ id: cars.id, brand: cars.brand });
  105. * ```
  106. */
  107. returning(): SQLiteDeleteReturningAll<this, TDynamic>;
  108. returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): SQLiteDeleteReturning<this, TDynamic, TSelectedFields>;
  109. toSQL(): Query;
  110. prepare(): SQLiteDeletePrepare<this>;
  111. run: ReturnType<this['prepare']>['run'];
  112. all: ReturnType<this['prepare']>['all'];
  113. get: ReturnType<this['prepare']>['get'];
  114. values: ReturnType<this['prepare']>['values'];
  115. execute(placeholderValues?: Record<string, unknown>): Promise<SQLiteDeleteExecute<this>>;
  116. $dynamic(): SQLiteDeleteDynamic<this>;
  117. }