delete.cjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. PgDeleteBase: () => PgDeleteBase
  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_tracing = require("../../tracing.cjs");
  29. var import_utils = require("../../utils.cjs");
  30. var import_utils2 = require("../utils.cjs");
  31. class PgDeleteBase extends import_query_promise.QueryPromise {
  32. constructor(table, session, dialect, withList) {
  33. super();
  34. this.session = session;
  35. this.dialect = dialect;
  36. this.config = { table, withList };
  37. }
  38. static [import_entity.entityKind] = "PgDelete";
  39. config;
  40. cacheConfig;
  41. /**
  42. * Adds a `where` clause to the query.
  43. *
  44. * Calling this method will delete only those rows that fulfill a specified condition.
  45. *
  46. * See docs: {@link https://orm.drizzle.team/docs/delete}
  47. *
  48. * @param where the `where` clause.
  49. *
  50. * @example
  51. * You can use conditional operators and `sql function` to filter the rows to be deleted.
  52. *
  53. * ```ts
  54. * // Delete all cars with green color
  55. * await db.delete(cars).where(eq(cars.color, 'green'));
  56. * // or
  57. * await db.delete(cars).where(sql`${cars.color} = 'green'`)
  58. * ```
  59. *
  60. * You can logically combine conditional operators with `and()` and `or()` operators:
  61. *
  62. * ```ts
  63. * // Delete all BMW cars with a green color
  64. * await db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  65. *
  66. * // Delete all cars with the green or blue color
  67. * await db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  68. * ```
  69. */
  70. where(where) {
  71. this.config.where = where;
  72. return this;
  73. }
  74. returning(fields = this.config.table[import_table.Table.Symbol.Columns]) {
  75. this.config.returningFields = fields;
  76. this.config.returning = (0, import_utils.orderSelectedFields)(fields);
  77. return this;
  78. }
  79. /** @internal */
  80. getSQL() {
  81. return this.dialect.buildDeleteQuery(this.config);
  82. }
  83. toSQL() {
  84. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  85. return rest;
  86. }
  87. /** @internal */
  88. _prepare(name) {
  89. return import_tracing.tracer.startActiveSpan("drizzle.prepareQuery", () => {
  90. return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name, true, void 0, {
  91. type: "delete",
  92. tables: (0, import_utils2.extractUsedTable)(this.config.table)
  93. }, this.cacheConfig);
  94. });
  95. }
  96. prepare(name) {
  97. return this._prepare(name);
  98. }
  99. authToken;
  100. /** @internal */
  101. setToken(token) {
  102. this.authToken = token;
  103. return this;
  104. }
  105. execute = (placeholderValues) => {
  106. return import_tracing.tracer.startActiveSpan("drizzle.operation", () => {
  107. return this._prepare().execute(placeholderValues, this.authToken);
  108. });
  109. };
  110. /** @internal */
  111. getSelectedFields() {
  112. return this.config.returningFields ? new Proxy(
  113. this.config.returningFields,
  114. new import_selection_proxy.SelectionProxyHandler({
  115. alias: (0, import_table.getTableName)(this.config.table),
  116. sqlAliasedBehavior: "alias",
  117. sqlBehavior: "error"
  118. })
  119. ) : void 0;
  120. }
  121. $dynamic() {
  122. return this;
  123. }
  124. }
  125. // Annotate the CommonJS export names for ESM import in node:
  126. 0 && (module.exports = {
  127. PgDeleteBase
  128. });
  129. //# sourceMappingURL=delete.cjs.map