@cloudbase/database 1.4.3 → 1.5.0

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 (196) 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 +8 -4
  38. package/dist/commonjs/query.js +308 -201
  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 +4 -1
  67. package/dist/commonjs/utils/utils.js +93 -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 +8 -4
  106. package/dist/esm/query.js +299 -192
  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 +4 -1
  137. package/dist/esm/utils/utils.js +78 -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 +33 -29
  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 +46 -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/tsconfig.esm.json +4 -2
  155. package/tsconfig.json +16 -6
  156. package/tsconfig.test.json +15 -0
  157. package/dist/commonjs/realtime/error.d.ts +0 -7
  158. package/dist/commonjs/realtime/error.js +0 -11
  159. package/dist/commonjs/realtime/listener.d.ts +0 -14
  160. package/dist/commonjs/realtime/listener.js +0 -17
  161. package/dist/commonjs/realtime/message.d.ts +0 -3
  162. package/dist/commonjs/realtime/message.js +0 -10
  163. package/dist/commonjs/realtime/snapshot.d.ts +0 -16
  164. package/dist/commonjs/realtime/snapshot.js +0 -42
  165. package/dist/commonjs/realtime/virtual-websocket-client.d.ts +0 -63
  166. package/dist/commonjs/realtime/virtual-websocket-client.js +0 -736
  167. package/dist/commonjs/realtime/websocket-client.d.ts +0 -78
  168. package/dist/commonjs/realtime/websocket-client.js +0 -567
  169. package/dist/commonjs/realtime/ws-event.d.ts +0 -126
  170. package/dist/commonjs/realtime/ws-event.js +0 -139
  171. package/dist/commonjs/transaction.d.ts +0 -42
  172. package/dist/commonjs/transaction.js +0 -126
  173. package/dist/esm/realtime/error.d.ts +0 -7
  174. package/dist/esm/realtime/error.js +0 -8
  175. package/dist/esm/realtime/listener.d.ts +0 -14
  176. package/dist/esm/realtime/listener.js +0 -14
  177. package/dist/esm/realtime/message.d.ts +0 -3
  178. package/dist/esm/realtime/message.js +0 -6
  179. package/dist/esm/realtime/snapshot.d.ts +0 -16
  180. package/dist/esm/realtime/snapshot.js +0 -39
  181. package/dist/esm/realtime/virtual-websocket-client.d.ts +0 -63
  182. package/dist/esm/realtime/virtual-websocket-client.js +0 -733
  183. package/dist/esm/realtime/websocket-client.d.ts +0 -78
  184. package/dist/esm/realtime/websocket-client.js +0 -564
  185. package/dist/esm/realtime/ws-event.d.ts +0 -126
  186. package/dist/esm/realtime/ws-event.js +0 -137
  187. package/dist/esm/transaction.d.ts +0 -42
  188. package/dist/esm/transaction.js +0 -121
  189. package/src/realtime/error.ts +0 -19
  190. package/src/realtime/listener.ts +0 -35
  191. package/src/realtime/message.ts +0 -14
  192. package/src/realtime/snapshot.ts +0 -60
  193. package/src/realtime/virtual-websocket-client.ts +0 -1195
  194. package/src/realtime/websocket-client.ts +0 -995
  195. package/src/realtime/ws-event.ts +0 -163
  196. package/src/transaction.ts +0 -190
@@ -1,202 +1,312 @@
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 (g && (g = 0, op[0] && (_ = 0)), _) 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
+ };
1
48
  import { Db } from './index';
2
49
  import { Util } from './util';
3
50
  import { UpdateSerializer } from './serializer/update';
4
51
  import { serialize } from './serializer/datatype';
5
52
  import { UpdateCommand } from './commands/update';
6
- import { RealtimeWebSocketClient } from './realtime/websocket-client';
7
53
  import { QueryType } from './constant';
8
- import { getReqOpts, stringifyByEJSON, processReturn } from './utils/utils';
54
+ import { getWsInstance, getReqOpts, stringifyByEJSON, processReturn, withCallback } from './utils/utils';
9
55
  import { ERRORS } from './const/code';
10
56
  import { EJSON } from 'bson';
11
- export class DocumentReference {
12
- constructor(db, coll, apiOptions, docID, transactionId) {
13
- this.watch = (options) => {
14
- if (!Db.ws) {
15
- Db.ws = new RealtimeWebSocketClient({
16
- context: {
17
- appConfig: {
18
- docSizeLimit: 1000,
19
- realtimePingInterval: 10000,
20
- realtimePongWaitTimeout: 5000,
21
- request: this.request
22
- }
23
- }
24
- });
25
- }
26
- return Db.ws.watch(Object.assign(Object.assign({}, options), { envId: this._db.config.env, collectionName: this._coll, query: JSON.stringify({
27
- _id: this.id
57
+ var DocumentReference = (function () {
58
+ function DocumentReference(db, coll, apiOptions, docID, transactionId) {
59
+ var _this = this;
60
+ this.watch = function (options) {
61
+ var ws = getWsInstance(_this._db);
62
+ return ws.watch(__assign(__assign({}, options), { envId: _this._db.config.env, collectionName: _this._coll, query: JSON.stringify({
63
+ _id: _this.id
28
64
  }) }));
29
65
  };
30
66
  this._db = db;
31
67
  this._coll = coll;
32
68
  this.id = docID;
33
69
  this._transactionId = transactionId;
34
- this.request = new Db.reqClass(this._db.config);
70
+ this.request = Db.createRequest(this._db.config);
35
71
  this._apiOptions = apiOptions;
36
72
  }
37
- async create(data) {
38
- if (this.id) {
39
- data['_id'] = this.id;
40
- }
41
- let params = {
42
- collectionName: this._coll,
43
- data: [stringifyByEJSON(serialize(data))],
44
- transactionId: this._transactionId
45
- };
46
- const res = await this.request.send('database.insertDocument', params, getReqOpts(this._apiOptions));
47
- if (res.code) {
48
- return res;
49
- }
50
- if (this._transactionId) {
51
- return {
52
- inserted: 1,
53
- ok: 1,
54
- id: res.data.insertedIds[0],
55
- requestId: res.requestId
56
- };
57
- }
58
- return {
59
- id: res.data.insertedIds[0],
60
- requestId: res.requestId
61
- };
62
- }
63
- async set(data) {
64
- if (!this.id) {
65
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: 'docId不能为空' }));
66
- }
67
- if (!data || typeof data !== 'object') {
68
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
69
- }
70
- if (data.hasOwnProperty('_id')) {
71
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
72
- }
73
- let hasOperator = false;
74
- const checkMixed = objs => {
75
- if (typeof objs === 'object') {
76
- for (let key in objs) {
77
- if (objs[key] instanceof UpdateCommand) {
78
- hasOperator = true;
79
- }
80
- else if (typeof objs[key] === 'object') {
81
- checkMixed(objs[key]);
82
- }
73
+ DocumentReference.prototype.create = function (data, callback) {
74
+ return withCallback(this._create(data), callback);
75
+ };
76
+ DocumentReference.prototype._create = function (data) {
77
+ return __awaiter(this, void 0, void 0, function () {
78
+ var isBatch, params, res;
79
+ return __generator(this, function (_a) {
80
+ switch (_a.label) {
81
+ case 0:
82
+ isBatch = Array.isArray(data);
83
+ if (!isBatch && this.id) {
84
+ data['_id'] = this.id;
85
+ }
86
+ params = {
87
+ collectionName: this._coll,
88
+ data: isBatch
89
+ ? data.map(function (item) { return stringifyByEJSON(serialize(item)); })
90
+ : [stringifyByEJSON(serialize(data))],
91
+ transactionId: this._transactionId
92
+ };
93
+ return [4, this.request.send('database.insertDocument', params, getReqOpts(this._apiOptions))];
94
+ case 1:
95
+ res = _a.sent();
96
+ if (res.code) {
97
+ return [2, res];
98
+ }
99
+ if (this._transactionId) {
100
+ return [2, {
101
+ inserted: 1,
102
+ ok: 1,
103
+ id: res.data.insertedIds[0],
104
+ requestId: res.requestId
105
+ }];
106
+ }
107
+ if (isBatch) {
108
+ return [2, {
109
+ ids: (res.data && res.data.insertedIds) || [],
110
+ requestId: res.requestId
111
+ }];
112
+ }
113
+ return [2, {
114
+ id: res.data.insertedIds[0],
115
+ requestId: res.requestId
116
+ }];
83
117
  }
84
- }
85
- };
86
- checkMixed(data);
87
- if (hasOperator) {
88
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.DATABASE_REQUEST_FAILED), { message: 'update operator complicit' }));
89
- }
90
- let param = {
91
- collectionName: this._coll,
92
- queryType: QueryType.DOC,
93
- data: stringifyByEJSON(serialize(data)),
94
- transactionId: this._transactionId,
95
- multi: false,
96
- merge: false,
97
- upsert: true
98
- };
99
- if (this.id) {
100
- param['query'] = stringifyByEJSON({ _id: this.id });
101
- }
102
- const res = await this.request.send('database.modifyDocument', param, getReqOpts(this._apiOptions));
103
- if (res.code) {
104
- return res;
105
- }
106
- if (this._transactionId) {
107
- return {
108
- updated: res.data.updated,
109
- upserted: [{ _id: res.data.upsert_id }],
110
- requestId: res.requestId
111
- };
112
- }
113
- return {
114
- updated: res.data.updated,
115
- upsertedId: res.data.upsert_id,
116
- requestId: res.requestId
117
- };
118
- }
119
- async update(data) {
120
- if (!data || typeof data !== 'object') {
121
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
122
- }
123
- if (data.hasOwnProperty('_id')) {
124
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
125
- }
126
- const query = stringifyByEJSON({ _id: this.id });
127
- const param = {
128
- collectionName: this._coll,
129
- transactionId: this._transactionId,
130
- data: UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false),
131
- query,
132
- queryType: QueryType.DOC,
133
- multi: false,
134
- merge: true,
135
- upsert: false
136
- };
137
- const res = await this.request.send('database.modifyDocument', param, getReqOpts(this._apiOptions));
138
- if (res.code) {
139
- return res;
140
- }
141
- return {
142
- updated: res.data.updated,
143
- requestId: res.requestId
144
- };
145
- }
146
- async delete() {
147
- return this.remove();
148
- }
149
- async remove() {
150
- const query = stringifyByEJSON({ _id: this.id });
151
- const param = {
152
- collectionName: this._coll,
153
- transactionId: this._transactionId,
154
- query: query,
155
- queryType: QueryType.DOC,
156
- multi: false
157
- };
158
- const res = await this.request.send('database.removeDocument', param, getReqOpts(this._apiOptions));
159
- if (res.code) {
160
- return res;
161
- }
162
- return {
163
- deleted: res.data.deleted,
164
- requestId: res.requestId
165
- };
166
- }
167
- async get() {
168
- const query = stringifyByEJSON({ _id: this.id });
169
- const { projection } = this._apiOptions;
170
- const param = {
171
- collectionName: this._coll,
172
- query,
173
- transactionId: this._transactionId,
174
- queryType: QueryType.DOC,
175
- multi: false
176
- };
177
- if (projection) {
178
- param.projection = stringifyByEJSON(projection);
179
- }
180
- const res = await this.request.send('database.getDocument', param, getReqOpts(this._apiOptions));
181
- if (res.code) {
182
- return res;
183
- }
184
- const list = res.data.list.map(item => EJSON.parse(item));
185
- const documents = Util.formatResDocumentData(list);
186
- if (this._transactionId) {
187
- return {
188
- data: documents[0] || null,
189
- requestId: res.requestId
190
- };
191
- }
192
- return {
193
- data: documents,
194
- requestId: res.requestId
195
- };
196
- }
197
- field(projection) {
198
- let transformProjection = {};
199
- for (let k in projection) {
118
+ });
119
+ });
120
+ };
121
+ DocumentReference.prototype.set = function (data, callback) {
122
+ return withCallback(this._set(data), callback);
123
+ };
124
+ DocumentReference.prototype._set = function (data) {
125
+ return __awaiter(this, void 0, void 0, function () {
126
+ var hasOperator, checkMixed, param, res;
127
+ return __generator(this, function (_a) {
128
+ switch (_a.label) {
129
+ case 0:
130
+ if (!this.id) {
131
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: 'docId不能为空' }))];
132
+ }
133
+ if (!data || typeof data !== 'object') {
134
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }))];
135
+ }
136
+ if (data.hasOwnProperty('_id')) {
137
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }))];
138
+ }
139
+ hasOperator = false;
140
+ checkMixed = function (objs) {
141
+ if (typeof objs === 'object') {
142
+ for (var key in objs) {
143
+ if (objs[key] instanceof UpdateCommand) {
144
+ hasOperator = true;
145
+ }
146
+ else if (typeof objs[key] === 'object') {
147
+ checkMixed(objs[key]);
148
+ }
149
+ }
150
+ }
151
+ };
152
+ checkMixed(data);
153
+ if (hasOperator) {
154
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.DATABASE_REQUEST_FAILED), { message: 'update operator complicit' }))];
155
+ }
156
+ param = {
157
+ collectionName: this._coll,
158
+ queryType: QueryType.DOC,
159
+ data: stringifyByEJSON(serialize(data)),
160
+ transactionId: this._transactionId,
161
+ multi: false,
162
+ merge: false,
163
+ upsert: true
164
+ };
165
+ if (this.id) {
166
+ param['query'] = stringifyByEJSON({ _id: this.id });
167
+ }
168
+ return [4, this.request.send('database.modifyDocument', param, getReqOpts(this._apiOptions))];
169
+ case 1:
170
+ res = _a.sent();
171
+ if (res.code) {
172
+ return [2, res];
173
+ }
174
+ if (this._transactionId) {
175
+ return [2, {
176
+ updated: res.data.updated,
177
+ upserted: [{ _id: res.data.upsert_id }],
178
+ requestId: res.requestId
179
+ }];
180
+ }
181
+ return [2, {
182
+ updated: res.data.updated,
183
+ upsertedId: res.data.upsert_id,
184
+ requestId: res.requestId
185
+ }];
186
+ }
187
+ });
188
+ });
189
+ };
190
+ DocumentReference.prototype.update = function (data, callback) {
191
+ return withCallback(this._update(data), callback);
192
+ };
193
+ DocumentReference.prototype._update = function (data) {
194
+ return __awaiter(this, void 0, void 0, function () {
195
+ var query, param, res;
196
+ return __generator(this, function (_a) {
197
+ switch (_a.label) {
198
+ case 0:
199
+ if (!data || typeof data !== 'object') {
200
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }))];
201
+ }
202
+ if (data.hasOwnProperty('_id')) {
203
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }))];
204
+ }
205
+ query = stringifyByEJSON({ _id: this.id });
206
+ param = {
207
+ collectionName: this._coll,
208
+ transactionId: this._transactionId,
209
+ data: UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false),
210
+ query: query,
211
+ queryType: QueryType.DOC,
212
+ multi: false,
213
+ merge: true,
214
+ upsert: false
215
+ };
216
+ return [4, this.request.send('database.modifyDocument', param, getReqOpts(this._apiOptions))];
217
+ case 1:
218
+ res = _a.sent();
219
+ if (res.code) {
220
+ return [2, res];
221
+ }
222
+ return [2, {
223
+ updated: res.data.updated,
224
+ requestId: res.requestId
225
+ }];
226
+ }
227
+ });
228
+ });
229
+ };
230
+ DocumentReference.prototype.delete = function (callback) {
231
+ return this.remove(callback);
232
+ };
233
+ DocumentReference.prototype.remove = function (callback) {
234
+ return withCallback(this._remove(), callback);
235
+ };
236
+ DocumentReference.prototype._remove = function () {
237
+ return __awaiter(this, void 0, void 0, function () {
238
+ var query, param, res;
239
+ return __generator(this, function (_a) {
240
+ switch (_a.label) {
241
+ case 0:
242
+ query = stringifyByEJSON({ _id: this.id });
243
+ param = {
244
+ collectionName: this._coll,
245
+ transactionId: this._transactionId,
246
+ query: query,
247
+ queryType: QueryType.DOC,
248
+ multi: false
249
+ };
250
+ return [4, this.request.send('database.removeDocument', param, getReqOpts(this._apiOptions))];
251
+ case 1:
252
+ res = _a.sent();
253
+ if (res.code) {
254
+ return [2, res];
255
+ }
256
+ return [2, {
257
+ deleted: res.data.deleted,
258
+ requestId: res.requestId
259
+ }];
260
+ }
261
+ });
262
+ });
263
+ };
264
+ DocumentReference.prototype.get = function (callback) {
265
+ return withCallback(this._get(), callback);
266
+ };
267
+ DocumentReference.prototype._get = function () {
268
+ return __awaiter(this, void 0, void 0, function () {
269
+ var query, projection, param, res, list, documents;
270
+ return __generator(this, function (_a) {
271
+ switch (_a.label) {
272
+ case 0:
273
+ query = stringifyByEJSON({ _id: this.id });
274
+ projection = this._apiOptions.projection;
275
+ param = {
276
+ collectionName: this._coll,
277
+ query: query,
278
+ transactionId: this._transactionId,
279
+ queryType: QueryType.DOC,
280
+ multi: false
281
+ };
282
+ if (projection) {
283
+ param.projection = stringifyByEJSON(projection);
284
+ }
285
+ return [4, this.request.send('database.getDocument', param, getReqOpts(this._apiOptions))];
286
+ case 1:
287
+ res = _a.sent();
288
+ if (res.code) {
289
+ return [2, res];
290
+ }
291
+ list = res.data.list.map(function (item) { return EJSON.parse(item); });
292
+ documents = Util.formatResDocumentData(list);
293
+ if (this._transactionId) {
294
+ return [2, {
295
+ data: documents[0] || null,
296
+ requestId: res.requestId
297
+ }];
298
+ }
299
+ return [2, {
300
+ data: documents,
301
+ requestId: res.requestId
302
+ }];
303
+ }
304
+ });
305
+ });
306
+ };
307
+ DocumentReference.prototype.field = function (projection) {
308
+ var transformProjection = {};
309
+ for (var k in projection) {
200
310
  if (typeof projection[k] === 'boolean') {
201
311
  transformProjection[k] = projection[k] === true ? 1 : 0;
202
312
  }
@@ -207,8 +317,10 @@ export class DocumentReference {
207
317
  transformProjection[k] = projection[k];
208
318
  }
209
319
  }
210
- let newApiOption = Object.assign({}, this._apiOptions);
320
+ var newApiOption = __assign({}, this._apiOptions);
211
321
  newApiOption.projection = transformProjection;
212
322
  return new DocumentReference(this._db, this._coll, newApiOption, this.id, this._transactionId);
213
- }
214
- }
323
+ };
324
+ return DocumentReference;
325
+ }());
326
+ export { DocumentReference };
@@ -0,0 +1 @@
1
+ export {};
@@ -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
  }
@@ -1,54 +1,83 @@
1
+ var __values = (this && this.__values) || function(o) {
2
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
3
+ if (m) return m.call(o);
4
+ if (o && typeof o.length === "number") return {
5
+ next: function () {
6
+ if (o && i >= o.length) o = void 0;
7
+ return { value: o && o[i++], done: !o };
8
+ }
9
+ };
10
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
11
+ };
1
12
  import { SYMBOL_GEO_LINE_STRING } from '../helper/symbol';
2
13
  import { Point } from './point';
3
14
  import { isArray, isNumber } from '../utils/type';
4
- export class LineString {
5
- constructor(points) {
15
+ var LineString = (function () {
16
+ function LineString(points) {
6
17
  if (!isArray(points)) {
7
- throw new TypeError(`"points" must be of type Point[]. Received type ${typeof points}`);
18
+ throw new TypeError("\"points\" must be of type Point[]. Received type ".concat(typeof points));
8
19
  }
9
20
  if (points.length < 2) {
10
21
  throw new Error('"points" must contain 2 points at least');
11
22
  }
12
- points.forEach(point => {
23
+ points.forEach(function (point) {
13
24
  if (!(point instanceof Point)) {
14
- throw new TypeError(`"points" must be of type Point[]. Received type ${typeof point}[]`);
25
+ throw new TypeError("\"points\" must be of type Point[]. Received type ".concat(typeof point, "[]"));
15
26
  }
16
27
  });
17
28
  this.points = points;
18
29
  }
19
- parse(key) {
20
- return {
21
- [key]: {
30
+ LineString.prototype.parse = function (key) {
31
+ var _a;
32
+ return _a = {},
33
+ _a[key] = {
22
34
  type: 'LineString',
23
- coordinates: this.points.map(point => point.toJSON().coordinates)
24
- }
25
- };
26
- }
27
- toJSON() {
35
+ coordinates: this.points.map(function (point) { return point.toJSON().coordinates; })
36
+ },
37
+ _a;
38
+ };
39
+ LineString.prototype.toJSON = function () {
28
40
  return {
29
41
  type: 'LineString',
30
- coordinates: this.points.map(point => point.toJSON().coordinates)
42
+ coordinates: this.points.map(function (point) { return point.toJSON().coordinates; })
31
43
  };
32
- }
33
- static validate(lineString) {
44
+ };
45
+ LineString.validate = function (lineString) {
46
+ var e_1, _a;
34
47
  if (lineString.type !== 'LineString' || !isArray(lineString.coordinates)) {
35
48
  return false;
36
49
  }
37
- for (let point of lineString.coordinates) {
38
- if (!isNumber(point[0]) || !isNumber(point[1])) {
39
- return false;
50
+ try {
51
+ for (var _b = __values(lineString.coordinates), _c = _b.next(); !_c.done; _c = _b.next()) {
52
+ var point = _c.value;
53
+ if (!isNumber(point[0]) || !isNumber(point[1])) {
54
+ return false;
55
+ }
40
56
  }
41
57
  }
58
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
59
+ finally {
60
+ try {
61
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
62
+ }
63
+ finally { if (e_1) throw e_1.error; }
64
+ }
42
65
  return true;
43
- }
44
- static isClosed(lineString) {
45
- const firstPoint = lineString.points[0];
46
- const lastPoint = lineString.points[lineString.points.length - 1];
66
+ };
67
+ LineString.isClosed = function (lineString) {
68
+ var firstPoint = lineString.points[0];
69
+ var lastPoint = lineString.points[lineString.points.length - 1];
47
70
  if (firstPoint.latitude === lastPoint.latitude && firstPoint.longitude === lastPoint.longitude) {
48
71
  return true;
49
72
  }
50
- }
51
- get _internalType() {
52
- return SYMBOL_GEO_LINE_STRING;
53
- }
54
- }
73
+ };
74
+ Object.defineProperty(LineString.prototype, "_internalType", {
75
+ get: function () {
76
+ return SYMBOL_GEO_LINE_STRING;
77
+ },
78
+ enumerable: false,
79
+ configurable: true
80
+ });
81
+ return LineString;
82
+ }());
83
+ export { LineString };
@@ -14,5 +14,5 @@ export declare class MultiLineString {
14
14
  coordinates: number[][][];
15
15
  };
16
16
  static validate(multiLineString: ISerializedMultiLineString): boolean;
17
- readonly _internalType: import("../utils/symbol").InternalSymbol;
17
+ get _internalType(): import("../utils/symbol").InternalSymbol;
18
18
  }