delete.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { entityKind } from "../../entity.js";
  2. import { QueryPromise } from "../../query-promise.js";
  3. import { SelectionProxyHandler } from "../../selection-proxy.js";
  4. import { getTableName, Table } from "../../table.js";
  5. import { tracer } from "../../tracing.js";
  6. import { orderSelectedFields } from "../../utils.js";
  7. import { extractUsedTable } from "../utils.js";
  8. class PgDeleteBase extends QueryPromise {
  9. constructor(table, session, dialect, withList) {
  10. super();
  11. this.session = session;
  12. this.dialect = dialect;
  13. this.config = { table, withList };
  14. }
  15. static [entityKind] = "PgDelete";
  16. config;
  17. cacheConfig;
  18. /**
  19. * Adds a `where` clause to the query.
  20. *
  21. * Calling this method will delete only those rows that fulfill a specified condition.
  22. *
  23. * See docs: {@link https://orm.drizzle.team/docs/delete}
  24. *
  25. * @param where the `where` clause.
  26. *
  27. * @example
  28. * You can use conditional operators and `sql function` to filter the rows to be deleted.
  29. *
  30. * ```ts
  31. * // Delete all cars with green color
  32. * await db.delete(cars).where(eq(cars.color, 'green'));
  33. * // or
  34. * await db.delete(cars).where(sql`${cars.color} = 'green'`)
  35. * ```
  36. *
  37. * You can logically combine conditional operators with `and()` and `or()` operators:
  38. *
  39. * ```ts
  40. * // Delete all BMW cars with a green color
  41. * await db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  42. *
  43. * // Delete all cars with the green or blue color
  44. * await db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  45. * ```
  46. */
  47. where(where) {
  48. this.config.where = where;
  49. return this;
  50. }
  51. returning(fields = this.config.table[Table.Symbol.Columns]) {
  52. this.config.returningFields = fields;
  53. this.config.returning = orderSelectedFields(fields);
  54. return this;
  55. }
  56. /** @internal */
  57. getSQL() {
  58. return this.dialect.buildDeleteQuery(this.config);
  59. }
  60. toSQL() {
  61. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  62. return rest;
  63. }
  64. /** @internal */
  65. _prepare(name) {
  66. return tracer.startActiveSpan("drizzle.prepareQuery", () => {
  67. return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name, true, void 0, {
  68. type: "delete",
  69. tables: extractUsedTable(this.config.table)
  70. }, this.cacheConfig);
  71. });
  72. }
  73. prepare(name) {
  74. return this._prepare(name);
  75. }
  76. authToken;
  77. /** @internal */
  78. setToken(token) {
  79. this.authToken = token;
  80. return this;
  81. }
  82. execute = (placeholderValues) => {
  83. return tracer.startActiveSpan("drizzle.operation", () => {
  84. return this._prepare().execute(placeholderValues, this.authToken);
  85. });
  86. };
  87. /** @internal */
  88. getSelectedFields() {
  89. return this.config.returningFields ? new Proxy(
  90. this.config.returningFields,
  91. new SelectionProxyHandler({
  92. alias: getTableName(this.config.table),
  93. sqlAliasedBehavior: "alias",
  94. sqlBehavior: "error"
  95. })
  96. ) : void 0;
  97. }
  98. $dynamic() {
  99. return this;
  100. }
  101. }
  102. export {
  103. PgDeleteBase
  104. };
  105. //# sourceMappingURL=delete.js.map