update.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { entityKind, is } from "../../entity.js";
  2. import { PgTable } from "../table.js";
  3. import { QueryPromise } from "../../query-promise.js";
  4. import { SelectionProxyHandler } from "../../selection-proxy.js";
  5. import { SQL } from "../../sql/sql.js";
  6. import { Subquery } from "../../subquery.js";
  7. import { getTableName, Table } from "../../table.js";
  8. import {
  9. getTableLikeName,
  10. mapUpdateSet,
  11. orderSelectedFields
  12. } from "../../utils.js";
  13. import { ViewBaseConfig } from "../../view-common.js";
  14. import { extractUsedTable } from "../utils.js";
  15. class PgUpdateBuilder {
  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] = "PgUpdateBuilder";
  23. authToken;
  24. setToken(token) {
  25. this.authToken = token;
  26. return this;
  27. }
  28. set(values) {
  29. return new PgUpdateBase(
  30. this.table,
  31. mapUpdateSet(this.table, values),
  32. this.session,
  33. this.dialect,
  34. this.withList
  35. ).setToken(this.authToken);
  36. }
  37. }
  38. class PgUpdateBase extends QueryPromise {
  39. constructor(table, set, session, dialect, withList) {
  40. super();
  41. this.session = session;
  42. this.dialect = dialect;
  43. this.config = { set, table, withList, joins: [] };
  44. this.tableName = getTableLikeName(table);
  45. this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
  46. }
  47. static [entityKind] = "PgUpdate";
  48. config;
  49. tableName;
  50. joinsNotNullableMap;
  51. cacheConfig;
  52. from(source) {
  53. const src = source;
  54. const tableName = getTableLikeName(src);
  55. if (typeof tableName === "string") {
  56. this.joinsNotNullableMap[tableName] = true;
  57. }
  58. this.config.from = src;
  59. return this;
  60. }
  61. getTableLikeFields(table) {
  62. if (is(table, PgTable)) {
  63. return table[Table.Symbol.Columns];
  64. } else if (is(table, Subquery)) {
  65. return table._.selectedFields;
  66. }
  67. return table[ViewBaseConfig].selectedFields;
  68. }
  69. createJoin(joinType) {
  70. return (table, on) => {
  71. const tableName = getTableLikeName(table);
  72. if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
  73. throw new Error(`Alias "${tableName}" is already used in this query`);
  74. }
  75. if (typeof on === "function") {
  76. const from = this.config.from && !is(this.config.from, SQL) ? this.getTableLikeFields(this.config.from) : void 0;
  77. on = on(
  78. new Proxy(
  79. this.config.table[Table.Symbol.Columns],
  80. new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
  81. ),
  82. from && new Proxy(
  83. from,
  84. new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })
  85. )
  86. );
  87. }
  88. this.config.joins.push({ on, table, joinType, alias: tableName });
  89. if (typeof tableName === "string") {
  90. switch (joinType) {
  91. case "left": {
  92. this.joinsNotNullableMap[tableName] = false;
  93. break;
  94. }
  95. case "right": {
  96. this.joinsNotNullableMap = Object.fromEntries(
  97. Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
  98. );
  99. this.joinsNotNullableMap[tableName] = true;
  100. break;
  101. }
  102. case "inner": {
  103. this.joinsNotNullableMap[tableName] = true;
  104. break;
  105. }
  106. case "full": {
  107. this.joinsNotNullableMap = Object.fromEntries(
  108. Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false])
  109. );
  110. this.joinsNotNullableMap[tableName] = false;
  111. break;
  112. }
  113. }
  114. }
  115. return this;
  116. };
  117. }
  118. leftJoin = this.createJoin("left");
  119. rightJoin = this.createJoin("right");
  120. innerJoin = this.createJoin("inner");
  121. fullJoin = this.createJoin("full");
  122. /**
  123. * Adds a 'where' clause to the query.
  124. *
  125. * Calling this method will update only those rows that fulfill a specified condition.
  126. *
  127. * See docs: {@link https://orm.drizzle.team/docs/update}
  128. *
  129. * @param where the 'where' clause.
  130. *
  131. * @example
  132. * You can use conditional operators and `sql function` to filter the rows to be updated.
  133. *
  134. * ```ts
  135. * // Update all cars with green color
  136. * await db.update(cars).set({ color: 'red' })
  137. * .where(eq(cars.color, 'green'));
  138. * // or
  139. * await db.update(cars).set({ color: 'red' })
  140. * .where(sql`${cars.color} = 'green'`)
  141. * ```
  142. *
  143. * You can logically combine conditional operators with `and()` and `or()` operators:
  144. *
  145. * ```ts
  146. * // Update all BMW cars with a green color
  147. * await db.update(cars).set({ color: 'red' })
  148. * .where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  149. *
  150. * // Update all cars with the green or blue color
  151. * await db.update(cars).set({ color: 'red' })
  152. * .where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  153. * ```
  154. */
  155. where(where) {
  156. this.config.where = where;
  157. return this;
  158. }
  159. returning(fields) {
  160. if (!fields) {
  161. fields = Object.assign({}, this.config.table[Table.Symbol.Columns]);
  162. if (this.config.from) {
  163. const tableName = getTableLikeName(this.config.from);
  164. if (typeof tableName === "string" && this.config.from && !is(this.config.from, SQL)) {
  165. const fromFields = this.getTableLikeFields(this.config.from);
  166. fields[tableName] = fromFields;
  167. }
  168. for (const join of this.config.joins) {
  169. const tableName2 = getTableLikeName(join.table);
  170. if (typeof tableName2 === "string" && !is(join.table, SQL)) {
  171. const fromFields = this.getTableLikeFields(join.table);
  172. fields[tableName2] = fromFields;
  173. }
  174. }
  175. }
  176. }
  177. this.config.returningFields = fields;
  178. this.config.returning = orderSelectedFields(fields);
  179. return this;
  180. }
  181. /** @internal */
  182. getSQL() {
  183. return this.dialect.buildUpdateQuery(this.config);
  184. }
  185. toSQL() {
  186. const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
  187. return rest;
  188. }
  189. /** @internal */
  190. _prepare(name) {
  191. const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name, true, void 0, {
  192. type: "insert",
  193. tables: extractUsedTable(this.config.table)
  194. }, this.cacheConfig);
  195. query.joinsNotNullableMap = this.joinsNotNullableMap;
  196. return query;
  197. }
  198. prepare(name) {
  199. return this._prepare(name);
  200. }
  201. authToken;
  202. /** @internal */
  203. setToken(token) {
  204. this.authToken = token;
  205. return this;
  206. }
  207. execute = (placeholderValues) => {
  208. return this._prepare().execute(placeholderValues, this.authToken);
  209. };
  210. /** @internal */
  211. getSelectedFields() {
  212. return this.config.returningFields ? new Proxy(
  213. this.config.returningFields,
  214. new SelectionProxyHandler({
  215. alias: getTableName(this.config.table),
  216. sqlAliasedBehavior: "alias",
  217. sqlBehavior: "error"
  218. })
  219. ) : void 0;
  220. }
  221. $dynamic() {
  222. return this;
  223. }
  224. }
  225. export {
  226. PgUpdateBase,
  227. PgUpdateBuilder
  228. };
  229. //# sourceMappingURL=update.js.map