session.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 { tracer } from "../tracing.js";
  8. import { mapResultRow } from "../utils.js";
  9. class PostgresJsPreparedQuery extends PgPreparedQuery {
  10. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  11. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  12. this.client = client;
  13. this.queryString = queryString;
  14. this.params = params;
  15. this.logger = logger;
  16. this.fields = fields;
  17. this._isResponseInArrayMode = _isResponseInArrayMode;
  18. this.customResultMapper = customResultMapper;
  19. }
  20. static [entityKind] = "PostgresJsPreparedQuery";
  21. async execute(placeholderValues = {}) {
  22. return tracer.startActiveSpan("drizzle.execute", async (span) => {
  23. const params = fillPlaceholders(this.params, placeholderValues);
  24. span?.setAttributes({
  25. "drizzle.query.text": this.queryString,
  26. "drizzle.query.params": JSON.stringify(params)
  27. });
  28. this.logger.logQuery(this.queryString, params);
  29. const { fields, queryString: query, client, joinsNotNullableMap, customResultMapper } = this;
  30. if (!fields && !customResultMapper) {
  31. return tracer.startActiveSpan("drizzle.driver.execute", () => {
  32. return this.queryWithCache(query, params, async () => {
  33. return await client.unsafe(query, params);
  34. });
  35. });
  36. }
  37. const rows = await tracer.startActiveSpan("drizzle.driver.execute", () => {
  38. span?.setAttributes({
  39. "drizzle.query.text": query,
  40. "drizzle.query.params": JSON.stringify(params)
  41. });
  42. return this.queryWithCache(query, params, async () => {
  43. return await client.unsafe(query, params).values();
  44. });
  45. });
  46. return tracer.startActiveSpan("drizzle.mapResponse", () => {
  47. return customResultMapper ? customResultMapper(rows) : rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  48. });
  49. });
  50. }
  51. all(placeholderValues = {}) {
  52. return tracer.startActiveSpan("drizzle.execute", async (span) => {
  53. const params = fillPlaceholders(this.params, placeholderValues);
  54. span?.setAttributes({
  55. "drizzle.query.text": this.queryString,
  56. "drizzle.query.params": JSON.stringify(params)
  57. });
  58. this.logger.logQuery(this.queryString, params);
  59. return tracer.startActiveSpan("drizzle.driver.execute", () => {
  60. span?.setAttributes({
  61. "drizzle.query.text": this.queryString,
  62. "drizzle.query.params": JSON.stringify(params)
  63. });
  64. return this.queryWithCache(this.queryString, params, async () => {
  65. return this.client.unsafe(this.queryString, params);
  66. });
  67. });
  68. });
  69. }
  70. /** @internal */
  71. isResponseInArrayMode() {
  72. return this._isResponseInArrayMode;
  73. }
  74. }
  75. class PostgresJsSession extends PgSession {
  76. constructor(client, dialect, schema, options = {}) {
  77. super(dialect);
  78. this.client = client;
  79. this.schema = schema;
  80. this.options = options;
  81. this.logger = options.logger ?? new NoopLogger();
  82. this.cache = options.cache ?? new NoopCache();
  83. }
  84. static [entityKind] = "PostgresJsSession";
  85. logger;
  86. cache;
  87. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  88. return new PostgresJsPreparedQuery(
  89. this.client,
  90. query.sql,
  91. query.params,
  92. this.logger,
  93. this.cache,
  94. queryMetadata,
  95. cacheConfig,
  96. fields,
  97. isResponseInArrayMode,
  98. customResultMapper
  99. );
  100. }
  101. query(query, params) {
  102. this.logger.logQuery(query, params);
  103. return this.client.unsafe(query, params).values();
  104. }
  105. queryObjects(query, params) {
  106. return this.client.unsafe(query, params);
  107. }
  108. transaction(transaction, config) {
  109. return this.client.begin(async (client) => {
  110. const session = new PostgresJsSession(
  111. client,
  112. this.dialect,
  113. this.schema,
  114. this.options
  115. );
  116. const tx = new PostgresJsTransaction(this.dialect, session, this.schema);
  117. if (config) {
  118. await tx.setTransaction(config);
  119. }
  120. return transaction(tx);
  121. });
  122. }
  123. }
  124. class PostgresJsTransaction extends PgTransaction {
  125. constructor(dialect, session, schema, nestedIndex = 0) {
  126. super(dialect, session, schema, nestedIndex);
  127. this.session = session;
  128. }
  129. static [entityKind] = "PostgresJsTransaction";
  130. transaction(transaction) {
  131. return this.session.client.savepoint((client) => {
  132. const session = new PostgresJsSession(
  133. client,
  134. this.dialect,
  135. this.schema,
  136. this.session.options
  137. );
  138. const tx = new PostgresJsTransaction(this.dialect, session, this.schema);
  139. return transaction(tx);
  140. });
  141. }
  142. }
  143. export {
  144. PostgresJsPreparedQuery,
  145. PostgresJsSession,
  146. PostgresJsTransaction
  147. };
  148. //# sourceMappingURL=session.js.map