session.cjs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. AwsDataApiPreparedQuery: () => AwsDataApiPreparedQuery,
  22. AwsDataApiSession: () => AwsDataApiSession,
  23. AwsDataApiTransaction: () => AwsDataApiTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_client_rds_data = require("@aws-sdk/client-rds-data");
  27. var import_cache = require("../../cache/core/cache.cjs");
  28. var import_entity = require("../../entity.cjs");
  29. var import_pg_core = require("../../pg-core/index.cjs");
  30. var import_sql = require("../../sql/sql.cjs");
  31. var import_utils = require("../../utils.cjs");
  32. var import_common = require("../common/index.cjs");
  33. class AwsDataApiPreparedQuery extends import_pg_core.PgPreparedQuery {
  34. constructor(client, queryString, params, typings, options, cache, queryMetadata, cacheConfig, fields, transactionId, _isResponseInArrayMode, customResultMapper) {
  35. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  36. this.client = client;
  37. this.queryString = queryString;
  38. this.params = params;
  39. this.typings = typings;
  40. this.options = options;
  41. this.fields = fields;
  42. this.transactionId = transactionId;
  43. this._isResponseInArrayMode = _isResponseInArrayMode;
  44. this.customResultMapper = customResultMapper;
  45. this.rawQuery = new import_client_rds_data.ExecuteStatementCommand({
  46. sql: queryString,
  47. parameters: [],
  48. secretArn: options.secretArn,
  49. resourceArn: options.resourceArn,
  50. database: options.database,
  51. transactionId,
  52. includeResultMetadata: !fields && !customResultMapper
  53. });
  54. }
  55. static [import_entity.entityKind] = "AwsDataApiPreparedQuery";
  56. rawQuery;
  57. async execute(placeholderValues = {}) {
  58. const { fields, joinsNotNullableMap, customResultMapper } = this;
  59. const result = await this.values(placeholderValues);
  60. if (!fields && !customResultMapper) {
  61. const { columnMetadata, rows } = result;
  62. if (!columnMetadata) {
  63. return result;
  64. }
  65. const mappedRows = rows.map((sourceRow) => {
  66. const row = {};
  67. for (const [index, value] of sourceRow.entries()) {
  68. const metadata = columnMetadata[index];
  69. if (!metadata) {
  70. throw new Error(
  71. `Unexpected state: no column metadata found for index ${index}. Please report this issue on GitHub: https://github.com/drizzle-team/drizzle-orm/issues/new/choose`
  72. );
  73. }
  74. if (!metadata.name) {
  75. throw new Error(
  76. `Unexpected state: no column name for index ${index} found in the column metadata. Please report this issue on GitHub: https://github.com/drizzle-team/drizzle-orm/issues/new/choose`
  77. );
  78. }
  79. row[metadata.name] = value;
  80. }
  81. return row;
  82. });
  83. return Object.assign(result, { rows: mappedRows });
  84. }
  85. return customResultMapper ? customResultMapper(result.rows) : result.rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  86. }
  87. async all(placeholderValues) {
  88. const result = await this.execute(placeholderValues);
  89. if (!this.fields && !this.customResultMapper) {
  90. return result.rows;
  91. }
  92. return result;
  93. }
  94. async values(placeholderValues = {}) {
  95. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues ?? {});
  96. this.rawQuery.input.parameters = params.map((param, index) => ({
  97. name: `${index + 1}`,
  98. ...(0, import_common.toValueParam)(param, this.typings[index])
  99. }));
  100. this.options.logger?.logQuery(this.rawQuery.input.sql, this.rawQuery.input.parameters);
  101. const result = await this.queryWithCache(this.queryString, params, async () => {
  102. return await this.client.send(this.rawQuery);
  103. });
  104. const rows = result.records?.map((row) => {
  105. return row.map((field) => (0, import_common.getValueFromDataApi)(field));
  106. }) ?? [];
  107. return {
  108. ...result,
  109. rows
  110. };
  111. }
  112. /** @internal */
  113. mapResultRows(records, columnMetadata) {
  114. return records.map((record) => {
  115. const row = {};
  116. for (const [index, field] of record.entries()) {
  117. const { name } = columnMetadata[index];
  118. row[name ?? index] = (0, import_common.getValueFromDataApi)(field);
  119. }
  120. return row;
  121. });
  122. }
  123. /** @internal */
  124. isResponseInArrayMode() {
  125. return this._isResponseInArrayMode;
  126. }
  127. }
  128. class AwsDataApiSession extends import_pg_core.PgSession {
  129. constructor(client, dialect, schema, options, transactionId) {
  130. super(dialect);
  131. this.client = client;
  132. this.schema = schema;
  133. this.options = options;
  134. this.transactionId = transactionId;
  135. this.rawQuery = {
  136. secretArn: options.secretArn,
  137. resourceArn: options.resourceArn,
  138. database: options.database
  139. };
  140. this.cache = options.cache ?? new import_cache.NoopCache();
  141. }
  142. static [import_entity.entityKind] = "AwsDataApiSession";
  143. /** @internal */
  144. rawQuery;
  145. cache;
  146. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig, transactionId) {
  147. return new AwsDataApiPreparedQuery(
  148. this.client,
  149. query.sql,
  150. query.params,
  151. query.typings ?? [],
  152. this.options,
  153. this.cache,
  154. queryMetadata,
  155. cacheConfig,
  156. fields,
  157. transactionId ?? this.transactionId,
  158. isResponseInArrayMode,
  159. customResultMapper
  160. );
  161. }
  162. execute(query) {
  163. return this.prepareQuery(
  164. this.dialect.sqlToQuery(query),
  165. void 0,
  166. void 0,
  167. false,
  168. void 0,
  169. void 0,
  170. void 0,
  171. this.transactionId
  172. ).execute();
  173. }
  174. async transaction(transaction, config) {
  175. const { transactionId } = await this.client.send(new import_client_rds_data.BeginTransactionCommand(this.rawQuery));
  176. const session = new AwsDataApiSession(this.client, this.dialect, this.schema, this.options, transactionId);
  177. const tx = new AwsDataApiTransaction(this.dialect, session, this.schema);
  178. if (config) {
  179. await tx.setTransaction(config);
  180. }
  181. try {
  182. const result = await transaction(tx);
  183. await this.client.send(new import_client_rds_data.CommitTransactionCommand({ ...this.rawQuery, transactionId }));
  184. return result;
  185. } catch (e) {
  186. await this.client.send(new import_client_rds_data.RollbackTransactionCommand({ ...this.rawQuery, transactionId }));
  187. throw e;
  188. }
  189. }
  190. }
  191. class AwsDataApiTransaction extends import_pg_core.PgTransaction {
  192. static [import_entity.entityKind] = "AwsDataApiTransaction";
  193. async transaction(transaction) {
  194. const savepointName = `sp${this.nestedIndex + 1}`;
  195. const tx = new AwsDataApiTransaction(
  196. this.dialect,
  197. this.session,
  198. this.schema,
  199. this.nestedIndex + 1
  200. );
  201. await this.session.execute(import_sql.sql.raw(`savepoint ${savepointName}`));
  202. try {
  203. const result = await transaction(tx);
  204. await this.session.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));
  205. return result;
  206. } catch (e) {
  207. await this.session.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  208. throw e;
  209. }
  210. }
  211. }
  212. // Annotate the CommonJS export names for ESM import in node:
  213. 0 && (module.exports = {
  214. AwsDataApiPreparedQuery,
  215. AwsDataApiSession,
  216. AwsDataApiTransaction
  217. });
  218. //# sourceMappingURL=session.cjs.map