@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.
- package/dist/commonjs/ObjectId/index.d.ts +1 -1
- package/dist/commonjs/ObjectId/index.js +16 -9
- package/dist/commonjs/aggregate.js +126 -75
- package/dist/commonjs/collection.d.ts +3 -2
- package/dist/commonjs/collection.js +131 -58
- package/dist/commonjs/command.d.ts +5 -5
- package/dist/commonjs/command.js +181 -154
- package/dist/commonjs/commands/logic.js +30 -20
- package/dist/commonjs/commands/query.d.ts +1 -1
- package/dist/commonjs/commands/query.js +85 -65
- package/dist/commonjs/commands/update.js +9 -7
- package/dist/commonjs/config/error.config.js +1 -0
- package/dist/commonjs/const/code.js +1 -0
- package/dist/commonjs/constant.d.ts +7 -7
- package/dist/commonjs/constant.js +13 -11
- package/dist/commonjs/document.d.ts +10 -6
- package/dist/commonjs/document.js +305 -193
- package/dist/commonjs/geo/index.js +20 -9
- package/dist/commonjs/geo/lineString.d.ts +1 -1
- package/dist/commonjs/geo/lineString.js +62 -33
- package/dist/commonjs/geo/multiLineString.d.ts +1 -1
- package/dist/commonjs/geo/multiLineString.js +71 -32
- package/dist/commonjs/geo/multiPoint.d.ts +1 -1
- package/dist/commonjs/geo/multiPoint.js +58 -29
- package/dist/commonjs/geo/multiPolygon.d.ts +1 -1
- package/dist/commonjs/geo/multiPolygon.js +96 -36
- package/dist/commonjs/geo/point.d.ts +1 -1
- package/dist/commonjs/geo/point.js +30 -23
- package/dist/commonjs/geo/polygon.d.ts +1 -1
- package/dist/commonjs/geo/polygon.js +78 -39
- package/dist/commonjs/helper/symbol.js +17 -5
- package/dist/commonjs/index.d.ts +8 -2
- package/dist/commonjs/index.js +73 -20
- package/dist/commonjs/lib/util.d.ts +1 -0
- package/dist/commonjs/lib/util.js +26 -0
- package/dist/commonjs/operator-map.js +7 -6
- package/dist/commonjs/query.d.ts +9 -5
- package/dist/commonjs/query.js +330 -205
- package/dist/commonjs/regexp/index.d.ts +1 -1
- package/dist/commonjs/regexp/index.js +16 -9
- package/dist/commonjs/serializer/common.d.ts +2 -2
- package/dist/commonjs/serializer/common.js +59 -22
- package/dist/commonjs/serializer/datatype.d.ts +2 -2
- package/dist/commonjs/serializer/datatype.js +58 -21
- package/dist/commonjs/serializer/query.d.ts +1 -1
- package/dist/commonjs/serializer/query.js +137 -127
- package/dist/commonjs/serializer/update.d.ts +1 -1
- package/dist/commonjs/serializer/update.js +83 -78
- package/dist/commonjs/serverDate/index.d.ts +1 -1
- package/dist/commonjs/serverDate/index.js +16 -9
- package/dist/commonjs/transaction/collection.d.ts +7 -0
- package/dist/commonjs/transaction/collection.js +49 -0
- package/dist/commonjs/transaction/document.d.ts +27 -0
- package/dist/commonjs/transaction/document.js +205 -0
- package/dist/commonjs/transaction/index.js +196 -86
- package/dist/commonjs/transaction/query.d.ts +43 -0
- package/dist/commonjs/transaction/query.js +261 -0
- package/dist/commonjs/typings/index.d.ts +4 -4
- package/dist/commonjs/typings/realtime.d.ts +16 -16
- package/dist/commonjs/util.js +123 -92
- package/dist/commonjs/utils/error.d.ts +2 -1
- package/dist/commonjs/utils/error.js +74 -38
- package/dist/commonjs/utils/symbol.js +33 -14
- package/dist/commonjs/utils/type.d.ts +1 -1
- package/dist/commonjs/utils/type.js +25 -13
- package/dist/commonjs/utils/utils.d.ts +5 -1
- package/dist/commonjs/utils/utils.js +153 -22
- package/dist/commonjs/validate.js +79 -63
- package/dist/esm/ObjectId/index.d.ts +1 -1
- package/dist/esm/ObjectId/index.js +15 -8
- package/dist/esm/aggregate.js +118 -66
- package/dist/esm/collection.d.ts +3 -2
- package/dist/esm/collection.js +125 -52
- package/dist/esm/command.d.ts +5 -5
- package/dist/esm/command.js +175 -147
- package/dist/esm/commands/logic.js +30 -20
- package/dist/esm/commands/query.d.ts +1 -1
- package/dist/esm/commands/query.js +91 -71
- package/dist/esm/commands/update.js +8 -6
- package/dist/esm/config/error.config.js +1 -1
- package/dist/esm/const/code.js +1 -1
- package/dist/esm/constant.d.ts +7 -7
- package/dist/esm/constant.js +12 -11
- package/dist/esm/document.d.ts +10 -6
- package/dist/esm/document.js +297 -185
- package/dist/esm/geo/interface.js +1 -0
- package/dist/esm/geo/lineString.d.ts +1 -1
- package/dist/esm/geo/lineString.js +57 -28
- package/dist/esm/geo/multiLineString.d.ts +1 -1
- package/dist/esm/geo/multiLineString.js +66 -27
- package/dist/esm/geo/multiPoint.d.ts +1 -1
- package/dist/esm/geo/multiPoint.js +53 -24
- package/dist/esm/geo/multiPolygon.d.ts +1 -1
- package/dist/esm/geo/multiPolygon.js +91 -31
- package/dist/esm/geo/point.d.ts +1 -1
- package/dist/esm/geo/point.js +26 -19
- package/dist/esm/geo/polygon.d.ts +1 -1
- package/dist/esm/geo/polygon.js +73 -34
- package/dist/esm/helper/symbol.js +13 -13
- package/dist/esm/index.d.ts +8 -2
- package/dist/esm/index.js +62 -9
- package/dist/esm/lib/util.d.ts +1 -0
- package/dist/esm/lib/util.js +22 -0
- package/dist/esm/operator-map.js +4 -4
- package/dist/esm/query.d.ts +9 -5
- package/dist/esm/query.js +321 -196
- package/dist/esm/regexp/index.d.ts +1 -1
- package/dist/esm/regexp/index.js +15 -8
- package/dist/esm/serializer/common.d.ts +2 -2
- package/dist/esm/serializer/common.js +49 -13
- package/dist/esm/serializer/datatype.d.ts +2 -2
- package/dist/esm/serializer/datatype.js +44 -8
- package/dist/esm/serializer/query.d.ts +1 -1
- package/dist/esm/serializer/query.js +112 -102
- package/dist/esm/serializer/update.d.ts +1 -1
- package/dist/esm/serializer/update.js +69 -64
- package/dist/esm/serverDate/index.d.ts +1 -1
- package/dist/esm/serverDate/index.js +15 -8
- package/dist/esm/transaction/collection.d.ts +7 -0
- package/dist/esm/transaction/collection.js +46 -0
- package/dist/esm/transaction/document.d.ts +27 -0
- package/dist/esm/transaction/document.js +202 -0
- package/dist/esm/transaction/index.js +191 -81
- package/dist/esm/transaction/query.d.ts +43 -0
- package/dist/esm/transaction/query.js +258 -0
- package/dist/esm/typings/index.d.ts +4 -4
- package/dist/esm/typings/index.js +1 -0
- package/dist/esm/typings/realtime.d.ts +16 -16
- package/dist/esm/typings/realtime.js +1 -0
- package/dist/esm/util.js +121 -90
- package/dist/esm/utils/error.d.ts +2 -1
- package/dist/esm/utils/error.js +70 -35
- package/dist/esm/utils/symbol.js +33 -14
- package/dist/esm/utils/type.d.ts +1 -1
- package/dist/esm/utils/type.js +12 -12
- package/dist/esm/utils/utils.d.ts +5 -1
- package/dist/esm/utils/utils.js +137 -16
- package/dist/esm/validate.js +65 -49
- package/jest.config.js +14 -0
- package/package.json +14 -10
- package/src/aggregate.ts +1 -5
- package/src/collection.ts +15 -2
- package/src/document.ts +45 -34
- package/src/index.ts +51 -4
- package/src/query.ts +48 -38
- package/src/serializer/query.ts +18 -6
- package/src/transaction/index.ts +1 -1
- package/src/typings/index.ts +0 -4
- package/src/utils/utils.ts +105 -0
- package/src/validate.ts +3 -6
- package/tests/gateway-contract.test.ts +180 -0
- package/tests/index.test.ts +283 -0
- package/tests/transaction-query.test.ts +207 -0
- package/tests/watch-query.test.ts +144 -0
- package/tsconfig.esm.json +4 -2
- package/tsconfig.json +16 -6
- package/tsconfig.test.json +15 -0
- package/dist/commonjs/realtime/error.d.ts +0 -7
- package/dist/commonjs/realtime/error.js +0 -11
- package/dist/commonjs/realtime/listener.d.ts +0 -14
- package/dist/commonjs/realtime/listener.js +0 -17
- package/dist/commonjs/realtime/message.d.ts +0 -3
- package/dist/commonjs/realtime/message.js +0 -10
- package/dist/commonjs/realtime/snapshot.d.ts +0 -16
- package/dist/commonjs/realtime/snapshot.js +0 -42
- package/dist/commonjs/realtime/virtual-websocket-client.d.ts +0 -63
- package/dist/commonjs/realtime/virtual-websocket-client.js +0 -736
- package/dist/commonjs/realtime/websocket-client.d.ts +0 -78
- package/dist/commonjs/realtime/websocket-client.js +0 -567
- package/dist/commonjs/realtime/ws-event.d.ts +0 -126
- package/dist/commonjs/realtime/ws-event.js +0 -139
- package/dist/commonjs/transaction.d.ts +0 -42
- package/dist/commonjs/transaction.js +0 -126
- package/dist/esm/realtime/error.d.ts +0 -7
- package/dist/esm/realtime/error.js +0 -8
- package/dist/esm/realtime/listener.d.ts +0 -14
- package/dist/esm/realtime/listener.js +0 -14
- package/dist/esm/realtime/message.d.ts +0 -3
- package/dist/esm/realtime/message.js +0 -6
- package/dist/esm/realtime/snapshot.d.ts +0 -16
- package/dist/esm/realtime/snapshot.js +0 -39
- package/dist/esm/realtime/virtual-websocket-client.d.ts +0 -63
- package/dist/esm/realtime/virtual-websocket-client.js +0 -733
- package/dist/esm/realtime/websocket-client.d.ts +0 -78
- package/dist/esm/realtime/websocket-client.js +0 -564
- package/dist/esm/realtime/ws-event.d.ts +0 -126
- package/dist/esm/realtime/ws-event.js +0 -137
- package/dist/esm/transaction.d.ts +0 -42
- package/dist/esm/transaction.js +0 -121
- package/src/realtime/error.ts +0 -19
- package/src/realtime/listener.ts +0 -35
- package/src/realtime/message.ts +0 -14
- package/src/realtime/snapshot.ts +0 -60
- package/src/realtime/virtual-websocket-client.ts +0 -1195
- package/src/realtime/websocket-client.ts +0 -995
- package/src/realtime/ws-event.ts +0 -163
- package/src/transaction.ts +0 -190
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
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
|
+
};
|
|
2
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
exports.Validate = void 0;
|
|
15
|
+
var constant_1 = require("./constant");
|
|
16
|
+
var util_1 = require("./util");
|
|
17
|
+
var code_1 = require("./const/code");
|
|
18
|
+
var utils_1 = require("./utils/utils");
|
|
19
|
+
var type_1 = require("./utils/type");
|
|
20
|
+
var symbol_1 = require("./helper/symbol");
|
|
21
|
+
var validOptionsKeys = ['limit', 'offset', 'projection', 'order', 'multiple', 'timeout', 'raw'];
|
|
22
|
+
var Validate = (function () {
|
|
23
|
+
function Validate() {
|
|
24
|
+
}
|
|
25
|
+
Validate.isGeopoint = function (point, degree) {
|
|
12
26
|
if (util_1.Util.whichType(degree) !== constant_1.FieldType.Number) {
|
|
13
27
|
throw new Error('Geo Point must be number type');
|
|
14
28
|
}
|
|
15
|
-
|
|
29
|
+
var degreeAbs = Math.abs(degree);
|
|
16
30
|
if (point === 'latitude' && degreeAbs > 90) {
|
|
17
31
|
throw new Error('latitude should be a number ranges from -90 to 90');
|
|
18
32
|
}
|
|
@@ -20,92 +34,93 @@ class Validate {
|
|
|
20
34
|
throw new Error('longitude should be a number ranges from -180 to 180');
|
|
21
35
|
}
|
|
22
36
|
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
37
|
+
};
|
|
38
|
+
Validate.isInteger = function (param, num) {
|
|
25
39
|
if (!Number.isInteger(num)) {
|
|
26
40
|
throw new Error(param + constant_1.ErrorCode.IntergerError);
|
|
27
41
|
}
|
|
28
42
|
return true;
|
|
29
|
-
}
|
|
30
|
-
|
|
43
|
+
};
|
|
44
|
+
Validate.mustBeBoolean = function (param, bool) {
|
|
31
45
|
if (typeof bool !== 'boolean') {
|
|
32
46
|
throw new Error(param + constant_1.ErrorCode.BooleanError);
|
|
33
47
|
}
|
|
34
48
|
return true;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (type_1.getType(value) !== 'object') {
|
|
38
|
-
throw utils_1.E(
|
|
39
|
-
}
|
|
40
|
-
for (
|
|
41
|
-
|
|
42
|
-
if (type_1.getType(subValue) === 'number') {
|
|
49
|
+
};
|
|
50
|
+
Validate.isProjection = function (param, value) {
|
|
51
|
+
if ((0, type_1.getType)(value) !== 'object') {
|
|
52
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "".concat(param, " projection must be an object") }));
|
|
53
|
+
}
|
|
54
|
+
for (var key in value) {
|
|
55
|
+
var subValue = value[key];
|
|
56
|
+
if ((0, type_1.getType)(subValue) === 'number') {
|
|
43
57
|
if (subValue !== 0 && subValue !== 1) {
|
|
44
|
-
throw utils_1.E(
|
|
58
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "if the value in projection is of number, it must be 0 or 1" }));
|
|
45
59
|
}
|
|
46
60
|
}
|
|
47
|
-
else if (type_1.getType(subValue) === 'object') {
|
|
61
|
+
else if ((0, type_1.getType)(subValue) === 'object') {
|
|
48
62
|
}
|
|
49
63
|
else {
|
|
50
|
-
throw utils_1.E(
|
|
64
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: 'invalid projection' }));
|
|
51
65
|
}
|
|
52
66
|
}
|
|
53
67
|
return true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (type_1.getType(value) !== 'object') {
|
|
57
|
-
throw utils_1.E(
|
|
68
|
+
};
|
|
69
|
+
Validate.isOrder = function (param, value) {
|
|
70
|
+
if ((0, type_1.getType)(value) !== 'object') {
|
|
71
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "".concat(param, " order must be an object") }));
|
|
58
72
|
}
|
|
59
|
-
for (
|
|
60
|
-
|
|
73
|
+
for (var key in value) {
|
|
74
|
+
var subValue = value[key];
|
|
61
75
|
if (subValue !== 1 && subValue !== -1) {
|
|
62
|
-
throw utils_1.E(
|
|
76
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "order value must be 1 or -1" }));
|
|
63
77
|
}
|
|
64
78
|
}
|
|
65
79
|
return true;
|
|
66
|
-
}
|
|
67
|
-
|
|
80
|
+
};
|
|
81
|
+
Validate.isFieldOrder = function (direction) {
|
|
68
82
|
if (constant_1.OrderDirectionList.indexOf(direction) === -1) {
|
|
69
83
|
throw new Error(constant_1.ErrorCode.DirectionError);
|
|
70
84
|
}
|
|
71
85
|
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
86
|
+
};
|
|
87
|
+
Validate.isFieldPath = function (path) {
|
|
74
88
|
if (!/^[a-zA-Z0-9-_\.]/.test(path)) {
|
|
75
89
|
throw new Error();
|
|
76
90
|
}
|
|
77
91
|
return true;
|
|
78
|
-
}
|
|
79
|
-
|
|
92
|
+
};
|
|
93
|
+
Validate.isOperator = function (op) {
|
|
80
94
|
if (constant_1.WhereFilterOpList.indexOf(op) === -1) {
|
|
81
95
|
throw new Error(constant_1.ErrorCode.OpStrError);
|
|
82
96
|
}
|
|
83
97
|
return true;
|
|
84
|
-
}
|
|
85
|
-
|
|
98
|
+
};
|
|
99
|
+
Validate.isCollName = function (name) {
|
|
86
100
|
if (!/^[a-zA-Z0-9]([a-zA-Z0-9-_]){1,32}$/.test(name)) {
|
|
87
101
|
throw new Error(constant_1.ErrorCode.CollNameError);
|
|
88
102
|
}
|
|
89
103
|
return true;
|
|
90
|
-
}
|
|
91
|
-
|
|
104
|
+
};
|
|
105
|
+
Validate.isDocID = function (docId) {
|
|
92
106
|
if (!/^([a-fA-F0-9]){24}$/.test(docId)) {
|
|
93
107
|
throw new Error(constant_1.ErrorCode.DocIDError);
|
|
94
108
|
}
|
|
95
109
|
return true;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
};
|
|
111
|
+
Validate.isValidOptions = function (options) {
|
|
112
|
+
if (options === void 0) { options = {}; }
|
|
113
|
+
if ((0, type_1.getType)(options) !== 'object') {
|
|
114
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "options must be an object" }));
|
|
115
|
+
}
|
|
116
|
+
var keys = Object.keys(options);
|
|
117
|
+
for (var index in keys) {
|
|
103
118
|
if (validOptionsKeys.indexOf(keys[index]) < 0) {
|
|
104
|
-
throw utils_1.E(
|
|
119
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "".concat(keys[index], " is invalid options key") }));
|
|
105
120
|
}
|
|
106
121
|
}
|
|
107
|
-
|
|
108
|
-
|
|
122
|
+
var _a = options, limit = _a.limit, offset = _a.offset, projection = _a.projection, order = _a.order;
|
|
123
|
+
var multiple = options.multiple;
|
|
109
124
|
if (limit !== undefined) {
|
|
110
125
|
Validate.isInteger('limit', limit);
|
|
111
126
|
}
|
|
@@ -125,23 +140,23 @@ class Validate {
|
|
|
125
140
|
Validate.isInteger('timeout', options.timeout);
|
|
126
141
|
}
|
|
127
142
|
return true;
|
|
128
|
-
}
|
|
129
|
-
|
|
143
|
+
};
|
|
144
|
+
Validate.isValidAggregation = function (stage) {
|
|
130
145
|
if (Object.keys(stage).length !== 1) {
|
|
131
|
-
throw utils_1.E(
|
|
146
|
+
throw (0, utils_1.E)(__assign(__assign({}, code_1.ERRORS.INVALID_PARAM), { message: "aggregation stage must have one key" }));
|
|
132
147
|
}
|
|
133
148
|
return true;
|
|
134
|
-
}
|
|
135
|
-
|
|
149
|
+
};
|
|
150
|
+
Validate.isCentersPhere = function (param) {
|
|
136
151
|
if (Array.isArray(param) && param.length === 2) {
|
|
137
|
-
if (type_1.getType(param[0]) === 'object' &&
|
|
152
|
+
if ((0, type_1.getType)(param[0]) === 'object' &&
|
|
138
153
|
param[0]._internalType === symbol_1.SYMBOL_GEO_POINT &&
|
|
139
154
|
typeof param[1] === 'number') {
|
|
140
155
|
return true;
|
|
141
156
|
}
|
|
142
157
|
if (Array.isArray(param[0]) && param[0].length === 2) {
|
|
143
|
-
|
|
144
|
-
|
|
158
|
+
var longitude = param[0][0];
|
|
159
|
+
var latitude = param[0][1];
|
|
145
160
|
Validate.isGeopoint('longitude', longitude);
|
|
146
161
|
Validate.isGeopoint('latitude', latitude);
|
|
147
162
|
if (typeof param[1] === 'number') {
|
|
@@ -149,7 +164,8 @@ class Validate {
|
|
|
149
164
|
}
|
|
150
165
|
}
|
|
151
166
|
}
|
|
152
|
-
throw new Error(
|
|
153
|
-
}
|
|
154
|
-
|
|
167
|
+
throw new Error("".concat(constant_1.ErrorCode.CentersPhereError));
|
|
168
|
+
};
|
|
169
|
+
return Validate;
|
|
170
|
+
}());
|
|
155
171
|
exports.Validate = Validate;
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import { SYMBOL_OBJECTID } from '../helper/symbol';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
var ObjectId = (function () {
|
|
3
|
+
function ObjectId(_a) {
|
|
4
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.id, id = _c === void 0 ? '' : _c;
|
|
4
5
|
this.id = id;
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
Object.defineProperty(ObjectId.prototype, "_internalType", {
|
|
8
|
+
get: function () {
|
|
9
|
+
return SYMBOL_OBJECTID;
|
|
10
|
+
},
|
|
11
|
+
enumerable: false,
|
|
12
|
+
configurable: true
|
|
13
|
+
});
|
|
14
|
+
ObjectId.prototype.parse = function () {
|
|
10
15
|
return {
|
|
11
16
|
$oid: this.id
|
|
12
17
|
};
|
|
13
|
-
}
|
|
14
|
-
|
|
18
|
+
};
|
|
19
|
+
return ObjectId;
|
|
20
|
+
}());
|
|
21
|
+
export { ObjectId };
|
|
15
22
|
export function ObjectIdConstructor(opt) {
|
|
16
23
|
return new ObjectId(opt);
|
|
17
24
|
}
|
package/dist/esm/aggregate.js
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
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;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
1
37
|
import { Db } from './index';
|
|
2
38
|
import { EJSON } from 'bson';
|
|
3
39
|
import { QuerySerializer } from './serializer/query';
|
|
@@ -5,50 +41,64 @@ import { stringifyByEJSON } from './utils/utils';
|
|
|
5
41
|
import { getType } from './utils/type';
|
|
6
42
|
import { Validate } from './validate';
|
|
7
43
|
import { Point } from './geo/point';
|
|
8
|
-
|
|
9
|
-
|
|
44
|
+
var Aggregation = (function () {
|
|
45
|
+
function Aggregation(db, collectionName, rawPipeline) {
|
|
46
|
+
var _this = this;
|
|
10
47
|
this._stages = [];
|
|
11
48
|
if (db && collectionName) {
|
|
12
49
|
this._db = db;
|
|
13
|
-
this._request =
|
|
50
|
+
this._request = Db.createRequest(this._db.config);
|
|
14
51
|
this._collectionName = collectionName;
|
|
15
52
|
if (rawPipeline && rawPipeline.length > 0) {
|
|
16
|
-
rawPipeline.forEach((stage)
|
|
53
|
+
rawPipeline.forEach(function (stage) {
|
|
17
54
|
Validate.isValidAggregation(stage);
|
|
18
|
-
|
|
19
|
-
|
|
55
|
+
var stageName = Object.keys(stage)[0];
|
|
56
|
+
_this._pipe(stageName, stage[stageName], true);
|
|
20
57
|
});
|
|
21
58
|
}
|
|
22
59
|
}
|
|
23
60
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
61
|
+
Aggregation.prototype.end = function () {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
+
var result;
|
|
64
|
+
return __generator(this, function (_a) {
|
|
65
|
+
switch (_a.label) {
|
|
66
|
+
case 0:
|
|
67
|
+
if (!this._collectionName || !this._db) {
|
|
68
|
+
throw new Error('Aggregation pipeline cannot send request');
|
|
69
|
+
}
|
|
70
|
+
return [4, this._request.send('database.aggregateDocuments', {
|
|
71
|
+
collectionName: this._collectionName,
|
|
72
|
+
stages: this._stages
|
|
73
|
+
})];
|
|
74
|
+
case 1:
|
|
75
|
+
result = _a.sent();
|
|
76
|
+
if (result && result.data && result.data.list) {
|
|
77
|
+
return [2, {
|
|
78
|
+
requestId: result.requestId,
|
|
79
|
+
data: result.data.list.map(EJSON.parse)
|
|
80
|
+
}];
|
|
81
|
+
}
|
|
82
|
+
return [2, result];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
31
85
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
requestId: result.requestId,
|
|
35
|
-
data: result.data.list.map(EJSON.parse)
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
unwrap() {
|
|
86
|
+
};
|
|
87
|
+
Aggregation.prototype.unwrap = function () {
|
|
41
88
|
return this._stages;
|
|
42
|
-
}
|
|
43
|
-
done() {
|
|
44
|
-
return this._stages.map((
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
89
|
+
};
|
|
90
|
+
Aggregation.prototype.done = function () {
|
|
91
|
+
return this._stages.map(function (_a) {
|
|
92
|
+
var _b;
|
|
93
|
+
var stageKey = _a.stageKey, stageValue = _a.stageValue;
|
|
94
|
+
return _b = {},
|
|
95
|
+
_b[stageKey] = JSON.parse(stageValue),
|
|
96
|
+
_b;
|
|
48
97
|
});
|
|
49
|
-
}
|
|
50
|
-
_pipe(stage, param, raw
|
|
51
|
-
|
|
98
|
+
};
|
|
99
|
+
Aggregation.prototype._pipe = function (stage, param, raw) {
|
|
100
|
+
if (raw === void 0) { raw = false; }
|
|
101
|
+
var transformParam = '';
|
|
52
102
|
if (getType(param) === 'object') {
|
|
53
103
|
transformParam = stringifyByEJSON(param);
|
|
54
104
|
}
|
|
@@ -56,24 +106,24 @@ export default class Aggregation {
|
|
|
56
106
|
transformParam = JSON.stringify(param);
|
|
57
107
|
}
|
|
58
108
|
this._stages.push({
|
|
59
|
-
stageKey: raw ? stage :
|
|
109
|
+
stageKey: raw ? stage : "$".concat(stage),
|
|
60
110
|
stageValue: transformParam
|
|
61
111
|
});
|
|
62
112
|
return this;
|
|
63
|
-
}
|
|
64
|
-
addFields(param) {
|
|
113
|
+
};
|
|
114
|
+
Aggregation.prototype.addFields = function (param) {
|
|
65
115
|
return this._pipe('addFields', param);
|
|
66
|
-
}
|
|
67
|
-
bucket(param) {
|
|
116
|
+
};
|
|
117
|
+
Aggregation.prototype.bucket = function (param) {
|
|
68
118
|
return this._pipe('bucket', param);
|
|
69
|
-
}
|
|
70
|
-
bucketAuto(param) {
|
|
119
|
+
};
|
|
120
|
+
Aggregation.prototype.bucketAuto = function (param) {
|
|
71
121
|
return this._pipe('bucketAuto', param);
|
|
72
|
-
}
|
|
73
|
-
count(param) {
|
|
122
|
+
};
|
|
123
|
+
Aggregation.prototype.count = function (param) {
|
|
74
124
|
return this._pipe('count', param);
|
|
75
|
-
}
|
|
76
|
-
geoNear(param) {
|
|
125
|
+
};
|
|
126
|
+
Aggregation.prototype.geoNear = function (param) {
|
|
77
127
|
if (param.query) {
|
|
78
128
|
param.query = QuerySerializer.encode(param.query);
|
|
79
129
|
}
|
|
@@ -84,38 +134,40 @@ export default class Aggregation {
|
|
|
84
134
|
param.near = new Point(param.near.longitude, param.near.latitude).toJSON();
|
|
85
135
|
}
|
|
86
136
|
return this._pipe('geoNear', param);
|
|
87
|
-
}
|
|
88
|
-
group(param) {
|
|
137
|
+
};
|
|
138
|
+
Aggregation.prototype.group = function (param) {
|
|
89
139
|
return this._pipe('group', param);
|
|
90
|
-
}
|
|
91
|
-
limit(param) {
|
|
140
|
+
};
|
|
141
|
+
Aggregation.prototype.limit = function (param) {
|
|
92
142
|
return this._pipe('limit', param);
|
|
93
|
-
}
|
|
94
|
-
match(param) {
|
|
143
|
+
};
|
|
144
|
+
Aggregation.prototype.match = function (param) {
|
|
95
145
|
return this._pipe('match', QuerySerializer.encode(param));
|
|
96
|
-
}
|
|
97
|
-
project(param) {
|
|
146
|
+
};
|
|
147
|
+
Aggregation.prototype.project = function (param) {
|
|
98
148
|
return this._pipe('project', param);
|
|
99
|
-
}
|
|
100
|
-
lookup(param) {
|
|
149
|
+
};
|
|
150
|
+
Aggregation.prototype.lookup = function (param) {
|
|
101
151
|
return this._pipe('lookup', param);
|
|
102
|
-
}
|
|
103
|
-
replaceRoot(param) {
|
|
152
|
+
};
|
|
153
|
+
Aggregation.prototype.replaceRoot = function (param) {
|
|
104
154
|
return this._pipe('replaceRoot', param);
|
|
105
|
-
}
|
|
106
|
-
sample(param) {
|
|
155
|
+
};
|
|
156
|
+
Aggregation.prototype.sample = function (param) {
|
|
107
157
|
return this._pipe('sample', param);
|
|
108
|
-
}
|
|
109
|
-
skip(param) {
|
|
158
|
+
};
|
|
159
|
+
Aggregation.prototype.skip = function (param) {
|
|
110
160
|
return this._pipe('skip', param);
|
|
111
|
-
}
|
|
112
|
-
sort(param) {
|
|
161
|
+
};
|
|
162
|
+
Aggregation.prototype.sort = function (param) {
|
|
113
163
|
return this._pipe('sort', param);
|
|
114
|
-
}
|
|
115
|
-
sortByCount(param) {
|
|
164
|
+
};
|
|
165
|
+
Aggregation.prototype.sortByCount = function (param) {
|
|
116
166
|
return this._pipe('sortByCount', param);
|
|
117
|
-
}
|
|
118
|
-
unwind(param) {
|
|
167
|
+
};
|
|
168
|
+
Aggregation.prototype.unwind = function (param) {
|
|
119
169
|
return this._pipe('unwind', param);
|
|
120
|
-
}
|
|
121
|
-
|
|
170
|
+
};
|
|
171
|
+
return Aggregation;
|
|
172
|
+
}());
|
|
173
|
+
export default Aggregation;
|
package/dist/esm/collection.d.ts
CHANGED
|
@@ -3,15 +3,16 @@ import { Query, QueryOption, UpdateOption } from './query';
|
|
|
3
3
|
import Aggregation from './aggregate';
|
|
4
4
|
export declare class CollectionReference extends Query {
|
|
5
5
|
protected _transactionId: string;
|
|
6
|
-
|
|
6
|
+
get name(): string;
|
|
7
7
|
doc(docID: string | number): DocumentReference;
|
|
8
|
-
add(data: any): Promise<{
|
|
8
|
+
add(data: any, callback?: any): Promise<{
|
|
9
9
|
ids?: string[];
|
|
10
10
|
id?: string;
|
|
11
11
|
inserted?: number;
|
|
12
12
|
ok?: number;
|
|
13
13
|
requestId: string;
|
|
14
14
|
}>;
|
|
15
|
+
private _add;
|
|
15
16
|
aggregate(rawPipeline?: object[]): Aggregation;
|
|
16
17
|
options(apiOptions: QueryOption | UpdateOption): CollectionReference;
|
|
17
18
|
}
|