update.js 3.5 KB

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