CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
session.cjs188 linesDownload Raw Back to pglite
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  PglitePreparedQuery: () => PglitePreparedQuery,22  PgliteSession: () => PgliteSession,23  PgliteTransaction: () => PgliteTransaction24});25module.exports = __toCommonJS(session_exports);26var import_entity = require("../entity.cjs");27var import_logger = require("../logger.cjs");28var import_pg_core = require("../pg-core/index.cjs");29var import_session = require("../pg-core/session.cjs");30var import_sql = require("../sql/sql.cjs");31var import_utils = require("../utils.cjs");32var import_pglite = require("@electric-sql/pglite");33var import_cache = require("../cache/core/cache.cjs");34class PglitePreparedQuery 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.queryString = queryString;39    this.params = params;40    this.logger = logger;41    this.fields = fields;42    this._isResponseInArrayMode = _isResponseInArrayMode;43    this.customResultMapper = customResultMapper;44    this.rawQueryConfig = {45      rowMode: "object",46      parsers: {47        [import_pglite.types.TIMESTAMP]: (value) => value,48        [import_pglite.types.TIMESTAMPTZ]: (value) => value,49        [import_pglite.types.INTERVAL]: (value) => value,50        [import_pglite.types.DATE]: (value) => value,51        // numeric[]52        [1231]: (value) => value,53        // timestamp[]54        [1115]: (value) => value,55        // timestamp with timezone[]56        [1185]: (value) => value,57        // interval[]58        [1187]: (value) => value,59        // date[]60        [1182]: (value) => value61      }62    };63    this.queryConfig = {64      rowMode: "array",65      parsers: {66        [import_pglite.types.TIMESTAMP]: (value) => value,67        [import_pglite.types.TIMESTAMPTZ]: (value) => value,68        [import_pglite.types.INTERVAL]: (value) => value,69        [import_pglite.types.DATE]: (value) => value,70        // numeric[]71        [1231]: (value) => value,72        // timestamp[]73        [1115]: (value) => value,74        // timestamp with timezone[]75        [1185]: (value) => value,76        // interval[]77        [1187]: (value) => value,78        // date[]79        [1182]: (value) => value80      }81    };82  }83  static [import_entity.entityKind] = "PglitePreparedQuery";84  rawQueryConfig;85  queryConfig;86  async execute(placeholderValues = {}) {87    const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);88    this.logger.logQuery(this.queryString, params);89    const { fields, client, queryConfig, joinsNotNullableMap, customResultMapper, queryString, rawQueryConfig } = this;90    if (!fields && !customResultMapper) {91      return this.queryWithCache(queryString, params, async () => {92        return await client.query(queryString, params, rawQueryConfig);93      });94    }95    const result = await this.queryWithCache(queryString, params, async () => {96      return await client.query(queryString, params, queryConfig);97    });98    return customResultMapper ? customResultMapper(result.rows) : result.rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));99  }100  all(placeholderValues = {}) {101    const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);102    this.logger.logQuery(this.queryString, params);103    return this.queryWithCache(this.queryString, params, async () => {104      return await this.client.query(this.queryString, params, this.rawQueryConfig);105    }).then((result) => result.rows);106  }107  /** @internal */108  isResponseInArrayMode() {109    return this._isResponseInArrayMode;110  }111}112class PgliteSession extends import_session.PgSession {113  constructor(client, dialect, schema, options = {}) {114    super(dialect);115    this.client = client;116    this.schema = schema;117    this.options = options;118    this.logger = options.logger ?? new import_logger.NoopLogger();119    this.cache = options.cache ?? new import_cache.NoopCache();120  }121  static [import_entity.entityKind] = "PgliteSession";122  logger;123  cache;124  prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {125    return new PglitePreparedQuery(126      this.client,127      query.sql,128      query.params,129      this.logger,130      this.cache,131      queryMetadata,132      cacheConfig,133      fields,134      name,135      isResponseInArrayMode,136      customResultMapper137    );138  }139  async transaction(transaction, config) {140    return this.client.transaction(async (client) => {141      const session = new PgliteSession(142        client,143        this.dialect,144        this.schema,145        this.options146      );147      const tx = new PgliteTransaction(this.dialect, session, this.schema);148      if (config) {149        await tx.setTransaction(config);150      }151      return transaction(tx);152    });153  }154  async count(sql2) {155    const res = await this.execute(sql2);156    return Number(157      res["rows"][0]["count"]158    );159  }160}161class PgliteTransaction extends import_pg_core.PgTransaction {162  static [import_entity.entityKind] = "PgliteTransaction";163  async transaction(transaction) {164    const savepointName = `sp${this.nestedIndex + 1}`;165    const tx = new PgliteTransaction(166      this.dialect,167      this.session,168      this.schema,169      this.nestedIndex + 1170    );171    await tx.execute(import_sql.sql.raw(`savepoint ${savepointName}`));172    try {173      const result = await transaction(tx);174      await tx.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));175      return result;176    } catch (err) {177      await tx.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));178      throw err;179    }180  }181}182// Annotate the CommonJS export names for ESM import in node:1830 && (module.exports = {184  PglitePreparedQuery,185  PgliteSession,186  PgliteTransaction187});188//# sourceMappingURL=session.cjs.map