session.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { NoopCache } from "../cache/core/index.js";
  2. import { Column } from "../column.js";
  3. import { entityKind, is } from "../entity.js";
  4. import { NoopLogger } from "../logger.js";
  5. import { MySqlTransaction } from "../mysql-core/index.js";
  6. import { MySqlPreparedQuery as PreparedQueryBase, MySqlSession } from "../mysql-core/session.js";
  7. import { fillPlaceholders } from "../sql/sql.js";
  8. import { mapResultRow } from "../utils.js";
  9. class MySqlRemoteSession extends MySqlSession {
  10. constructor(client, dialect, schema, options) {
  11. super(dialect);
  12. this.client = client;
  13. this.schema = schema;
  14. this.logger = options.logger ?? new NoopLogger();
  15. this.cache = options.cache ?? new NoopCache();
  16. }
  17. static [entityKind] = "MySqlRemoteSession";
  18. logger;
  19. cache;
  20. prepareQuery(query, fields, customResultMapper, generatedIds, returningIds, queryMetadata, cacheConfig) {
  21. return new PreparedQuery(
  22. this.client,
  23. query.sql,
  24. query.params,
  25. this.logger,
  26. this.cache,
  27. queryMetadata,
  28. cacheConfig,
  29. fields,
  30. customResultMapper,
  31. generatedIds,
  32. returningIds
  33. );
  34. }
  35. all(query) {
  36. const querySql = this.dialect.sqlToQuery(query);
  37. this.logger.logQuery(querySql.sql, querySql.params);
  38. return this.client(querySql.sql, querySql.params, "all").then(({ rows }) => rows);
  39. }
  40. async transaction(_transaction, _config) {
  41. throw new Error("Transactions are not supported by the MySql Proxy driver");
  42. }
  43. }
  44. class MySqlProxyTransaction extends MySqlTransaction {
  45. static [entityKind] = "MySqlProxyTransaction";
  46. async transaction(_transaction) {
  47. throw new Error("Transactions are not supported by the MySql Proxy driver");
  48. }
  49. }
  50. class PreparedQuery extends PreparedQueryBase {
  51. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, customResultMapper, generatedIds, returningIds) {
  52. super(cache, queryMetadata, cacheConfig);
  53. this.client = client;
  54. this.queryString = queryString;
  55. this.params = params;
  56. this.logger = logger;
  57. this.fields = fields;
  58. this.customResultMapper = customResultMapper;
  59. this.generatedIds = generatedIds;
  60. this.returningIds = returningIds;
  61. }
  62. static [entityKind] = "MySqlProxyPreparedQuery";
  63. async execute(placeholderValues = {}) {
  64. const params = fillPlaceholders(this.params, placeholderValues);
  65. const { fields, client, queryString, logger, joinsNotNullableMap, customResultMapper, returningIds, generatedIds } = this;
  66. logger.logQuery(queryString, params);
  67. if (!fields && !customResultMapper) {
  68. const { rows: data } = await this.queryWithCache(queryString, params, async () => {
  69. return await client(queryString, params, "execute");
  70. });
  71. const insertId = data[0].insertId;
  72. const affectedRows = data[0].affectedRows;
  73. if (returningIds) {
  74. const returningResponse = [];
  75. let j = 0;
  76. for (let i = insertId; i < insertId + affectedRows; i++) {
  77. for (const column of returningIds) {
  78. const key = returningIds[0].path[0];
  79. if (is(column.field, Column)) {
  80. if (column.field.primary && column.field.autoIncrement) {
  81. returningResponse.push({ [key]: i });
  82. }
  83. if (column.field.defaultFn && generatedIds) {
  84. returningResponse.push({ [key]: generatedIds[j][key] });
  85. }
  86. }
  87. }
  88. j++;
  89. }
  90. return returningResponse;
  91. }
  92. return data;
  93. }
  94. const { rows } = await this.queryWithCache(queryString, params, async () => {
  95. return await client(queryString, params, "all");
  96. });
  97. if (customResultMapper) {
  98. return customResultMapper(rows);
  99. }
  100. return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  101. }
  102. iterator(_placeholderValues = {}) {
  103. throw new Error("Streaming is not supported by the MySql Proxy driver");
  104. }
  105. }
  106. export {
  107. MySqlProxyTransaction,
  108. MySqlRemoteSession,
  109. PreparedQuery
  110. };
  111. //# sourceMappingURL=session.js.map