session.cjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. XataHttpPreparedQuery: () => XataHttpPreparedQuery,
  22. XataHttpSession: () => XataHttpSession,
  23. XataTransaction: () => XataTransaction
  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. class XataHttpPreparedQuery extends import_session.PgPreparedQuery {
  34. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  35. super(query, cache, queryMetadata, cacheConfig);
  36. this.client = client;
  37. this.logger = logger;
  38. this.fields = fields;
  39. this._isResponseInArrayMode = _isResponseInArrayMode;
  40. this.customResultMapper = customResultMapper;
  41. }
  42. static [import_entity.entityKind] = "XataHttpPreparedQuery";
  43. async execute(placeholderValues = {}) {
  44. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues);
  45. this.logger.logQuery(this.query.sql, params);
  46. const { fields, client, query, customResultMapper, joinsNotNullableMap } = this;
  47. if (!fields && !customResultMapper) {
  48. return this.queryWithCache(query.sql, params, async () => {
  49. return await client.sql({ statement: query.sql, params });
  50. });
  51. }
  52. const { rows, warning } = await this.queryWithCache(query.sql, params, async () => {
  53. return await client.sql({ statement: query.sql, params, responseType: "array" });
  54. });
  55. if (warning) console.warn(warning);
  56. return customResultMapper ? customResultMapper(rows) : rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  57. }
  58. all(placeholderValues = {}) {
  59. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues);
  60. this.logger.logQuery(this.query.sql, params);
  61. return this.queryWithCache(this.query.sql, params, async () => {
  62. return this.client.sql({ statement: this.query.sql, params, responseType: "array" });
  63. }).then((result) => result.rows);
  64. }
  65. values(placeholderValues = {}) {
  66. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues);
  67. this.logger.logQuery(this.query.sql, params);
  68. return this.queryWithCache(this.query.sql, params, async () => {
  69. return this.client.sql({ statement: this.query.sql, params });
  70. }).then((result) => result.records);
  71. }
  72. /** @internal */
  73. isResponseInArrayMode() {
  74. return this._isResponseInArrayMode;
  75. }
  76. }
  77. class XataHttpSession extends import_session.PgSession {
  78. constructor(client, dialect, schema, options = {}) {
  79. super(dialect);
  80. this.client = client;
  81. this.schema = schema;
  82. this.options = options;
  83. this.logger = options.logger ?? new import_logger.NoopLogger();
  84. this.cache = options.cache ?? new import_core.NoopCache();
  85. }
  86. static [import_entity.entityKind] = "XataHttpSession";
  87. logger;
  88. cache;
  89. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  90. return new XataHttpPreparedQuery(
  91. this.client,
  92. query,
  93. this.logger,
  94. this.cache,
  95. queryMetadata,
  96. cacheConfig,
  97. fields,
  98. isResponseInArrayMode,
  99. customResultMapper
  100. );
  101. }
  102. async query(query, params) {
  103. this.logger.logQuery(query, params);
  104. const result = await this.client.sql({ statement: query, params, responseType: "array" });
  105. return {
  106. rowCount: result.rows.length,
  107. rows: result.rows,
  108. rowAsArray: true
  109. };
  110. }
  111. async queryObjects(query, params) {
  112. const result = await this.client.sql({ statement: query, params });
  113. return {
  114. rowCount: result.records.length,
  115. rows: result.records,
  116. rowAsArray: false
  117. };
  118. }
  119. async transaction(_transaction, _config = {}) {
  120. throw new Error("No transactions support in Xata Http driver");
  121. }
  122. }
  123. class XataTransaction extends import_pg_core.PgTransaction {
  124. static [import_entity.entityKind] = "XataHttpTransaction";
  125. async transaction(_transaction) {
  126. throw new Error("No transactions support in Xata Http driver");
  127. }
  128. }
  129. // Annotate the CommonJS export names for ESM import in node:
  130. 0 && (module.exports = {
  131. XataHttpPreparedQuery,
  132. XataHttpSession,
  133. XataTransaction
  134. });
  135. //# sourceMappingURL=session.cjs.map