AK-21/Graphite-Industrial-Intelligence
0
1import { entityKind } from "../entity.js";2import { NoopLogger } from "../logger.js";3import { PgTransaction } from "../pg-core/index.js";4import { PgPreparedQuery, PgSession } from "../pg-core/session.js";5import { fillPlaceholders, sql } from "../sql/sql.js";6import { mapResultRow } from "../utils.js";7import { types } from "@electric-sql/pglite";8import { NoopCache } from "../cache/core/cache.js";9class PglitePreparedQuery extends PgPreparedQuery {10 constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _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 this.rawQueryConfig = {20 rowMode: "object",21 parsers: {22 [types.TIMESTAMP]: (value) => value,23 [types.TIMESTAMPTZ]: (value) => value,24 [types.INTERVAL]: (value) => value,25 [types.DATE]: (value) => value,26 // numeric[]27 [1231]: (value) => value,28 // timestamp[]29 [1115]: (value) => value,30 // timestamp with timezone[]31 [1185]: (value) => value,32 // interval[]33 [1187]: (value) => value,34 // date[]35 [1182]: (value) => value36 }37 };38 this.queryConfig = {39 rowMode: "array",40 parsers: {41 [types.TIMESTAMP]: (value) => value,42 [types.TIMESTAMPTZ]: (value) => value,43 [types.INTERVAL]: (value) => value,44 [types.DATE]: (value) => value,45 // numeric[]46 [1231]: (value) => value,47 // timestamp[]48 [1115]: (value) => value,49 // timestamp with timezone[]50 [1185]: (value) => value,51 // interval[]52 [1187]: (value) => value,53 // date[]54 [1182]: (value) => value55 }56 };57 }58 static [entityKind] = "PglitePreparedQuery";59 rawQueryConfig;60 queryConfig;61 async execute(placeholderValues = {}) {62 const params = fillPlaceholders(this.params, placeholderValues);63 this.logger.logQuery(this.queryString, params);64 const { fields, client, queryConfig, joinsNotNullableMap, customResultMapper, queryString, rawQueryConfig } = this;65 if (!fields && !customResultMapper) {66 return this.queryWithCache(queryString, params, async () => {67 return await client.query(queryString, params, rawQueryConfig);68 });69 }70 const result = await this.queryWithCache(queryString, params, async () => {71 return await client.query(queryString, params, queryConfig);72 });73 return customResultMapper ? customResultMapper(result.rows) : result.rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));74 }75 all(placeholderValues = {}) {76 const params = fillPlaceholders(this.params, placeholderValues);77 this.logger.logQuery(this.queryString, params);78 return this.queryWithCache(this.queryString, params, async () => {79 return await this.client.query(this.queryString, params, this.rawQueryConfig);80 }).then((result) => result.rows);81 }82 /** @internal */83 isResponseInArrayMode() {84 return this._isResponseInArrayMode;85 }86}87class PgliteSession extends PgSession {88 constructor(client, dialect, schema, options = {}) {89 super(dialect);90 this.client = client;91 this.schema = schema;92 this.options = options;93 this.logger = options.logger ?? new NoopLogger();94 this.cache = options.cache ?? new NoopCache();95 }96 static [entityKind] = "PgliteSession";97 logger;98 cache;99 prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {100 return new PglitePreparedQuery(101 this.client,102 query.sql,103 query.params,104 this.logger,105 this.cache,106 queryMetadata,107 cacheConfig,108 fields,109 name,110 isResponseInArrayMode,111 customResultMapper112 );113 }114 async transaction(transaction, config) {115 return this.client.transaction(async (client) => {116 const session = new PgliteSession(117 client,118 this.dialect,119 this.schema,120 this.options121 );122 const tx = new PgliteTransaction(this.dialect, session, this.schema);123 if (config) {124 await tx.setTransaction(config);125 }126 return transaction(tx);127 });128 }129 async count(sql2) {130 const res = await this.execute(sql2);131 return Number(132 res["rows"][0]["count"]133 );134 }135}136class PgliteTransaction extends PgTransaction {137 static [entityKind] = "PgliteTransaction";138 async transaction(transaction) {139 const savepointName = `sp${this.nestedIndex + 1}`;140 const tx = new PgliteTransaction(141 this.dialect,142 this.session,143 this.schema,144 this.nestedIndex + 1145 );146 await tx.execute(sql.raw(`savepoint ${savepointName}`));147 try {148 const result = await transaction(tx);149 await tx.execute(sql.raw(`release savepoint ${savepointName}`));150 return result;151 } catch (err) {152 await tx.execute(sql.raw(`rollback to savepoint ${savepointName}`));153 throw err;154 }155 }156}157export {158 PglitePreparedQuery,159 PgliteSession,160 PgliteTransaction161};162//# sourceMappingURL=session.js.map