@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
@@ -3,7 +3,7 @@ export declare class ObjectId {
3
3
  constructor({ id }?: {
4
4
  id?: string;
5
5
  });
6
- readonly _internalType: import("../utils/symbol").InternalSymbol;
6
+ get _internalType(): import("../utils/symbol").InternalSymbol;
7
7
  parse(): {
8
8
  $oid: string;
9
9
  };
@@ -1,19 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const symbol_1 = require("../helper/symbol");
4
- class ObjectId {
5
- constructor({ id = '' } = {}) {
3
+ exports.ObjectIdConstructor = exports.ObjectId = void 0;
4
+ var symbol_1 = require("../helper/symbol");
5
+ var ObjectId = (function () {
6
+ function ObjectId(_a) {
7
+ var _b = _a === void 0 ? {} : _a, _c = _b.id, id = _c === void 0 ? '' : _c;
6
8
  this.id = id;
7
9
  }
8
- get _internalType() {
9
- return symbol_1.SYMBOL_OBJECTID;
10
- }
11
- parse() {
10
+ Object.defineProperty(ObjectId.prototype, "_internalType", {
11
+ get: function () {
12
+ return symbol_1.SYMBOL_OBJECTID;
13
+ },
14
+ enumerable: false,
15
+ configurable: true
16
+ });
17
+ ObjectId.prototype.parse = function () {
12
18
  return {
13
19
  $oid: this.id
14
20
  };
15
- }
16
- }
21
+ };
22
+ return ObjectId;
23
+ }());
17
24
  exports.ObjectId = ObjectId;
18
25
  function ObjectIdConstructor(opt) {
19
26
  return new ObjectId(opt);
@@ -1,81 +1,131 @@
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
- const index_1 = require("./index");
4
- const bson_1 = require("bson");
5
- const query_1 = require("./serializer/query");
6
- const utils_1 = require("./utils/utils");
7
- const type_1 = require("./utils/type");
8
- const validate_1 = require("./validate");
9
- const point_1 = require("./geo/point");
10
- class Aggregation {
11
- constructor(db, collectionName, rawPipeline) {
39
+ var index_1 = require("./index");
40
+ var bson_1 = require("bson");
41
+ var query_1 = require("./serializer/query");
42
+ var utils_1 = require("./utils/utils");
43
+ var type_1 = require("./utils/type");
44
+ var validate_1 = require("./validate");
45
+ var point_1 = require("./geo/point");
46
+ var Aggregation = (function () {
47
+ function Aggregation(db, collectionName, rawPipeline) {
48
+ var _this = this;
12
49
  this._stages = [];
13
50
  if (db && collectionName) {
14
51
  this._db = db;
15
- this._request = new index_1.Db.reqClass(this._db.config);
52
+ this._request = index_1.Db.createRequest(this._db.config);
16
53
  this._collectionName = collectionName;
17
54
  if (rawPipeline && rawPipeline.length > 0) {
18
- rawPipeline.forEach((stage) => {
55
+ rawPipeline.forEach(function (stage) {
19
56
  validate_1.Validate.isValidAggregation(stage);
20
- const stageName = Object.keys(stage)[0];
21
- this._pipe(stageName, stage[stageName], true);
57
+ var stageName = Object.keys(stage)[0];
58
+ _this._pipe(stageName, stage[stageName], true);
22
59
  });
23
60
  }
24
61
  }
25
62
  }
26
- async end() {
27
- if (!this._collectionName || !this._db) {
28
- throw new Error('Aggregation pipeline cannot send request');
29
- }
30
- const result = await this._request.send('database.aggregateDocuments', {
31
- collectionName: this._collectionName,
32
- stages: this._stages
63
+ Aggregation.prototype.end = function () {
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var result;
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ if (!this._collectionName || !this._db) {
70
+ throw new Error('Aggregation pipeline cannot send request');
71
+ }
72
+ return [4, this._request.send('database.aggregateDocuments', {
73
+ collectionName: this._collectionName,
74
+ stages: this._stages
75
+ })];
76
+ case 1:
77
+ result = _a.sent();
78
+ if (result && result.data && result.data.list) {
79
+ return [2, {
80
+ requestId: result.requestId,
81
+ data: result.data.list.map(bson_1.EJSON.parse)
82
+ }];
83
+ }
84
+ return [2, result];
85
+ }
86
+ });
33
87
  });
34
- if (result && result.data && result.data.list) {
35
- return {
36
- requestId: result.requestId,
37
- data: result.data.list.map(bson_1.EJSON.parse)
38
- };
39
- }
40
- return result;
41
- }
42
- unwrap() {
88
+ };
89
+ Aggregation.prototype.unwrap = function () {
43
90
  return this._stages;
44
- }
45
- done() {
46
- return this._stages.map(({ stageKey, stageValue }) => {
47
- return {
48
- [stageKey]: JSON.parse(stageValue)
49
- };
91
+ };
92
+ Aggregation.prototype.done = function () {
93
+ return this._stages.map(function (_a) {
94
+ var _b;
95
+ var stageKey = _a.stageKey, stageValue = _a.stageValue;
96
+ return _b = {},
97
+ _b[stageKey] = JSON.parse(stageValue),
98
+ _b;
50
99
  });
51
- }
52
- _pipe(stage, param, raw = false) {
53
- let transformParam = '';
54
- if (type_1.getType(param) === 'object') {
55
- transformParam = utils_1.stringifyByEJSON(param);
100
+ };
101
+ Aggregation.prototype._pipe = function (stage, param, raw) {
102
+ if (raw === void 0) { raw = false; }
103
+ var transformParam = '';
104
+ if ((0, type_1.getType)(param) === 'object') {
105
+ transformParam = (0, utils_1.stringifyByEJSON)(param);
56
106
  }
57
107
  else {
58
108
  transformParam = JSON.stringify(param);
59
109
  }
60
110
  this._stages.push({
61
- stageKey: raw ? stage : `$${stage}`,
111
+ stageKey: raw ? stage : "$".concat(stage),
62
112
  stageValue: transformParam
63
113
  });
64
114
  return this;
65
- }
66
- addFields(param) {
115
+ };
116
+ Aggregation.prototype.addFields = function (param) {
67
117
  return this._pipe('addFields', param);
68
- }
69
- bucket(param) {
118
+ };
119
+ Aggregation.prototype.bucket = function (param) {
70
120
  return this._pipe('bucket', param);
71
- }
72
- bucketAuto(param) {
121
+ };
122
+ Aggregation.prototype.bucketAuto = function (param) {
73
123
  return this._pipe('bucketAuto', param);
74
- }
75
- count(param) {
124
+ };
125
+ Aggregation.prototype.count = function (param) {
76
126
  return this._pipe('count', param);
77
- }
78
- geoNear(param) {
127
+ };
128
+ Aggregation.prototype.geoNear = function (param) {
79
129
  if (param.query) {
80
130
  param.query = query_1.QuerySerializer.encode(param.query);
81
131
  }
@@ -86,39 +136,40 @@ class Aggregation {
86
136
  param.near = new point_1.Point(param.near.longitude, param.near.latitude).toJSON();
87
137
  }
88
138
  return this._pipe('geoNear', param);
89
- }
90
- group(param) {
139
+ };
140
+ Aggregation.prototype.group = function (param) {
91
141
  return this._pipe('group', param);
92
- }
93
- limit(param) {
142
+ };
143
+ Aggregation.prototype.limit = function (param) {
94
144
  return this._pipe('limit', param);
95
- }
96
- match(param) {
145
+ };
146
+ Aggregation.prototype.match = function (param) {
97
147
  return this._pipe('match', query_1.QuerySerializer.encode(param));
98
- }
99
- project(param) {
148
+ };
149
+ Aggregation.prototype.project = function (param) {
100
150
  return this._pipe('project', param);
101
- }
102
- lookup(param) {
151
+ };
152
+ Aggregation.prototype.lookup = function (param) {
103
153
  return this._pipe('lookup', param);
104
- }
105
- replaceRoot(param) {
154
+ };
155
+ Aggregation.prototype.replaceRoot = function (param) {
106
156
  return this._pipe('replaceRoot', param);
107
- }
108
- sample(param) {
157
+ };
158
+ Aggregation.prototype.sample = function (param) {
109
159
  return this._pipe('sample', param);
110
- }
111
- skip(param) {
160
+ };
161
+ Aggregation.prototype.skip = function (param) {
112
162
  return this._pipe('skip', param);
113
- }
114
- sort(param) {
163
+ };
164
+ Aggregation.prototype.sort = function (param) {
115
165
  return this._pipe('sort', param);
116
- }
117
- sortByCount(param) {
166
+ };
167
+ Aggregation.prototype.sortByCount = function (param) {
118
168
  return this._pipe('sortByCount', param);
119
- }
120
- unwind(param) {
169
+ };
170
+ Aggregation.prototype.unwind = function (param) {
121
171
  return this._pipe('unwind', param);
122
- }
123
- }
172
+ };
173
+ return Aggregation;
174
+ }());
124
175
  exports.default = Aggregation;
@@ -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
- readonly name: string;
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
  }
@@ -1,72 +1,145 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
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");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ 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;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
2
53
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const document_1 = require("./document");
4
- const query_1 = require("./query");
5
- const aggregate_1 = require("./aggregate");
6
- const datatype_1 = require("./serializer/datatype");
7
- const utils_1 = require("./utils/utils");
8
- const validate_1 = require("./validate");
9
- const type_1 = require("./utils/type");
10
- class CollectionReference extends query_1.Query {
11
- constructor(db, coll, apiOptions, transactionId) {
12
- super(db, coll, '', apiOptions, transactionId);
54
+ exports.CollectionReference = void 0;
55
+ var document_1 = require("./document");
56
+ var query_1 = require("./query");
57
+ var aggregate_1 = require("./aggregate");
58
+ var datatype_1 = require("./serializer/datatype");
59
+ var utils_1 = require("./utils/utils");
60
+ var validate_1 = require("./validate");
61
+ var type_1 = require("./utils/type");
62
+ var CollectionReference = (function (_super) {
63
+ __extends(CollectionReference, _super);
64
+ function CollectionReference(db, coll, apiOptions, transactionId) {
65
+ var _this = _super.call(this, db, coll, '', apiOptions, transactionId) || this;
13
66
  if (transactionId) {
14
- this._transactionId = transactionId;
67
+ _this._transactionId = transactionId;
15
68
  }
69
+ return _this;
16
70
  }
17
- get name() {
18
- return this._coll;
19
- }
20
- doc(docID) {
71
+ Object.defineProperty(CollectionReference.prototype, "name", {
72
+ get: function () {
73
+ return this._coll;
74
+ },
75
+ enumerable: false,
76
+ configurable: true
77
+ });
78
+ CollectionReference.prototype.doc = function (docID) {
21
79
  if (typeof docID !== 'string' && typeof docID !== 'number') {
22
80
  throw new Error('docId必须为字符串或数字');
23
81
  }
24
82
  return new document_1.DocumentReference(this._db, this._coll, this._apiOptions, docID, this._transactionId);
25
- }
26
- async add(data) {
27
- let transformData = data;
28
- if (!type_1.isArray(data)) {
29
- transformData = [data];
30
- }
31
- transformData = transformData.map(item => {
32
- return utils_1.stringifyByEJSON(datatype_1.serialize(item));
83
+ };
84
+ CollectionReference.prototype.add = function (data, callback) {
85
+ return (0, utils_1.withCallback)(this._add(data), callback);
86
+ };
87
+ CollectionReference.prototype._add = function (data) {
88
+ return __awaiter(this, void 0, void 0, function () {
89
+ var transformData, params, res;
90
+ return __generator(this, function (_a) {
91
+ switch (_a.label) {
92
+ case 0:
93
+ transformData = data;
94
+ if (!(0, type_1.isArray)(data)) {
95
+ transformData = [data];
96
+ }
97
+ transformData = transformData.map(function (item) {
98
+ return (0, utils_1.stringifyByEJSON)((0, datatype_1.serialize)(item));
99
+ });
100
+ params = {
101
+ collectionName: this._coll,
102
+ data: transformData
103
+ };
104
+ if (this._transactionId) {
105
+ params.transactionId = this._transactionId;
106
+ }
107
+ return [4, this._request.send('database.insertDocument', params, (0, utils_1.getReqOpts)(this._apiOptions))];
108
+ case 1:
109
+ res = _a.sent();
110
+ if (res.code) {
111
+ return [2, res];
112
+ }
113
+ if (!(0, type_1.isArray)(data)) {
114
+ if (this._transactionId) {
115
+ return [2, {
116
+ inserted: 1,
117
+ ok: 1,
118
+ id: res.data.insertedIds[0],
119
+ requestId: res.requestId
120
+ }];
121
+ }
122
+ return [2, {
123
+ id: res.data.insertedIds[0],
124
+ requestId: res.requestId
125
+ }];
126
+ }
127
+ return [2, {
128
+ ids: res.data.insertedIds,
129
+ requestId: res.requestId
130
+ }];
131
+ }
132
+ });
33
133
  });
34
- let params = {
35
- collectionName: this._coll,
36
- data: transformData
37
- };
38
- if (this._transactionId) {
39
- params.transactionId = this._transactionId;
40
- }
41
- const res = await this._request.send('database.insertDocument', params, utils_1.getReqOpts(this._apiOptions));
42
- if (res.code) {
43
- return res;
44
- }
45
- if (!type_1.isArray(data)) {
46
- if (this._transactionId) {
47
- return {
48
- inserted: 1,
49
- ok: 1,
50
- id: res.data.insertedIds[0],
51
- requestId: res.requestId
52
- };
53
- }
54
- return {
55
- id: res.data.insertedIds[0],
56
- requestId: res.requestId
57
- };
58
- }
59
- return {
60
- ids: res.data.insertedIds,
61
- requestId: res.requestId
62
- };
63
- }
64
- aggregate(rawPipeline = []) {
134
+ };
135
+ CollectionReference.prototype.aggregate = function (rawPipeline) {
136
+ if (rawPipeline === void 0) { rawPipeline = []; }
65
137
  return new aggregate_1.default(this._db, this._coll, (this._apiOptions.raw || false) ? rawPipeline : []);
66
- }
67
- options(apiOptions) {
138
+ };
139
+ CollectionReference.prototype.options = function (apiOptions) {
68
140
  validate_1.Validate.isValidOptions(apiOptions);
69
141
  return new CollectionReference(this._db, this._coll, apiOptions, this._transactionId);
70
- }
71
- }
142
+ };
143
+ return CollectionReference;
144
+ }(query_1.Query));
72
145
  exports.CollectionReference = CollectionReference;
@@ -2,7 +2,7 @@ import { QueryCommand } from './commands/query';
2
2
  import { LogicCommand } from './commands/logic';
3
3
  import { UpdateCommand } from './commands/update';
4
4
  import Aggregation from './aggregate';
5
- export declare type IQueryCondition = Record<string, any> | LogicCommand;
5
+ export type IQueryCondition = Record<string, any> | LogicCommand;
6
6
  export declare const Command: {
7
7
  eq(val: any): QueryCommand;
8
8
  neq(val: any): QueryCommand;
@@ -20,10 +20,10 @@ export declare const Command: {
20
20
  geoNear(val: any): QueryCommand;
21
21
  geoWithin(val: any): QueryCommand;
22
22
  geoIntersects(val: any): QueryCommand;
23
- and(...__expressions__: import("./serializer/datatype").IQueryCondition[]): LogicCommand;
24
- nor(...__expressions__: import("./serializer/datatype").IQueryCondition[]): LogicCommand;
25
- or(...__expressions__: import("./serializer/datatype").IQueryCondition[]): LogicCommand;
26
- not(...__expressions__: import("./serializer/datatype").IQueryCondition[]): LogicCommand;
23
+ and(...__expressions__: IQueryCondition[]): LogicCommand;
24
+ nor(...__expressions__: IQueryCondition[]): LogicCommand;
25
+ or(...__expressions__: IQueryCondition[]): LogicCommand;
26
+ not(...__expressions__: IQueryCondition[]): LogicCommand;
27
27
  set(val: any): UpdateCommand;
28
28
  remove(): UpdateCommand;
29
29
  inc(val: number): UpdateCommand;