session.cjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. SingleStorePreparedQuery: () => SingleStorePreparedQuery,
  22. SingleStoreSession: () => SingleStoreSession,
  23. SingleStoreTransaction: () => SingleStoreTransaction
  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/sql.cjs");
  30. var import_db = require("./db.cjs");
  31. class SingleStorePreparedQuery {
  32. constructor(cache, queryMetadata, cacheConfig) {
  33. this.cache = cache;
  34. this.queryMetadata = queryMetadata;
  35. this.cacheConfig = cacheConfig;
  36. if (cache && cache.strategy() === "all" && cacheConfig === void 0) {
  37. this.cacheConfig = { enable: true, autoInvalidate: true };
  38. }
  39. if (!this.cacheConfig?.enable) {
  40. this.cacheConfig = void 0;
  41. }
  42. }
  43. static [import_entity.entityKind] = "SingleStorePreparedQuery";
  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. /** @internal */
  111. joinsNotNullableMap;
  112. }
  113. class SingleStoreSession {
  114. constructor(dialect) {
  115. this.dialect = dialect;
  116. }
  117. static [import_entity.entityKind] = "SingleStoreSession";
  118. execute(query) {
  119. return this.prepareQuery(
  120. this.dialect.sqlToQuery(query),
  121. void 0
  122. ).execute();
  123. }
  124. async count(sql2) {
  125. const res = await this.execute(sql2);
  126. return Number(
  127. res[0][0]["count"]
  128. );
  129. }
  130. getSetTransactionSQL(config) {
  131. const parts = [];
  132. if (config.isolationLevel) {
  133. parts.push(`isolation level ${config.isolationLevel}`);
  134. }
  135. return parts.length ? import_sql.sql`set transaction ${import_sql.sql.raw(parts.join(" "))}` : void 0;
  136. }
  137. getStartTransactionSQL(config) {
  138. const parts = [];
  139. if (config.withConsistentSnapshot) {
  140. parts.push("with consistent snapshot");
  141. }
  142. if (config.accessMode) {
  143. parts.push(config.accessMode);
  144. }
  145. return parts.length ? import_sql.sql`start transaction ${import_sql.sql.raw(parts.join(" "))}` : void 0;
  146. }
  147. }
  148. class SingleStoreTransaction extends import_db.SingleStoreDatabase {
  149. constructor(dialect, session, schema, nestedIndex) {
  150. super(dialect, session, schema);
  151. this.schema = schema;
  152. this.nestedIndex = nestedIndex;
  153. }
  154. static [import_entity.entityKind] = "SingleStoreTransaction";
  155. rollback() {
  156. throw new import_errors.TransactionRollbackError();
  157. }
  158. }
  159. // Annotate the CommonJS export names for ESM import in node:
  160. 0 && (module.exports = {
  161. SingleStorePreparedQuery,
  162. SingleStoreSession,
  163. SingleStoreTransaction
  164. });
  165. //# sourceMappingURL=session.cjs.map