session.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { NoopCache } from "../cache/core/cache.js";
  2. import { entityKind } from "../entity.js";
  3. import { NoopLogger } from "../logger.js";
  4. import { PgTransaction } from "../pg-core/index.js";
  5. import { PgPreparedQuery as PreparedQueryBase, PgSession } from "../pg-core/session.js";
  6. import { fillPlaceholders } from "../sql/sql.js";
  7. import { tracer } from "../tracing.js";
  8. import { mapResultRow } from "../utils.js";
  9. class PgRemoteSession extends PgSession {
  10. constructor(client, dialect, schema, options = {}) {
  11. super(dialect);
  12. this.client = client;
  13. this.schema = schema;
  14. this.logger = options.logger ?? new NoopLogger();
  15. this.cache = options.cache ?? new NoopCache();
  16. }
  17. static [entityKind] = "PgRemoteSession";
  18. logger;
  19. cache;
  20. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  21. return new PreparedQuery(
  22. this.client,
  23. query.sql,
  24. query.params,
  25. query.typings,
  26. this.logger,
  27. this.cache,
  28. queryMetadata,
  29. cacheConfig,
  30. fields,
  31. isResponseInArrayMode,
  32. customResultMapper
  33. );
  34. }
  35. async transaction(_transaction, _config) {
  36. throw new Error("Transactions are not supported by the Postgres Proxy driver");
  37. }
  38. }
  39. class PgProxyTransaction extends PgTransaction {
  40. static [entityKind] = "PgProxyTransaction";
  41. async transaction(_transaction) {
  42. throw new Error("Transactions are not supported by the Postgres Proxy driver");
  43. }
  44. }
  45. class PreparedQuery extends PreparedQueryBase {
  46. constructor(client, queryString, params, typings, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  47. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  48. this.client = client;
  49. this.queryString = queryString;
  50. this.params = params;
  51. this.typings = typings;
  52. this.logger = logger;
  53. this.fields = fields;
  54. this._isResponseInArrayMode = _isResponseInArrayMode;
  55. this.customResultMapper = customResultMapper;
  56. }
  57. static [entityKind] = "PgProxyPreparedQuery";
  58. async execute(placeholderValues = {}) {
  59. return tracer.startActiveSpan("drizzle.execute", async (span) => {
  60. const params = fillPlaceholders(this.params, placeholderValues);
  61. const { fields, client, queryString, joinsNotNullableMap, customResultMapper, logger, typings } = this;
  62. span?.setAttributes({
  63. "drizzle.query.text": queryString,
  64. "drizzle.query.params": JSON.stringify(params)
  65. });
  66. logger.logQuery(queryString, params);
  67. if (!fields && !customResultMapper) {
  68. return tracer.startActiveSpan("drizzle.driver.execute", async () => {
  69. const { rows: rows2 } = await this.queryWithCache(queryString, params, async () => {
  70. return await client(queryString, params, "execute", typings);
  71. });
  72. return rows2;
  73. });
  74. }
  75. const rows = await tracer.startActiveSpan("drizzle.driver.execute", async () => {
  76. span?.setAttributes({
  77. "drizzle.query.text": queryString,
  78. "drizzle.query.params": JSON.stringify(params)
  79. });
  80. const { rows: rows2 } = await this.queryWithCache(queryString, params, async () => {
  81. return await client(queryString, params, "all", typings);
  82. });
  83. return rows2;
  84. });
  85. return tracer.startActiveSpan("drizzle.mapResponse", () => {
  86. return customResultMapper ? customResultMapper(rows) : rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  87. });
  88. });
  89. }
  90. async all() {
  91. }
  92. /** @internal */
  93. isResponseInArrayMode() {
  94. return this._isResponseInArrayMode;
  95. }
  96. }
  97. export {
  98. PgProxyTransaction,
  99. PgRemoteSession,
  100. PreparedQuery
  101. };
  102. //# sourceMappingURL=session.js.map