@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
package/dist/esm/query.js CHANGED
@@ -1,181 +1,278 @@
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
+ };
48
+ var __read = (this && this.__read) || function (o, n) {
49
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
50
+ if (!m) return o;
51
+ var i = m.call(o), r, ar = [], e;
52
+ try {
53
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
54
+ }
55
+ catch (error) { e = { error: error }; }
56
+ finally {
57
+ try {
58
+ if (r && !r.done && (m = i["return"])) m.call(i);
59
+ }
60
+ finally { if (e) throw e.error; }
61
+ }
62
+ return ar;
63
+ };
1
64
  import { QueryType } from './constant';
2
65
  import { Db } from './index';
3
66
  import { Validate } from './validate';
4
67
  import { Util } from './util';
5
68
  import { QuerySerializer } from './serializer/query';
6
69
  import { UpdateSerializer } from './serializer/update';
7
- import { RealtimeWebSocketClient } from './realtime/websocket-client';
8
70
  import { ErrorCode } from './constant';
9
- import { getReqOpts, stringifyByEJSON, processReturn } from './utils/utils';
71
+ import { getWsInstance, getReqOpts, stringifyByEJSON, toWatchQuery, processReturn, withCallback } from './utils/utils';
10
72
  import { ERRORS } from './const/code';
11
73
  import { EJSON } from 'bson';
12
- export class Query {
13
- constructor(db, coll, fieldFilters, apiOptions, transactionId) {
14
- this.watch = (options) => {
15
- if (!Db.ws) {
16
- Db.ws = new RealtimeWebSocketClient({
17
- context: {
18
- appConfig: {
19
- docSizeLimit: 1000,
20
- realtimePingInterval: 10000,
21
- realtimePongWaitTimeout: 5000,
22
- request: this._request
23
- }
24
- }
25
- });
26
- }
27
- const { limit, order } = this._apiOptions;
28
- return Db.ws.watch(Object.assign(Object.assign({}, options), { envId: this._db.config.env, collectionName: this._coll, query: JSON.stringify(this._fieldFilters), limit, orderBy: order
29
- ? order.reduce((acc, cur) => {
30
- acc[cur.field] = cur.direction;
31
- return acc;
32
- }, {})
33
- : undefined }));
74
+ var Query = (function () {
75
+ function Query(db, coll, fieldFilters, apiOptions, transactionId) {
76
+ var _this = this;
77
+ this.watch = function (options) {
78
+ var ws = getWsInstance(_this._db);
79
+ var _a = _this._apiOptions, limit = _a.limit, order = _a.order;
80
+ var orderBy = order
81
+ ? Object.entries(order).reduce(function (acc, _a) {
82
+ var _b = __read(_a, 2), field = _b[0], direction = _b[1];
83
+ acc[field] = direction === -1 ? 'desc' : 'asc';
84
+ return acc;
85
+ }, {})
86
+ : undefined;
87
+ return ws.watch(__assign(__assign({}, options), { envId: _this._db.config.env, collectionName: _this._coll, query: toWatchQuery(_this._fieldFilters), limit: limit, orderBy: orderBy }));
34
88
  };
35
89
  this._db = db;
36
90
  this._coll = coll;
37
91
  this._fieldFilters = fieldFilters;
38
92
  this._apiOptions = apiOptions || {};
39
- this._request = new Db.reqClass(this._db.config);
93
+ this._request = Db.createRequest(this._db.config);
40
94
  this._transactionId = transactionId;
41
95
  }
42
- async get() {
43
- const order = this._apiOptions.order;
44
- let param = {
45
- collectionName: this._coll,
46
- queryType: QueryType.WHERE,
47
- transactionId: this._transactionId
48
- };
49
- if (this._fieldFilters) {
50
- param.query = this._fieldFilters;
51
- }
52
- if (order) {
53
- param.order = stringifyByEJSON(order);
54
- }
55
- const offset = this._apiOptions.offset;
56
- if (offset) {
57
- param.offset = offset;
58
- }
59
- const limit = this._apiOptions.limit;
60
- if (limit) {
61
- param.limit = limit < 1000 ? limit : 1000;
62
- }
63
- else {
64
- param.limit = 100;
65
- }
66
- const projection = this._apiOptions.projection;
67
- if (projection) {
68
- param.projection = stringifyByEJSON(projection);
69
- }
70
- const res = await this._request.send('database.getDocument', param, getReqOpts(this._apiOptions));
71
- if (res.code) {
72
- return res;
73
- }
74
- const list = res.data.list.map(item => EJSON.parse(item));
75
- const documents = Util.formatResDocumentData(list);
76
- const result = {
77
- data: documents,
78
- requestId: res.requestId
79
- };
80
- if (res.total)
81
- result.total = res.total;
82
- if (res.limit)
83
- result.limit = res.limit;
84
- if (res.offset)
85
- result.offset = res.offset;
86
- return result;
87
- }
88
- async count() {
89
- let param = {
90
- collectionName: this._coll,
91
- queryType: QueryType.WHERE
92
- };
93
- if (this._fieldFilters) {
94
- param.query = this._fieldFilters;
95
- }
96
- const res = await this._request.send('database.calculateDocument', param, getReqOpts(this._apiOptions));
97
- if (res.code) {
98
- return res;
99
- }
100
- return {
101
- requestId: res.requestId,
102
- total: res.data.total
103
- };
104
- }
105
- where(query) {
96
+ Query.prototype.get = function (callback) {
97
+ return withCallback(this._get(), callback);
98
+ };
99
+ Query.prototype._get = function () {
100
+ return __awaiter(this, void 0, void 0, function () {
101
+ var order, param, offset, limit, projection, res, list, documents, result;
102
+ return __generator(this, function (_a) {
103
+ switch (_a.label) {
104
+ case 0:
105
+ order = this._apiOptions.order;
106
+ param = {
107
+ collectionName: this._coll,
108
+ queryType: QueryType.WHERE,
109
+ transactionId: this._transactionId
110
+ };
111
+ if (this._fieldFilters) {
112
+ param.query = this._fieldFilters;
113
+ }
114
+ if (order) {
115
+ param.order = stringifyByEJSON(order);
116
+ }
117
+ offset = this._apiOptions.offset;
118
+ if (offset) {
119
+ param.offset = offset;
120
+ }
121
+ limit = this._apiOptions.limit;
122
+ if (limit) {
123
+ param.limit = limit < 1000 ? limit : 1000;
124
+ }
125
+ else {
126
+ param.limit = 100;
127
+ }
128
+ projection = this._apiOptions.projection;
129
+ if (projection) {
130
+ param.projection = stringifyByEJSON(projection);
131
+ }
132
+ return [4, this._request.send('database.getDocument', param, getReqOpts(this._apiOptions))];
133
+ case 1:
134
+ res = _a.sent();
135
+ if (res.code) {
136
+ return [2, res];
137
+ }
138
+ list = res.data.list.map(function (item) { return EJSON.parse(item); });
139
+ documents = Util.formatResDocumentData(list);
140
+ result = {
141
+ data: documents,
142
+ requestId: res.requestId
143
+ };
144
+ if (res.total)
145
+ result.total = res.total;
146
+ if (res.limit)
147
+ result.limit = res.limit;
148
+ if (res.offset)
149
+ result.offset = res.offset;
150
+ return [2, result];
151
+ }
152
+ });
153
+ });
154
+ };
155
+ Query.prototype.count = function (callback) {
156
+ return withCallback(this._count(), callback);
157
+ };
158
+ Query.prototype._count = function () {
159
+ return __awaiter(this, void 0, void 0, function () {
160
+ var param, res;
161
+ return __generator(this, function (_a) {
162
+ switch (_a.label) {
163
+ case 0:
164
+ param = {
165
+ collectionName: this._coll,
166
+ queryType: QueryType.WHERE
167
+ };
168
+ if (this._fieldFilters) {
169
+ param.query = this._fieldFilters;
170
+ }
171
+ return [4, this._request.send('database.calculateDocument', param, getReqOpts(this._apiOptions))];
172
+ case 1:
173
+ res = _a.sent();
174
+ if (res.code) {
175
+ return [2, res];
176
+ }
177
+ return [2, {
178
+ requestId: res.requestId,
179
+ total: res.data.total
180
+ }];
181
+ }
182
+ });
183
+ });
184
+ };
185
+ Query.prototype.where = function (query) {
106
186
  if (Object.prototype.toString.call(query).slice(8, -1) !== 'Object') {
107
187
  throw Error(ErrorCode.QueryParamTypeError);
108
188
  }
109
- const keys = Object.keys(query);
110
- const checkFlag = keys.some(item => {
189
+ var keys = Object.keys(query);
190
+ var checkFlag = keys.some(function (item) {
111
191
  return query[item] !== undefined;
112
192
  });
113
193
  if (keys.length && !checkFlag) {
114
194
  throw Error(ErrorCode.QueryParamValueError);
115
195
  }
116
196
  return new Query(this._db, this._coll, QuerySerializer.encodeEJSON(query, this._apiOptions.raw || false), this._apiOptions, this._transactionId);
117
- }
118
- options(apiOptions) {
197
+ };
198
+ Query.prototype.options = function (apiOptions) {
119
199
  Validate.isValidOptions(apiOptions);
120
200
  return new Query(this._db, this._coll, this._fieldFilters, apiOptions, this._transactionId);
121
- }
122
- orderBy(fieldPath, directionStr) {
201
+ };
202
+ Query.prototype.orderBy = function (fieldPath, directionStr) {
203
+ var _a;
123
204
  Validate.isFieldPath(fieldPath);
124
205
  Validate.isFieldOrder(directionStr);
125
- const newOrder = {
126
- [fieldPath]: directionStr === 'desc' ? -1 : 1
127
- };
128
- const order = this._apiOptions.order || {};
129
- const newApiOption = Object.assign({}, this._apiOptions, {
206
+ var newOrder = (_a = {},
207
+ _a[fieldPath] = directionStr === 'desc' ? -1 : 1,
208
+ _a);
209
+ var order = this._apiOptions.order || {};
210
+ var newApiOption = Object.assign({}, this._apiOptions, {
130
211
  order: Object.assign({}, order, newOrder)
131
212
  });
132
213
  return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId);
133
- }
134
- limit(limit) {
214
+ };
215
+ Query.prototype.limit = function (limit) {
135
216
  Validate.isInteger('limit', limit);
136
- let newApiOption = Object.assign({}, this._apiOptions);
217
+ var newApiOption = __assign({}, this._apiOptions);
137
218
  newApiOption.limit = limit;
138
219
  return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId);
139
- }
140
- skip(offset) {
220
+ };
221
+ Query.prototype.skip = function (offset) {
141
222
  Validate.isInteger('offset', offset);
142
- let newApiOption = Object.assign({}, this._apiOptions);
223
+ var newApiOption = __assign({}, this._apiOptions);
143
224
  newApiOption.offset = offset;
144
225
  return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId);
145
- }
146
- async update(data) {
147
- if (!data || typeof data !== 'object') {
148
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
149
- }
150
- if (data.hasOwnProperty('_id')) {
151
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
152
- }
153
- let { multiple } = this._apiOptions;
154
- const multi = multiple === undefined ? true : multiple;
155
- let param = {
156
- collectionName: this._coll,
157
- queryType: QueryType.WHERE,
158
- multi,
159
- merge: true,
160
- upsert: false,
161
- data: UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false)
162
- };
163
- if (this._fieldFilters) {
164
- param.query = this._fieldFilters;
165
- }
166
- const res = await this._request.send('database.modifyDocument', param, getReqOpts(this._apiOptions));
167
- if (res.code) {
168
- return res;
169
- }
170
- return {
171
- requestId: res.requestId,
172
- updated: res.data.updated,
173
- upsertId: res.data.upsert_id
174
- };
175
- }
176
- field(projection) {
177
- let transformProjection = {};
178
- for (let k in projection) {
226
+ };
227
+ Query.prototype.update = function (data, callback) {
228
+ return withCallback(this._update(data), callback);
229
+ };
230
+ Query.prototype._update = function (data) {
231
+ return __awaiter(this, void 0, void 0, function () {
232
+ var multiple, multi, param, res;
233
+ return __generator(this, function (_a) {
234
+ switch (_a.label) {
235
+ case 0:
236
+ if (!data || typeof data !== 'object') {
237
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }))];
238
+ }
239
+ if (data.hasOwnProperty('_id')) {
240
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }))];
241
+ }
242
+ multiple = this._apiOptions.multiple;
243
+ multi = multiple === undefined ? true : multiple;
244
+ param = {
245
+ collectionName: this._coll,
246
+ queryType: QueryType.WHERE,
247
+ multi: multi,
248
+ merge: true,
249
+ upsert: false,
250
+ data: UpdateSerializer.encodeEJSON(data, this._apiOptions.raw || false)
251
+ };
252
+ if (this._transactionId) {
253
+ param.transactionId = this._transactionId;
254
+ }
255
+ if (this._fieldFilters) {
256
+ param.query = this._fieldFilters;
257
+ }
258
+ return [4, this._request.send('database.modifyDocument', param, getReqOpts(this._apiOptions))];
259
+ case 1:
260
+ res = _a.sent();
261
+ if (res.code) {
262
+ return [2, res];
263
+ }
264
+ return [2, {
265
+ requestId: res.requestId,
266
+ updated: res.data.updated,
267
+ upsertId: res.data.upsert_id
268
+ }];
269
+ }
270
+ });
271
+ });
272
+ };
273
+ Query.prototype.field = function (projection) {
274
+ var transformProjection = {};
275
+ for (var k in projection) {
179
276
  if (typeof projection[k] === 'boolean') {
180
277
  transformProjection[k] = projection[k] === true ? 1 : 0;
181
278
  }
@@ -186,61 +283,89 @@ export class Query {
186
283
  transformProjection[k] = projection[k];
187
284
  }
188
285
  }
189
- let newApiOption = Object.assign({}, this._apiOptions);
286
+ var newApiOption = __assign({}, this._apiOptions);
190
287
  newApiOption.projection = transformProjection;
191
288
  return new Query(this._db, this._coll, this._fieldFilters, newApiOption, this._transactionId);
192
- }
193
- async remove() {
194
- const { offset, limit, projection, order } = this._apiOptions;
195
- if (offset !== undefined ||
196
- limit !== undefined ||
197
- projection !== undefined ||
198
- order !== undefined) {
199
- console.warn('`offset`, `limit`, `projection`, `orderBy` are not supported in remove() operation');
200
- }
201
- let { multiple } = this._apiOptions;
202
- const multi = multiple === undefined ? true : multiple;
203
- const param = {
204
- collectionName: this._coll,
205
- query: this._fieldFilters,
206
- queryType: QueryType.WHERE,
207
- multi
208
- };
209
- const res = await this._request.send('database.removeDocument', param, getReqOpts(this._apiOptions));
210
- if (res.code) {
211
- return res;
212
- }
213
- return {
214
- requestId: res.requestId,
215
- deleted: res.data.deleted
216
- };
217
- }
218
- async updateAndReturn(data) {
219
- if (!data || typeof data !== 'object') {
220
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }));
221
- }
222
- if (data.hasOwnProperty('_id')) {
223
- return processReturn(this._db.config.throwOnCode, Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }));
224
- }
225
- let param = {
226
- collectionName: this._coll,
227
- queryType: QueryType.WHERE,
228
- data: UpdateSerializer.encodeEJSON(data, false)
229
- };
230
- if (this._transactionId) {
231
- param.transactionId = this._transactionId;
232
- }
233
- if (this._fieldFilters) {
234
- param.query = this._fieldFilters;
235
- }
236
- const res = await this._request.send('database.modifyAndReturnDoc', param, getReqOpts(this._apiOptions));
237
- if (res.code) {
238
- return res;
239
- }
240
- return {
241
- requestId: res.requestId,
242
- updated: res.data.updated,
243
- doc: res.data.doc && EJSON.parse(res.data.doc)
244
- };
245
- }
246
- }
289
+ };
290
+ Query.prototype.remove = function (callback) {
291
+ return withCallback(this._remove(), callback);
292
+ };
293
+ Query.prototype._remove = function () {
294
+ return __awaiter(this, void 0, void 0, function () {
295
+ var _a, offset, limit, projection, order, multiple, multi, param, res;
296
+ return __generator(this, function (_b) {
297
+ switch (_b.label) {
298
+ case 0:
299
+ _a = this._apiOptions, offset = _a.offset, limit = _a.limit, projection = _a.projection, order = _a.order;
300
+ if (offset !== undefined ||
301
+ limit !== undefined ||
302
+ projection !== undefined ||
303
+ order !== undefined) {
304
+ console.warn('`offset`, `limit`, `projection`, `orderBy` are not supported in remove() operation');
305
+ }
306
+ multiple = this._apiOptions.multiple;
307
+ multi = multiple === undefined ? true : multiple;
308
+ param = {
309
+ collectionName: this._coll,
310
+ query: this._fieldFilters,
311
+ queryType: QueryType.WHERE,
312
+ multi: multi
313
+ };
314
+ if (this._transactionId) {
315
+ param.transactionId = this._transactionId;
316
+ }
317
+ return [4, this._request.send('database.removeDocument', param, getReqOpts(this._apiOptions))];
318
+ case 1:
319
+ res = _b.sent();
320
+ if (res.code) {
321
+ return [2, res];
322
+ }
323
+ return [2, {
324
+ requestId: res.requestId,
325
+ deleted: res.data.deleted
326
+ }];
327
+ }
328
+ });
329
+ });
330
+ };
331
+ Query.prototype.updateAndReturn = function (data) {
332
+ return __awaiter(this, void 0, void 0, function () {
333
+ var param, res;
334
+ return __generator(this, function (_a) {
335
+ switch (_a.label) {
336
+ case 0:
337
+ if (!data || typeof data !== 'object') {
338
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '参数必需是非空对象' }))];
339
+ }
340
+ if (data.hasOwnProperty('_id')) {
341
+ return [2, processReturn(this._db.config.throwOnCode, __assign(__assign({}, ERRORS.INVALID_PARAM), { message: '不能更新_id的值' }))];
342
+ }
343
+ param = {
344
+ collectionName: this._coll,
345
+ queryType: QueryType.WHERE,
346
+ data: UpdateSerializer.encodeEJSON(data, false)
347
+ };
348
+ if (this._transactionId) {
349
+ param.transactionId = this._transactionId;
350
+ }
351
+ if (this._fieldFilters) {
352
+ param.query = this._fieldFilters;
353
+ }
354
+ return [4, this._request.send('database.modifyAndReturnDoc', param, getReqOpts(this._apiOptions))];
355
+ case 1:
356
+ res = _a.sent();
357
+ if (res.code) {
358
+ return [2, res];
359
+ }
360
+ return [2, {
361
+ requestId: res.requestId,
362
+ updated: res.data.updated,
363
+ doc: res.data.doc && EJSON.parse(res.data.doc)
364
+ }];
365
+ }
366
+ });
367
+ });
368
+ };
369
+ return Query;
370
+ }());
371
+ export { Query };
@@ -13,6 +13,6 @@ export declare class RegExp {
13
13
  options: string;
14
14
  };
15
15
  };
16
- readonly _internalType: import("../utils/symbol").InternalSymbol;
16
+ get _internalType(): import("../utils/symbol").InternalSymbol;
17
17
  }
18
18
  export declare function RegExpConstructor(param: any): RegExp;
@@ -1,6 +1,7 @@
1
1
  import { SYMBOL_REGEXP } from '../helper/symbol';
2
- export class RegExp {
3
- constructor({ regexp, options }) {
2
+ var RegExp = (function () {
3
+ function RegExp(_a) {
4
+ var regexp = _a.regexp, options = _a.options;
4
5
  if (!regexp) {
5
6
  throw new TypeError('regexp must be a string');
6
7
  }
@@ -9,18 +10,24 @@ export class RegExp {
9
10
  options: options || ''
10
11
  };
11
12
  }
12
- parse() {
13
+ RegExp.prototype.parse = function () {
13
14
  return {
14
15
  $regularExpression: {
15
16
  pattern: this.$regularExpression.pattern,
16
17
  options: this.$regularExpression.options
17
18
  }
18
19
  };
19
- }
20
- get _internalType() {
21
- return SYMBOL_REGEXP;
22
- }
23
- }
20
+ };
21
+ Object.defineProperty(RegExp.prototype, "_internalType", {
22
+ get: function () {
23
+ return SYMBOL_REGEXP;
24
+ },
25
+ enumerable: false,
26
+ configurable: true
27
+ });
28
+ return RegExp;
29
+ }());
30
+ export { RegExp };
24
31
  export function RegExpConstructor(param) {
25
32
  return new RegExp(param);
26
33
  }
@@ -1,6 +1,6 @@
1
1
  import { LogicCommand } from '../commands/logic';
2
- export declare type IQueryCondition = Record<string, any> | LogicCommand;
3
- export declare type AnyObject = {
2
+ export type IQueryCondition = Record<string, any> | LogicCommand;
3
+ export type AnyObject = {
4
4
  [x: string]: any;
5
5
  };
6
6
  export declare function flattenQueryObject(query: Record<string, any>): Record<string, any>;