session.cjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. GelDbPreparedQuery: () => GelDbPreparedQuery,
  22. GelDbSession: () => GelDbSession,
  23. GelDbTransaction: () => GelDbTransaction
  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_session = require("../gel-core/session.cjs");
  29. var import_logger = require("../logger.cjs");
  30. var import_sql = require("../sql/sql.cjs");
  31. var import_tracing = require("../tracing.cjs");
  32. var import_utils = require("../utils.cjs");
  33. class GelDbPreparedQuery extends import_session.GelPreparedQuery {
  34. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper, transaction = false) {
  35. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  36. this.client = client;
  37. this.queryString = queryString;
  38. this.params = params;
  39. this.logger = logger;
  40. this.fields = fields;
  41. this._isResponseInArrayMode = _isResponseInArrayMode;
  42. this.customResultMapper = customResultMapper;
  43. this.transaction = transaction;
  44. }
  45. static [import_entity.entityKind] = "GelPreparedQuery";
  46. async execute(placeholderValues = {}) {
  47. return import_tracing.tracer.startActiveSpan("drizzle.execute", async () => {
  48. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  49. this.logger.logQuery(this.queryString, params);
  50. const { fields, queryString: query, client, joinsNotNullableMap, customResultMapper } = this;
  51. if (!fields && !customResultMapper) {
  52. return import_tracing.tracer.startActiveSpan("drizzle.driver.execute", async (span) => {
  53. span?.setAttributes({
  54. "drizzle.query.text": query,
  55. "drizzle.query.params": JSON.stringify(params)
  56. });
  57. return await this.queryWithCache(query, params, async () => {
  58. return await client.querySQL(query, params.length ? params : void 0);
  59. });
  60. });
  61. }
  62. const result = await import_tracing.tracer.startActiveSpan("drizzle.driver.execute", async (span) => {
  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 await client.withSQLRowMode("array").querySQL(query, params.length ? params : void 0);
  69. });
  70. });
  71. return import_tracing.tracer.startActiveSpan("drizzle.mapResponse", () => {
  72. return customResultMapper ? customResultMapper(result) : result.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  73. });
  74. });
  75. }
  76. async all(placeholderValues = {}) {
  77. return await import_tracing.tracer.startActiveSpan("drizzle.execute", async () => {
  78. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  79. this.logger.logQuery(this.queryString, params);
  80. return await import_tracing.tracer.startActiveSpan("drizzle.driver.execute", async (span) => {
  81. span?.setAttributes({
  82. "drizzle.query.text": this.queryString,
  83. "drizzle.query.params": JSON.stringify(params)
  84. });
  85. return await this.queryWithCache(this.queryString, params, async () => {
  86. return await this.client.withSQLRowMode("array").querySQL(
  87. this.queryString,
  88. params.length ? params : void 0
  89. ).then((result) => result);
  90. });
  91. });
  92. });
  93. }
  94. /** @internal */
  95. isResponseInArrayMode() {
  96. return this._isResponseInArrayMode;
  97. }
  98. }
  99. class GelDbSession extends import_session.GelSession {
  100. constructor(client, dialect, schema, options = {}) {
  101. super(dialect);
  102. this.client = client;
  103. this.schema = schema;
  104. this.options = options;
  105. this.logger = options.logger ?? new import_logger.NoopLogger();
  106. this.cache = options.cache ?? new import_core.NoopCache();
  107. }
  108. static [import_entity.entityKind] = "GelDbSession";
  109. logger;
  110. cache;
  111. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  112. return new GelDbPreparedQuery(
  113. this.client,
  114. query.sql,
  115. query.params,
  116. this.logger,
  117. this.cache,
  118. queryMetadata,
  119. cacheConfig,
  120. fields,
  121. isResponseInArrayMode,
  122. customResultMapper
  123. );
  124. }
  125. async transaction(transaction) {
  126. return await this.client.transaction(async (clientTx) => {
  127. const session = new GelDbSession(clientTx, this.dialect, this.schema, this.options);
  128. const tx = new GelDbTransaction(this.dialect, session, this.schema);
  129. return await transaction(tx);
  130. });
  131. }
  132. async count(sql) {
  133. const res = await this.execute(sql);
  134. return Number(res[0]["count"]);
  135. }
  136. }
  137. class GelDbTransaction extends import_session.GelTransaction {
  138. static [import_entity.entityKind] = "GelDbTransaction";
  139. async transaction(transaction) {
  140. const tx = new GelDbTransaction(
  141. this.dialect,
  142. this.session,
  143. this.schema
  144. );
  145. return await transaction(tx);
  146. }
  147. }
  148. // Annotate the CommonJS export names for ESM import in node:
  149. 0 && (module.exports = {
  150. GelDbPreparedQuery,
  151. GelDbSession,
  152. GelDbTransaction
  153. });
  154. //# sourceMappingURL=session.cjs.map