session.cjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. GelPreparedQuery: () => GelPreparedQuery,
  22. GelSession: () => GelSession,
  23. GelTransaction: () => GelTransaction
  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_tracing = require("../tracing.cjs");
  30. var import_db = require("./db.cjs");
  31. class GelPreparedQuery {
  32. constructor(query, cache, queryMetadata, cacheConfig) {
  33. this.query = query;
  34. this.cache = cache;
  35. this.queryMetadata = queryMetadata;
  36. this.cacheConfig = cacheConfig;
  37. if (cache && cache.strategy() === "all" && cacheConfig === void 0) {
  38. this.cacheConfig = { enable: true, autoInvalidate: true };
  39. }
  40. if (!this.cacheConfig?.enable) {
  41. this.cacheConfig = void 0;
  42. }
  43. }
  44. /** @internal */
  45. async queryWithCache(queryString, params, query) {
  46. if (this.cache === void 0 || (0, import_entity.is)(this.cache, import_cache.NoopCache) || this.queryMetadata === void 0) {
  47. try {
  48. return await query();
  49. } catch (e) {
  50. throw new import_errors.DrizzleQueryError(queryString, params, e);
  51. }
  52. }
  53. if (this.cacheConfig && !this.cacheConfig.enable) {
  54. try {
  55. return await query();
  56. } catch (e) {
  57. throw new import_errors.DrizzleQueryError(queryString, params, e);
  58. }
  59. }
  60. if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0) {
  61. try {
  62. const [res] = await Promise.all([
  63. query(),
  64. this.cache.onMutate({ tables: this.queryMetadata.tables })
  65. ]);
  66. return res;
  67. } catch (e) {
  68. throw new import_errors.DrizzleQueryError(queryString, params, e);
  69. }
  70. }
  71. if (!this.cacheConfig) {
  72. try {
  73. return await query();
  74. } catch (e) {
  75. throw new import_errors.DrizzleQueryError(queryString, params, e);
  76. }
  77. }
  78. if (this.queryMetadata.type === "select") {
  79. const fromCache = await this.cache.get(
  80. this.cacheConfig.tag ?? (await (0, import_cache.hashQuery)(queryString, params)),
  81. this.queryMetadata.tables,
  82. this.cacheConfig.tag !== void 0,
  83. this.cacheConfig.autoInvalidate
  84. );
  85. if (fromCache === void 0) {
  86. let result;
  87. try {
  88. result = await query();
  89. } catch (e) {
  90. throw new import_errors.DrizzleQueryError(queryString, params, e);
  91. }
  92. await this.cache.put(
  93. this.cacheConfig.tag ?? (await (0, import_cache.hashQuery)(queryString, params)),
  94. result,
  95. // make sure we send tables that were used in a query only if user wants to invalidate it on each write
  96. this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [],
  97. this.cacheConfig.tag !== void 0,
  98. this.cacheConfig.config
  99. );
  100. return result;
  101. }
  102. return fromCache;
  103. }
  104. try {
  105. return await query();
  106. } catch (e) {
  107. throw new import_errors.DrizzleQueryError(queryString, params, e);
  108. }
  109. }
  110. authToken;
  111. getQuery() {
  112. return this.query;
  113. }
  114. mapResult(response, _isFromBatch) {
  115. return response;
  116. }
  117. static [import_entity.entityKind] = "GelPreparedQuery";
  118. /** @internal */
  119. joinsNotNullableMap;
  120. }
  121. class GelSession {
  122. constructor(dialect) {
  123. this.dialect = dialect;
  124. }
  125. static [import_entity.entityKind] = "GelSession";
  126. execute(query) {
  127. return import_tracing.tracer.startActiveSpan("drizzle.operation", () => {
  128. const prepared = import_tracing.tracer.startActiveSpan("drizzle.prepareQuery", () => {
  129. return this.prepareQuery(
  130. this.dialect.sqlToQuery(query),
  131. void 0,
  132. void 0,
  133. false
  134. );
  135. });
  136. return prepared.execute(void 0);
  137. });
  138. }
  139. all(query) {
  140. return this.prepareQuery(
  141. this.dialect.sqlToQuery(query),
  142. void 0,
  143. void 0,
  144. false
  145. ).all();
  146. }
  147. async count(sql) {
  148. const res = await this.execute(sql);
  149. return Number(
  150. res[0]["count"]
  151. );
  152. }
  153. }
  154. class GelTransaction extends import_db.GelDatabase {
  155. constructor(dialect, session, schema) {
  156. super(dialect, session, schema);
  157. this.schema = schema;
  158. }
  159. static [import_entity.entityKind] = "GelTransaction";
  160. rollback() {
  161. throw new import_errors.TransactionRollbackError();
  162. }
  163. }
  164. // Annotate the CommonJS export names for ESM import in node:
  165. 0 && (module.exports = {
  166. GelPreparedQuery,
  167. GelSession,
  168. GelTransaction
  169. });
  170. //# sourceMappingURL=session.cjs.map