CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
session.js148 linesDownload Raw Back to bun-sql
1import { NoopCache } from "../cache/core/index.js";2import { entityKind } from "../entity.js";3import { NoopLogger } from "../logger.js";4import { PgTransaction } from "../pg-core/index.js";5import { PgPreparedQuery, PgSession } from "../pg-core/session.js";6import { fillPlaceholders } from "../sql/sql.js";7import { tracer } from "../tracing.js";8import { mapResultRow } from "../utils.js";9class BunSQLPreparedQuery 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] = "BunSQLPreparedQuery";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", async () => {32          return await this.queryWithCache(query, params, async () => {33            return await client.unsafe(query, params);34          });35        });36      }37      const rows = await tracer.startActiveSpan("drizzle.driver.execute", async () => {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 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", async () => {60        span?.setAttributes({61          "drizzle.query.text": this.queryString,62          "drizzle.query.params": JSON.stringify(params)63        });64        return await this.queryWithCache(this.queryString, params, async () => {65          return await this.client.unsafe(this.queryString, params);66        });67      });68    });69  }70  /** @internal */71  isResponseInArrayMode() {72    return this._isResponseInArrayMode;73  }74}75class BunSQLSession 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] = "BunSQLSession";85  logger;86  cache;87  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {88    return new BunSQLPreparedQuery(89      this.client,90      query.sql,91      query.params,92      this.logger,93      this.cache,94      queryMetadata,95      cacheConfig,96      fields,97      isResponseInArrayMode,98      customResultMapper99    );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 BunSQLSession(111        client,112        this.dialect,113        this.schema,114        this.options115      );116      const tx = new BunSQLTransaction(this.dialect, session, this.schema);117      if (config) {118        await tx.setTransaction(config);119      }120      return transaction(tx);121    });122  }123}124class BunSQLTransaction extends PgTransaction {125  constructor(dialect, session, schema, nestedIndex = 0) {126    super(dialect, session, schema, nestedIndex);127    this.session = session;128  }129  static [entityKind] = "BunSQLTransaction";130  transaction(transaction) {131    return this.session.client.savepoint((client) => {132      const session = new BunSQLSession(133        client,134        this.dialect,135        this.schema,136        this.session.options137      );138      const tx = new BunSQLTransaction(this.dialect, session, this.schema);139      return transaction(tx);140    });141  }142}143export {144  BunSQLPreparedQuery,145  BunSQLSession,146  BunSQLTransaction147};148//# sourceMappingURL=session.js.map