update.cjs 8.0 KB

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