session.cjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. ExecuteResultSync: () => ExecuteResultSync,
  22. SQLitePreparedQuery: () => SQLitePreparedQuery,
  23. SQLiteSession: () => SQLiteSession,
  24. SQLiteTransaction: () => SQLiteTransaction
  25. });
  26. module.exports = __toCommonJS(session_exports);
  27. var import_cache = require("../cache/core/cache.cjs");
  28. var import_entity = require("../entity.cjs");
  29. var import_errors = require("../errors.cjs");
  30. var import_query_promise = require("../query-promise.cjs");
  31. var import_db = require("./db.cjs");
  32. class ExecuteResultSync extends import_query_promise.QueryPromise {
  33. constructor(resultCb) {
  34. super();
  35. this.resultCb = resultCb;
  36. }
  37. static [import_entity.entityKind] = "ExecuteResultSync";
  38. async execute() {
  39. return this.resultCb();
  40. }
  41. sync() {
  42. return this.resultCb();
  43. }
  44. }
  45. class SQLitePreparedQuery {
  46. constructor(mode, executeMethod, query, cache, queryMetadata, cacheConfig) {
  47. this.mode = mode;
  48. this.executeMethod = executeMethod;
  49. this.query = query;
  50. this.cache = cache;
  51. this.queryMetadata = queryMetadata;
  52. this.cacheConfig = cacheConfig;
  53. if (cache && cache.strategy() === "all" && cacheConfig === void 0) {
  54. this.cacheConfig = { enable: true, autoInvalidate: true };
  55. }
  56. if (!this.cacheConfig?.enable) {
  57. this.cacheConfig = void 0;
  58. }
  59. }
  60. static [import_entity.entityKind] = "PreparedQuery";
  61. /** @internal */
  62. joinsNotNullableMap;
  63. /** @internal */
  64. async queryWithCache(queryString, params, query) {
  65. if (this.cache === void 0 || (0, import_entity.is)(this.cache, import_cache.NoopCache) || this.queryMetadata === void 0) {
  66. try {
  67. return await query();
  68. } catch (e) {
  69. throw new import_errors.DrizzleQueryError(queryString, params, e);
  70. }
  71. }
  72. if (this.cacheConfig && !this.cacheConfig.enable) {
  73. try {
  74. return await query();
  75. } catch (e) {
  76. throw new import_errors.DrizzleQueryError(queryString, params, e);
  77. }
  78. }
  79. if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0) {
  80. try {
  81. const [res] = await Promise.all([
  82. query(),
  83. this.cache.onMutate({ tables: this.queryMetadata.tables })
  84. ]);
  85. return res;
  86. } catch (e) {
  87. throw new import_errors.DrizzleQueryError(queryString, params, e);
  88. }
  89. }
  90. if (!this.cacheConfig) {
  91. try {
  92. return await query();
  93. } catch (e) {
  94. throw new import_errors.DrizzleQueryError(queryString, params, e);
  95. }
  96. }
  97. if (this.queryMetadata.type === "select") {
  98. const fromCache = await this.cache.get(
  99. this.cacheConfig.tag ?? (await (0, import_cache.hashQuery)(queryString, params)),
  100. this.queryMetadata.tables,
  101. this.cacheConfig.tag !== void 0,
  102. this.cacheConfig.autoInvalidate
  103. );
  104. if (fromCache === void 0) {
  105. let result;
  106. try {
  107. result = await query();
  108. } catch (e) {
  109. throw new import_errors.DrizzleQueryError(queryString, params, e);
  110. }
  111. await this.cache.put(
  112. this.cacheConfig.tag ?? (await (0, import_cache.hashQuery)(queryString, params)),
  113. result,
  114. // make sure we send tables that were used in a query only if user wants to invalidate it on each write
  115. this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [],
  116. this.cacheConfig.tag !== void 0,
  117. this.cacheConfig.config
  118. );
  119. return result;
  120. }
  121. return fromCache;
  122. }
  123. try {
  124. return await query();
  125. } catch (e) {
  126. throw new import_errors.DrizzleQueryError(queryString, params, e);
  127. }
  128. }
  129. getQuery() {
  130. return this.query;
  131. }
  132. mapRunResult(result, _isFromBatch) {
  133. return result;
  134. }
  135. mapAllResult(_result, _isFromBatch) {
  136. throw new Error("Not implemented");
  137. }
  138. mapGetResult(_result, _isFromBatch) {
  139. throw new Error("Not implemented");
  140. }
  141. execute(placeholderValues) {
  142. if (this.mode === "async") {
  143. return this[this.executeMethod](placeholderValues);
  144. }
  145. return new ExecuteResultSync(() => this[this.executeMethod](placeholderValues));
  146. }
  147. mapResult(response, isFromBatch) {
  148. switch (this.executeMethod) {
  149. case "run": {
  150. return this.mapRunResult(response, isFromBatch);
  151. }
  152. case "all": {
  153. return this.mapAllResult(response, isFromBatch);
  154. }
  155. case "get": {
  156. return this.mapGetResult(response, isFromBatch);
  157. }
  158. }
  159. }
  160. }
  161. class SQLiteSession {
  162. constructor(dialect) {
  163. this.dialect = dialect;
  164. }
  165. static [import_entity.entityKind] = "SQLiteSession";
  166. prepareOneTimeQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  167. return this.prepareQuery(
  168. query,
  169. fields,
  170. executeMethod,
  171. isResponseInArrayMode,
  172. customResultMapper,
  173. queryMetadata,
  174. cacheConfig
  175. );
  176. }
  177. run(query) {
  178. const staticQuery = this.dialect.sqlToQuery(query);
  179. try {
  180. return this.prepareOneTimeQuery(staticQuery, void 0, "run", false).run();
  181. } catch (err) {
  182. throw new import_errors.DrizzleError({ cause: err, message: `Failed to run the query '${staticQuery.sql}'` });
  183. }
  184. }
  185. /** @internal */
  186. extractRawRunValueFromBatchResult(result) {
  187. return result;
  188. }
  189. all(query) {
  190. return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), void 0, "run", false).all();
  191. }
  192. /** @internal */
  193. extractRawAllValueFromBatchResult(_result) {
  194. throw new Error("Not implemented");
  195. }
  196. get(query) {
  197. return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), void 0, "run", false).get();
  198. }
  199. /** @internal */
  200. extractRawGetValueFromBatchResult(_result) {
  201. throw new Error("Not implemented");
  202. }
  203. values(query) {
  204. return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), void 0, "run", false).values();
  205. }
  206. async count(sql) {
  207. const result = await this.values(sql);
  208. return result[0][0];
  209. }
  210. /** @internal */
  211. extractRawValuesValueFromBatchResult(_result) {
  212. throw new Error("Not implemented");
  213. }
  214. }
  215. class SQLiteTransaction extends import_db.BaseSQLiteDatabase {
  216. constructor(resultType, dialect, session, schema, nestedIndex = 0) {
  217. super(resultType, dialect, session, schema);
  218. this.schema = schema;
  219. this.nestedIndex = nestedIndex;
  220. }
  221. static [import_entity.entityKind] = "SQLiteTransaction";
  222. rollback() {
  223. throw new import_errors.TransactionRollbackError();
  224. }
  225. }
  226. // Annotate the CommonJS export names for ESM import in node:
  227. 0 && (module.exports = {
  228. ExecuteResultSync,
  229. SQLitePreparedQuery,
  230. SQLiteSession,
  231. SQLiteTransaction
  232. });
  233. //# sourceMappingURL=session.cjs.map