session.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { sql } from "../sql/sql.js";
  5. import { SingleStoreDatabase } from "./db.js";
  6. class SingleStorePreparedQuery {
  7. constructor(cache, queryMetadata, cacheConfig) {
  8. this.cache = cache;
  9. this.queryMetadata = queryMetadata;
  10. this.cacheConfig = cacheConfig;
  11. if (cache && cache.strategy() === "all" && cacheConfig === void 0) {
  12. this.cacheConfig = { enable: true, autoInvalidate: true };
  13. }
  14. if (!this.cacheConfig?.enable) {
  15. this.cacheConfig = void 0;
  16. }
  17. }
  18. static [entityKind] = "SingleStorePreparedQuery";
  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. /** @internal */
  86. joinsNotNullableMap;
  87. }
  88. class SingleStoreSession {
  89. constructor(dialect) {
  90. this.dialect = dialect;
  91. }
  92. static [entityKind] = "SingleStoreSession";
  93. execute(query) {
  94. return this.prepareQuery(
  95. this.dialect.sqlToQuery(query),
  96. void 0
  97. ).execute();
  98. }
  99. async count(sql2) {
  100. const res = await this.execute(sql2);
  101. return Number(
  102. res[0][0]["count"]
  103. );
  104. }
  105. getSetTransactionSQL(config) {
  106. const parts = [];
  107. if (config.isolationLevel) {
  108. parts.push(`isolation level ${config.isolationLevel}`);
  109. }
  110. return parts.length ? sql`set transaction ${sql.raw(parts.join(" "))}` : void 0;
  111. }
  112. getStartTransactionSQL(config) {
  113. const parts = [];
  114. if (config.withConsistentSnapshot) {
  115. parts.push("with consistent snapshot");
  116. }
  117. if (config.accessMode) {
  118. parts.push(config.accessMode);
  119. }
  120. return parts.length ? sql`start transaction ${sql.raw(parts.join(" "))}` : void 0;
  121. }
  122. }
  123. class SingleStoreTransaction extends SingleStoreDatabase {
  124. constructor(dialect, session, schema, nestedIndex) {
  125. super(dialect, session, schema);
  126. this.schema = schema;
  127. this.nestedIndex = nestedIndex;
  128. }
  129. static [entityKind] = "SingleStoreTransaction";
  130. rollback() {
  131. throw new TransactionRollbackError();
  132. }
  133. }
  134. export {
  135. SingleStorePreparedQuery,
  136. SingleStoreSession,
  137. SingleStoreTransaction
  138. };
  139. //# sourceMappingURL=session.js.map