session.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { NoopCache } from "../cache/core/index.js";
  2. import { entityKind } from "../entity.js";
  3. import { NoopLogger } from "../logger.js";
  4. import { PgTransaction } from "../pg-core/index.js";
  5. import { PgPreparedQuery, PgSession } from "../pg-core/session.js";
  6. import { fillPlaceholders } from "../sql/sql.js";
  7. import { mapResultRow } from "../utils.js";
  8. const rawQueryConfig = {
  9. arrayMode: false,
  10. fullResults: true
  11. };
  12. const queryConfig = {
  13. arrayMode: true,
  14. fullResults: true
  15. };
  16. class NeonHttpPreparedQuery extends PgPreparedQuery {
  17. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  18. super(query, cache, queryMetadata, cacheConfig);
  19. this.client = client;
  20. this.logger = logger;
  21. this.fields = fields;
  22. this._isResponseInArrayMode = _isResponseInArrayMode;
  23. this.customResultMapper = customResultMapper;
  24. this.clientQuery = client.query ?? client;
  25. }
  26. static [entityKind] = "NeonHttpPreparedQuery";
  27. clientQuery;
  28. /** @internal */
  29. async execute(placeholderValues = {}, token = this.authToken) {
  30. const params = fillPlaceholders(this.query.params, placeholderValues);
  31. this.logger.logQuery(this.query.sql, params);
  32. const { fields, clientQuery, query, customResultMapper } = this;
  33. if (!fields && !customResultMapper) {
  34. return this.queryWithCache(query.sql, params, async () => {
  35. return clientQuery(
  36. query.sql,
  37. params,
  38. token === void 0 ? rawQueryConfig : {
  39. ...rawQueryConfig,
  40. authToken: token
  41. }
  42. );
  43. });
  44. }
  45. const result = await this.queryWithCache(query.sql, params, async () => {
  46. return await clientQuery(
  47. query.sql,
  48. params,
  49. token === void 0 ? queryConfig : {
  50. ...queryConfig,
  51. authToken: token
  52. }
  53. );
  54. });
  55. return this.mapResult(result);
  56. }
  57. mapResult(result) {
  58. if (!this.fields && !this.customResultMapper) {
  59. return result;
  60. }
  61. const rows = result.rows;
  62. if (this.customResultMapper) {
  63. return this.customResultMapper(rows);
  64. }
  65. return rows.map((row) => mapResultRow(this.fields, row, this.joinsNotNullableMap));
  66. }
  67. all(placeholderValues = {}) {
  68. const params = fillPlaceholders(this.query.params, placeholderValues);
  69. this.logger.logQuery(this.query.sql, params);
  70. return this.clientQuery(
  71. this.query.sql,
  72. params,
  73. this.authToken === void 0 ? rawQueryConfig : {
  74. ...rawQueryConfig,
  75. authToken: this.authToken
  76. }
  77. ).then((result) => result.rows);
  78. }
  79. /** @internal */
  80. values(placeholderValues = {}, token) {
  81. const params = fillPlaceholders(this.query.params, placeholderValues);
  82. this.logger.logQuery(this.query.sql, params);
  83. return this.clientQuery(this.query.sql, params, { arrayMode: true, fullResults: true, authToken: token }).then((result) => result.rows);
  84. }
  85. /** @internal */
  86. isResponseInArrayMode() {
  87. return this._isResponseInArrayMode;
  88. }
  89. }
  90. class NeonHttpSession extends PgSession {
  91. constructor(client, dialect, schema, options = {}) {
  92. super(dialect);
  93. this.client = client;
  94. this.schema = schema;
  95. this.options = options;
  96. this.clientQuery = client.query ?? client;
  97. this.logger = options.logger ?? new NoopLogger();
  98. this.cache = options.cache ?? new NoopCache();
  99. }
  100. static [entityKind] = "NeonHttpSession";
  101. clientQuery;
  102. logger;
  103. cache;
  104. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  105. return new NeonHttpPreparedQuery(
  106. this.client,
  107. query,
  108. this.logger,
  109. this.cache,
  110. queryMetadata,
  111. cacheConfig,
  112. fields,
  113. isResponseInArrayMode,
  114. customResultMapper
  115. );
  116. }
  117. async batch(queries) {
  118. const preparedQueries = [];
  119. const builtQueries = [];
  120. for (const query of queries) {
  121. const preparedQuery = query._prepare();
  122. const builtQuery = preparedQuery.getQuery();
  123. preparedQueries.push(preparedQuery);
  124. builtQueries.push(
  125. this.clientQuery(builtQuery.sql, builtQuery.params, {
  126. fullResults: true,
  127. arrayMode: preparedQuery.isResponseInArrayMode()
  128. })
  129. );
  130. }
  131. const batchResults = await this.client.transaction(builtQueries, queryConfig);
  132. return batchResults.map((result, i) => preparedQueries[i].mapResult(result, true));
  133. }
  134. // change return type to QueryRows<true>
  135. async query(query, params) {
  136. this.logger.logQuery(query, params);
  137. const result = await this.clientQuery(query, params, { arrayMode: true, fullResults: true });
  138. return result;
  139. }
  140. // change return type to QueryRows<false>
  141. async queryObjects(query, params) {
  142. return this.clientQuery(query, params, { arrayMode: false, fullResults: true });
  143. }
  144. /** @internal */
  145. async count(sql, token) {
  146. const res = await this.execute(sql, token);
  147. return Number(
  148. res["rows"][0]["count"]
  149. );
  150. }
  151. async transaction(_transaction, _config = {}) {
  152. throw new Error("No transactions support in neon-http driver");
  153. }
  154. }
  155. class NeonTransaction extends PgTransaction {
  156. static [entityKind] = "NeonHttpTransaction";
  157. async transaction(_transaction) {
  158. throw new Error("No transactions support in neon-http driver");
  159. }
  160. }
  161. export {
  162. NeonHttpPreparedQuery,
  163. NeonHttpSession,
  164. NeonTransaction
  165. };
  166. //# sourceMappingURL=session.js.map