session.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { entityKind } from "../entity.js";
  2. import { NoopLogger } from "../logger.js";
  3. import { fillPlaceholders, sql } from "../sql/sql.js";
  4. import { SQLiteTransaction } from "../sqlite-core/index.js";
  5. import { SQLitePreparedQuery as PreparedQueryBase, SQLiteSession } from "../sqlite-core/session.js";
  6. import { mapResultRow } from "../utils.js";
  7. class SQLiteBunSession extends SQLiteSession {
  8. constructor(client, dialect, schema, options = {}) {
  9. super(dialect);
  10. this.client = client;
  11. this.schema = schema;
  12. this.logger = options.logger ?? new NoopLogger();
  13. }
  14. static [entityKind] = "SQLiteBunSession";
  15. logger;
  16. exec(query) {
  17. this.client.exec(query);
  18. }
  19. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper) {
  20. const stmt = this.client.prepare(query.sql);
  21. return new PreparedQuery(
  22. stmt,
  23. query,
  24. this.logger,
  25. fields,
  26. executeMethod,
  27. isResponseInArrayMode,
  28. customResultMapper
  29. );
  30. }
  31. transaction(transaction, config = {}) {
  32. const tx = new SQLiteBunTransaction("sync", this.dialect, this, this.schema);
  33. let result;
  34. const nativeTx = this.client.transaction(() => {
  35. result = transaction(tx);
  36. });
  37. nativeTx[config.behavior ?? "deferred"]();
  38. return result;
  39. }
  40. }
  41. class SQLiteBunTransaction extends SQLiteTransaction {
  42. static [entityKind] = "SQLiteBunTransaction";
  43. transaction(transaction) {
  44. const savepointName = `sp${this.nestedIndex}`;
  45. const tx = new SQLiteBunTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  46. this.session.run(sql.raw(`savepoint ${savepointName}`));
  47. try {
  48. const result = transaction(tx);
  49. this.session.run(sql.raw(`release savepoint ${savepointName}`));
  50. return result;
  51. } catch (err) {
  52. this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
  53. throw err;
  54. }
  55. }
  56. }
  57. class PreparedQuery extends PreparedQueryBase {
  58. constructor(stmt, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  59. super("sync", executeMethod, query);
  60. this.stmt = stmt;
  61. this.logger = logger;
  62. this.fields = fields;
  63. this._isResponseInArrayMode = _isResponseInArrayMode;
  64. this.customResultMapper = customResultMapper;
  65. }
  66. static [entityKind] = "SQLiteBunPreparedQuery";
  67. run(placeholderValues) {
  68. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  69. this.logger.logQuery(this.query.sql, params);
  70. return this.stmt.run(...params);
  71. }
  72. all(placeholderValues) {
  73. const { fields, query, logger, joinsNotNullableMap, stmt, customResultMapper } = this;
  74. if (!fields && !customResultMapper) {
  75. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  76. logger.logQuery(query.sql, params);
  77. return stmt.all(...params);
  78. }
  79. const rows = this.values(placeholderValues);
  80. if (customResultMapper) {
  81. return customResultMapper(rows);
  82. }
  83. return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  84. }
  85. get(placeholderValues) {
  86. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  87. this.logger.logQuery(this.query.sql, params);
  88. const row = this.stmt.values(...params)[0];
  89. if (!row) {
  90. return void 0;
  91. }
  92. const { fields, joinsNotNullableMap, customResultMapper } = this;
  93. if (!fields && !customResultMapper) {
  94. return row;
  95. }
  96. if (customResultMapper) {
  97. return customResultMapper([row]);
  98. }
  99. return mapResultRow(fields, row, joinsNotNullableMap);
  100. }
  101. values(placeholderValues) {
  102. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  103. this.logger.logQuery(this.query.sql, params);
  104. return this.stmt.values(...params);
  105. }
  106. /** @internal */
  107. isResponseInArrayMode() {
  108. return this._isResponseInArrayMode;
  109. }
  110. }
  111. export {
  112. PreparedQuery,
  113. SQLiteBunSession,
  114. SQLiteBunTransaction
  115. };
  116. //# sourceMappingURL=session.js.map