@cloudbase/database 1.4.3-alpha.0 → 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.
- 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.js +4 -3
- package/dist/commonjs/operator-map.js +7 -6
- package/dist/commonjs/query.d.ts +8 -4
- package/dist/commonjs/query.js +308 -201
- 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 +1 -1
- package/dist/commonjs/transaction/collection.js +5 -2
- package/dist/commonjs/transaction/document.d.ts +1 -1
- package/dist/commonjs/transaction/document.js +5 -5
- package/dist/commonjs/transaction/index.js +196 -86
- package/dist/commonjs/transaction/query.d.ts +39 -1
- package/dist/commonjs/transaction/query.js +252 -1
- 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 +4 -1
- package/dist/commonjs/utils/utils.js +93 -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.js +1 -2
- package/dist/esm/operator-map.js +4 -4
- package/dist/esm/query.d.ts +8 -4
- package/dist/esm/query.js +299 -192
- 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 +1 -1
- package/dist/esm/transaction/collection.js +4 -2
- package/dist/esm/transaction/document.d.ts +1 -1
- package/dist/esm/transaction/document.js +2 -3
- package/dist/esm/transaction/index.js +191 -81
- package/dist/esm/transaction/query.d.ts +39 -1
- package/dist/esm/transaction/query.js +251 -1
- 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 +4 -1
- package/dist/esm/utils/utils.js +78 -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 +33 -29
- 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 +46 -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/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,21 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
exports.ServerDateConstructor = exports.ServerDate = void 0;
|
|
4
|
+
var symbol_1 = require("../helper/symbol");
|
|
5
|
+
var ServerDate = (function () {
|
|
6
|
+
function ServerDate(_a) {
|
|
7
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.offset, offset = _c === void 0 ? 0 : _c;
|
|
6
8
|
this.offset = offset;
|
|
7
9
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
Object.defineProperty(ServerDate.prototype, "_internalType", {
|
|
11
|
+
get: function () {
|
|
12
|
+
return symbol_1.SYMBOL_SERVER_DATE;
|
|
13
|
+
},
|
|
14
|
+
enumerable: false,
|
|
15
|
+
configurable: true
|
|
16
|
+
});
|
|
17
|
+
ServerDate.prototype.parse = function () {
|
|
12
18
|
return {
|
|
13
19
|
$tcb_server_date: {
|
|
14
20
|
offset: this.offset
|
|
15
21
|
}
|
|
16
22
|
};
|
|
17
|
-
}
|
|
18
|
-
|
|
23
|
+
};
|
|
24
|
+
return ServerDate;
|
|
25
|
+
}());
|
|
19
26
|
exports.ServerDate = ServerDate;
|
|
20
27
|
function ServerDateConstructor(opt) {
|
|
21
28
|
return new ServerDate(opt);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DocumentReference } from './document';
|
|
2
2
|
import { Query } from './query';
|
|
3
3
|
export declare class CollectionReference extends Query {
|
|
4
|
-
|
|
4
|
+
get name(): string;
|
|
5
5
|
doc(docID?: string | number): DocumentReference;
|
|
6
6
|
add(data: any): Promise<any>;
|
|
7
7
|
}
|
|
@@ -3,16 +3,19 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
3
3
|
var extendStatics = function (d, b) {
|
|
4
4
|
extendStatics = Object.setPrototypeOf ||
|
|
5
5
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
15
|
};
|
|
14
16
|
})();
|
|
15
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.CollectionReference = void 0;
|
|
16
19
|
var document_1 = require("./document");
|
|
17
20
|
var query_1 = require("./query");
|
|
18
21
|
var CollectionReference = (function (_super) {
|
|
@@ -24,7 +27,7 @@ var CollectionReference = (function (_super) {
|
|
|
24
27
|
get: function () {
|
|
25
28
|
return this._coll;
|
|
26
29
|
},
|
|
27
|
-
enumerable:
|
|
30
|
+
enumerable: false,
|
|
28
31
|
configurable: true
|
|
29
32
|
});
|
|
30
33
|
CollectionReference.prototype.doc = function (docID) {
|
|
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
25
25
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
26
|
function step(op) {
|
|
27
27
|
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
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
30
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
31
|
switch (op[0]) {
|
|
@@ -47,6 +47,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.DocumentReference = void 0;
|
|
50
51
|
var bson_1 = require("bson");
|
|
51
52
|
var code_1 = require("../const/code");
|
|
52
53
|
var update_1 = require("../serializer/update");
|
|
@@ -73,7 +74,7 @@ var DocumentReference = (function () {
|
|
|
73
74
|
params = {
|
|
74
75
|
collectionName: this._coll,
|
|
75
76
|
transactionId: this._transactionId,
|
|
76
|
-
data: bson_1.EJSON.stringify(datatype_1.serialize(data), { relaxed: false })
|
|
77
|
+
data: bson_1.EJSON.stringify((0, datatype_1.serialize)(data), { relaxed: false })
|
|
77
78
|
};
|
|
78
79
|
if (this.id) {
|
|
79
80
|
params['_id'] = this.id;
|
|
@@ -87,8 +88,7 @@ var DocumentReference = (function () {
|
|
|
87
88
|
inserted = bson_1.EJSON.parse(res.inserted);
|
|
88
89
|
ok = bson_1.EJSON.parse(res.ok);
|
|
89
90
|
if (ok == 1 && inserted == 1) {
|
|
90
|
-
return [2, __assign(__assign({}, res), { ok: ok,
|
|
91
|
-
inserted: inserted })];
|
|
91
|
+
return [2, __assign(__assign({}, res), { ok: ok, inserted: inserted })];
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
94
94
|
throw new Error(code_1.ERRORS.INSERT_DOC_FAIL.message);
|
|
@@ -136,7 +136,7 @@ var DocumentReference = (function () {
|
|
|
136
136
|
query: {
|
|
137
137
|
_id: { $eq: this.id }
|
|
138
138
|
},
|
|
139
|
-
data: bson_1.EJSON.stringify(datatype_1.serialize(data), { relaxed: false }),
|
|
139
|
+
data: bson_1.EJSON.stringify((0, datatype_1.serialize)(data), { relaxed: false }),
|
|
140
140
|
upsert: true
|
|
141
141
|
};
|
|
142
142
|
return [4, this._request.send(UPDATE_DOC, param)];
|
|
@@ -1,105 +1,215 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
39
|
+
exports.runTransaction = exports.startTransaction = exports.Transaction = void 0;
|
|
40
|
+
var index_1 = require("../index");
|
|
41
|
+
var collection_1 = require("../collection");
|
|
42
|
+
var code_1 = require("../const/code");
|
|
43
|
+
var START = 'database.startTransaction';
|
|
44
|
+
var COMMIT = 'database.commitTransaction';
|
|
45
|
+
var ABORT = 'database.abortTransaction';
|
|
46
|
+
var Transaction = (function () {
|
|
47
|
+
function Transaction(db) {
|
|
11
48
|
this._db = db;
|
|
12
|
-
this._request =
|
|
49
|
+
this._request = index_1.Db.createRequest(this._db.config);
|
|
13
50
|
this.aborted = false;
|
|
14
51
|
this.commited = false;
|
|
15
52
|
this.inited = false;
|
|
16
53
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
54
|
+
Transaction.prototype.init = function () {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
56
|
+
var res;
|
|
57
|
+
return __generator(this, function (_a) {
|
|
58
|
+
switch (_a.label) {
|
|
59
|
+
case 0: return [4, this._request.send(START)];
|
|
60
|
+
case 1:
|
|
61
|
+
res = _a.sent();
|
|
62
|
+
if (res.code) {
|
|
63
|
+
throw res;
|
|
64
|
+
}
|
|
65
|
+
this.inited = true;
|
|
66
|
+
this._id = res.transactionId;
|
|
67
|
+
return [2];
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
Transaction.prototype.collection = function (collName) {
|
|
26
73
|
if (!collName) {
|
|
27
74
|
throw new Error('Collection name is required');
|
|
28
75
|
}
|
|
29
76
|
return new collection_1.CollectionReference(this._db, collName, {}, this._id);
|
|
30
|
-
}
|
|
31
|
-
getTransactionId() {
|
|
77
|
+
};
|
|
78
|
+
Transaction.prototype.getTransactionId = function () {
|
|
32
79
|
return this._id;
|
|
33
|
-
}
|
|
34
|
-
getRequestMethod() {
|
|
80
|
+
};
|
|
81
|
+
Transaction.prototype.getRequestMethod = function () {
|
|
35
82
|
return this._request;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw res;
|
|
54
|
-
this.aborted = true;
|
|
55
|
-
this.abortReason = customRollbackRes;
|
|
56
|
-
return res;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
exports.Transaction = Transaction;
|
|
60
|
-
async function startTransaction() {
|
|
61
|
-
const transaction = new Transaction(this);
|
|
62
|
-
await transaction.init();
|
|
63
|
-
return transaction;
|
|
64
|
-
}
|
|
65
|
-
exports.startTransaction = startTransaction;
|
|
66
|
-
async function runTransaction(callback, times = 3) {
|
|
67
|
-
let transaction;
|
|
68
|
-
try {
|
|
69
|
-
transaction = new Transaction(this);
|
|
70
|
-
await transaction.init();
|
|
71
|
-
const callbackRes = await callback(transaction);
|
|
72
|
-
if (transaction.aborted === true) {
|
|
73
|
-
throw transaction.abortReason;
|
|
74
|
-
}
|
|
75
|
-
await transaction.commit();
|
|
76
|
-
return callbackRes;
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
if (transaction.inited === false) {
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
82
|
-
const throwWithRollback = async (error) => {
|
|
83
|
-
if (!transaction.aborted && !transaction.commited) {
|
|
84
|
-
try {
|
|
85
|
-
await transaction.rollback();
|
|
83
|
+
};
|
|
84
|
+
Transaction.prototype.commit = function () {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
86
|
+
var param, res;
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
switch (_a.label) {
|
|
89
|
+
case 0:
|
|
90
|
+
param = {
|
|
91
|
+
transactionId: this._id
|
|
92
|
+
};
|
|
93
|
+
return [4, this._request.send(COMMIT, param)];
|
|
94
|
+
case 1:
|
|
95
|
+
res = _a.sent();
|
|
96
|
+
if (res.code)
|
|
97
|
+
throw res;
|
|
98
|
+
this.commited = true;
|
|
99
|
+
return [2, res];
|
|
86
100
|
}
|
|
87
|
-
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
Transaction.prototype.rollback = function (customRollbackRes) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
106
|
+
var param, res;
|
|
107
|
+
return __generator(this, function (_a) {
|
|
108
|
+
switch (_a.label) {
|
|
109
|
+
case 0:
|
|
110
|
+
param = {
|
|
111
|
+
transactionId: this._id
|
|
112
|
+
};
|
|
113
|
+
return [4, this._request.send(ABORT, param)];
|
|
114
|
+
case 1:
|
|
115
|
+
res = _a.sent();
|
|
116
|
+
if (res.code)
|
|
117
|
+
throw res;
|
|
118
|
+
this.aborted = true;
|
|
119
|
+
this.abortReason = customRollbackRes;
|
|
120
|
+
return [2, res];
|
|
88
121
|
}
|
|
89
|
-
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
return Transaction;
|
|
126
|
+
}());
|
|
127
|
+
exports.Transaction = Transaction;
|
|
128
|
+
function startTransaction() {
|
|
129
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
130
|
+
var transaction;
|
|
131
|
+
return __generator(this, function (_a) {
|
|
132
|
+
switch (_a.label) {
|
|
133
|
+
case 0:
|
|
134
|
+
transaction = new Transaction(this);
|
|
135
|
+
return [4, transaction.init()];
|
|
136
|
+
case 1:
|
|
137
|
+
_a.sent();
|
|
138
|
+
return [2, transaction];
|
|
90
139
|
}
|
|
91
|
-
|
|
92
|
-
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
exports.startTransaction = startTransaction;
|
|
144
|
+
function runTransaction(callback, times) {
|
|
145
|
+
if (times === void 0) { times = 3; }
|
|
146
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
147
|
+
var transaction, callbackRes, error_1, throwWithRollback;
|
|
148
|
+
var _this = this;
|
|
149
|
+
return __generator(this, function (_a) {
|
|
150
|
+
switch (_a.label) {
|
|
151
|
+
case 0:
|
|
152
|
+
_a.trys.push([0, 4, , 10]);
|
|
153
|
+
transaction = new Transaction(this);
|
|
154
|
+
return [4, transaction.init()];
|
|
155
|
+
case 1:
|
|
156
|
+
_a.sent();
|
|
157
|
+
return [4, callback(transaction)];
|
|
158
|
+
case 2:
|
|
159
|
+
callbackRes = _a.sent();
|
|
160
|
+
if (transaction.aborted === true) {
|
|
161
|
+
throw transaction.abortReason;
|
|
162
|
+
}
|
|
163
|
+
return [4, transaction.commit()];
|
|
164
|
+
case 3:
|
|
165
|
+
_a.sent();
|
|
166
|
+
return [2, callbackRes];
|
|
167
|
+
case 4:
|
|
168
|
+
error_1 = _a.sent();
|
|
169
|
+
if (transaction.inited === false) {
|
|
170
|
+
throw error_1;
|
|
171
|
+
}
|
|
172
|
+
throwWithRollback = function (error) { return __awaiter(_this, void 0, void 0, function () {
|
|
173
|
+
var err_1;
|
|
174
|
+
return __generator(this, function (_a) {
|
|
175
|
+
switch (_a.label) {
|
|
176
|
+
case 0:
|
|
177
|
+
if (!(!transaction.aborted && !transaction.commited)) return [3, 5];
|
|
178
|
+
_a.label = 1;
|
|
179
|
+
case 1:
|
|
180
|
+
_a.trys.push([1, 3, , 4]);
|
|
181
|
+
return [4, transaction.rollback()];
|
|
182
|
+
case 2:
|
|
183
|
+
_a.sent();
|
|
184
|
+
return [3, 4];
|
|
185
|
+
case 3:
|
|
186
|
+
err_1 = _a.sent();
|
|
187
|
+
return [3, 4];
|
|
188
|
+
case 4: throw error;
|
|
189
|
+
case 5:
|
|
190
|
+
if (transaction.aborted === true) {
|
|
191
|
+
throw transaction.abortReason;
|
|
192
|
+
}
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}); };
|
|
197
|
+
if (!(times <= 0)) return [3, 6];
|
|
198
|
+
return [4, throwWithRollback(error_1)];
|
|
199
|
+
case 5:
|
|
200
|
+
_a.sent();
|
|
201
|
+
_a.label = 6;
|
|
202
|
+
case 6:
|
|
203
|
+
if (!(error_1 && error_1.code === code_1.ERRORS.DATABASE_TRANSACTION_CONFLICT.code)) return [3, 8];
|
|
204
|
+
return [4, runTransaction.bind(this)(callback, --times)];
|
|
205
|
+
case 7: return [2, _a.sent()];
|
|
206
|
+
case 8: return [4, throwWithRollback(error_1)];
|
|
207
|
+
case 9:
|
|
208
|
+
_a.sent();
|
|
209
|
+
return [3, 10];
|
|
210
|
+
case 10: return [2];
|
|
93
211
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (times <= 0) {
|
|
97
|
-
await throwWithRollback(error);
|
|
98
|
-
}
|
|
99
|
-
if (error && error.code === code_1.ERRORS.DATABASE_TRANSACTION_CONFLICT.code) {
|
|
100
|
-
return await runTransaction.bind(this)(callback, --times);
|
|
101
|
-
}
|
|
102
|
-
await throwWithRollback(error);
|
|
103
|
-
}
|
|
212
|
+
});
|
|
213
|
+
});
|
|
104
214
|
}
|
|
105
215
|
exports.runTransaction = runTransaction;
|
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
import { Transaction } from './index';
|
|
2
|
+
import { OrderByDirection } from '../constant';
|
|
3
|
+
interface QueryOrder {
|
|
4
|
+
field?: string;
|
|
5
|
+
direction?: 'asc' | 'desc';
|
|
6
|
+
}
|
|
7
|
+
interface QueryOption {
|
|
8
|
+
limit?: number;
|
|
9
|
+
offset?: number;
|
|
10
|
+
projection?: Object;
|
|
11
|
+
}
|
|
12
|
+
interface GetResult {
|
|
13
|
+
data: any[];
|
|
14
|
+
requestId: string;
|
|
15
|
+
total?: number;
|
|
16
|
+
limit?: number;
|
|
17
|
+
offset?: number;
|
|
18
|
+
}
|
|
19
|
+
interface UpdateResult {
|
|
20
|
+
requestId: string;
|
|
21
|
+
updated: number;
|
|
22
|
+
}
|
|
23
|
+
interface DeleteResult {
|
|
24
|
+
requestId: string;
|
|
25
|
+
deleted: number | string;
|
|
26
|
+
}
|
|
2
27
|
export declare class Query {
|
|
3
28
|
protected _transaction: Transaction;
|
|
4
|
-
constructor(transaction: Transaction, coll: string);
|
|
29
|
+
constructor(transaction: Transaction, coll: string, fieldFilters?: Object, fieldOrders?: QueryOrder[], queryOptions?: QueryOption);
|
|
30
|
+
where(query: object): Query;
|
|
31
|
+
orderBy(fieldPath: string, directionStr: OrderByDirection): Query;
|
|
32
|
+
limit(limit: number): Query;
|
|
33
|
+
skip(offset: number): Query;
|
|
34
|
+
field(projection: any): Query;
|
|
35
|
+
get(): Promise<GetResult>;
|
|
36
|
+
count(): Promise<{
|
|
37
|
+
requestId: string;
|
|
38
|
+
total: number;
|
|
39
|
+
}>;
|
|
40
|
+
update(data: Object): Promise<UpdateResult>;
|
|
41
|
+
remove(): Promise<DeleteResult>;
|
|
5
42
|
}
|
|
43
|
+
export {};
|