session.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 {
  6. SQLitePreparedQuery,
  7. SQLiteSession
  8. } from "../sqlite-core/session.js";
  9. import { mapResultRow } from "../utils.js";
  10. class ExpoSQLiteSession extends SQLiteSession {
  11. constructor(client, dialect, schema, options = {}) {
  12. super(dialect);
  13. this.client = client;
  14. this.schema = schema;
  15. this.logger = options.logger ?? new NoopLogger();
  16. }
  17. static [entityKind] = "ExpoSQLiteSession";
  18. logger;
  19. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper) {
  20. const stmt = this.client.prepareSync(query.sql);
  21. return new ExpoSQLitePreparedQuery(
  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 ExpoSQLiteTransaction("sync", this.dialect, this, this.schema);
  33. this.run(sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
  34. try {
  35. const result = transaction(tx);
  36. this.run(sql`commit`);
  37. return result;
  38. } catch (err) {
  39. this.run(sql`rollback`);
  40. throw err;
  41. }
  42. }
  43. }
  44. class ExpoSQLiteTransaction extends SQLiteTransaction {
  45. static [entityKind] = "ExpoSQLiteTransaction";
  46. transaction(transaction) {
  47. const savepointName = `sp${this.nestedIndex}`;
  48. const tx = new ExpoSQLiteTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  49. this.session.run(sql.raw(`savepoint ${savepointName}`));
  50. try {
  51. const result = transaction(tx);
  52. this.session.run(sql.raw(`release savepoint ${savepointName}`));
  53. return result;
  54. } catch (err) {
  55. this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
  56. throw err;
  57. }
  58. }
  59. }
  60. class ExpoSQLitePreparedQuery extends SQLitePreparedQuery {
  61. constructor(stmt, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  62. super("sync", executeMethod, query);
  63. this.stmt = stmt;
  64. this.logger = logger;
  65. this.fields = fields;
  66. this._isResponseInArrayMode = _isResponseInArrayMode;
  67. this.customResultMapper = customResultMapper;
  68. }
  69. static [entityKind] = "ExpoSQLitePreparedQuery";
  70. run(placeholderValues) {
  71. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  72. this.logger.logQuery(this.query.sql, params);
  73. const { changes, lastInsertRowId } = this.stmt.executeSync(params);
  74. return {
  75. changes,
  76. lastInsertRowId
  77. };
  78. }
  79. all(placeholderValues) {
  80. const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;
  81. if (!fields && !customResultMapper) {
  82. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  83. logger.logQuery(query.sql, params);
  84. return stmt.executeSync(params).getAllSync();
  85. }
  86. const rows = this.values(placeholderValues);
  87. if (customResultMapper) {
  88. return customResultMapper(rows);
  89. }
  90. return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  91. }
  92. get(placeholderValues) {
  93. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  94. this.logger.logQuery(this.query.sql, params);
  95. const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;
  96. if (!fields && !customResultMapper) {
  97. return stmt.executeSync(params).getFirstSync();
  98. }
  99. const rows = this.values(placeholderValues);
  100. const row = rows[0];
  101. if (!row) {
  102. return void 0;
  103. }
  104. if (customResultMapper) {
  105. return customResultMapper(rows);
  106. }
  107. return mapResultRow(fields, row, joinsNotNullableMap);
  108. }
  109. values(placeholderValues) {
  110. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  111. this.logger.logQuery(this.query.sql, params);
  112. return this.stmt.executeForRawResultSync(params).getAllSync();
  113. }
  114. /** @internal */
  115. isResponseInArrayMode() {
  116. return this._isResponseInArrayMode;
  117. }
  118. }
  119. export {
  120. ExpoSQLitePreparedQuery,
  121. ExpoSQLiteSession,
  122. ExpoSQLiteTransaction
  123. };
  124. //# sourceMappingURL=session.js.map