session.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {
  2. types,
  3. VercelPool
  4. } from "@vercel/postgres";
  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 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-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] = "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. }
  136. class 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. customResultMapper
  161. );
  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: params
  169. });
  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. }
  197. class 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 + 1
  206. );
  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. }
  218. export {
  219. VercelPgPreparedQuery,
  220. VercelPgSession,
  221. VercelPgTransaction
  222. };
  223. //# sourceMappingURL=session.js.map