session.cjs 5.2 KB

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