CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
session.cjs246 linesDownload Raw Back to vercel-postgres
1"use strict";2var __defProp = Object.defineProperty;3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;4var __getOwnPropNames = Object.getOwnPropertyNames;5var __hasOwnProp = Object.prototype.hasOwnProperty;6var __export = (target, all) => {7  for (var name in all)8    __defProp(target, name, { get: all[name], enumerable: true });9};10var __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};18var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);19var session_exports = {};20__export(session_exports, {21  VercelPgPreparedQuery: () => VercelPgPreparedQuery,22  VercelPgSession: () => VercelPgSession,23  VercelPgTransaction: () => VercelPgTransaction24});25module.exports = __toCommonJS(session_exports);26var import_postgres = require("@vercel/postgres");27var import_cache = require("../cache/core/cache.cjs");28var import_entity = require("../entity.cjs");29var import_logger = require("../logger.cjs");30var import_pg_core = require("../pg-core/index.cjs");31var import_session = require("../pg-core/session.cjs");32var import_sql = require("../sql/sql.cjs");33var import_utils = require("../utils.cjs");34class VercelPgPreparedQuery extends import_session.PgPreparedQuery {35  constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper) {36    super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);37    this.client = client;38    this.params = params;39    this.logger = logger;40    this.fields = fields;41    this._isResponseInArrayMode = _isResponseInArrayMode;42    this.customResultMapper = customResultMapper;43    this.rawQuery = {44      name,45      text: queryString,46      types: {47        // @ts-ignore48        getTypeParser: (typeId, format) => {49          if (typeId === import_postgres.types.builtins.TIMESTAMPTZ) {50            return (val) => val;51          }52          if (typeId === import_postgres.types.builtins.TIMESTAMP) {53            return (val) => val;54          }55          if (typeId === import_postgres.types.builtins.DATE) {56            return (val) => val;57          }58          if (typeId === import_postgres.types.builtins.INTERVAL) {59            return (val) => val;60          }61          if (typeId === 1231) {62            return (val) => val;63          }64          if (typeId === 1115) {65            return (val) => val;66          }67          if (typeId === 1185) {68            return (val) => val;69          }70          if (typeId === 1187) {71            return (val) => val;72          }73          if (typeId === 1182) {74            return (val) => val;75          }76          return import_postgres.types.getTypeParser(typeId, format);77        }78      }79    };80    this.queryConfig = {81      name,82      text: queryString,83      rowMode: "array",84      types: {85        // @ts-ignore86        getTypeParser: (typeId, format) => {87          if (typeId === import_postgres.types.builtins.TIMESTAMPTZ) {88            return (val) => val;89          }90          if (typeId === import_postgres.types.builtins.TIMESTAMP) {91            return (val) => val;92          }93          if (typeId === import_postgres.types.builtins.DATE) {94            return (val) => val;95          }96          if (typeId === import_postgres.types.builtins.INTERVAL) {97            return (val) => val;98          }99          if (typeId === 1231) {100            return (val) => val;101          }102          if (typeId === 1115) {103            return (val) => val;104          }105          if (typeId === 1185) {106            return (val) => val;107          }108          if (typeId === 1187) {109            return (val) => val;110          }111          if (typeId === 1182) {112            return (val) => val;113          }114          return import_postgres.types.getTypeParser(typeId, format);115        }116      }117    };118  }119  static [import_entity.entityKind] = "VercelPgPreparedQuery";120  rawQuery;121  queryConfig;122  async execute(placeholderValues = {}) {123    const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);124    this.logger.logQuery(this.rawQuery.text, params);125    const { fields, rawQuery, client, queryConfig: query, joinsNotNullableMap, customResultMapper } = this;126    if (!fields && !customResultMapper) {127      return this.queryWithCache(rawQuery.text, params, async () => {128        return await client.query(rawQuery, params);129      });130    }131    const { rows } = await this.queryWithCache(query.text, params, async () => {132      return await client.query(query, params);133    });134    if (customResultMapper) {135      return customResultMapper(rows);136    }137    return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));138  }139  all(placeholderValues = {}) {140    const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);141    this.logger.logQuery(this.rawQuery.text, params);142    return this.queryWithCache(this.rawQuery.text, params, async () => {143      return await this.client.query(this.rawQuery, params);144    }).then((result) => result.rows);145  }146  values(placeholderValues = {}) {147    const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);148    this.logger.logQuery(this.rawQuery.text, params);149    return this.queryWithCache(this.queryConfig.text, params, async () => {150      return await this.client.query(this.queryConfig, params);151    }).then((result) => result.rows);152  }153  /** @internal */154  isResponseInArrayMode() {155    return this._isResponseInArrayMode;156  }157}158class VercelPgSession extends import_session.PgSession {159  constructor(client, dialect, schema, options = {}) {160    super(dialect);161    this.client = client;162    this.schema = schema;163    this.options = options;164    this.logger = options.logger ?? new import_logger.NoopLogger();165    this.cache = options.cache ?? new import_cache.NoopCache();166  }167  static [import_entity.entityKind] = "VercelPgSession";168  logger;169  cache;170  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {171    return new VercelPgPreparedQuery(172      this.client,173      query.sql,174      query.params,175      this.logger,176      this.cache,177      queryMetadata,178      cacheConfig,179      fields,180      name,181      isResponseInArrayMode,182      customResultMapper183    );184  }185  async query(query, params) {186    this.logger.logQuery(query, params);187    const result = await this.client.query({188      rowMode: "array",189      text: query,190      values: params191    });192    return result;193  }194  async queryObjects(query, params) {195    return this.client.query(query, params);196  }197  async count(sql2) {198    const result = await this.execute(sql2);199    return Number(result["rows"][0]["count"]);200  }201  async transaction(transaction, config) {202    const session = this.client instanceof import_postgres.VercelPool ? new VercelPgSession(await this.client.connect(), this.dialect, this.schema, this.options) : this;203    const tx = new VercelPgTransaction(this.dialect, session, this.schema);204    await tx.execute(import_sql.sql`begin${config ? import_sql.sql` ${tx.getTransactionConfigSQL(config)}` : void 0}`);205    try {206      const result = await transaction(tx);207      await tx.execute(import_sql.sql`commit`);208      return result;209    } catch (error) {210      await tx.execute(import_sql.sql`rollback`);211      throw error;212    } finally {213      if (this.client instanceof import_postgres.VercelPool) {214        session.client.release();215      }216    }217  }218}219class VercelPgTransaction extends import_pg_core.PgTransaction {220  static [import_entity.entityKind] = "VercelPgTransaction";221  async transaction(transaction) {222    const savepointName = `sp${this.nestedIndex + 1}`;223    const tx = new VercelPgTransaction(224      this.dialect,225      this.session,226      this.schema,227      this.nestedIndex + 1228    );229    await tx.execute(import_sql.sql.raw(`savepoint ${savepointName}`));230    try {231      const result = await transaction(tx);232      await tx.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));233      return result;234    } catch (err) {235      await tx.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));236      throw err;237    }238  }239}240// Annotate the CommonJS export names for ESM import in node:2410 && (module.exports = {242  VercelPgPreparedQuery,243  VercelPgSession,244  VercelPgTransaction245});246//# sourceMappingURL=session.cjs.map