session.cjs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __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. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var session_exports = {};
  20. __export(session_exports, {
  21. VercelPgPreparedQuery: () => VercelPgPreparedQuery,
  22. VercelPgSession: () => VercelPgSession,
  23. VercelPgTransaction: () => VercelPgTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_postgres = require("@vercel/postgres");
  27. var import_cache = require("../cache/core/cache.cjs");
  28. var import_entity = require("../entity.cjs");
  29. var import_logger = require("../logger.cjs");
  30. var import_pg_core = require("../pg-core/index.cjs");
  31. var import_session = require("../pg-core/session.cjs");
  32. var import_sql = require("../sql/sql.cjs");
  33. var import_utils = require("../utils.cjs");
  34. class 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-ignore
  48. 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-ignore
  86. 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. }
  158. class 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. customResultMapper
  183. );
  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: params
  191. });
  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. }
  219. class 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 + 1
  228. );
  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:
  241. 0 && (module.exports = {
  242. VercelPgPreparedQuery,
  243. VercelPgSession,
  244. VercelPgTransaction
  245. });
  246. //# sourceMappingURL=session.cjs.map