delete.cjs 4.6 KB

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