session.cjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. BunSQLPreparedQuery: () => BunSQLPreparedQuery,
  22. BunSQLSession: () => BunSQLSession,
  23. BunSQLTransaction: () => BunSQLTransaction
  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_tracing = require("../tracing.cjs");
  33. var import_utils = require("../utils.cjs");
  34. class BunSQLPreparedQuery extends import_session.PgPreparedQuery {
  35. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper) {
  36. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  37. this.client = client;
  38. this.queryString = queryString;
  39. this.params = params;
  40. this.logger = logger;
  41. this.fields = fields;
  42. this._isResponseInArrayMode = _isResponseInArrayMode;
  43. this.customResultMapper = customResultMapper;
  44. }
  45. static [import_entity.entityKind] = "BunSQLPreparedQuery";
  46. async execute(placeholderValues = {}) {
  47. return import_tracing.tracer.startActiveSpan("drizzle.execute", async (span) => {
  48. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  49. span?.setAttributes({
  50. "drizzle.query.text": this.queryString,
  51. "drizzle.query.params": JSON.stringify(params)
  52. });
  53. this.logger.logQuery(this.queryString, params);
  54. const { fields, queryString: query, client, joinsNotNullableMap, customResultMapper } = this;
  55. if (!fields && !customResultMapper) {
  56. return import_tracing.tracer.startActiveSpan("drizzle.driver.execute", async () => {
  57. return await this.queryWithCache(query, params, async () => {
  58. return await client.unsafe(query, params);
  59. });
  60. });
  61. }
  62. const rows = await import_tracing.tracer.startActiveSpan("drizzle.driver.execute", async () => {
  63. span?.setAttributes({
  64. "drizzle.query.text": query,
  65. "drizzle.query.params": JSON.stringify(params)
  66. });
  67. return await this.queryWithCache(query, params, async () => {
  68. return client.unsafe(query, params).values();
  69. });
  70. });
  71. return import_tracing.tracer.startActiveSpan("drizzle.mapResponse", () => {
  72. return customResultMapper ? customResultMapper(rows) : rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  73. });
  74. });
  75. }
  76. all(placeholderValues = {}) {
  77. return import_tracing.tracer.startActiveSpan("drizzle.execute", async (span) => {
  78. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  79. span?.setAttributes({
  80. "drizzle.query.text": this.queryString,
  81. "drizzle.query.params": JSON.stringify(params)
  82. });
  83. this.logger.logQuery(this.queryString, params);
  84. return import_tracing.tracer.startActiveSpan("drizzle.driver.execute", async () => {
  85. span?.setAttributes({
  86. "drizzle.query.text": this.queryString,
  87. "drizzle.query.params": JSON.stringify(params)
  88. });
  89. return await this.queryWithCache(this.queryString, params, async () => {
  90. return await this.client.unsafe(this.queryString, params);
  91. });
  92. });
  93. });
  94. }
  95. /** @internal */
  96. isResponseInArrayMode() {
  97. return this._isResponseInArrayMode;
  98. }
  99. }
  100. class BunSQLSession extends import_session.PgSession {
  101. constructor(client, dialect, schema, options = {}) {
  102. super(dialect);
  103. this.client = client;
  104. this.schema = schema;
  105. this.options = options;
  106. this.logger = options.logger ?? new import_logger.NoopLogger();
  107. this.cache = options.cache ?? new import_core.NoopCache();
  108. }
  109. static [import_entity.entityKind] = "BunSQLSession";
  110. logger;
  111. cache;
  112. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  113. return new BunSQLPreparedQuery(
  114. this.client,
  115. query.sql,
  116. query.params,
  117. this.logger,
  118. this.cache,
  119. queryMetadata,
  120. cacheConfig,
  121. fields,
  122. isResponseInArrayMode,
  123. customResultMapper
  124. );
  125. }
  126. query(query, params) {
  127. this.logger.logQuery(query, params);
  128. return this.client.unsafe(query, params).values();
  129. }
  130. queryObjects(query, params) {
  131. return this.client.unsafe(query, params);
  132. }
  133. transaction(transaction, config) {
  134. return this.client.begin(async (client) => {
  135. const session = new BunSQLSession(
  136. client,
  137. this.dialect,
  138. this.schema,
  139. this.options
  140. );
  141. const tx = new BunSQLTransaction(this.dialect, session, this.schema);
  142. if (config) {
  143. await tx.setTransaction(config);
  144. }
  145. return transaction(tx);
  146. });
  147. }
  148. }
  149. class BunSQLTransaction extends import_pg_core.PgTransaction {
  150. constructor(dialect, session, schema, nestedIndex = 0) {
  151. super(dialect, session, schema, nestedIndex);
  152. this.session = session;
  153. }
  154. static [import_entity.entityKind] = "BunSQLTransaction";
  155. transaction(transaction) {
  156. return this.session.client.savepoint((client) => {
  157. const session = new BunSQLSession(
  158. client,
  159. this.dialect,
  160. this.schema,
  161. this.session.options
  162. );
  163. const tx = new BunSQLTransaction(this.dialect, session, this.schema);
  164. return transaction(tx);
  165. });
  166. }
  167. }
  168. // Annotate the CommonJS export names for ESM import in node:
  169. 0 && (module.exports = {
  170. BunSQLPreparedQuery,
  171. BunSQLSession,
  172. BunSQLTransaction
  173. });
  174. //# sourceMappingURL=session.cjs.map