session.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class XataHttpPreparedQuery extends PgPreparedQuery {
  9. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  10. super(query, cache, queryMetadata, cacheConfig);
  11. this.client = client;
  12. this.logger = logger;
  13. this.fields = fields;
  14. this._isResponseInArrayMode = _isResponseInArrayMode;
  15. this.customResultMapper = customResultMapper;
  16. }
  17. static [entityKind] = "XataHttpPreparedQuery";
  18. async execute(placeholderValues = {}) {
  19. const params = fillPlaceholders(this.query.params, placeholderValues);
  20. this.logger.logQuery(this.query.sql, params);
  21. const { fields, client, query, customResultMapper, joinsNotNullableMap } = this;
  22. if (!fields && !customResultMapper) {
  23. return this.queryWithCache(query.sql, params, async () => {
  24. return await client.sql({ statement: query.sql, params });
  25. });
  26. }
  27. const { rows, warning } = await this.queryWithCache(query.sql, params, async () => {
  28. return await client.sql({ statement: query.sql, params, responseType: "array" });
  29. });
  30. if (warning) console.warn(warning);
  31. return customResultMapper ? customResultMapper(rows) : rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  32. }
  33. all(placeholderValues = {}) {
  34. const params = fillPlaceholders(this.query.params, placeholderValues);
  35. this.logger.logQuery(this.query.sql, params);
  36. return this.queryWithCache(this.query.sql, params, async () => {
  37. return this.client.sql({ statement: this.query.sql, params, responseType: "array" });
  38. }).then((result) => result.rows);
  39. }
  40. values(placeholderValues = {}) {
  41. const params = fillPlaceholders(this.query.params, placeholderValues);
  42. this.logger.logQuery(this.query.sql, params);
  43. return this.queryWithCache(this.query.sql, params, async () => {
  44. return this.client.sql({ statement: this.query.sql, params });
  45. }).then((result) => result.records);
  46. }
  47. /** @internal */
  48. isResponseInArrayMode() {
  49. return this._isResponseInArrayMode;
  50. }
  51. }
  52. class XataHttpSession extends PgSession {
  53. constructor(client, dialect, schema, options = {}) {
  54. super(dialect);
  55. this.client = client;
  56. this.schema = schema;
  57. this.options = options;
  58. this.logger = options.logger ?? new NoopLogger();
  59. this.cache = options.cache ?? new NoopCache();
  60. }
  61. static [entityKind] = "XataHttpSession";
  62. logger;
  63. cache;
  64. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  65. return new XataHttpPreparedQuery(
  66. this.client,
  67. query,
  68. this.logger,
  69. this.cache,
  70. queryMetadata,
  71. cacheConfig,
  72. fields,
  73. isResponseInArrayMode,
  74. customResultMapper
  75. );
  76. }
  77. async query(query, params) {
  78. this.logger.logQuery(query, params);
  79. const result = await this.client.sql({ statement: query, params, responseType: "array" });
  80. return {
  81. rowCount: result.rows.length,
  82. rows: result.rows,
  83. rowAsArray: true
  84. };
  85. }
  86. async queryObjects(query, params) {
  87. const result = await this.client.sql({ statement: query, params });
  88. return {
  89. rowCount: result.records.length,
  90. rows: result.records,
  91. rowAsArray: false
  92. };
  93. }
  94. async transaction(_transaction, _config = {}) {
  95. throw new Error("No transactions support in Xata Http driver");
  96. }
  97. }
  98. class XataTransaction extends PgTransaction {
  99. static [entityKind] = "XataHttpTransaction";
  100. async transaction(_transaction) {
  101. throw new Error("No transactions support in Xata Http driver");
  102. }
  103. }
  104. export {
  105. XataHttpPreparedQuery,
  106. XataHttpSession,
  107. XataTransaction
  108. };
  109. //# sourceMappingURL=session.js.map