delete.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { entityKind } from "../../entity.js";
  2. import { QueryPromise } from "../../query-promise.js";
  3. import { SelectionProxyHandler } from "../../selection-proxy.js";
  4. import { SQLiteTable } from "../table.js";
  5. import { Table } from "../../table.js";
  6. import { orderSelectedFields } from "../../utils.js";
  7. import { extractUsedTable } from "../utils.js";
  8. class SQLiteDeleteBase extends QueryPromise {
  9. constructor(table, session, dialect, withList) {
  10. super();
  11. this.table = table;
  12. this.session = session;
  13. this.dialect = dialect;
  14. this.config = { table, withList };
  15. }
  16. static [entityKind] = "SQLiteDelete";
  17. /** @internal */
  18. config;
  19. /**
  20. * Adds a `where` clause to the query.
  21. *
  22. * Calling this method will delete only those rows that fulfill a specified condition.
  23. *
  24. * See docs: {@link https://orm.drizzle.team/docs/delete}
  25. *
  26. * @param where the `where` clause.
  27. *
  28. * @example
  29. * You can use conditional operators and `sql function` to filter the rows to be deleted.
  30. *
  31. * ```ts
  32. * // Delete all cars with green color
  33. * db.delete(cars).where(eq(cars.color, 'green'));
  34. * // or
  35. * db.delete(cars).where(sql`${cars.color} = 'green'`)
  36. * ```
  37. *
  38. * You can logically combine conditional operators with `and()` and `or()` operators:
  39. *
  40. * ```ts
  41. * // Delete all BMW cars with a green color
  42. * db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  43. *
  44. * // Delete all cars with the green or blue color
  45. * db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  46. * ```
  47. */
  48. where(where) {
  49. this.config.where = where;
  50. return this;
  51. }
  52. orderBy(...columns) {
  53. if (typeof columns[0] === "function") {
  54. const orderBy = columns[0](
  55. new Proxy(
  56. this.config.table[Table.Symbol.Columns],
  57. new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
  58. )
  59. );
  60. const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
  61. this.config.orderBy = orderByArray;
  62. } else {
  63. const orderByArray = columns;
  64. this.config.orderBy = orderByArray;
  65. }
  66. return this;
  67. }
  68. limit(limit) {
  69. this.config.limit = limit;
  70. return this;
  71. }
  72. returning(fields = this.table[SQLiteTable.Symbol.Columns]) {
  73. this.config.returning = orderSelectedFields(fields);
  74. return this;
  75. }
  76. /** @internal */
  77. getSQL() {
  78. return this.dialect.buildDeleteQuery(this.config);
  79. }
  80. toSQL() {
  81. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  82. return rest;
  83. }
  84. /** @internal */
  85. _prepare(isOneTimeQuery = true) {
  86. return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
  87. this.dialect.sqlToQuery(this.getSQL()),
  88. this.config.returning,
  89. this.config.returning ? "all" : "run",
  90. true,
  91. void 0,
  92. {
  93. type: "delete",
  94. tables: extractUsedTable(this.config.table)
  95. }
  96. );
  97. }
  98. prepare() {
  99. return this._prepare(false);
  100. }
  101. run = (placeholderValues) => {
  102. return this._prepare().run(placeholderValues);
  103. };
  104. all = (placeholderValues) => {
  105. return this._prepare().all(placeholderValues);
  106. };
  107. get = (placeholderValues) => {
  108. return this._prepare().get(placeholderValues);
  109. };
  110. values = (placeholderValues) => {
  111. return this._prepare().values(placeholderValues);
  112. };
  113. async execute(placeholderValues) {
  114. return this._prepare().execute(placeholderValues);
  115. }
  116. $dynamic() {
  117. return this;
  118. }
  119. }
  120. export {
  121. SQLiteDeleteBase
  122. };
  123. //# sourceMappingURL=delete.js.map