delete.cjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var delete_exports = {};
  20. __export(delete_exports, {
  21. MySqlDeleteBase: () => MySqlDeleteBase
  22. });
  23. module.exports = __toCommonJS(delete_exports);
  24. var import_entity = require("../../entity.cjs");
  25. var import_query_promise = require("../../query-promise.cjs");
  26. var import_selection_proxy = require("../../selection-proxy.cjs");
  27. var import_table = require("../../table.cjs");
  28. var import_utils = require("../utils.cjs");
  29. class MySqlDeleteBase extends import_query_promise.QueryPromise {
  30. constructor(table, session, dialect, withList) {
  31. super();
  32. this.table = table;
  33. this.session = session;
  34. this.dialect = dialect;
  35. this.config = { table, withList };
  36. }
  37. static [import_entity.entityKind] = "MySqlDelete";
  38. config;
  39. /**
  40. * Adds a `where` clause to the query.
  41. *
  42. * Calling this method will delete only those rows that fulfill a specified condition.
  43. *
  44. * See docs: {@link https://orm.drizzle.team/docs/delete}
  45. *
  46. * @param where the `where` clause.
  47. *
  48. * @example
  49. * You can use conditional operators and `sql function` to filter the rows to be deleted.
  50. *
  51. * ```ts
  52. * // Delete all cars with green color
  53. * db.delete(cars).where(eq(cars.color, 'green'));
  54. * // or
  55. * db.delete(cars).where(sql`${cars.color} = 'green'`)
  56. * ```
  57. *
  58. * You can logically combine conditional operators with `and()` and `or()` operators:
  59. *
  60. * ```ts
  61. * // Delete all BMW cars with a green color
  62. * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  63. *
  64. * // Delete all cars with the green or blue color
  65. * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  66. * ```
  67. */
  68. where(where) {
  69. this.config.where = where;
  70. return this;
  71. }
  72. orderBy(...columns) {
  73. if (typeof columns[0] === "function") {
  74. const orderBy = columns[0](
  75. new Proxy(
  76. this.config.table[import_table.Table.Symbol.Columns],
  77. new import_selection_proxy.SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
  78. )
  79. );
  80. const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
  81. this.config.orderBy = orderByArray;
  82. } else {
  83. const orderByArray = columns;
  84. this.config.orderBy = orderByArray;
  85. }
  86. return this;
  87. }
  88. limit(limit) {
  89. this.config.limit = limit;
  90. return this;
  91. }
  92. /** @internal */
  93. getSQL() {
  94. return this.dialect.buildDeleteQuery(this.config);
  95. }
  96. toSQL() {
  97. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  98. return rest;
  99. }
  100. prepare() {
  101. return this.session.prepareQuery(
  102. this.dialect.sqlToQuery(this.getSQL()),
  103. this.config.returning,
  104. void 0,
  105. void 0,
  106. void 0,
  107. {
  108. type: "delete",
  109. tables: (0, import_utils.extractUsedTable)(this.config.table)
  110. }
  111. );
  112. }
  113. execute = (placeholderValues) => {
  114. return this.prepare().execute(placeholderValues);
  115. };
  116. createIterator = () => {
  117. const self = this;
  118. return async function* (placeholderValues) {
  119. yield* self.prepare().iterator(placeholderValues);
  120. };
  121. };
  122. iterator = this.createIterator();
  123. $dynamic() {
  124. return this;
  125. }
  126. }
  127. // Annotate the CommonJS export names for ESM import in node:
  128. 0 && (module.exports = {
  129. MySqlDeleteBase
  130. });
  131. //# sourceMappingURL=delete.cjs.map