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