session.cjs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. OPSQLitePreparedQuery: () => OPSQLitePreparedQuery,
  22. OPSQLiteSession: () => OPSQLiteSession,
  23. OPSQLiteTransaction: () => OPSQLiteTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_core = require("../cache/core/index.cjs");
  27. var import_entity = require("../entity.cjs");
  28. var import_logger = require("../logger.cjs");
  29. var import_sql = require("../sql/sql.cjs");
  30. var import_sqlite_core = require("../sqlite-core/index.cjs");
  31. var import_session = require("../sqlite-core/session.cjs");
  32. var import_utils = require("../utils.cjs");
  33. class OPSQLiteSession extends import_session.SQLiteSession {
  34. constructor(client, dialect, schema, options = {}) {
  35. super(dialect);
  36. this.client = client;
  37. this.schema = schema;
  38. this.logger = options.logger ?? new import_logger.NoopLogger();
  39. this.cache = options.cache ?? new import_core.NoopCache();
  40. }
  41. static [import_entity.entityKind] = "OPSQLiteSession";
  42. logger;
  43. cache;
  44. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  45. return new OPSQLitePreparedQuery(
  46. this.client,
  47. query,
  48. this.logger,
  49. this.cache,
  50. queryMetadata,
  51. cacheConfig,
  52. fields,
  53. executeMethod,
  54. isResponseInArrayMode,
  55. customResultMapper
  56. );
  57. }
  58. transaction(transaction, config = {}) {
  59. const tx = new OPSQLiteTransaction("async", this.dialect, this, this.schema);
  60. this.run(import_sql.sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
  61. try {
  62. const result = transaction(tx);
  63. this.run(import_sql.sql`commit`);
  64. return result;
  65. } catch (err) {
  66. this.run(import_sql.sql`rollback`);
  67. throw err;
  68. }
  69. }
  70. }
  71. class OPSQLiteTransaction extends import_sqlite_core.SQLiteTransaction {
  72. static [import_entity.entityKind] = "OPSQLiteTransaction";
  73. transaction(transaction) {
  74. const savepointName = `sp${this.nestedIndex}`;
  75. const tx = new OPSQLiteTransaction("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  76. this.session.run(import_sql.sql.raw(`savepoint ${savepointName}`));
  77. try {
  78. const result = transaction(tx);
  79. this.session.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
  80. return result;
  81. } catch (err) {
  82. this.session.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  83. throw err;
  84. }
  85. }
  86. }
  87. class OPSQLitePreparedQuery extends import_session.SQLitePreparedQuery {
  88. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  89. super("sync", executeMethod, query, cache, queryMetadata, cacheConfig);
  90. this.client = client;
  91. this.logger = logger;
  92. this.fields = fields;
  93. this._isResponseInArrayMode = _isResponseInArrayMode;
  94. this.customResultMapper = customResultMapper;
  95. }
  96. static [import_entity.entityKind] = "OPSQLitePreparedQuery";
  97. async run(placeholderValues) {
  98. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  99. this.logger.logQuery(this.query.sql, params);
  100. return await this.queryWithCache(this.query.sql, params, async () => {
  101. return this.client.executeAsync(this.query.sql, params);
  102. });
  103. }
  104. async all(placeholderValues) {
  105. const { fields, joinsNotNullableMap, query, logger, customResultMapper, client } = this;
  106. if (!fields && !customResultMapper) {
  107. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  108. logger.logQuery(query.sql, params);
  109. return await this.queryWithCache(query.sql, params, async () => {
  110. return client.execute(query.sql, params).rows?._array || [];
  111. });
  112. }
  113. const rows = await this.values(placeholderValues);
  114. if (customResultMapper) {
  115. return customResultMapper(rows);
  116. }
  117. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  118. }
  119. async get(placeholderValues) {
  120. const { fields, joinsNotNullableMap, customResultMapper, query, logger, client } = this;
  121. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  122. logger.logQuery(query.sql, params);
  123. if (!fields && !customResultMapper) {
  124. const rows2 = await this.queryWithCache(query.sql, params, async () => {
  125. return client.execute(query.sql, params).rows?._array || [];
  126. });
  127. return rows2[0];
  128. }
  129. const rows = await this.values(placeholderValues);
  130. const row = rows[0];
  131. if (!row) {
  132. return void 0;
  133. }
  134. if (customResultMapper) {
  135. return customResultMapper(rows);
  136. }
  137. return (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap);
  138. }
  139. async values(placeholderValues) {
  140. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  141. this.logger.logQuery(this.query.sql, params);
  142. return await this.queryWithCache(this.query.sql, params, async () => {
  143. return await this.client.executeRawAsync(this.query.sql, params);
  144. });
  145. }
  146. /** @internal */
  147. isResponseInArrayMode() {
  148. return this._isResponseInArrayMode;
  149. }
  150. }
  151. // Annotate the CommonJS export names for ESM import in node:
  152. 0 && (module.exports = {
  153. OPSQLitePreparedQuery,
  154. OPSQLiteSession,
  155. OPSQLiteTransaction
  156. });
  157. //# sourceMappingURL=session.cjs.map