session.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { NoopCache } from "../cache/core/index.js";
  2. import { entityKind } from "../entity.js";
  3. import { GelPreparedQuery, GelSession, GelTransaction } from "../gel-core/session.js";
  4. import { NoopLogger } from "../logger.js";
  5. import { fillPlaceholders } from "../sql/sql.js";
  6. import { tracer } from "../tracing.js";
  7. import { mapResultRow } from "../utils.js";
  8. class GelDbPreparedQuery extends GelPreparedQuery {
  9. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, _isResponseInArrayMode, customResultMapper, transaction = false) {
  10. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  11. this.client = client;
  12. this.queryString = queryString;
  13. this.params = params;
  14. this.logger = logger;
  15. this.fields = fields;
  16. this._isResponseInArrayMode = _isResponseInArrayMode;
  17. this.customResultMapper = customResultMapper;
  18. this.transaction = transaction;
  19. }
  20. static [entityKind] = "GelPreparedQuery";
  21. async execute(placeholderValues = {}) {
  22. return tracer.startActiveSpan("drizzle.execute", async () => {
  23. const params = fillPlaceholders(this.params, placeholderValues);
  24. this.logger.logQuery(this.queryString, params);
  25. const { fields, queryString: query, client, joinsNotNullableMap, customResultMapper } = this;
  26. if (!fields && !customResultMapper) {
  27. return tracer.startActiveSpan("drizzle.driver.execute", async (span) => {
  28. span?.setAttributes({
  29. "drizzle.query.text": query,
  30. "drizzle.query.params": JSON.stringify(params)
  31. });
  32. return await this.queryWithCache(query, params, async () => {
  33. return await client.querySQL(query, params.length ? params : void 0);
  34. });
  35. });
  36. }
  37. const result = await tracer.startActiveSpan("drizzle.driver.execute", async (span) => {
  38. span?.setAttributes({
  39. "drizzle.query.text": query,
  40. "drizzle.query.params": JSON.stringify(params)
  41. });
  42. return await this.queryWithCache(query, params, async () => {
  43. return await client.withSQLRowMode("array").querySQL(query, params.length ? params : void 0);
  44. });
  45. });
  46. return tracer.startActiveSpan("drizzle.mapResponse", () => {
  47. return customResultMapper ? customResultMapper(result) : result.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  48. });
  49. });
  50. }
  51. async all(placeholderValues = {}) {
  52. return await tracer.startActiveSpan("drizzle.execute", async () => {
  53. const params = fillPlaceholders(this.params, placeholderValues);
  54. this.logger.logQuery(this.queryString, params);
  55. return await tracer.startActiveSpan("drizzle.driver.execute", async (span) => {
  56. span?.setAttributes({
  57. "drizzle.query.text": this.queryString,
  58. "drizzle.query.params": JSON.stringify(params)
  59. });
  60. return await this.queryWithCache(this.queryString, params, async () => {
  61. return await this.client.withSQLRowMode("array").querySQL(
  62. this.queryString,
  63. params.length ? params : void 0
  64. ).then((result) => result);
  65. });
  66. });
  67. });
  68. }
  69. /** @internal */
  70. isResponseInArrayMode() {
  71. return this._isResponseInArrayMode;
  72. }
  73. }
  74. class GelDbSession extends GelSession {
  75. constructor(client, dialect, schema, options = {}) {
  76. super(dialect);
  77. this.client = client;
  78. this.schema = schema;
  79. this.options = options;
  80. this.logger = options.logger ?? new NoopLogger();
  81. this.cache = options.cache ?? new NoopCache();
  82. }
  83. static [entityKind] = "GelDbSession";
  84. logger;
  85. cache;
  86. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  87. return new GelDbPreparedQuery(
  88. this.client,
  89. query.sql,
  90. query.params,
  91. this.logger,
  92. this.cache,
  93. queryMetadata,
  94. cacheConfig,
  95. fields,
  96. isResponseInArrayMode,
  97. customResultMapper
  98. );
  99. }
  100. async transaction(transaction) {
  101. return await this.client.transaction(async (clientTx) => {
  102. const session = new GelDbSession(clientTx, this.dialect, this.schema, this.options);
  103. const tx = new GelDbTransaction(this.dialect, session, this.schema);
  104. return await transaction(tx);
  105. });
  106. }
  107. async count(sql) {
  108. const res = await this.execute(sql);
  109. return Number(res[0]["count"]);
  110. }
  111. }
  112. class GelDbTransaction extends GelTransaction {
  113. static [entityKind] = "GelDbTransaction";
  114. async transaction(transaction) {
  115. const tx = new GelDbTransaction(
  116. this.dialect,
  117. this.session,
  118. this.schema
  119. );
  120. return await transaction(tx);
  121. }
  122. }
  123. export {
  124. GelDbPreparedQuery,
  125. GelDbSession,
  126. GelDbTransaction
  127. };
  128. //# sourceMappingURL=session.js.map