transaction.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const bson_1 = require("bson");
  4. const index_1 = require("./index");
  5. class DocumentSnapshot {
  6. constructor(data, requestId) {
  7. this._data = data;
  8. this.requestId = requestId;
  9. }
  10. data() {
  11. return this._data;
  12. }
  13. }
  14. const START = 'database.startTransaction';
  15. const COMMIT = 'database.commitTransaction';
  16. const ABORT = 'database.abortTransaction';
  17. const GET_DOC = 'database.getInTransaction';
  18. const UPDATE_DOC = 'database.updateDocInTransaction';
  19. const DELETE_DOC = 'database.deleteDocInTransaction';
  20. class Transaction {
  21. constructor(db) {
  22. this._db = db;
  23. this._request = new index_1.Db.reqClass(this._db.config);
  24. }
  25. async init() {
  26. const res = await this._request.send(START, {});
  27. if (res.code) {
  28. throw res;
  29. }
  30. this._id = res.transactionId;
  31. }
  32. async get(documentRef) {
  33. const param = {
  34. collectionName: documentRef._coll,
  35. transactionId: this._id,
  36. _id: documentRef.id
  37. };
  38. const res = await this._request.send(GET_DOC, param);
  39. if (res.code)
  40. throw res;
  41. return new DocumentSnapshot(bson_1.EJSON.parse(res.data), res.requestId);
  42. }
  43. async set(documentRef, data) {
  44. const param = {
  45. collectionName: documentRef._coll,
  46. transactionId: this._id,
  47. _id: documentRef.id,
  48. data: bson_1.EJSON.stringify(data, { relaxed: false }),
  49. upsert: true
  50. };
  51. const res = await this._request.send(UPDATE_DOC, param);
  52. if (res.code)
  53. throw res;
  54. return Object.assign(Object.assign({}, res), { updated: bson_1.EJSON.parse(res.updated), upserted: res.upserted
  55. ? JSON.parse(res.upserted)
  56. : null });
  57. }
  58. async update(documentRef, data) {
  59. const param = {
  60. collectionName: documentRef._coll,
  61. transactionId: this._id,
  62. _id: documentRef.id,
  63. data: bson_1.EJSON.stringify({
  64. $set: data
  65. }, {
  66. relaxed: false
  67. })
  68. };
  69. const res = await this._request.send(UPDATE_DOC, param);
  70. if (res.code)
  71. throw res;
  72. return Object.assign(Object.assign({}, res), { updated: bson_1.EJSON.parse(res.updated) });
  73. }
  74. async delete(documentRef) {
  75. const param = {
  76. collectionName: documentRef._coll,
  77. transactionId: this._id,
  78. _id: documentRef.id
  79. };
  80. const res = await this._request.send(DELETE_DOC, param);
  81. if (res.code)
  82. throw res;
  83. return Object.assign(Object.assign({}, res), { deleted: bson_1.EJSON.parse(res.deleted) });
  84. }
  85. async commit() {
  86. const param = {
  87. transactionId: this._id
  88. };
  89. const res = await this._request.send(COMMIT, param);
  90. if (res.code)
  91. throw res;
  92. return res;
  93. }
  94. async rollback() {
  95. const param = {
  96. transactionId: this._id
  97. };
  98. const res = await this._request.send(ABORT, param);
  99. if (res.code)
  100. throw res;
  101. return res;
  102. }
  103. }
  104. exports.Transaction = Transaction;
  105. async function startTransaction() {
  106. const transaction = new Transaction(this);
  107. await transaction.init();
  108. return transaction;
  109. }
  110. exports.startTransaction = startTransaction;
  111. async function runTransaction(callback, times = 3) {
  112. if (times <= 0) {
  113. throw new Error('Transaction failed');
  114. }
  115. try {
  116. const transaction = new Transaction(this);
  117. await transaction.init();
  118. await callback(transaction);
  119. await transaction.commit();
  120. }
  121. catch (error) {
  122. console.log(error);
  123. return runTransaction.bind(this)(callback, --times);
  124. }
  125. }
  126. exports.runTransaction = runTransaction;