session.cjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. MySqlProxyTransaction: () => MySqlProxyTransaction,
  22. MySqlRemoteSession: () => MySqlRemoteSession,
  23. PreparedQuery: () => PreparedQuery
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_core = require("../cache/core/index.cjs");
  27. var import_column = require("../column.cjs");
  28. var import_entity = require("../entity.cjs");
  29. var import_logger = require("../logger.cjs");
  30. var import_mysql_core = require("../mysql-core/index.cjs");
  31. var import_session = require("../mysql-core/session.cjs");
  32. var import_sql = require("../sql/sql.cjs");
  33. var import_utils = require("../utils.cjs");
  34. class MySqlRemoteSession extends import_session.MySqlSession {
  35. constructor(client, dialect, schema, options) {
  36. super(dialect);
  37. this.client = client;
  38. this.schema = schema;
  39. this.logger = options.logger ?? new import_logger.NoopLogger();
  40. this.cache = options.cache ?? new import_core.NoopCache();
  41. }
  42. static [import_entity.entityKind] = "MySqlRemoteSession";
  43. logger;
  44. cache;
  45. prepareQuery(query, fields, customResultMapper, generatedIds, returningIds, queryMetadata, cacheConfig) {
  46. return new PreparedQuery(
  47. this.client,
  48. query.sql,
  49. query.params,
  50. this.logger,
  51. this.cache,
  52. queryMetadata,
  53. cacheConfig,
  54. fields,
  55. customResultMapper,
  56. generatedIds,
  57. returningIds
  58. );
  59. }
  60. all(query) {
  61. const querySql = this.dialect.sqlToQuery(query);
  62. this.logger.logQuery(querySql.sql, querySql.params);
  63. return this.client(querySql.sql, querySql.params, "all").then(({ rows }) => rows);
  64. }
  65. async transaction(_transaction, _config) {
  66. throw new Error("Transactions are not supported by the MySql Proxy driver");
  67. }
  68. }
  69. class MySqlProxyTransaction extends import_mysql_core.MySqlTransaction {
  70. static [import_entity.entityKind] = "MySqlProxyTransaction";
  71. async transaction(_transaction) {
  72. throw new Error("Transactions are not supported by the MySql Proxy driver");
  73. }
  74. }
  75. class PreparedQuery extends import_session.MySqlPreparedQuery {
  76. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, customResultMapper, generatedIds, returningIds) {
  77. super(cache, queryMetadata, cacheConfig);
  78. this.client = client;
  79. this.queryString = queryString;
  80. this.params = params;
  81. this.logger = logger;
  82. this.fields = fields;
  83. this.customResultMapper = customResultMapper;
  84. this.generatedIds = generatedIds;
  85. this.returningIds = returningIds;
  86. }
  87. static [import_entity.entityKind] = "MySqlProxyPreparedQuery";
  88. async execute(placeholderValues = {}) {
  89. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  90. const { fields, client, queryString, logger, joinsNotNullableMap, customResultMapper, returningIds, generatedIds } = this;
  91. logger.logQuery(queryString, params);
  92. if (!fields && !customResultMapper) {
  93. const { rows: data } = await this.queryWithCache(queryString, params, async () => {
  94. return await client(queryString, params, "execute");
  95. });
  96. const insertId = data[0].insertId;
  97. const affectedRows = data[0].affectedRows;
  98. if (returningIds) {
  99. const returningResponse = [];
  100. let j = 0;
  101. for (let i = insertId; i < insertId + affectedRows; i++) {
  102. for (const column of returningIds) {
  103. const key = returningIds[0].path[0];
  104. if ((0, import_entity.is)(column.field, import_column.Column)) {
  105. if (column.field.primary && column.field.autoIncrement) {
  106. returningResponse.push({ [key]: i });
  107. }
  108. if (column.field.defaultFn && generatedIds) {
  109. returningResponse.push({ [key]: generatedIds[j][key] });
  110. }
  111. }
  112. }
  113. j++;
  114. }
  115. return returningResponse;
  116. }
  117. return data;
  118. }
  119. const { rows } = await this.queryWithCache(queryString, params, async () => {
  120. return await client(queryString, params, "all");
  121. });
  122. if (customResultMapper) {
  123. return customResultMapper(rows);
  124. }
  125. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  126. }
  127. iterator(_placeholderValues = {}) {
  128. throw new Error("Streaming is not supported by the MySql Proxy driver");
  129. }
  130. }
  131. // Annotate the CommonJS export names for ESM import in node:
  132. 0 && (module.exports = {
  133. MySqlProxyTransaction,
  134. MySqlRemoteSession,
  135. PreparedQuery
  136. });
  137. //# sourceMappingURL=session.cjs.map