session.cjs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. RemotePreparedQuery: () => RemotePreparedQuery,
  22. SQLiteProxyTransaction: () => SQLiteProxyTransaction,
  23. SQLiteRemoteSession: () => SQLiteRemoteSession
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_core = require("../cache/core/index.cjs");
  27. var import_entity = require("../entity.cjs");
  28. var import_logger = require("../logger.cjs");
  29. var import_sql = require("../sql/sql.cjs");
  30. var import_sqlite_core = require("../sqlite-core/index.cjs");
  31. var import_session = require("../sqlite-core/session.cjs");
  32. var import_utils = require("../utils.cjs");
  33. class SQLiteRemoteSession extends import_session.SQLiteSession {
  34. constructor(client, dialect, schema, batchCLient, options = {}) {
  35. super(dialect);
  36. this.client = client;
  37. this.schema = schema;
  38. this.batchCLient = batchCLient;
  39. this.logger = options.logger ?? new import_logger.NoopLogger();
  40. this.cache = options.cache ?? new import_core.NoopCache();
  41. }
  42. static [import_entity.entityKind] = "SQLiteRemoteSession";
  43. logger;
  44. cache;
  45. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  46. return new RemotePreparedQuery(
  47. this.client,
  48. query,
  49. this.logger,
  50. this.cache,
  51. queryMetadata,
  52. cacheConfig,
  53. fields,
  54. executeMethod,
  55. isResponseInArrayMode,
  56. customResultMapper
  57. );
  58. }
  59. async batch(queries) {
  60. const preparedQueries = [];
  61. const builtQueries = [];
  62. for (const query of queries) {
  63. const preparedQuery = query._prepare();
  64. const builtQuery = preparedQuery.getQuery();
  65. preparedQueries.push(preparedQuery);
  66. builtQueries.push({ sql: builtQuery.sql, params: builtQuery.params, method: builtQuery.method });
  67. }
  68. const batchResults = await this.batchCLient(builtQueries);
  69. return batchResults.map((result, i) => preparedQueries[i].mapResult(result, true));
  70. }
  71. async transaction(transaction, config) {
  72. const tx = new SQLiteProxyTransaction("async", this.dialect, this, this.schema);
  73. await this.run(import_sql.sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
  74. try {
  75. const result = await transaction(tx);
  76. await this.run(import_sql.sql`commit`);
  77. return result;
  78. } catch (err) {
  79. await this.run(import_sql.sql`rollback`);
  80. throw err;
  81. }
  82. }
  83. extractRawAllValueFromBatchResult(result) {
  84. return result.rows;
  85. }
  86. extractRawGetValueFromBatchResult(result) {
  87. return result.rows[0];
  88. }
  89. extractRawValuesValueFromBatchResult(result) {
  90. return result.rows;
  91. }
  92. }
  93. class SQLiteProxyTransaction extends import_sqlite_core.SQLiteTransaction {
  94. static [import_entity.entityKind] = "SQLiteProxyTransaction";
  95. async transaction(transaction) {
  96. const savepointName = `sp${this.nestedIndex}`;
  97. const tx = new SQLiteProxyTransaction("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  98. await this.session.run(import_sql.sql.raw(`savepoint ${savepointName}`));
  99. try {
  100. const result = await transaction(tx);
  101. await this.session.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
  102. return result;
  103. } catch (err) {
  104. await this.session.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  105. throw err;
  106. }
  107. }
  108. }
  109. class RemotePreparedQuery extends import_session.SQLitePreparedQuery {
  110. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  111. super("async", executeMethod, query, cache, queryMetadata, cacheConfig);
  112. this.client = client;
  113. this.logger = logger;
  114. this.fields = fields;
  115. this._isResponseInArrayMode = _isResponseInArrayMode;
  116. this.customResultMapper = customResultMapper;
  117. this.customResultMapper = customResultMapper;
  118. this.method = executeMethod;
  119. }
  120. static [import_entity.entityKind] = "SQLiteProxyPreparedQuery";
  121. method;
  122. getQuery() {
  123. return { ...this.query, method: this.method };
  124. }
  125. async run(placeholderValues) {
  126. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  127. this.logger.logQuery(this.query.sql, params);
  128. return await this.queryWithCache(this.query.sql, params, async () => {
  129. return await this.client(this.query.sql, params, "run");
  130. });
  131. }
  132. mapAllResult(rows, isFromBatch) {
  133. if (isFromBatch) {
  134. rows = rows.rows;
  135. }
  136. if (!this.fields && !this.customResultMapper) {
  137. return rows;
  138. }
  139. if (this.customResultMapper) {
  140. return this.customResultMapper(rows);
  141. }
  142. return rows.map((row) => {
  143. return (0, import_utils.mapResultRow)(
  144. this.fields,
  145. row,
  146. this.joinsNotNullableMap
  147. );
  148. });
  149. }
  150. async all(placeholderValues) {
  151. const { query, logger, client } = this;
  152. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  153. logger.logQuery(query.sql, params);
  154. const { rows } = await this.queryWithCache(query.sql, params, async () => {
  155. return await client(query.sql, params, "all");
  156. });
  157. return this.mapAllResult(rows);
  158. }
  159. async get(placeholderValues) {
  160. const { query, logger, client } = this;
  161. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  162. logger.logQuery(query.sql, params);
  163. const clientResult = await this.queryWithCache(query.sql, params, async () => {
  164. return await client(query.sql, params, "get");
  165. });
  166. return this.mapGetResult(clientResult.rows);
  167. }
  168. mapGetResult(rows, isFromBatch) {
  169. if (isFromBatch) {
  170. rows = rows.rows;
  171. }
  172. const row = rows;
  173. if (!this.fields && !this.customResultMapper) {
  174. return row;
  175. }
  176. if (!row) {
  177. return void 0;
  178. }
  179. if (this.customResultMapper) {
  180. return this.customResultMapper([rows]);
  181. }
  182. return (0, import_utils.mapResultRow)(
  183. this.fields,
  184. row,
  185. this.joinsNotNullableMap
  186. );
  187. }
  188. async values(placeholderValues) {
  189. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  190. this.logger.logQuery(this.query.sql, params);
  191. const clientResult = await this.queryWithCache(this.query.sql, params, async () => {
  192. return await this.client(this.query.sql, params, "values");
  193. });
  194. return clientResult.rows;
  195. }
  196. /** @internal */
  197. isResponseInArrayMode() {
  198. return this._isResponseInArrayMode;
  199. }
  200. }
  201. // Annotate the CommonJS export names for ESM import in node:
  202. 0 && (module.exports = {
  203. RemotePreparedQuery,
  204. SQLiteProxyTransaction,
  205. SQLiteRemoteSession
  206. });
  207. //# sourceMappingURL=session.cjs.map