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