delete.d.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import type { WithCacheConfig } from "../../cache/core/types.js";
  2. import { entityKind } from "../../entity.js";
  3. import type { PgDialect } from "../dialect.js";
  4. import type { PgPreparedQuery, PgQueryResultHKT, PgQueryResultKind, PgSession, PreparedQueryConfig } from "../session.js";
  5. import type { PgTable } from "../table.js";
  6. import type { TypedQueryBuilder } from "../../query-builders/query-builder.js";
  7. import type { SelectResultFields } from "../../query-builders/select.types.js";
  8. import { QueryPromise } from "../../query-promise.js";
  9. import type { RunnableQuery } from "../../runnable-query.js";
  10. import type { ColumnsSelection, Query, SQL, SQLWrapper } from "../../sql/sql.js";
  11. import type { Subquery } from "../../subquery.js";
  12. import type { SelectedFieldsFlat, SelectedFieldsOrdered } from "./select.types.js";
  13. export type PgDeleteWithout<T extends AnyPgDeleteBase, TDynamic extends boolean, K extends keyof T & string> = TDynamic extends true ? T : Omit<PgDeleteBase<T['_']['table'], T['_']['queryResult'], T['_']['selectedFields'], T['_']['returning'], TDynamic, T['_']['excludedMethods'] | K>, T['_']['excludedMethods'] | K>;
  14. export type PgDelete<TTable extends PgTable = PgTable, TQueryResult extends PgQueryResultHKT = PgQueryResultHKT, TSelectedFields extends ColumnsSelection | undefined = undefined, TReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined> = PgDeleteBase<TTable, TQueryResult, TSelectedFields, TReturning, true, never>;
  15. export interface PgDeleteConfig {
  16. where?: SQL | undefined;
  17. table: PgTable;
  18. returningFields?: SelectedFieldsFlat;
  19. returning?: SelectedFieldsOrdered;
  20. withList?: Subquery[];
  21. }
  22. export type PgDeleteReturningAll<T extends AnyPgDeleteBase, TDynamic extends boolean> = PgDeleteWithout<PgDeleteBase<T['_']['table'], T['_']['queryResult'], T['_']['table']['_']['columns'], T['_']['table']['$inferSelect'], TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  23. export type PgDeleteReturning<T extends AnyPgDeleteBase, TDynamic extends boolean, TSelectedFields extends SelectedFieldsFlat> = PgDeleteWithout<PgDeleteBase<T['_']['table'], T['_']['queryResult'], TSelectedFields, SelectResultFields<TSelectedFields>, TDynamic, T['_']['excludedMethods']>, TDynamic, 'returning'>;
  24. export type PgDeletePrepare<T extends AnyPgDeleteBase> = PgPreparedQuery<PreparedQueryConfig & {
  25. execute: T['_']['returning'] extends undefined ? PgQueryResultKind<T['_']['queryResult'], never> : T['_']['returning'][];
  26. }>;
  27. export type PgDeleteDynamic<T extends AnyPgDeleteBase> = PgDelete<T['_']['table'], T['_']['queryResult'], T['_']['selectedFields'], T['_']['returning']>;
  28. export type AnyPgDeleteBase = PgDeleteBase<any, any, any, any, any, any>;
  29. export interface PgDeleteBase<TTable extends PgTable, TQueryResult extends PgQueryResultHKT, TSelectedFields extends ColumnsSelection | undefined = undefined, TReturning extends Record<string, unknown> | undefined = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends TypedQueryBuilder<TSelectedFields, TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[]>, QueryPromise<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[]>, RunnableQuery<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[], 'pg'>, SQLWrapper {
  30. readonly _: {
  31. readonly dialect: 'pg';
  32. readonly table: TTable;
  33. readonly queryResult: TQueryResult;
  34. readonly selectedFields: TSelectedFields;
  35. readonly returning: TReturning;
  36. readonly dynamic: TDynamic;
  37. readonly excludedMethods: TExcludedMethods;
  38. readonly result: TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[];
  39. };
  40. }
  41. export declare class PgDeleteBase<TTable extends PgTable, TQueryResult extends PgQueryResultHKT, TSelectedFields extends ColumnsSelection | undefined = undefined, TReturning extends Record<string, unknown> | undefined = undefined, TDynamic extends boolean = false, TExcludedMethods extends string = never> extends QueryPromise<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[]> implements TypedQueryBuilder<TSelectedFields, TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[]>, RunnableQuery<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[], 'pg'>, SQLWrapper {
  42. private session;
  43. private dialect;
  44. static readonly [entityKind]: string;
  45. private config;
  46. protected cacheConfig?: WithCacheConfig;
  47. constructor(table: TTable, session: PgSession, dialect: PgDialect, withList?: Subquery[]);
  48. /**
  49. * Adds a `where` clause to the query.
  50. *
  51. * Calling this method will delete only those rows that fulfill a specified condition.
  52. *
  53. * See docs: {@link https://orm.drizzle.team/docs/delete}
  54. *
  55. * @param where the `where` clause.
  56. *
  57. * @example
  58. * You can use conditional operators and `sql function` to filter the rows to be deleted.
  59. *
  60. * ```ts
  61. * // Delete all cars with green color
  62. * await db.delete(cars).where(eq(cars.color, 'green'));
  63. * // or
  64. * await db.delete(cars).where(sql`${cars.color} = 'green'`)
  65. * ```
  66. *
  67. * You can logically combine conditional operators with `and()` and `or()` operators:
  68. *
  69. * ```ts
  70. * // Delete all BMW cars with a green color
  71. * await db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  72. *
  73. * // Delete all cars with the green or blue color
  74. * await db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  75. * ```
  76. */
  77. where(where: SQL | undefined): PgDeleteWithout<this, TDynamic, 'where'>;
  78. /**
  79. * Adds a `returning` clause to the query.
  80. *
  81. * Calling this method will return the specified fields of the deleted rows. If no fields are specified, all fields will be returned.
  82. *
  83. * See docs: {@link https://orm.drizzle.team/docs/delete#delete-with-return}
  84. *
  85. * @example
  86. * ```ts
  87. * // Delete all cars with the green color and return all fields
  88. * const deletedCars: Car[] = await db.delete(cars)
  89. * .where(eq(cars.color, 'green'))
  90. * .returning();
  91. *
  92. * // Delete all cars with the green color and return only their id and brand fields
  93. * const deletedCarsIdsAndBrands: { id: number, brand: string }[] = await db.delete(cars)
  94. * .where(eq(cars.color, 'green'))
  95. * .returning({ id: cars.id, brand: cars.brand });
  96. * ```
  97. */
  98. returning(): PgDeleteReturningAll<this, TDynamic>;
  99. returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): PgDeleteReturning<this, TDynamic, TSelectedFields>;
  100. toSQL(): Query;
  101. prepare(name: string): PgDeletePrepare<this>;
  102. private authToken?;
  103. execute: ReturnType<this['prepare']>['execute'];
  104. $dynamic(): PgDeleteDynamic<this>;
  105. }