update.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { entityKind, is } 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 { Subquery } from "../../subquery.js";
  6. import { Table } from "../../table.js";
  7. import {
  8. getTableLikeName,
  9. mapUpdateSet,
  10. orderSelectedFields
  11. } from "../../utils.js";
  12. import { ViewBaseConfig } from "../../view-common.js";
  13. import { extractUsedTable } from "../utils.js";
  14. import { SQLiteViewBase } from "../view-base.js";
  15. class SQLiteUpdateBuilder {
  16. constructor(table, session, dialect, withList) {
  17. this.table = table;
  18. this.session = session;
  19. this.dialect = dialect;
  20. this.withList = withList;
  21. }
  22. static [entityKind] = "SQLiteUpdateBuilder";
  23. set(values) {
  24. return new SQLiteUpdateBase(
  25. this.table,
  26. mapUpdateSet(this.table, values),
  27. this.session,
  28. this.dialect,
  29. this.withList
  30. );
  31. }
  32. }
  33. class SQLiteUpdateBase extends QueryPromise {
  34. constructor(table, set, session, dialect, withList) {
  35. super();
  36. this.session = session;
  37. this.dialect = dialect;
  38. this.config = { set, table, withList, joins: [] };
  39. }
  40. static [entityKind] = "SQLiteUpdate";
  41. /** @internal */
  42. config;
  43. from(source) {
  44. this.config.from = source;
  45. return this;
  46. }
  47. createJoin(joinType) {
  48. return (table, on) => {
  49. const tableName = getTableLikeName(table);
  50. if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
  51. throw new Error(`Alias "${tableName}" is already used in this query`);
  52. }
  53. if (typeof on === "function") {
  54. const from = this.config.from ? is(table, SQLiteTable) ? table[Table.Symbol.Columns] : is(table, Subquery) ? table._.selectedFields : is(table, SQLiteViewBase) ? table[ViewBaseConfig].selectedFields : void 0 : void 0;
  55. on = on(
  56. new Proxy(
  57. this.config.table[Table.Symbol.Columns],
  58. new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
  59. ),
  60. from && new Proxy(
  61. from,
  62. new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
  63. )
  64. );
  65. }
  66. this.config.joins.push({ on, table, joinType, alias: tableName });
  67. return this;
  68. };
  69. }
  70. leftJoin = this.createJoin("left");
  71. rightJoin = this.createJoin("right");
  72. innerJoin = this.createJoin("inner");
  73. fullJoin = this.createJoin("full");
  74. /**
  75. * Adds a 'where' clause to the query.
  76. *
  77. * Calling this method will update only those rows that fulfill a specified condition.
  78. *
  79. * See docs: {@link https://orm.drizzle.team/docs/update}
  80. *
  81. * @param where the 'where' clause.
  82. *
  83. * @example
  84. * You can use conditional operators and `sql function` to filter the rows to be updated.
  85. *
  86. * ```ts
  87. * // Update all cars with green color
  88. * db.update(cars).set({ color: 'red' })
  89. * .where(eq(cars.color, 'green'));
  90. * // or
  91. * db.update(cars).set({ color: 'red' })
  92. * .where(sql`${cars.color} = 'green'`)
  93. * ```
  94. *
  95. * You can logically combine conditional operators with `and()` and `or()` operators:
  96. *
  97. * ```ts
  98. * // Update all BMW cars with a green color
  99. * db.update(cars).set({ color: 'red' })
  100. * .where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  101. *
  102. * // Update all cars with the green or blue color
  103. * db.update(cars).set({ color: 'red' })
  104. * .where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  105. * ```
  106. */
  107. where(where) {
  108. this.config.where = where;
  109. return this;
  110. }
  111. orderBy(...columns) {
  112. if (typeof columns[0] === "function") {
  113. const orderBy = columns[0](
  114. new Proxy(
  115. this.config.table[Table.Symbol.Columns],
  116. new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" })
  117. )
  118. );
  119. const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
  120. this.config.orderBy = orderByArray;
  121. } else {
  122. const orderByArray = columns;
  123. this.config.orderBy = orderByArray;
  124. }
  125. return this;
  126. }
  127. limit(limit) {
  128. this.config.limit = limit;
  129. return this;
  130. }
  131. returning(fields = this.config.table[SQLiteTable.Symbol.Columns]) {
  132. this.config.returning = orderSelectedFields(fields);
  133. return this;
  134. }
  135. /** @internal */
  136. getSQL() {
  137. return this.dialect.buildUpdateQuery(this.config);
  138. }
  139. toSQL() {
  140. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  141. return rest;
  142. }
  143. /** @internal */
  144. _prepare(isOneTimeQuery = true) {
  145. return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
  146. this.dialect.sqlToQuery(this.getSQL()),
  147. this.config.returning,
  148. this.config.returning ? "all" : "run",
  149. true,
  150. void 0,
  151. {
  152. type: "insert",
  153. tables: extractUsedTable(this.config.table)
  154. }
  155. );
  156. }
  157. prepare() {
  158. return this._prepare(false);
  159. }
  160. run = (placeholderValues) => {
  161. return this._prepare().run(placeholderValues);
  162. };
  163. all = (placeholderValues) => {
  164. return this._prepare().all(placeholderValues);
  165. };
  166. get = (placeholderValues) => {
  167. return this._prepare().get(placeholderValues);
  168. };
  169. values = (placeholderValues) => {
  170. return this._prepare().values(placeholderValues);
  171. };
  172. async execute() {
  173. return this.config.returning ? this.all() : this.run();
  174. }
  175. $dynamic() {
  176. return this;
  177. }
  178. }
  179. export {
  180. SQLiteUpdateBase,
  181. SQLiteUpdateBuilder
  182. };
  183. //# sourceMappingURL=update.js.map