session.cjs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. NeonPreparedQuery: () => NeonPreparedQuery,
  22. NeonSession: () => NeonSession,
  23. NeonTransaction: () => NeonTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_serverless = require("@neondatabase/serverless");
  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 NeonPreparedQuery 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.rawQueryConfig = {
  44. name,
  45. text: queryString,
  46. types: {
  47. // @ts-ignore
  48. getTypeParser: (typeId, format) => {
  49. if (typeId === import_serverless.types.builtins.TIMESTAMPTZ) {
  50. return (val) => val;
  51. }
  52. if (typeId === import_serverless.types.builtins.TIMESTAMP) {
  53. return (val) => val;
  54. }
  55. if (typeId === import_serverless.types.builtins.DATE) {
  56. return (val) => val;
  57. }
  58. if (typeId === import_serverless.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_serverless.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_serverless.types.builtins.TIMESTAMPTZ) {
  88. return (val) => val;
  89. }
  90. if (typeId === import_serverless.types.builtins.TIMESTAMP) {
  91. return (val) => val;
  92. }
  93. if (typeId === import_serverless.types.builtins.DATE) {
  94. return (val) => val;
  95. }
  96. if (typeId === import_serverless.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_serverless.types.getTypeParser(typeId, format);
  115. }
  116. }
  117. };
  118. }
  119. static [import_entity.entityKind] = "NeonPreparedQuery";
  120. rawQueryConfig;
  121. queryConfig;
  122. async execute(placeholderValues = {}) {
  123. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  124. this.logger.logQuery(this.rawQueryConfig.text, params);
  125. const { fields, client, rawQueryConfig: rawQuery, queryConfig: query, joinsNotNullableMap, customResultMapper } = this;
  126. if (!fields && !customResultMapper) {
  127. return await this.queryWithCache(rawQuery.text, params, async () => {
  128. return await client.query(rawQuery, params);
  129. });
  130. }
  131. const result = await this.queryWithCache(query.text, params, async () => {
  132. return await client.query(query, params);
  133. });
  134. return customResultMapper ? customResultMapper(result.rows) : result.rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  135. }
  136. all(placeholderValues = {}) {
  137. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  138. this.logger.logQuery(this.rawQueryConfig.text, params);
  139. return this.queryWithCache(this.rawQueryConfig.text, params, async () => {
  140. return await this.client.query(this.rawQueryConfig, params);
  141. }).then((result) => result.rows);
  142. }
  143. values(placeholderValues = {}) {
  144. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  145. this.logger.logQuery(this.rawQueryConfig.text, params);
  146. return this.queryWithCache(this.queryConfig.text, params, async () => {
  147. return await this.client.query(this.queryConfig, params);
  148. }).then((result) => result.rows);
  149. }
  150. /** @internal */
  151. isResponseInArrayMode() {
  152. return this._isResponseInArrayMode;
  153. }
  154. }
  155. class NeonSession extends import_session.PgSession {
  156. constructor(client, dialect, schema, options = {}) {
  157. super(dialect);
  158. this.client = client;
  159. this.schema = schema;
  160. this.options = options;
  161. this.logger = options.logger ?? new import_logger.NoopLogger();
  162. this.cache = options.cache ?? new import_cache.NoopCache();
  163. }
  164. static [import_entity.entityKind] = "NeonSession";
  165. logger;
  166. cache;
  167. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  168. return new NeonPreparedQuery(
  169. this.client,
  170. query.sql,
  171. query.params,
  172. this.logger,
  173. this.cache,
  174. queryMetadata,
  175. cacheConfig,
  176. fields,
  177. name,
  178. isResponseInArrayMode,
  179. customResultMapper
  180. );
  181. }
  182. async query(query, params) {
  183. this.logger.logQuery(query, params);
  184. const result = await this.client.query({
  185. rowMode: "array",
  186. text: query,
  187. values: params
  188. });
  189. return result;
  190. }
  191. async queryObjects(query, params) {
  192. return this.client.query(query, params);
  193. }
  194. async count(sql2) {
  195. const res = await this.execute(sql2);
  196. return Number(
  197. res["rows"][0]["count"]
  198. );
  199. }
  200. async transaction(transaction, config = {}) {
  201. const session = this.client instanceof import_serverless.Pool ? new NeonSession(await this.client.connect(), this.dialect, this.schema, this.options) : this;
  202. const tx = new NeonTransaction(this.dialect, session, this.schema);
  203. await tx.execute(import_sql.sql`begin ${tx.getTransactionConfigSQL(config)}`);
  204. try {
  205. const result = await transaction(tx);
  206. await tx.execute(import_sql.sql`commit`);
  207. return result;
  208. } catch (error) {
  209. await tx.execute(import_sql.sql`rollback`);
  210. throw error;
  211. } finally {
  212. if (this.client instanceof import_serverless.Pool) {
  213. session.client.release();
  214. }
  215. }
  216. }
  217. }
  218. class NeonTransaction extends import_pg_core.PgTransaction {
  219. static [import_entity.entityKind] = "NeonTransaction";
  220. async transaction(transaction) {
  221. const savepointName = `sp${this.nestedIndex + 1}`;
  222. const tx = new NeonTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
  223. await tx.execute(import_sql.sql.raw(`savepoint ${savepointName}`));
  224. try {
  225. const result = await transaction(tx);
  226. await tx.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));
  227. return result;
  228. } catch (e) {
  229. await tx.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  230. throw e;
  231. }
  232. }
  233. }
  234. // Annotate the CommonJS export names for ESM import in node:
  235. 0 && (module.exports = {
  236. NeonPreparedQuery,
  237. NeonSession,
  238. NeonTransaction
  239. });
  240. //# sourceMappingURL=session.cjs.map