delete.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { entityKind } from "../../entity.js";
  2. import { QueryPromise } from "../../query-promise.js";
  3. import { SelectionProxyHandler } from "../../selection-proxy.js";
  4. import { Table } from "../../table.js";
  5. import { extractUsedTable } from "../utils.js";
  6. class MySqlDeleteBase extends QueryPromise {
  7. constructor(table, session, dialect, withList) {
  8. super();
  9. this.table = table;
  10. this.session = session;
  11. this.dialect = dialect;
  12. this.config = { table, withList };
  13. }
  14. static [entityKind] = "MySqlDelete";
  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. * db.delete(cars).where(eq(cars.color, 'green'));
  31. * // or
  32. * 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. * 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. * 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. orderBy(...columns) {
  50. if (typeof columns[0] === "function") {
  51. const orderBy = columns[0](
  52. new Proxy(
  53. this.config.table[Table.Symbol.Columns],
  54. new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
  55. )
  56. );
  57. const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
  58. this.config.orderBy = orderByArray;
  59. } else {
  60. const orderByArray = columns;
  61. this.config.orderBy = orderByArray;
  62. }
  63. return this;
  64. }
  65. limit(limit) {
  66. this.config.limit = limit;
  67. return this;
  68. }
  69. /** @internal */
  70. getSQL() {
  71. return this.dialect.buildDeleteQuery(this.config);
  72. }
  73. toSQL() {
  74. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  75. return rest;
  76. }
  77. prepare() {
  78. return this.session.prepareQuery(
  79. this.dialect.sqlToQuery(this.getSQL()),
  80. this.config.returning,
  81. void 0,
  82. void 0,
  83. void 0,
  84. {
  85. type: "delete",
  86. tables: extractUsedTable(this.config.table)
  87. }
  88. );
  89. }
  90. execute = (placeholderValues) => {
  91. return this.prepare().execute(placeholderValues);
  92. };
  93. createIterator = () => {
  94. const self = this;
  95. return async function* (placeholderValues) {
  96. yield* self.prepare().iterator(placeholderValues);
  97. };
  98. };
  99. iterator = this.createIterator();
  100. $dynamic() {
  101. return this;
  102. }
  103. }
  104. export {
  105. MySqlDeleteBase
  106. };
  107. //# sourceMappingURL=delete.js.map