session.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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,
  8. SQLiteSession
  9. } from "../sqlite-core/session.js";
  10. import { mapResultRow } from "../utils.js";
  11. class OPSQLiteSession 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] = "OPSQLiteSession";
  20. logger;
  21. cache;
  22. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  23. return new OPSQLitePreparedQuery(
  24. this.client,
  25. query,
  26. this.logger,
  27. this.cache,
  28. queryMetadata,
  29. cacheConfig,
  30. fields,
  31. executeMethod,
  32. isResponseInArrayMode,
  33. customResultMapper
  34. );
  35. }
  36. transaction(transaction, config = {}) {
  37. const tx = new OPSQLiteTransaction("async", this.dialect, this, this.schema);
  38. this.run(sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
  39. try {
  40. const result = transaction(tx);
  41. this.run(sql`commit`);
  42. return result;
  43. } catch (err) {
  44. this.run(sql`rollback`);
  45. throw err;
  46. }
  47. }
  48. }
  49. class OPSQLiteTransaction extends SQLiteTransaction {
  50. static [entityKind] = "OPSQLiteTransaction";
  51. transaction(transaction) {
  52. const savepointName = `sp${this.nestedIndex}`;
  53. const tx = new OPSQLiteTransaction("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  54. this.session.run(sql.raw(`savepoint ${savepointName}`));
  55. try {
  56. const result = transaction(tx);
  57. this.session.run(sql.raw(`release savepoint ${savepointName}`));
  58. return result;
  59. } catch (err) {
  60. this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
  61. throw err;
  62. }
  63. }
  64. }
  65. class OPSQLitePreparedQuery extends SQLitePreparedQuery {
  66. constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  67. super("sync", executeMethod, query, cache, queryMetadata, cacheConfig);
  68. this.client = client;
  69. this.logger = logger;
  70. this.fields = fields;
  71. this._isResponseInArrayMode = _isResponseInArrayMode;
  72. this.customResultMapper = customResultMapper;
  73. }
  74. static [entityKind] = "OPSQLitePreparedQuery";
  75. async run(placeholderValues) {
  76. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  77. this.logger.logQuery(this.query.sql, params);
  78. return await this.queryWithCache(this.query.sql, params, async () => {
  79. return this.client.executeAsync(this.query.sql, params);
  80. });
  81. }
  82. async all(placeholderValues) {
  83. const { fields, joinsNotNullableMap, query, logger, customResultMapper, client } = this;
  84. if (!fields && !customResultMapper) {
  85. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  86. logger.logQuery(query.sql, params);
  87. return await this.queryWithCache(query.sql, params, async () => {
  88. return client.execute(query.sql, params).rows?._array || [];
  89. });
  90. }
  91. const rows = await this.values(placeholderValues);
  92. if (customResultMapper) {
  93. return customResultMapper(rows);
  94. }
  95. return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  96. }
  97. async get(placeholderValues) {
  98. const { fields, joinsNotNullableMap, customResultMapper, query, logger, client } = this;
  99. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  100. logger.logQuery(query.sql, params);
  101. if (!fields && !customResultMapper) {
  102. const rows2 = await this.queryWithCache(query.sql, params, async () => {
  103. return client.execute(query.sql, params).rows?._array || [];
  104. });
  105. return rows2[0];
  106. }
  107. const rows = await this.values(placeholderValues);
  108. const row = rows[0];
  109. if (!row) {
  110. return void 0;
  111. }
  112. if (customResultMapper) {
  113. return customResultMapper(rows);
  114. }
  115. return mapResultRow(fields, row, joinsNotNullableMap);
  116. }
  117. async values(placeholderValues) {
  118. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  119. this.logger.logQuery(this.query.sql, params);
  120. return await this.queryWithCache(this.query.sql, params, async () => {
  121. return await this.client.executeRawAsync(this.query.sql, params);
  122. });
  123. }
  124. /** @internal */
  125. isResponseInArrayMode() {
  126. return this._isResponseInArrayMode;
  127. }
  128. }
  129. export {
  130. OPSQLitePreparedQuery,
  131. OPSQLiteSession,
  132. OPSQLiteTransaction
  133. };
  134. //# sourceMappingURL=session.js.map