session.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { hashQuery, NoopCache } from "../cache/core/cache.js";
  2. import { entityKind, is } from "../entity.js";
  3. import { DrizzleQueryError, TransactionRollbackError } from "../errors.js";
  4. import { tracer } from "../tracing.js";
  5. import { GelDatabase } from "./db.js";
  6. class GelPreparedQuery {
  7. constructor(query, cache, queryMetadata, cacheConfig) {
  8. this.query = query;
  9. this.cache = cache;
  10. this.queryMetadata = queryMetadata;
  11. this.cacheConfig = cacheConfig;
  12. if (cache && cache.strategy() === "all" && cacheConfig === void 0) {
  13. this.cacheConfig = { enable: true, autoInvalidate: true };
  14. }
  15. if (!this.cacheConfig?.enable) {
  16. this.cacheConfig = void 0;
  17. }
  18. }
  19. /** @internal */
  20. async queryWithCache(queryString, params, query) {
  21. if (this.cache === void 0 || is(this.cache, NoopCache) || this.queryMetadata === void 0) {
  22. try {
  23. return await query();
  24. } catch (e) {
  25. throw new DrizzleQueryError(queryString, params, e);
  26. }
  27. }
  28. if (this.cacheConfig && !this.cacheConfig.enable) {
  29. try {
  30. return await query();
  31. } catch (e) {
  32. throw new DrizzleQueryError(queryString, params, e);
  33. }
  34. }
  35. if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0) {
  36. try {
  37. const [res] = await Promise.all([
  38. query(),
  39. this.cache.onMutate({ tables: this.queryMetadata.tables })
  40. ]);
  41. return res;
  42. } catch (e) {
  43. throw new DrizzleQueryError(queryString, params, e);
  44. }
  45. }
  46. if (!this.cacheConfig) {
  47. try {
  48. return await query();
  49. } catch (e) {
  50. throw new DrizzleQueryError(queryString, params, e);
  51. }
  52. }
  53. if (this.queryMetadata.type === "select") {
  54. const fromCache = await this.cache.get(
  55. this.cacheConfig.tag ?? (await hashQuery(queryString, params)),
  56. this.queryMetadata.tables,
  57. this.cacheConfig.tag !== void 0,
  58. this.cacheConfig.autoInvalidate
  59. );
  60. if (fromCache === void 0) {
  61. let result;
  62. try {
  63. result = await query();
  64. } catch (e) {
  65. throw new DrizzleQueryError(queryString, params, e);
  66. }
  67. await this.cache.put(
  68. this.cacheConfig.tag ?? (await hashQuery(queryString, params)),
  69. result,
  70. // make sure we send tables that were used in a query only if user wants to invalidate it on each write
  71. this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [],
  72. this.cacheConfig.tag !== void 0,
  73. this.cacheConfig.config
  74. );
  75. return result;
  76. }
  77. return fromCache;
  78. }
  79. try {
  80. return await query();
  81. } catch (e) {
  82. throw new DrizzleQueryError(queryString, params, e);
  83. }
  84. }
  85. authToken;
  86. getQuery() {
  87. return this.query;
  88. }
  89. mapResult(response, _isFromBatch) {
  90. return response;
  91. }
  92. static [entityKind] = "GelPreparedQuery";
  93. /** @internal */
  94. joinsNotNullableMap;
  95. }
  96. class GelSession {
  97. constructor(dialect) {
  98. this.dialect = dialect;
  99. }
  100. static [entityKind] = "GelSession";
  101. execute(query) {
  102. return tracer.startActiveSpan("drizzle.operation", () => {
  103. const prepared = tracer.startActiveSpan("drizzle.prepareQuery", () => {
  104. return this.prepareQuery(
  105. this.dialect.sqlToQuery(query),
  106. void 0,
  107. void 0,
  108. false
  109. );
  110. });
  111. return prepared.execute(void 0);
  112. });
  113. }
  114. all(query) {
  115. return this.prepareQuery(
  116. this.dialect.sqlToQuery(query),
  117. void 0,
  118. void 0,
  119. false
  120. ).all();
  121. }
  122. async count(sql) {
  123. const res = await this.execute(sql);
  124. return Number(
  125. res[0]["count"]
  126. );
  127. }
  128. }
  129. class GelTransaction extends GelDatabase {
  130. constructor(dialect, session, schema) {
  131. super(dialect, session, schema);
  132. this.schema = schema;
  133. }
  134. static [entityKind] = "GelTransaction";
  135. rollback() {
  136. throw new TransactionRollbackError();
  137. }
  138. }
  139. export {
  140. GelPreparedQuery,
  141. GelSession,
  142. GelTransaction
  143. };
  144. //# sourceMappingURL=session.js.map