delete.js 2.5 KB

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