session.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { NoopCache } from "../cache/core/index.js";
  2. import { entityKind } from "../entity.js";
  3. import { NoopLogger } from "../logger.js";
  4. import { fillPlaceholders, sql } from "../sql/sql.js";
  5. import { SQLiteTransaction } from "../sqlite-core/index.js";
  6. import { SQLitePreparedQuery, SQLiteSession } from "../sqlite-core/session.js";
  7. import { mapResultRow } from "../utils.js";
  8. class SQLiteRemoteSession extends SQLiteSession {
  9. constructor(client, dialect, schema, batchCLient, options = {}) {
  10. super(dialect);
  11. this.client = client;
  12. this.schema = schema;
  13. this.batchCLient = batchCLient;
  14. this.logger = options.logger ?? new NoopLogger();
  15. this.cache = options.cache ?? new NoopCache();
  16. }
  17. static [entityKind] = "SQLiteRemoteSession";
  18. logger;
  19. cache;
  20. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  21. return new RemotePreparedQuery(
  22. this.client,
  23. query,
  24. this.logger,
  25. this.cache,
  26. queryMetadata,
  27. cacheConfig,
  28. fields,
  29. executeMethod,
  30. isResponseInArrayMode,
  31. customResultMapper
  32. );
  33. }
  34. async batch(queries) {
  35. const preparedQueries = [];
  36. const builtQueries = [];
  37. for (const query of queries) {
  38. const preparedQuery = query._prepare();
  39. const builtQuery = preparedQuery.getQuery();
  40. preparedQueries.push(preparedQuery);
  41. builtQueries.push({ sql: builtQuery.sql, params: builtQuery.params, method: builtQuery.method });
  42. }
  43. const batchResults = await this.batchCLient(builtQueries);
  44. return batchResults.map((result, i) => preparedQueries[i].mapResult(result, true));
  45. }
  46. async transaction(transaction, config) {
  47. const tx = new SQLiteProxyTransaction("async", this.dialect, this, this.schema);
  48. await this.run(sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
  49. try {
  50. const result = await transaction(tx);
  51. await this.run(sql`commit`);
  52. return result;
  53. } catch (err) {
  54. await this.run(sql`rollback`);
  55. throw err;
  56. }
  57. }
  58. extractRawAllValueFromBatchResult(result) {
  59. return result.rows;
  60. }
  61. extractRawGetValueFromBatchResult(result) {
  62. return result.rows[0];
  63. }
  64. extractRawValuesValueFromBatchResult(result) {
  65. return result.rows;
  66. }
  67. }
  68. class SQLiteProxyTransaction extends SQLiteTransaction {
  69. static [entityKind] = "SQLiteProxyTransaction";
  70. async transaction(transaction) {
  71. const savepointName = `sp${this.nestedIndex}`;
  72. const tx = new SQLiteProxyTransaction("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  73. await this.session.run(sql.raw(`savepoint ${savepointName}`));
  74. try {
  75. const result = await transaction(tx);
  76. await this.session.run(sql.raw(`release savepoint ${savepointName}`));
  77. return result;
  78. } catch (err) {
  79. await this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
  80. throw err;
  81. }
  82. }
  83. }
  84. class RemotePreparedQuery extends SQLitePreparedQuery {
  85. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  86. super("async", executeMethod, query, cache, queryMetadata, cacheConfig);
  87. this.client = client;
  88. this.logger = logger;
  89. this.fields = fields;
  90. this._isResponseInArrayMode = _isResponseInArrayMode;
  91. this.customResultMapper = customResultMapper;
  92. this.customResultMapper = customResultMapper;
  93. this.method = executeMethod;
  94. }
  95. static [entityKind] = "SQLiteProxyPreparedQuery";
  96. method;
  97. getQuery() {
  98. return { ...this.query, method: this.method };
  99. }
  100. async run(placeholderValues) {
  101. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  102. this.logger.logQuery(this.query.sql, params);
  103. return await this.queryWithCache(this.query.sql, params, async () => {
  104. return await this.client(this.query.sql, params, "run");
  105. });
  106. }
  107. mapAllResult(rows, isFromBatch) {
  108. if (isFromBatch) {
  109. rows = rows.rows;
  110. }
  111. if (!this.fields && !this.customResultMapper) {
  112. return rows;
  113. }
  114. if (this.customResultMapper) {
  115. return this.customResultMapper(rows);
  116. }
  117. return rows.map((row) => {
  118. return mapResultRow(
  119. this.fields,
  120. row,
  121. this.joinsNotNullableMap
  122. );
  123. });
  124. }
  125. async all(placeholderValues) {
  126. const { query, logger, client } = this;
  127. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  128. logger.logQuery(query.sql, params);
  129. const { rows } = await this.queryWithCache(query.sql, params, async () => {
  130. return await client(query.sql, params, "all");
  131. });
  132. return this.mapAllResult(rows);
  133. }
  134. async get(placeholderValues) {
  135. const { query, logger, client } = this;
  136. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  137. logger.logQuery(query.sql, params);
  138. const clientResult = await this.queryWithCache(query.sql, params, async () => {
  139. return await client(query.sql, params, "get");
  140. });
  141. return this.mapGetResult(clientResult.rows);
  142. }
  143. mapGetResult(rows, isFromBatch) {
  144. if (isFromBatch) {
  145. rows = rows.rows;
  146. }
  147. const row = rows;
  148. if (!this.fields && !this.customResultMapper) {
  149. return row;
  150. }
  151. if (!row) {
  152. return void 0;
  153. }
  154. if (this.customResultMapper) {
  155. return this.customResultMapper([rows]);
  156. }
  157. return mapResultRow(
  158. this.fields,
  159. row,
  160. this.joinsNotNullableMap
  161. );
  162. }
  163. async values(placeholderValues) {
  164. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  165. this.logger.logQuery(this.query.sql, params);
  166. const clientResult = await this.queryWithCache(this.query.sql, params, async () => {
  167. return await this.client(this.query.sql, params, "values");
  168. });
  169. return clientResult.rows;
  170. }
  171. /** @internal */
  172. isResponseInArrayMode() {
  173. return this._isResponseInArrayMode;
  174. }
  175. }
  176. export {
  177. RemotePreparedQuery,
  178. SQLiteProxyTransaction,
  179. SQLiteRemoteSession
  180. };
  181. //# sourceMappingURL=session.js.map