AK-21/Graphite-Industrial-Intelligence
0
1import {2 types,3 VercelPool4} from "@vercel/postgres";5import { NoopCache } from "../cache/core/cache.js";6import { entityKind } from "../entity.js";7import { NoopLogger } from "../logger.js";8import { PgTransaction } from "../pg-core/index.js";9import { PgPreparedQuery, PgSession } from "../pg-core/session.js";10import { fillPlaceholders, sql } from "../sql/sql.js";11import { mapResultRow } from "../utils.js";12class VercelPgPreparedQuery extends PgPreparedQuery {13 constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper) {14 super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);15 this.client = client;16 this.params = params;17 this.logger = logger;18 this.fields = fields;19 this._isResponseInArrayMode = _isResponseInArrayMode;20 this.customResultMapper = customResultMapper;21 this.rawQuery = {22 name,23 text: queryString,24 types: {25 // @ts-ignore26 getTypeParser: (typeId, format) => {27 if (typeId === types.builtins.TIMESTAMPTZ) {28 return (val) => val;29 }30 if (typeId === types.builtins.TIMESTAMP) {31 return (val) => val;32 }33 if (typeId === types.builtins.DATE) {34 return (val) => val;35 }36 if (typeId === types.builtins.INTERVAL) {37 return (val) => val;38 }39 if (typeId === 1231) {40 return (val) => val;41 }42 if (typeId === 1115) {43 return (val) => val;44 }45 if (typeId === 1185) {46 return (val) => val;47 }48 if (typeId === 1187) {49 return (val) => val;50 }51 if (typeId === 1182) {52 return (val) => val;53 }54 return types.getTypeParser(typeId, format);55 }56 }57 };58 this.queryConfig = {59 name,60 text: queryString,61 rowMode: "array",62 types: {63 // @ts-ignore64 getTypeParser: (typeId, format) => {65 if (typeId === types.builtins.TIMESTAMPTZ) {66 return (val) => val;67 }68 if (typeId === types.builtins.TIMESTAMP) {69 return (val) => val;70 }71 if (typeId === types.builtins.DATE) {72 return (val) => val;73 }74 if (typeId === types.builtins.INTERVAL) {75 return (val) => val;76 }77 if (typeId === 1231) {78 return (val) => val;79 }80 if (typeId === 1115) {81 return (val) => val;82 }83 if (typeId === 1185) {84 return (val) => val;85 }86 if (typeId === 1187) {87 return (val) => val;88 }89 if (typeId === 1182) {90 return (val) => val;91 }92 return types.getTypeParser(typeId, format);93 }94 }95 };96 }97 static [entityKind] = "VercelPgPreparedQuery";98 rawQuery;99 queryConfig;100 async execute(placeholderValues = {}) {101 const params = fillPlaceholders(this.params, placeholderValues);102 this.logger.logQuery(this.rawQuery.text, params);103 const { fields, rawQuery, client, queryConfig: query, joinsNotNullableMap, customResultMapper } = this;104 if (!fields && !customResultMapper) {105 return this.queryWithCache(rawQuery.text, params, async () => {106 return await client.query(rawQuery, params);107 });108 }109 const { rows } = await this.queryWithCache(query.text, params, async () => {110 return await client.query(query, params);111 });112 if (customResultMapper) {113 return customResultMapper(rows);114 }115 return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));116 }117 all(placeholderValues = {}) {118 const params = fillPlaceholders(this.params, placeholderValues);119 this.logger.logQuery(this.rawQuery.text, params);120 return this.queryWithCache(this.rawQuery.text, params, async () => {121 return await this.client.query(this.rawQuery, params);122 }).then((result) => result.rows);123 }124 values(placeholderValues = {}) {125 const params = fillPlaceholders(this.params, placeholderValues);126 this.logger.logQuery(this.rawQuery.text, params);127 return this.queryWithCache(this.queryConfig.text, params, async () => {128 return await this.client.query(this.queryConfig, params);129 }).then((result) => result.rows);130 }131 /** @internal */132 isResponseInArrayMode() {133 return this._isResponseInArrayMode;134 }135}136class VercelPgSession extends PgSession {137 constructor(client, dialect, schema, options = {}) {138 super(dialect);139 this.client = client;140 this.schema = schema;141 this.options = options;142 this.logger = options.logger ?? new NoopLogger();143 this.cache = options.cache ?? new NoopCache();144 }145 static [entityKind] = "VercelPgSession";146 logger;147 cache;148 prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {149 return new VercelPgPreparedQuery(150 this.client,151 query.sql,152 query.params,153 this.logger,154 this.cache,155 queryMetadata,156 cacheConfig,157 fields,158 name,159 isResponseInArrayMode,160 customResultMapper161 );162 }163 async query(query, params) {164 this.logger.logQuery(query, params);165 const result = await this.client.query({166 rowMode: "array",167 text: query,168 values: params169 });170 return result;171 }172 async queryObjects(query, params) {173 return this.client.query(query, params);174 }175 async count(sql2) {176 const result = await this.execute(sql2);177 return Number(result["rows"][0]["count"]);178 }179 async transaction(transaction, config) {180 const session = this.client instanceof VercelPool ? new VercelPgSession(await this.client.connect(), this.dialect, this.schema, this.options) : this;181 const tx = new VercelPgTransaction(this.dialect, session, this.schema);182 await tx.execute(sql`begin${config ? sql` ${tx.getTransactionConfigSQL(config)}` : void 0}`);183 try {184 const result = await transaction(tx);185 await tx.execute(sql`commit`);186 return result;187 } catch (error) {188 await tx.execute(sql`rollback`);189 throw error;190 } finally {191 if (this.client instanceof VercelPool) {192 session.client.release();193 }194 }195 }196}197class VercelPgTransaction extends PgTransaction {198 static [entityKind] = "VercelPgTransaction";199 async transaction(transaction) {200 const savepointName = `sp${this.nestedIndex + 1}`;201 const tx = new VercelPgTransaction(202 this.dialect,203 this.session,204 this.schema,205 this.nestedIndex + 1206 );207 await tx.execute(sql.raw(`savepoint ${savepointName}`));208 try {209 const result = await transaction(tx);210 await tx.execute(sql.raw(`release savepoint ${savepointName}`));211 return result;212 } catch (err) {213 await tx.execute(sql.raw(`rollback to savepoint ${savepointName}`));214 throw err;215 }216 }217}218export {219 VercelPgPreparedQuery,220 VercelPgSession,221 VercelPgTransaction222};223//# sourceMappingURL=session.js.map