document.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. import { EJSON } from 'bson';
  49. import { ERRORS } from '../const/code';
  50. import { UpdateSerializer } from '../serializer/update';
  51. import { serialize } from '../serializer/datatype';
  52. import { Util } from '../util';
  53. var GET_DOC = 'database.getInTransaction';
  54. var UPDATE_DOC = 'database.updateDocInTransaction';
  55. var DELETE_DOC = 'database.deleteDocInTransaction';
  56. var INSERT_DOC = 'database.insertDocInTransaction';
  57. var DocumentReference = (function () {
  58. function DocumentReference(transaction, coll, docID) {
  59. this._coll = coll;
  60. this.id = docID;
  61. this._transaction = transaction;
  62. this._request = this._transaction.getRequestMethod();
  63. this._transactionId = this._transaction.getTransactionId();
  64. }
  65. DocumentReference.prototype.create = function (data) {
  66. return __awaiter(this, void 0, void 0, function () {
  67. var params, res, inserted, ok;
  68. return __generator(this, function (_a) {
  69. switch (_a.label) {
  70. case 0:
  71. params = {
  72. collectionName: this._coll,
  73. transactionId: this._transactionId,
  74. data: EJSON.stringify(serialize(data), { relaxed: false })
  75. };
  76. if (this.id) {
  77. params['_id'] = this.id;
  78. }
  79. return [4, this._request.send(INSERT_DOC, params)];
  80. case 1:
  81. res = _a.sent();
  82. if (res.code) {
  83. throw res;
  84. }
  85. inserted = EJSON.parse(res.inserted);
  86. ok = EJSON.parse(res.ok);
  87. if (ok == 1 && inserted == 1) {
  88. return [2, __assign(__assign({}, res), { ok: ok,
  89. inserted: inserted })];
  90. }
  91. else {
  92. throw new Error(ERRORS.INSERT_DOC_FAIL.message);
  93. }
  94. return [2];
  95. }
  96. });
  97. });
  98. };
  99. DocumentReference.prototype.get = function () {
  100. return __awaiter(this, void 0, void 0, function () {
  101. var param, res;
  102. return __generator(this, function (_a) {
  103. switch (_a.label) {
  104. case 0:
  105. param = {
  106. collectionName: this._coll,
  107. transactionId: this._transactionId,
  108. query: {
  109. _id: { $eq: this.id }
  110. }
  111. };
  112. return [4, this._request.send(GET_DOC, param)];
  113. case 1:
  114. res = _a.sent();
  115. if (res.code)
  116. throw res;
  117. return [2, {
  118. data: res.data !== 'null' ? Util.formatField(EJSON.parse(res.data)) : EJSON.parse(res.data),
  119. requestId: res.requestId
  120. }];
  121. }
  122. });
  123. });
  124. };
  125. DocumentReference.prototype.set = function (data) {
  126. return __awaiter(this, void 0, void 0, function () {
  127. var param, res;
  128. return __generator(this, function (_a) {
  129. switch (_a.label) {
  130. case 0:
  131. param = {
  132. collectionName: this._coll,
  133. transactionId: this._transactionId,
  134. query: {
  135. _id: { $eq: this.id }
  136. },
  137. data: EJSON.stringify(serialize(data), { relaxed: false }),
  138. upsert: true
  139. };
  140. return [4, this._request.send(UPDATE_DOC, param)];
  141. case 1:
  142. res = _a.sent();
  143. if (res.code)
  144. throw res;
  145. return [2, __assign(__assign({}, res), { updated: EJSON.parse(res.updated), upserted: res.upserted
  146. ? JSON.parse(res.upserted)
  147. : null })];
  148. }
  149. });
  150. });
  151. };
  152. DocumentReference.prototype.update = function (data) {
  153. return __awaiter(this, void 0, void 0, function () {
  154. var param, res;
  155. return __generator(this, function (_a) {
  156. switch (_a.label) {
  157. case 0:
  158. param = {
  159. collectionName: this._coll,
  160. transactionId: this._transactionId,
  161. query: {
  162. _id: { $eq: this.id }
  163. },
  164. data: EJSON.stringify(UpdateSerializer.encode(data), {
  165. relaxed: false
  166. })
  167. };
  168. return [4, this._request.send(UPDATE_DOC, param)];
  169. case 1:
  170. res = _a.sent();
  171. if (res.code)
  172. throw res;
  173. return [2, __assign(__assign({}, res), { updated: EJSON.parse(res.updated) })];
  174. }
  175. });
  176. });
  177. };
  178. DocumentReference.prototype.delete = function () {
  179. return __awaiter(this, void 0, void 0, function () {
  180. var param, res;
  181. return __generator(this, function (_a) {
  182. switch (_a.label) {
  183. case 0:
  184. param = {
  185. collectionName: this._coll,
  186. transactionId: this._transactionId,
  187. query: {
  188. _id: { $eq: this.id }
  189. }
  190. };
  191. return [4, this._request.send(DELETE_DOC, param)];
  192. case 1:
  193. res = _a.sent();
  194. if (res.code)
  195. throw res;
  196. return [2, __assign(__assign({}, res), { deleted: EJSON.parse(res.deleted) })];
  197. }
  198. });
  199. });
  200. };
  201. return DocumentReference;
  202. }());
  203. export { DocumentReference };