update.cjs 6.8 KB

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