session.cjs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. NeonHttpPreparedQuery: () => NeonHttpPreparedQuery,
  22. NeonHttpSession: () => NeonHttpSession,
  23. NeonTransaction: () => NeonTransaction
  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_pg_core = require("../pg-core/index.cjs");
  30. var import_session = require("../pg-core/session.cjs");
  31. var import_sql = require("../sql/sql.cjs");
  32. var import_utils = require("../utils.cjs");
  33. const rawQueryConfig = {
  34. arrayMode: false,
  35. fullResults: true
  36. };
  37. const queryConfig = {
  38. arrayMode: true,
  39. fullResults: true
  40. };
  41. class NeonHttpPreparedQuery extends import_session.PgPreparedQuery {
  42. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  43. super(query, cache, queryMetadata, cacheConfig);
  44. this.client = client;
  45. this.logger = logger;
  46. this.fields = fields;
  47. this._isResponseInArrayMode = _isResponseInArrayMode;
  48. this.customResultMapper = customResultMapper;
  49. this.clientQuery = client.query ?? client;
  50. }
  51. static [import_entity.entityKind] = "NeonHttpPreparedQuery";
  52. clientQuery;
  53. /** @internal */
  54. async execute(placeholderValues = {}, token = this.authToken) {
  55. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues);
  56. this.logger.logQuery(this.query.sql, params);
  57. const { fields, clientQuery, query, customResultMapper } = this;
  58. if (!fields && !customResultMapper) {
  59. return this.queryWithCache(query.sql, params, async () => {
  60. return clientQuery(
  61. query.sql,
  62. params,
  63. token === void 0 ? rawQueryConfig : {
  64. ...rawQueryConfig,
  65. authToken: token
  66. }
  67. );
  68. });
  69. }
  70. const result = await this.queryWithCache(query.sql, params, async () => {
  71. return await clientQuery(
  72. query.sql,
  73. params,
  74. token === void 0 ? queryConfig : {
  75. ...queryConfig,
  76. authToken: token
  77. }
  78. );
  79. });
  80. return this.mapResult(result);
  81. }
  82. mapResult(result) {
  83. if (!this.fields && !this.customResultMapper) {
  84. return result;
  85. }
  86. const rows = result.rows;
  87. if (this.customResultMapper) {
  88. return this.customResultMapper(rows);
  89. }
  90. return rows.map((row) => (0, import_utils.mapResultRow)(this.fields, row, this.joinsNotNullableMap));
  91. }
  92. all(placeholderValues = {}) {
  93. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues);
  94. this.logger.logQuery(this.query.sql, params);
  95. return this.clientQuery(
  96. this.query.sql,
  97. params,
  98. this.authToken === void 0 ? rawQueryConfig : {
  99. ...rawQueryConfig,
  100. authToken: this.authToken
  101. }
  102. ).then((result) => result.rows);
  103. }
  104. /** @internal */
  105. values(placeholderValues = {}, token) {
  106. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues);
  107. this.logger.logQuery(this.query.sql, params);
  108. return this.clientQuery(this.query.sql, params, { arrayMode: true, fullResults: true, authToken: token }).then((result) => result.rows);
  109. }
  110. /** @internal */
  111. isResponseInArrayMode() {
  112. return this._isResponseInArrayMode;
  113. }
  114. }
  115. class NeonHttpSession extends import_session.PgSession {
  116. constructor(client, dialect, schema, options = {}) {
  117. super(dialect);
  118. this.client = client;
  119. this.schema = schema;
  120. this.options = options;
  121. this.clientQuery = client.query ?? client;
  122. this.logger = options.logger ?? new import_logger.NoopLogger();
  123. this.cache = options.cache ?? new import_core.NoopCache();
  124. }
  125. static [import_entity.entityKind] = "NeonHttpSession";
  126. clientQuery;
  127. logger;
  128. cache;
  129. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  130. return new NeonHttpPreparedQuery(
  131. this.client,
  132. query,
  133. this.logger,
  134. this.cache,
  135. queryMetadata,
  136. cacheConfig,
  137. fields,
  138. isResponseInArrayMode,
  139. customResultMapper
  140. );
  141. }
  142. async batch(queries) {
  143. const preparedQueries = [];
  144. const builtQueries = [];
  145. for (const query of queries) {
  146. const preparedQuery = query._prepare();
  147. const builtQuery = preparedQuery.getQuery();
  148. preparedQueries.push(preparedQuery);
  149. builtQueries.push(
  150. this.clientQuery(builtQuery.sql, builtQuery.params, {
  151. fullResults: true,
  152. arrayMode: preparedQuery.isResponseInArrayMode()
  153. })
  154. );
  155. }
  156. const batchResults = await this.client.transaction(builtQueries, queryConfig);
  157. return batchResults.map((result, i) => preparedQueries[i].mapResult(result, true));
  158. }
  159. // change return type to QueryRows<true>
  160. async query(query, params) {
  161. this.logger.logQuery(query, params);
  162. const result = await this.clientQuery(query, params, { arrayMode: true, fullResults: true });
  163. return result;
  164. }
  165. // change return type to QueryRows<false>
  166. async queryObjects(query, params) {
  167. return this.clientQuery(query, params, { arrayMode: false, fullResults: true });
  168. }
  169. /** @internal */
  170. async count(sql, token) {
  171. const res = await this.execute(sql, token);
  172. return Number(
  173. res["rows"][0]["count"]
  174. );
  175. }
  176. async transaction(_transaction, _config = {}) {
  177. throw new Error("No transactions support in neon-http driver");
  178. }
  179. }
  180. class NeonTransaction extends import_pg_core.PgTransaction {
  181. static [import_entity.entityKind] = "NeonHttpTransaction";
  182. async transaction(_transaction) {
  183. throw new Error("No transactions support in neon-http driver");
  184. }
  185. }
  186. // Annotate the CommonJS export names for ESM import in node:
  187. 0 && (module.exports = {
  188. NeonHttpPreparedQuery,
  189. NeonHttpSession,
  190. NeonTransaction
  191. });
  192. //# sourceMappingURL=session.cjs.map