update.cjs 4.7 KB

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