session.cjs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. PlanetScalePreparedQuery: () => PlanetScalePreparedQuery,
  22. PlanetScaleTransaction: () => PlanetScaleTransaction,
  23. PlanetscaleSession: () => PlanetscaleSession
  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_session = require("../mysql-core/session.cjs");
  31. var import_sql = require("../sql/sql.cjs");
  32. var import_utils = require("../utils.cjs");
  33. class PlanetScalePreparedQuery extends import_session.MySqlPreparedQuery {
  34. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, customResultMapper, generatedIds, returningIds) {
  35. super(cache, queryMetadata, cacheConfig);
  36. this.client = client;
  37. this.queryString = queryString;
  38. this.params = params;
  39. this.logger = logger;
  40. this.fields = fields;
  41. this.customResultMapper = customResultMapper;
  42. this.generatedIds = generatedIds;
  43. this.returningIds = returningIds;
  44. }
  45. static [import_entity.entityKind] = "PlanetScalePreparedQuery";
  46. rawQuery = { as: "object" };
  47. query = { as: "array" };
  48. async execute(placeholderValues = {}) {
  49. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  50. this.logger.logQuery(this.queryString, params);
  51. const {
  52. fields,
  53. client,
  54. queryString,
  55. rawQuery,
  56. query,
  57. joinsNotNullableMap,
  58. customResultMapper,
  59. returningIds,
  60. generatedIds
  61. } = this;
  62. if (!fields && !customResultMapper) {
  63. const res = await this.queryWithCache(queryString, params, async () => {
  64. return await client.execute(queryString, params, rawQuery);
  65. });
  66. const insertId = Number.parseFloat(res.insertId);
  67. const affectedRows = res.rowsAffected;
  68. if (returningIds) {
  69. const returningResponse = [];
  70. let j = 0;
  71. for (let i = insertId; i < insertId + affectedRows; i++) {
  72. for (const column of returningIds) {
  73. const key = returningIds[0].path[0];
  74. if ((0, import_entity.is)(column.field, import_column.Column)) {
  75. if (column.field.primary && column.field.autoIncrement) {
  76. returningResponse.push({ [key]: i });
  77. }
  78. if (column.field.defaultFn && generatedIds) {
  79. returningResponse.push({ [key]: generatedIds[j][key] });
  80. }
  81. }
  82. }
  83. j++;
  84. }
  85. return returningResponse;
  86. }
  87. return res;
  88. }
  89. const { rows } = await this.queryWithCache(queryString, params, async () => {
  90. return await client.execute(queryString, params, query);
  91. });
  92. if (customResultMapper) {
  93. return customResultMapper(rows);
  94. }
  95. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  96. }
  97. iterator(_placeholderValues) {
  98. throw new Error("Streaming is not supported by the PlanetScale Serverless driver");
  99. }
  100. }
  101. class PlanetscaleSession extends import_session.MySqlSession {
  102. constructor(baseClient, dialect, tx, schema, options = {}) {
  103. super(dialect);
  104. this.baseClient = baseClient;
  105. this.schema = schema;
  106. this.options = options;
  107. this.client = tx ?? baseClient;
  108. this.logger = options.logger ?? new import_logger.NoopLogger();
  109. this.cache = options.cache ?? new import_core.NoopCache();
  110. }
  111. static [import_entity.entityKind] = "PlanetscaleSession";
  112. logger;
  113. client;
  114. cache;
  115. prepareQuery(query, fields, customResultMapper, generatedIds, returningIds, queryMetadata, cacheConfig) {
  116. return new PlanetScalePreparedQuery(
  117. this.client,
  118. query.sql,
  119. query.params,
  120. this.logger,
  121. this.cache,
  122. queryMetadata,
  123. cacheConfig,
  124. fields,
  125. customResultMapper,
  126. generatedIds,
  127. returningIds
  128. );
  129. }
  130. async query(query, params) {
  131. this.logger.logQuery(query, params);
  132. return await this.client.execute(query, params, { as: "array" });
  133. }
  134. async queryObjects(query, params) {
  135. return this.client.execute(query, params, { as: "object" });
  136. }
  137. all(query) {
  138. const querySql = this.dialect.sqlToQuery(query);
  139. this.logger.logQuery(querySql.sql, querySql.params);
  140. return this.client.execute(querySql.sql, querySql.params, { as: "object" }).then((eQuery) => eQuery.rows);
  141. }
  142. async count(sql2) {
  143. const res = await this.execute(sql2);
  144. return Number(
  145. res["rows"][0]["count"]
  146. );
  147. }
  148. transaction(transaction) {
  149. return this.baseClient.transaction((pstx) => {
  150. const session = new PlanetscaleSession(this.baseClient, this.dialect, pstx, this.schema, this.options);
  151. const tx = new PlanetScaleTransaction(
  152. this.dialect,
  153. session,
  154. this.schema
  155. );
  156. return transaction(tx);
  157. });
  158. }
  159. }
  160. class PlanetScaleTransaction extends import_session.MySqlTransaction {
  161. static [import_entity.entityKind] = "PlanetScaleTransaction";
  162. constructor(dialect, session, schema, nestedIndex = 0) {
  163. super(dialect, session, schema, nestedIndex, "planetscale");
  164. }
  165. async transaction(transaction) {
  166. const savepointName = `sp${this.nestedIndex + 1}`;
  167. const tx = new PlanetScaleTransaction(
  168. this.dialect,
  169. this.session,
  170. this.schema,
  171. this.nestedIndex + 1
  172. );
  173. await tx.execute(import_sql.sql.raw(`savepoint ${savepointName}`));
  174. try {
  175. const result = await transaction(tx);
  176. await tx.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));
  177. return result;
  178. } catch (err) {
  179. await tx.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  180. throw err;
  181. }
  182. }
  183. }
  184. // Annotate the CommonJS export names for ESM import in node:
  185. 0 && (module.exports = {
  186. PlanetScalePreparedQuery,
  187. PlanetScaleTransaction,
  188. PlanetscaleSession
  189. });
  190. //# sourceMappingURL=session.cjs.map