session.cjs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 session_exports = {};
  20. __export(session_exports, {
  21. PgPreparedQuery: () => PgPreparedQuery,
  22. PgSession: () => PgSession,
  23. PgTransaction: () => PgTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_cache = require("../cache/core/cache.cjs");
  27. var import_entity = require("../entity.cjs");
  28. var import_errors = require("../errors.cjs");
  29. var import_sql = require("../sql/index.cjs");
  30. var import_tracing = require("../tracing.cjs");
  31. var import_db = require("./db.cjs");
  32. class PgPreparedQuery {
  33. constructor(query, cache, queryMetadata, cacheConfig) {
  34. this.query = query;
  35. this.cache = cache;
  36. this.queryMetadata = queryMetadata;
  37. this.cacheConfig = cacheConfig;
  38. if (cache && cache.strategy() === "all" && cacheConfig === void 0) {
  39. this.cacheConfig = { enable: true, autoInvalidate: true };
  40. }
  41. if (!this.cacheConfig?.enable) {
  42. this.cacheConfig = void 0;
  43. }
  44. }
  45. authToken;
  46. getQuery() {
  47. return this.query;
  48. }
  49. mapResult(response, _isFromBatch) {
  50. return response;
  51. }
  52. /** @internal */
  53. setToken(token) {
  54. this.authToken = token;
  55. return this;
  56. }
  57. static [import_entity.entityKind] = "PgPreparedQuery";
  58. /** @internal */
  59. joinsNotNullableMap;
  60. /** @internal */
  61. async queryWithCache(queryString, params, query) {
  62. if (this.cache === void 0 || (0, import_entity.is)(this.cache, import_cache.NoopCache) || this.queryMetadata === void 0) {
  63. try {
  64. return await query();
  65. } catch (e) {
  66. throw new import_errors.DrizzleQueryError(queryString, params, e);
  67. }
  68. }
  69. if (this.cacheConfig && !this.cacheConfig.enable) {
  70. try {
  71. return await query();
  72. } catch (e) {
  73. throw new import_errors.DrizzleQueryError(queryString, params, e);
  74. }
  75. }
  76. if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0) {
  77. try {
  78. const [res] = await Promise.all([
  79. query(),
  80. this.cache.onMutate({ tables: this.queryMetadata.tables })
  81. ]);
  82. return res;
  83. } catch (e) {
  84. throw new import_errors.DrizzleQueryError(queryString, params, e);
  85. }
  86. }
  87. if (!this.cacheConfig) {
  88. try {
  89. return await query();
  90. } catch (e) {
  91. throw new import_errors.DrizzleQueryError(queryString, params, e);
  92. }
  93. }
  94. if (this.queryMetadata.type === "select") {
  95. const fromCache = await this.cache.get(
  96. this.cacheConfig.tag ?? (await (0, import_cache.hashQuery)(queryString, params)),
  97. this.queryMetadata.tables,
  98. this.cacheConfig.tag !== void 0,
  99. this.cacheConfig.autoInvalidate
  100. );
  101. if (fromCache === void 0) {
  102. let result;
  103. try {
  104. result = await query();
  105. } catch (e) {
  106. throw new import_errors.DrizzleQueryError(queryString, params, e);
  107. }
  108. await this.cache.put(
  109. this.cacheConfig.tag ?? (await (0, import_cache.hashQuery)(queryString, params)),
  110. result,
  111. // make sure we send tables that were used in a query only if user wants to invalidate it on each write
  112. this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [],
  113. this.cacheConfig.tag !== void 0,
  114. this.cacheConfig.config
  115. );
  116. return result;
  117. }
  118. return fromCache;
  119. }
  120. try {
  121. return await query();
  122. } catch (e) {
  123. throw new import_errors.DrizzleQueryError(queryString, params, e);
  124. }
  125. }
  126. }
  127. class PgSession {
  128. constructor(dialect) {
  129. this.dialect = dialect;
  130. }
  131. static [import_entity.entityKind] = "PgSession";
  132. /** @internal */
  133. execute(query, token) {
  134. return import_tracing.tracer.startActiveSpan("drizzle.operation", () => {
  135. const prepared = import_tracing.tracer.startActiveSpan("drizzle.prepareQuery", () => {
  136. return this.prepareQuery(
  137. this.dialect.sqlToQuery(query),
  138. void 0,
  139. void 0,
  140. false
  141. );
  142. });
  143. return prepared.setToken(token).execute(void 0, token);
  144. });
  145. }
  146. all(query) {
  147. return this.prepareQuery(
  148. this.dialect.sqlToQuery(query),
  149. void 0,
  150. void 0,
  151. false
  152. ).all();
  153. }
  154. /** @internal */
  155. async count(sql2, token) {
  156. const res = await this.execute(sql2, token);
  157. return Number(
  158. res[0]["count"]
  159. );
  160. }
  161. }
  162. class PgTransaction extends import_db.PgDatabase {
  163. constructor(dialect, session, schema, nestedIndex = 0) {
  164. super(dialect, session, schema);
  165. this.schema = schema;
  166. this.nestedIndex = nestedIndex;
  167. }
  168. static [import_entity.entityKind] = "PgTransaction";
  169. rollback() {
  170. throw new import_errors.TransactionRollbackError();
  171. }
  172. /** @internal */
  173. getTransactionConfigSQL(config) {
  174. const chunks = [];
  175. if (config.isolationLevel) {
  176. chunks.push(`isolation level ${config.isolationLevel}`);
  177. }
  178. if (config.accessMode) {
  179. chunks.push(config.accessMode);
  180. }
  181. if (typeof config.deferrable === "boolean") {
  182. chunks.push(config.deferrable ? "deferrable" : "not deferrable");
  183. }
  184. return import_sql.sql.raw(chunks.join(" "));
  185. }
  186. setTransaction(config) {
  187. return this.session.execute(import_sql.sql`set transaction ${this.getTransactionConfigSQL(config)}`);
  188. }
  189. }
  190. // Annotate the CommonJS export names for ESM import in node:
  191. 0 && (module.exports = {
  192. PgPreparedQuery,
  193. PgSession,
  194. PgTransaction
  195. });
  196. //# sourceMappingURL=session.cjs.map