session.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import {
  2. Pool,
  3. types
  4. } from "@neondatabase/serverless";
  5. import { NoopCache } from "../cache/core/cache.js";
  6. import { entityKind } from "../entity.js";
  7. import { NoopLogger } from "../logger.js";
  8. import { PgTransaction } from "../pg-core/index.js";
  9. import { PgPreparedQuery, PgSession } from "../pg-core/session.js";
  10. import { fillPlaceholders, sql } from "../sql/sql.js";
  11. import { mapResultRow } from "../utils.js";
  12. class NeonPreparedQuery 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.rawQueryConfig = {
  22. name,
  23. text: queryString,
  24. types: {
  25. // @ts-ignore
  26. 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-ignore
  64. 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] = "NeonPreparedQuery";
  98. rawQueryConfig;
  99. queryConfig;
  100. async execute(placeholderValues = {}) {
  101. const params = fillPlaceholders(this.params, placeholderValues);
  102. this.logger.logQuery(this.rawQueryConfig.text, params);
  103. const { fields, client, rawQueryConfig: rawQuery, queryConfig: query, joinsNotNullableMap, customResultMapper } = this;
  104. if (!fields && !customResultMapper) {
  105. return await this.queryWithCache(rawQuery.text, params, async () => {
  106. return await client.query(rawQuery, params);
  107. });
  108. }
  109. const result = await this.queryWithCache(query.text, params, async () => {
  110. return await client.query(query, params);
  111. });
  112. return customResultMapper ? customResultMapper(result.rows) : result.rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  113. }
  114. all(placeholderValues = {}) {
  115. const params = fillPlaceholders(this.params, placeholderValues);
  116. this.logger.logQuery(this.rawQueryConfig.text, params);
  117. return this.queryWithCache(this.rawQueryConfig.text, params, async () => {
  118. return await this.client.query(this.rawQueryConfig, params);
  119. }).then((result) => result.rows);
  120. }
  121. values(placeholderValues = {}) {
  122. const params = fillPlaceholders(this.params, placeholderValues);
  123. this.logger.logQuery(this.rawQueryConfig.text, params);
  124. return this.queryWithCache(this.queryConfig.text, params, async () => {
  125. return await this.client.query(this.queryConfig, params);
  126. }).then((result) => result.rows);
  127. }
  128. /** @internal */
  129. isResponseInArrayMode() {
  130. return this._isResponseInArrayMode;
  131. }
  132. }
  133. class NeonSession extends PgSession {
  134. constructor(client, dialect, schema, options = {}) {
  135. super(dialect);
  136. this.client = client;
  137. this.schema = schema;
  138. this.options = options;
  139. this.logger = options.logger ?? new NoopLogger();
  140. this.cache = options.cache ?? new NoopCache();
  141. }
  142. static [entityKind] = "NeonSession";
  143. logger;
  144. cache;
  145. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  146. return new NeonPreparedQuery(
  147. this.client,
  148. query.sql,
  149. query.params,
  150. this.logger,
  151. this.cache,
  152. queryMetadata,
  153. cacheConfig,
  154. fields,
  155. name,
  156. isResponseInArrayMode,
  157. customResultMapper
  158. );
  159. }
  160. async query(query, params) {
  161. this.logger.logQuery(query, params);
  162. const result = await this.client.query({
  163. rowMode: "array",
  164. text: query,
  165. values: params
  166. });
  167. return result;
  168. }
  169. async queryObjects(query, params) {
  170. return this.client.query(query, params);
  171. }
  172. async count(sql2) {
  173. const res = await this.execute(sql2);
  174. return Number(
  175. res["rows"][0]["count"]
  176. );
  177. }
  178. async transaction(transaction, config = {}) {
  179. const session = this.client instanceof Pool ? new NeonSession(await this.client.connect(), this.dialect, this.schema, this.options) : this;
  180. const tx = new NeonTransaction(this.dialect, session, this.schema);
  181. await tx.execute(sql`begin ${tx.getTransactionConfigSQL(config)}`);
  182. try {
  183. const result = await transaction(tx);
  184. await tx.execute(sql`commit`);
  185. return result;
  186. } catch (error) {
  187. await tx.execute(sql`rollback`);
  188. throw error;
  189. } finally {
  190. if (this.client instanceof Pool) {
  191. session.client.release();
  192. }
  193. }
  194. }
  195. }
  196. class NeonTransaction extends PgTransaction {
  197. static [entityKind] = "NeonTransaction";
  198. async transaction(transaction) {
  199. const savepointName = `sp${this.nestedIndex + 1}`;
  200. const tx = new NeonTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
  201. await tx.execute(sql.raw(`savepoint ${savepointName}`));
  202. try {
  203. const result = await transaction(tx);
  204. await tx.execute(sql.raw(`release savepoint ${savepointName}`));
  205. return result;
  206. } catch (e) {
  207. await tx.execute(sql.raw(`rollback to savepoint ${savepointName}`));
  208. throw e;
  209. }
  210. }
  211. }
  212. export {
  213. NeonPreparedQuery,
  214. NeonSession,
  215. NeonTransaction
  216. };
  217. //# sourceMappingURL=session.js.map