update.cjs 8.5 KB

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