session.js 4.1 KB

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