@cloudbase/database 1.4.3 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/dist/commonjs/ObjectId/index.d.ts +1 -1
  2. package/dist/commonjs/ObjectId/index.js +16 -9
  3. package/dist/commonjs/aggregate.js +126 -75
  4. package/dist/commonjs/collection.d.ts +3 -2
  5. package/dist/commonjs/collection.js +131 -58
  6. package/dist/commonjs/command.d.ts +5 -5
  7. package/dist/commonjs/command.js +181 -154
  8. package/dist/commonjs/commands/logic.js +30 -20
  9. package/dist/commonjs/commands/query.d.ts +1 -1
  10. package/dist/commonjs/commands/query.js +85 -65
  11. package/dist/commonjs/commands/update.js +9 -7
  12. package/dist/commonjs/config/error.config.js +1 -0
  13. package/dist/commonjs/const/code.js +1 -0
  14. package/dist/commonjs/constant.d.ts +7 -7
  15. package/dist/commonjs/constant.js +13 -11
  16. package/dist/commonjs/document.d.ts +10 -6
  17. package/dist/commonjs/document.js +305 -193
  18. package/dist/commonjs/geo/index.js +20 -9
  19. package/dist/commonjs/geo/lineString.d.ts +1 -1
  20. package/dist/commonjs/geo/lineString.js +62 -33
  21. package/dist/commonjs/geo/multiLineString.d.ts +1 -1
  22. package/dist/commonjs/geo/multiLineString.js +71 -32
  23. package/dist/commonjs/geo/multiPoint.d.ts +1 -1
  24. package/dist/commonjs/geo/multiPoint.js +58 -29
  25. package/dist/commonjs/geo/multiPolygon.d.ts +1 -1
  26. package/dist/commonjs/geo/multiPolygon.js +96 -36
  27. package/dist/commonjs/geo/point.d.ts +1 -1
  28. package/dist/commonjs/geo/point.js +30 -23
  29. package/dist/commonjs/geo/polygon.d.ts +1 -1
  30. package/dist/commonjs/geo/polygon.js +78 -39
  31. package/dist/commonjs/helper/symbol.js +17 -5
  32. package/dist/commonjs/index.d.ts +8 -2
  33. package/dist/commonjs/index.js +73 -20
  34. package/dist/commonjs/lib/util.d.ts +1 -0
  35. package/dist/commonjs/lib/util.js +26 -0
  36. package/dist/commonjs/operator-map.js +7 -6
  37. package/dist/commonjs/query.d.ts +9 -5
  38. package/dist/commonjs/query.js +330 -205
  39. package/dist/commonjs/regexp/index.d.ts +1 -1
  40. package/dist/commonjs/regexp/index.js +16 -9
  41. package/dist/commonjs/serializer/common.d.ts +2 -2
  42. package/dist/commonjs/serializer/common.js +59 -22
  43. package/dist/commonjs/serializer/datatype.d.ts +2 -2
  44. package/dist/commonjs/serializer/datatype.js +58 -21
  45. package/dist/commonjs/serializer/query.d.ts +1 -1
  46. package/dist/commonjs/serializer/query.js +137 -127
  47. package/dist/commonjs/serializer/update.d.ts +1 -1
  48. package/dist/commonjs/serializer/update.js +83 -78
  49. package/dist/commonjs/serverDate/index.d.ts +1 -1
  50. package/dist/commonjs/serverDate/index.js +16 -9
  51. package/dist/commonjs/transaction/collection.d.ts +7 -0
  52. package/dist/commonjs/transaction/collection.js +49 -0
  53. package/dist/commonjs/transaction/document.d.ts +27 -0
  54. package/dist/commonjs/transaction/document.js +205 -0
  55. package/dist/commonjs/transaction/index.js +196 -86
  56. package/dist/commonjs/transaction/query.d.ts +43 -0
  57. package/dist/commonjs/transaction/query.js +261 -0
  58. package/dist/commonjs/typings/index.d.ts +4 -4
  59. package/dist/commonjs/typings/realtime.d.ts +16 -16
  60. package/dist/commonjs/util.js +123 -92
  61. package/dist/commonjs/utils/error.d.ts +2 -1
  62. package/dist/commonjs/utils/error.js +74 -38
  63. package/dist/commonjs/utils/symbol.js +33 -14
  64. package/dist/commonjs/utils/type.d.ts +1 -1
  65. package/dist/commonjs/utils/type.js +25 -13
  66. package/dist/commonjs/utils/utils.d.ts +5 -1
  67. package/dist/commonjs/utils/utils.js +153 -22
  68. package/dist/commonjs/validate.js +79 -63
  69. package/dist/esm/ObjectId/index.d.ts +1 -1
  70. package/dist/esm/ObjectId/index.js +15 -8
  71. package/dist/esm/aggregate.js +118 -66
  72. package/dist/esm/collection.d.ts +3 -2
  73. package/dist/esm/collection.js +125 -52
  74. package/dist/esm/command.d.ts +5 -5
  75. package/dist/esm/command.js +175 -147
  76. package/dist/esm/commands/logic.js +30 -20
  77. package/dist/esm/commands/query.d.ts +1 -1
  78. package/dist/esm/commands/query.js +91 -71
  79. package/dist/esm/commands/update.js +8 -6
  80. package/dist/esm/config/error.config.js +1 -1
  81. package/dist/esm/const/code.js +1 -1
  82. package/dist/esm/constant.d.ts +7 -7
  83. package/dist/esm/constant.js +12 -11
  84. package/dist/esm/document.d.ts +10 -6
  85. package/dist/esm/document.js +297 -185
  86. package/dist/esm/geo/interface.js +1 -0
  87. package/dist/esm/geo/lineString.d.ts +1 -1
  88. package/dist/esm/geo/lineString.js +57 -28
  89. package/dist/esm/geo/multiLineString.d.ts +1 -1
  90. package/dist/esm/geo/multiLineString.js +66 -27
  91. package/dist/esm/geo/multiPoint.d.ts +1 -1
  92. package/dist/esm/geo/multiPoint.js +53 -24
  93. package/dist/esm/geo/multiPolygon.d.ts +1 -1
  94. package/dist/esm/geo/multiPolygon.js +91 -31
  95. package/dist/esm/geo/point.d.ts +1 -1
  96. package/dist/esm/geo/point.js +26 -19
  97. package/dist/esm/geo/polygon.d.ts +1 -1
  98. package/dist/esm/geo/polygon.js +73 -34
  99. package/dist/esm/helper/symbol.js +13 -13
  100. package/dist/esm/index.d.ts +8 -2
  101. package/dist/esm/index.js +62 -9
  102. package/dist/esm/lib/util.d.ts +1 -0
  103. package/dist/esm/lib/util.js +22 -0
  104. package/dist/esm/operator-map.js +4 -4
  105. package/dist/esm/query.d.ts +9 -5
  106. package/dist/esm/query.js +321 -196
  107. package/dist/esm/regexp/index.d.ts +1 -1
  108. package/dist/esm/regexp/index.js +15 -8
  109. package/dist/esm/serializer/common.d.ts +2 -2
  110. package/dist/esm/serializer/common.js +49 -13
  111. package/dist/esm/serializer/datatype.d.ts +2 -2
  112. package/dist/esm/serializer/datatype.js +44 -8
  113. package/dist/esm/serializer/query.d.ts +1 -1
  114. package/dist/esm/serializer/query.js +112 -102
  115. package/dist/esm/serializer/update.d.ts +1 -1
  116. package/dist/esm/serializer/update.js +69 -64
  117. package/dist/esm/serverDate/index.d.ts +1 -1
  118. package/dist/esm/serverDate/index.js +15 -8
  119. package/dist/esm/transaction/collection.d.ts +7 -0
  120. package/dist/esm/transaction/collection.js +46 -0
  121. package/dist/esm/transaction/document.d.ts +27 -0
  122. package/dist/esm/transaction/document.js +202 -0
  123. package/dist/esm/transaction/index.js +191 -81
  124. package/dist/esm/transaction/query.d.ts +43 -0
  125. package/dist/esm/transaction/query.js +258 -0
  126. package/dist/esm/typings/index.d.ts +4 -4
  127. package/dist/esm/typings/index.js +1 -0
  128. package/dist/esm/typings/realtime.d.ts +16 -16
  129. package/dist/esm/typings/realtime.js +1 -0
  130. package/dist/esm/util.js +121 -90
  131. package/dist/esm/utils/error.d.ts +2 -1
  132. package/dist/esm/utils/error.js +70 -35
  133. package/dist/esm/utils/symbol.js +33 -14
  134. package/dist/esm/utils/type.d.ts +1 -1
  135. package/dist/esm/utils/type.js +12 -12
  136. package/dist/esm/utils/utils.d.ts +5 -1
  137. package/dist/esm/utils/utils.js +137 -16
  138. package/dist/esm/validate.js +65 -49
  139. package/jest.config.js +14 -0
  140. package/package.json +14 -10
  141. package/src/aggregate.ts +1 -5
  142. package/src/collection.ts +15 -2
  143. package/src/document.ts +45 -34
  144. package/src/index.ts +51 -4
  145. package/src/query.ts +48 -38
  146. package/src/serializer/query.ts +18 -6
  147. package/src/transaction/index.ts +1 -1
  148. package/src/typings/index.ts +0 -4
  149. package/src/utils/utils.ts +105 -0
  150. package/src/validate.ts +3 -6
  151. package/tests/gateway-contract.test.ts +180 -0
  152. package/tests/index.test.ts +283 -0
  153. package/tests/transaction-query.test.ts +207 -0
  154. package/tests/watch-query.test.ts +144 -0
  155. package/tsconfig.esm.json +4 -2
  156. package/tsconfig.json +16 -6
  157. package/tsconfig.test.json +15 -0
  158. package/dist/commonjs/realtime/error.d.ts +0 -7
  159. package/dist/commonjs/realtime/error.js +0 -11
  160. package/dist/commonjs/realtime/listener.d.ts +0 -14
  161. package/dist/commonjs/realtime/listener.js +0 -17
  162. package/dist/commonjs/realtime/message.d.ts +0 -3
  163. package/dist/commonjs/realtime/message.js +0 -10
  164. package/dist/commonjs/realtime/snapshot.d.ts +0 -16
  165. package/dist/commonjs/realtime/snapshot.js +0 -42
  166. package/dist/commonjs/realtime/virtual-websocket-client.d.ts +0 -63
  167. package/dist/commonjs/realtime/virtual-websocket-client.js +0 -736
  168. package/dist/commonjs/realtime/websocket-client.d.ts +0 -78
  169. package/dist/commonjs/realtime/websocket-client.js +0 -567
  170. package/dist/commonjs/realtime/ws-event.d.ts +0 -126
  171. package/dist/commonjs/realtime/ws-event.js +0 -139
  172. package/dist/commonjs/transaction.d.ts +0 -42
  173. package/dist/commonjs/transaction.js +0 -126
  174. package/dist/esm/realtime/error.d.ts +0 -7
  175. package/dist/esm/realtime/error.js +0 -8
  176. package/dist/esm/realtime/listener.d.ts +0 -14
  177. package/dist/esm/realtime/listener.js +0 -14
  178. package/dist/esm/realtime/message.d.ts +0 -3
  179. package/dist/esm/realtime/message.js +0 -6
  180. package/dist/esm/realtime/snapshot.d.ts +0 -16
  181. package/dist/esm/realtime/snapshot.js +0 -39
  182. package/dist/esm/realtime/virtual-websocket-client.d.ts +0 -63
  183. package/dist/esm/realtime/virtual-websocket-client.js +0 -733
  184. package/dist/esm/realtime/websocket-client.d.ts +0 -78
  185. package/dist/esm/realtime/websocket-client.js +0 -564
  186. package/dist/esm/realtime/ws-event.d.ts +0 -126
  187. package/dist/esm/realtime/ws-event.js +0 -137
  188. package/dist/esm/transaction.d.ts +0 -42
  189. package/dist/esm/transaction.js +0 -121
  190. package/src/realtime/error.ts +0 -19
  191. package/src/realtime/listener.ts +0 -35
  192. package/src/realtime/message.ts +0 -14
  193. package/src/realtime/snapshot.ts +0 -60
  194. package/src/realtime/virtual-websocket-client.ts +0 -1195
  195. package/src/realtime/websocket-client.ts +0 -995
  196. package/src/realtime/ws-event.ts +0 -163
  197. package/src/transaction.ts +0 -190
@@ -1,204 +1,315 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const index_1 = require("./index");
4
- const util_1 = require("./util");
5
- const update_1 = require("./serializer/update");
6
- const datatype_1 = require("./serializer/datatype");
7
- const update_2 = require("./commands/update");
8
- const websocket_client_1 = require("./realtime/websocket-client");
9
- const constant_1 = require("./constant");
10
- const utils_1 = require("./utils/utils");
11
- const code_1 = require("./const/code");
12
- const bson_1 = require("bson");
13
- class DocumentReference {
14
- constructor(db, coll, apiOptions, docID, transactionId) {
15
- this.watch = (options) => {
16
- if (!index_1.Db.ws) {
17
- index_1.Db.ws = new websocket_client_1.RealtimeWebSocketClient({
18
- context: {
19
- appConfig: {
20
- docSizeLimit: 1000,
21
- realtimePingInterval: 10000,
22
- realtimePongWaitTimeout: 5000,
23
- request: this.request
24
- }
25
- }
26
- });
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
27
43
  }
28
- return index_1.Db.ws.watch(Object.assign(Object.assign({}, options), { envId: this._db.config.env, collectionName: this._coll, query: JSON.stringify({
29
- _id: this.id
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.DocumentReference = void 0;
51
+ var index_1 = require("./index");
52
+ var util_1 = require("./util");
53
+ var update_1 = require("./serializer/update");
54
+ var datatype_1 = require("./serializer/datatype");
55
+ var update_2 = require("./commands/update");
56
+ var constant_1 = require("./constant");
57
+ var utils_1 = require("./utils/utils");
58
+ var code_1 = require("./const/code");
59
+ var bson_1 = require("bson");
60
+ var DocumentReference = (function () {
61
+ function DocumentReference(db, coll, apiOptions, docID, transactionId) {
62
+ var _this = this;
63
+ this.watch = function (options) {
64
+ var ws = (0, utils_1.getWsInstance)(_this._db);
65
+ return ws.watch(__assign(__assign({}, options), { envId: _this._db.config.env, collectionName: _this._coll, query: JSON.stringify({
66
+ _id: _this.id
30
67
  }) }));
31
68
  };
32
69
  this._db = db;
33
70
  this._coll = coll;
34
71
  this.id = docID;
35
72
  this._transactionId = transactionId;
36
- this.request = new index_1.Db.reqClass(this._db.config);
73
+ this.request = index_1.Db.createRequest(this._db.config);
37
74
  this._apiOptions = apiOptions;
38
75
  }
39
- async create(data) {
40
- if (this.id) {
41
- data['_id'] = this.id;
42
- }
43
- let params = {
44
- collectionName: this._coll,
45
- data: [utils_1.stringifyByEJSON(datatype_1.serialize(data))],
46
- transactionId: this._transactionId
47
- };
48
- const res = await this.request.send('database.insertDocument', params, utils_1.getReqOpts(this._apiOptions));
49
- if (res.code) {
50
- return res;
51
- }
52
- if (this._transactionId) {
53
- return {
54
- inserted: 1,
55
- ok: 1,
56
- id: res.data.insertedIds[0],
57
- requestId: res.requestId
58
- };
59
- }
60
- return {
61
- id: res.data.insertedIds[0],
62
- requestId: res.requestId
63
- };
64
- }
65
- async set(data) {
66
- if (!this.id) {
67
- return utils_1.processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, code_1.ERRORS.INVALID_PARAM), { message: 'docId不能为空' }));
68
- }
69
- if (!data || typeof data !== 'object') {
70
- return utils_1.processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, code_1.ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
71
- }
72
- if (data.hasOwnProperty('_id')) {
73
- return utils_1.processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, code_1.ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
74
- }
75
- let hasOperator = false;
76
- const checkMixed = objs => {
77
- if (typeof objs === 'object') {
78
- for (let key in objs) {
79
- if (objs[key] instanceof update_2.UpdateCommand) {
80
- hasOperator = true;
81
- }
82
- else if (typeof objs[key] === 'object') {
83
- checkMixed(objs[key]);
84
- }
76
+ DocumentReference.prototype.create = function (data, callback) {
77
+ return (0, utils_1.withCallback)(this._create(data), callback);
78
+ };
79
+ DocumentReference.prototype._create = function (data) {
80
+ return __awaiter(this, void 0, void 0, function () {
81
+ var isBatch, params, res;
82
+ return __generator(this, function (_a) {
83
+ switch (_a.label) {
84
+ case 0:
85
+ isBatch = Array.isArray(data);
86
+ if (!isBatch && this.id) {
87
+ data['_id'] = this.id;
88
+ }
89
+ params = {
90
+ collectionName: this._coll,
91
+ data: isBatch
92
+ ? data.map(function (item) { return (0, utils_1.stringifyByEJSON)((0, datatype_1.serialize)(item)); })
93
+ : [(0, utils_1.stringifyByEJSON)((0, datatype_1.serialize)(data))],
94
+ transactionId: this._transactionId
95
+ };
96
+ return [4, this.request.send('database.insertDocument', params, (0, utils_1.getReqOpts)(this._apiOptions))];
97
+ case 1:
98
+ res = _a.sent();
99
+ if (res.code) {
100
+ return [2, res];
101
+ }
102
+ if (this._transactionId) {
103
+ return [2, {
104
+ inserted: 1,
105
+ ok: 1,
106
+ id: res.data.insertedIds[0],
107
+ requestId: res.requestId
108
+ }];
109
+ }
110
+ if (isBatch) {
111
+ return [2, {
112
+ ids: (res.data && res.data.insertedIds) || [],
113
+ requestId: res.requestId
114
+ }];
115
+ }
116
+ return [2, {
117
+ id: res.data.insertedIds[0],
118
+ requestId: res.requestId
119
+ }];
85
120
  }
86
- }
87
- };
88
- checkMixed(data);
89
- if (hasOperator) {
90
- return utils_1.processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, code_1.ERRORS.DATABASE_REQUEST_FAILED), { message: 'update operator complicit' }));
91
- }
92
- let param = {
93
- collectionName: this._coll,
94
- queryType: constant_1.QueryType.DOC,
95
- data: utils_1.stringifyByEJSON(datatype_1.serialize(data)),
96
- transactionId: this._transactionId,
97
- multi: false,
98
- merge: false,
99
- upsert: true
100
- };
101
- if (this.id) {
102
- param['query'] = utils_1.stringifyByEJSON({ _id: this.id });
103
- }
104
- const res = await this.request.send('database.modifyDocument', param, utils_1.getReqOpts(this._apiOptions));
105
- if (res.code) {
106
- return res;
107
- }
108
- if (this._transactionId) {
109
- return {
110
- updated: res.data.updated,
111
- upserted: [{ _id: res.data.upsert_id }],
112
- requestId: res.requestId
113
- };
114
- }
115
- return {
116
- updated: res.data.updated,
117
- upsertedId: res.data.upsert_id,
118
- requestId: res.requestId
119
- };
120
- }
121
- async update(data) {
122
- if (!data || typeof data !== 'object') {
123
- return utils_1.processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, code_1.ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
124
- }
125
- if (data.hasOwnProperty('_id')) {
126
- return utils_1.processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, code_1.ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
127
- }
128
- const query = utils_1.stringifyByEJSON({ _id: this.id });
129
- const param = {
130
- collectionName: this._coll,
131
- transactionId: this._transactionId,
132
- data: update_1.UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false),
133
- query,
134
- queryType: constant_1.QueryType.DOC,
135
- multi: false,
136
- merge: true,
137
- upsert: false
138
- };
139
- const res = await this.request.send('database.modifyDocument', param, utils_1.getReqOpts(this._apiOptions));
140
- if (res.code) {
141
- return res;
142
- }
143
- return {
144
- updated: res.data.updated,
145
- requestId: res.requestId
146
- };
147
- }
148
- async delete() {
149
- return this.remove();
150
- }
151
- async remove() {
152
- const query = utils_1.stringifyByEJSON({ _id: this.id });
153
- const param = {
154
- collectionName: this._coll,
155
- transactionId: this._transactionId,
156
- query: query,
157
- queryType: constant_1.QueryType.DOC,
158
- multi: false
159
- };
160
- const res = await this.request.send('database.removeDocument', param, utils_1.getReqOpts(this._apiOptions));
161
- if (res.code) {
162
- return res;
163
- }
164
- return {
165
- deleted: res.data.deleted,
166
- requestId: res.requestId
167
- };
168
- }
169
- async get() {
170
- const query = utils_1.stringifyByEJSON({ _id: this.id });
171
- const { projection } = this._apiOptions;
172
- const param = {
173
- collectionName: this._coll,
174
- query,
175
- transactionId: this._transactionId,
176
- queryType: constant_1.QueryType.DOC,
177
- multi: false
178
- };
179
- if (projection) {
180
- param.projection = utils_1.stringifyByEJSON(projection);
181
- }
182
- const res = await this.request.send('database.getDocument', param, utils_1.getReqOpts(this._apiOptions));
183
- if (res.code) {
184
- return res;
185
- }
186
- const list = res.data.list.map(item => bson_1.EJSON.parse(item));
187
- const documents = util_1.Util.formatResDocumentData(list);
188
- if (this._transactionId) {
189
- return {
190
- data: documents[0] || null,
191
- requestId: res.requestId
192
- };
193
- }
194
- return {
195
- data: documents,
196
- requestId: res.requestId
197
- };
198
- }
199
- field(projection) {
200
- let transformProjection = {};
201
- for (let k in projection) {
121
+ });
122
+ });
123
+ };
124
+ DocumentReference.prototype.set = function (data, callback) {
125
+ return (0, utils_1.withCallback)(this._set(data), callback);
126
+ };
127
+ DocumentReference.prototype._set = function (data) {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ var hasOperator, checkMixed, param, res;
130
+ return __generator(this, function (_a) {
131
+ switch (_a.label) {
132
+ case 0:
133
+ if (!this.id) {
134
+ return [2, (0, utils_1.processReturn)(this._db.config.throwOnCode, __assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: 'docId不能为空' }))];
135
+ }
136
+ if (!data || typeof data !== 'object') {
137
+ return [2, (0, utils_1.processReturn)(this._db.config.throwOnCode, __assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }))];
138
+ }
139
+ if (data.hasOwnProperty('_id')) {
140
+ return [2, (0, utils_1.processReturn)(this._db.config.throwOnCode, __assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }))];
141
+ }
142
+ hasOperator = false;
143
+ checkMixed = function (objs) {
144
+ if (typeof objs === 'object') {
145
+ for (var key in objs) {
146
+ if (objs[key] instanceof update_2.UpdateCommand) {
147
+ hasOperator = true;
148
+ }
149
+ else if (typeof objs[key] === 'object') {
150
+ checkMixed(objs[key]);
151
+ }
152
+ }
153
+ }
154
+ };
155
+ checkMixed(data);
156
+ if (hasOperator) {
157
+ return [2, (0, utils_1.processReturn)(this._db.config.throwOnCode, __assign(__assign({}, code_1.ERRORS.DATABASE_REQUEST_FAILED), { message: 'update operator complicit' }))];
158
+ }
159
+ param = {
160
+ collectionName: this._coll,
161
+ queryType: constant_1.QueryType.DOC,
162
+ data: (0, utils_1.stringifyByEJSON)((0, datatype_1.serialize)(data)),
163
+ transactionId: this._transactionId,
164
+ multi: false,
165
+ merge: false,
166
+ upsert: true
167
+ };
168
+ if (this.id) {
169
+ param['query'] = (0, utils_1.stringifyByEJSON)({ _id: this.id });
170
+ }
171
+ return [4, this.request.send('database.modifyDocument', param, (0, utils_1.getReqOpts)(this._apiOptions))];
172
+ case 1:
173
+ res = _a.sent();
174
+ if (res.code) {
175
+ return [2, res];
176
+ }
177
+ if (this._transactionId) {
178
+ return [2, {
179
+ updated: res.data.updated,
180
+ upserted: [{ _id: res.data.upsert_id }],
181
+ requestId: res.requestId
182
+ }];
183
+ }
184
+ return [2, {
185
+ updated: res.data.updated,
186
+ upsertedId: res.data.upsert_id,
187
+ requestId: res.requestId
188
+ }];
189
+ }
190
+ });
191
+ });
192
+ };
193
+ DocumentReference.prototype.update = function (data, callback) {
194
+ return (0, utils_1.withCallback)(this._update(data), callback);
195
+ };
196
+ DocumentReference.prototype._update = function (data) {
197
+ return __awaiter(this, void 0, void 0, function () {
198
+ var query, param, res;
199
+ return __generator(this, function (_a) {
200
+ switch (_a.label) {
201
+ case 0:
202
+ if (!data || typeof data !== 'object') {
203
+ return [2, (0, utils_1.processReturn)(this._db.config.throwOnCode, __assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }))];
204
+ }
205
+ if (data.hasOwnProperty('_id')) {
206
+ return [2, (0, utils_1.processReturn)(this._db.config.throwOnCode, __assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }))];
207
+ }
208
+ query = (0, utils_1.stringifyByEJSON)({ _id: this.id });
209
+ param = {
210
+ collectionName: this._coll,
211
+ transactionId: this._transactionId,
212
+ data: update_1.UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false),
213
+ query: query,
214
+ queryType: constant_1.QueryType.DOC,
215
+ multi: false,
216
+ merge: true,
217
+ upsert: false
218
+ };
219
+ return [4, this.request.send('database.modifyDocument', param, (0, utils_1.getReqOpts)(this._apiOptions))];
220
+ case 1:
221
+ res = _a.sent();
222
+ if (res.code) {
223
+ return [2, res];
224
+ }
225
+ return [2, {
226
+ updated: res.data.updated,
227
+ requestId: res.requestId
228
+ }];
229
+ }
230
+ });
231
+ });
232
+ };
233
+ DocumentReference.prototype.delete = function (callback) {
234
+ return this.remove(callback);
235
+ };
236
+ DocumentReference.prototype.remove = function (callback) {
237
+ return (0, utils_1.withCallback)(this._remove(), callback);
238
+ };
239
+ DocumentReference.prototype._remove = function () {
240
+ return __awaiter(this, void 0, void 0, function () {
241
+ var query, param, res;
242
+ return __generator(this, function (_a) {
243
+ switch (_a.label) {
244
+ case 0:
245
+ query = (0, utils_1.stringifyByEJSON)({ _id: this.id });
246
+ param = {
247
+ collectionName: this._coll,
248
+ transactionId: this._transactionId,
249
+ query: query,
250
+ queryType: constant_1.QueryType.DOC,
251
+ multi: false
252
+ };
253
+ return [4, this.request.send('database.removeDocument', param, (0, utils_1.getReqOpts)(this._apiOptions))];
254
+ case 1:
255
+ res = _a.sent();
256
+ if (res.code) {
257
+ return [2, res];
258
+ }
259
+ return [2, {
260
+ deleted: res.data.deleted,
261
+ requestId: res.requestId
262
+ }];
263
+ }
264
+ });
265
+ });
266
+ };
267
+ DocumentReference.prototype.get = function (callback) {
268
+ return (0, utils_1.withCallback)(this._get(), callback);
269
+ };
270
+ DocumentReference.prototype._get = function () {
271
+ return __awaiter(this, void 0, void 0, function () {
272
+ var query, projection, param, res, list, documents;
273
+ return __generator(this, function (_a) {
274
+ switch (_a.label) {
275
+ case 0:
276
+ query = (0, utils_1.stringifyByEJSON)({ _id: this.id });
277
+ projection = this._apiOptions.projection;
278
+ param = {
279
+ collectionName: this._coll,
280
+ query: query,
281
+ transactionId: this._transactionId,
282
+ queryType: constant_1.QueryType.DOC,
283
+ multi: false
284
+ };
285
+ if (projection) {
286
+ param.projection = (0, utils_1.stringifyByEJSON)(projection);
287
+ }
288
+ return [4, this.request.send('database.getDocument', param, (0, utils_1.getReqOpts)(this._apiOptions))];
289
+ case 1:
290
+ res = _a.sent();
291
+ if (res.code) {
292
+ return [2, res];
293
+ }
294
+ list = res.data.list.map(function (item) { return bson_1.EJSON.parse(item); });
295
+ documents = util_1.Util.formatResDocumentData(list);
296
+ if (this._transactionId) {
297
+ return [2, {
298
+ data: documents[0] || null,
299
+ requestId: res.requestId
300
+ }];
301
+ }
302
+ return [2, {
303
+ data: documents,
304
+ requestId: res.requestId
305
+ }];
306
+ }
307
+ });
308
+ });
309
+ };
310
+ DocumentReference.prototype.field = function (projection) {
311
+ var transformProjection = {};
312
+ for (var k in projection) {
202
313
  if (typeof projection[k] === 'boolean') {
203
314
  transformProjection[k] = projection[k] === true ? 1 : 0;
204
315
  }
@@ -209,9 +320,10 @@ class DocumentReference {
209
320
  transformProjection[k] = projection[k];
210
321
  }
211
322
  }
212
- let newApiOption = Object.assign({}, this._apiOptions);
323
+ var newApiOption = __assign({}, this._apiOptions);
213
324
  newApiOption.projection = transformProjection;
214
325
  return new DocumentReference(this._db, this._coll, newApiOption, this.id, this._transactionId);
215
- }
216
- }
326
+ };
327
+ return DocumentReference;
328
+ }());
217
329
  exports.DocumentReference = DocumentReference;
@@ -1,11 +1,22 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
5
16
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./point"));
7
- __export(require("./lineString"));
8
- __export(require("./polygon"));
9
- __export(require("./multiPoint"));
10
- __export(require("./multiLineString"));
11
- __export(require("./multiPolygon"));
17
+ __exportStar(require("./point"), exports);
18
+ __exportStar(require("./lineString"), exports);
19
+ __exportStar(require("./polygon"), exports);
20
+ __exportStar(require("./multiPoint"), exports);
21
+ __exportStar(require("./multiLineString"), exports);
22
+ __exportStar(require("./multiPolygon"), exports);
@@ -15,5 +15,5 @@ export declare class LineString {
15
15
  };
16
16
  static validate(lineString: ISerializedLineString): boolean;
17
17
  static isClosed(lineString: LineString): boolean;
18
- readonly _internalType: import("../utils/symbol").InternalSymbol;
18
+ get _internalType(): import("../utils/symbol").InternalSymbol;
19
19
  }