session.cjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. BetterSQLiteSession: () => BetterSQLiteSession,
  22. BetterSQLiteTransaction: () => BetterSQLiteTransaction,
  23. PreparedQuery: () => PreparedQuery
  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 BetterSQLiteSession 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] = "BetterSQLiteSession";
  42. logger;
  43. cache;
  44. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  45. const stmt = this.client.prepare(query.sql);
  46. return new PreparedQuery(
  47. stmt,
  48. query,
  49. this.logger,
  50. this.cache,
  51. queryMetadata,
  52. cacheConfig,
  53. fields,
  54. executeMethod,
  55. isResponseInArrayMode,
  56. customResultMapper
  57. );
  58. }
  59. transaction(transaction, config = {}) {
  60. const tx = new BetterSQLiteTransaction("sync", this.dialect, this, this.schema);
  61. const nativeTx = this.client.transaction(transaction);
  62. return nativeTx[config.behavior ?? "deferred"](tx);
  63. }
  64. }
  65. class BetterSQLiteTransaction extends import_sqlite_core.SQLiteTransaction {
  66. static [import_entity.entityKind] = "BetterSQLiteTransaction";
  67. transaction(transaction) {
  68. const savepointName = `sp${this.nestedIndex}`;
  69. const tx = new BetterSQLiteTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  70. this.session.run(import_sql.sql.raw(`savepoint ${savepointName}`));
  71. try {
  72. const result = transaction(tx);
  73. this.session.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
  74. return result;
  75. } catch (err) {
  76. this.session.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  77. throw err;
  78. }
  79. }
  80. }
  81. class PreparedQuery extends import_session.SQLitePreparedQuery {
  82. constructor(stmt, query, logger, cache, queryMetadata, cacheConfig, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  83. super("sync", executeMethod, query, cache, queryMetadata, cacheConfig);
  84. this.stmt = stmt;
  85. this.logger = logger;
  86. this.fields = fields;
  87. this._isResponseInArrayMode = _isResponseInArrayMode;
  88. this.customResultMapper = customResultMapper;
  89. }
  90. static [import_entity.entityKind] = "BetterSQLitePreparedQuery";
  91. run(placeholderValues) {
  92. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  93. this.logger.logQuery(this.query.sql, params);
  94. return this.stmt.run(...params);
  95. }
  96. all(placeholderValues) {
  97. const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;
  98. if (!fields && !customResultMapper) {
  99. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  100. logger.logQuery(query.sql, params);
  101. return stmt.all(...params);
  102. }
  103. const rows = this.values(placeholderValues);
  104. if (customResultMapper) {
  105. return customResultMapper(rows);
  106. }
  107. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  108. }
  109. get(placeholderValues) {
  110. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  111. this.logger.logQuery(this.query.sql, params);
  112. const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;
  113. if (!fields && !customResultMapper) {
  114. return stmt.get(...params);
  115. }
  116. const row = stmt.raw().get(...params);
  117. if (!row) {
  118. return void 0;
  119. }
  120. if (customResultMapper) {
  121. return customResultMapper([row]);
  122. }
  123. return (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap);
  124. }
  125. values(placeholderValues) {
  126. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  127. this.logger.logQuery(this.query.sql, params);
  128. return this.stmt.raw().all(...params);
  129. }
  130. /** @internal */
  131. isResponseInArrayMode() {
  132. return this._isResponseInArrayMode;
  133. }
  134. }
  135. // Annotate the CommonJS export names for ESM import in node:
  136. 0 && (module.exports = {
  137. BetterSQLiteSession,
  138. BetterSQLiteTransaction,
  139. PreparedQuery
  140. });
  141. //# sourceMappingURL=session.cjs.map