@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.
Files changed (194) 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.js +4 -3
  35. package/dist/commonjs/operator-map.js +7 -6
  36. package/dist/commonjs/query.d.ts +8 -4
  37. package/dist/commonjs/query.js +308 -201
  38. package/dist/commonjs/regexp/index.d.ts +1 -1
  39. package/dist/commonjs/regexp/index.js +16 -9
  40. package/dist/commonjs/serializer/common.d.ts +2 -2
  41. package/dist/commonjs/serializer/common.js +59 -22
  42. package/dist/commonjs/serializer/datatype.d.ts +2 -2
  43. package/dist/commonjs/serializer/datatype.js +58 -21
  44. package/dist/commonjs/serializer/query.d.ts +1 -1
  45. package/dist/commonjs/serializer/query.js +137 -127
  46. package/dist/commonjs/serializer/update.d.ts +1 -1
  47. package/dist/commonjs/serializer/update.js +83 -78
  48. package/dist/commonjs/serverDate/index.d.ts +1 -1
  49. package/dist/commonjs/serverDate/index.js +16 -9
  50. package/dist/commonjs/transaction/collection.d.ts +1 -1
  51. package/dist/commonjs/transaction/collection.js +5 -2
  52. package/dist/commonjs/transaction/document.d.ts +1 -1
  53. package/dist/commonjs/transaction/document.js +5 -5
  54. package/dist/commonjs/transaction/index.js +196 -86
  55. package/dist/commonjs/transaction/query.d.ts +39 -1
  56. package/dist/commonjs/transaction/query.js +252 -1
  57. package/dist/commonjs/typings/index.d.ts +4 -4
  58. package/dist/commonjs/typings/realtime.d.ts +16 -16
  59. package/dist/commonjs/util.js +123 -92
  60. package/dist/commonjs/utils/error.d.ts +2 -1
  61. package/dist/commonjs/utils/error.js +74 -38
  62. package/dist/commonjs/utils/symbol.js +33 -14
  63. package/dist/commonjs/utils/type.d.ts +1 -1
  64. package/dist/commonjs/utils/type.js +25 -13
  65. package/dist/commonjs/utils/utils.d.ts +4 -1
  66. package/dist/commonjs/utils/utils.js +93 -22
  67. package/dist/commonjs/validate.js +79 -63
  68. package/dist/esm/ObjectId/index.d.ts +1 -1
  69. package/dist/esm/ObjectId/index.js +15 -8
  70. package/dist/esm/aggregate.js +118 -66
  71. package/dist/esm/collection.d.ts +3 -2
  72. package/dist/esm/collection.js +125 -52
  73. package/dist/esm/command.d.ts +5 -5
  74. package/dist/esm/command.js +175 -147
  75. package/dist/esm/commands/logic.js +30 -20
  76. package/dist/esm/commands/query.d.ts +1 -1
  77. package/dist/esm/commands/query.js +91 -71
  78. package/dist/esm/commands/update.js +8 -6
  79. package/dist/esm/config/error.config.js +1 -1
  80. package/dist/esm/const/code.js +1 -1
  81. package/dist/esm/constant.d.ts +7 -7
  82. package/dist/esm/constant.js +12 -11
  83. package/dist/esm/document.d.ts +10 -6
  84. package/dist/esm/document.js +297 -185
  85. package/dist/esm/geo/interface.js +1 -0
  86. package/dist/esm/geo/lineString.d.ts +1 -1
  87. package/dist/esm/geo/lineString.js +57 -28
  88. package/dist/esm/geo/multiLineString.d.ts +1 -1
  89. package/dist/esm/geo/multiLineString.js +66 -27
  90. package/dist/esm/geo/multiPoint.d.ts +1 -1
  91. package/dist/esm/geo/multiPoint.js +53 -24
  92. package/dist/esm/geo/multiPolygon.d.ts +1 -1
  93. package/dist/esm/geo/multiPolygon.js +91 -31
  94. package/dist/esm/geo/point.d.ts +1 -1
  95. package/dist/esm/geo/point.js +26 -19
  96. package/dist/esm/geo/polygon.d.ts +1 -1
  97. package/dist/esm/geo/polygon.js +73 -34
  98. package/dist/esm/helper/symbol.js +13 -13
  99. package/dist/esm/index.d.ts +8 -2
  100. package/dist/esm/index.js +62 -9
  101. package/dist/esm/lib/util.js +1 -2
  102. package/dist/esm/operator-map.js +4 -4
  103. package/dist/esm/query.d.ts +8 -4
  104. package/dist/esm/query.js +299 -192
  105. package/dist/esm/regexp/index.d.ts +1 -1
  106. package/dist/esm/regexp/index.js +15 -8
  107. package/dist/esm/serializer/common.d.ts +2 -2
  108. package/dist/esm/serializer/common.js +49 -13
  109. package/dist/esm/serializer/datatype.d.ts +2 -2
  110. package/dist/esm/serializer/datatype.js +44 -8
  111. package/dist/esm/serializer/query.d.ts +1 -1
  112. package/dist/esm/serializer/query.js +112 -102
  113. package/dist/esm/serializer/update.d.ts +1 -1
  114. package/dist/esm/serializer/update.js +69 -64
  115. package/dist/esm/serverDate/index.d.ts +1 -1
  116. package/dist/esm/serverDate/index.js +15 -8
  117. package/dist/esm/transaction/collection.d.ts +1 -1
  118. package/dist/esm/transaction/collection.js +4 -2
  119. package/dist/esm/transaction/document.d.ts +1 -1
  120. package/dist/esm/transaction/document.js +2 -3
  121. package/dist/esm/transaction/index.js +191 -81
  122. package/dist/esm/transaction/query.d.ts +39 -1
  123. package/dist/esm/transaction/query.js +251 -1
  124. package/dist/esm/typings/index.d.ts +4 -4
  125. package/dist/esm/typings/index.js +1 -0
  126. package/dist/esm/typings/realtime.d.ts +16 -16
  127. package/dist/esm/typings/realtime.js +1 -0
  128. package/dist/esm/util.js +121 -90
  129. package/dist/esm/utils/error.d.ts +2 -1
  130. package/dist/esm/utils/error.js +70 -35
  131. package/dist/esm/utils/symbol.js +33 -14
  132. package/dist/esm/utils/type.d.ts +1 -1
  133. package/dist/esm/utils/type.js +12 -12
  134. package/dist/esm/utils/utils.d.ts +4 -1
  135. package/dist/esm/utils/utils.js +78 -16
  136. package/dist/esm/validate.js +65 -49
  137. package/jest.config.js +14 -0
  138. package/package.json +14 -10
  139. package/src/aggregate.ts +1 -5
  140. package/src/collection.ts +15 -2
  141. package/src/document.ts +45 -34
  142. package/src/index.ts +51 -4
  143. package/src/query.ts +33 -29
  144. package/src/serializer/query.ts +18 -6
  145. package/src/transaction/index.ts +1 -1
  146. package/src/typings/index.ts +0 -4
  147. package/src/utils/utils.ts +46 -0
  148. package/src/validate.ts +3 -6
  149. package/tests/gateway-contract.test.ts +180 -0
  150. package/tests/index.test.ts +283 -0
  151. package/tests/transaction-query.test.ts +207 -0
  152. package/tsconfig.esm.json +4 -2
  153. package/tsconfig.json +16 -6
  154. package/tsconfig.test.json +15 -0
  155. package/dist/commonjs/realtime/error.d.ts +0 -7
  156. package/dist/commonjs/realtime/error.js +0 -11
  157. package/dist/commonjs/realtime/listener.d.ts +0 -14
  158. package/dist/commonjs/realtime/listener.js +0 -17
  159. package/dist/commonjs/realtime/message.d.ts +0 -3
  160. package/dist/commonjs/realtime/message.js +0 -10
  161. package/dist/commonjs/realtime/snapshot.d.ts +0 -16
  162. package/dist/commonjs/realtime/snapshot.js +0 -42
  163. package/dist/commonjs/realtime/virtual-websocket-client.d.ts +0 -63
  164. package/dist/commonjs/realtime/virtual-websocket-client.js +0 -736
  165. package/dist/commonjs/realtime/websocket-client.d.ts +0 -78
  166. package/dist/commonjs/realtime/websocket-client.js +0 -567
  167. package/dist/commonjs/realtime/ws-event.d.ts +0 -126
  168. package/dist/commonjs/realtime/ws-event.js +0 -139
  169. package/dist/commonjs/transaction.d.ts +0 -42
  170. package/dist/commonjs/transaction.js +0 -126
  171. package/dist/esm/realtime/error.d.ts +0 -7
  172. package/dist/esm/realtime/error.js +0 -8
  173. package/dist/esm/realtime/listener.d.ts +0 -14
  174. package/dist/esm/realtime/listener.js +0 -14
  175. package/dist/esm/realtime/message.d.ts +0 -3
  176. package/dist/esm/realtime/message.js +0 -6
  177. package/dist/esm/realtime/snapshot.d.ts +0 -16
  178. package/dist/esm/realtime/snapshot.js +0 -39
  179. package/dist/esm/realtime/virtual-websocket-client.d.ts +0 -63
  180. package/dist/esm/realtime/virtual-websocket-client.js +0 -733
  181. package/dist/esm/realtime/websocket-client.d.ts +0 -78
  182. package/dist/esm/realtime/websocket-client.js +0 -564
  183. package/dist/esm/realtime/ws-event.d.ts +0 -126
  184. package/dist/esm/realtime/ws-event.js +0 -137
  185. package/dist/esm/transaction.d.ts +0 -42
  186. package/dist/esm/transaction.js +0 -121
  187. package/src/realtime/error.ts +0 -19
  188. package/src/realtime/listener.ts +0 -35
  189. package/src/realtime/message.ts +0 -14
  190. package/src/realtime/snapshot.ts +0 -60
  191. package/src/realtime/virtual-websocket-client.ts +0 -1195
  192. package/src/realtime/websocket-client.ts +0 -995
  193. package/src/realtime/ws-event.ts +0 -163
  194. 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,17 +1,24 @@
1
1
  import { SYMBOL_OBJECTID } from '../helper/symbol';
2
- export class ObjectId {
3
- constructor({ id = '' } = {}) {
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
- get _internalType() {
7
- return SYMBOL_OBJECTID;
8
- }
9
- parse() {
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
  }
@@ -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
- export default class Aggregation {
9
- constructor(db, collectionName, rawPipeline) {
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 = new Db.reqClass(this._db.config);
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
- const stageName = Object.keys(stage)[0];
19
- this._pipe(stageName, stage[stageName], true);
55
+ var stageName = Object.keys(stage)[0];
56
+ _this._pipe(stageName, stage[stageName], true);
20
57
  });
21
58
  }
22
59
  }
23
60
  }
24
- async end() {
25
- if (!this._collectionName || !this._db) {
26
- throw new Error('Aggregation pipeline cannot send request');
27
- }
28
- const result = await this._request.send('database.aggregateDocuments', {
29
- collectionName: this._collectionName,
30
- stages: this._stages
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
- if (result && result.data && result.data.list) {
33
- return {
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(({ stageKey, stageValue }) => {
45
- return {
46
- [stageKey]: JSON.parse(stageValue)
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 = false) {
51
- let transformParam = '';
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 : `$${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;
@@ -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,69 +1,142 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ var __generator = (this && this.__generator) || function (thisArg, body) {
26
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
+ function verb(n) { return function (v) { return step([n, v]); }; }
29
+ function step(op) {
30
+ if (f) throw new TypeError("Generator is already executing.");
31
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
32
+ 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;
33
+ if (y = 0, t) op = [op[0] & 2, t.value];
34
+ switch (op[0]) {
35
+ case 0: case 1: t = op; break;
36
+ case 4: _.label++; return { value: op[1], done: false };
37
+ case 5: _.label++; y = op[1]; op = [0]; continue;
38
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
+ default:
40
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
+ if (t[2]) _.ops.pop();
45
+ _.trys.pop(); continue;
46
+ }
47
+ op = body.call(thisArg, _);
48
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
+ }
51
+ };
1
52
  import { DocumentReference } from './document';
2
53
  import { Query } from './query';
3
54
  import Aggregation from './aggregate';
4
55
  import { serialize } from './serializer/datatype';
5
- import { getReqOpts, stringifyByEJSON } from './utils/utils';
56
+ import { getReqOpts, stringifyByEJSON, withCallback } from './utils/utils';
6
57
  import { Validate } from './validate';
7
58
  import { isArray } from './utils/type';
8
- export class CollectionReference extends Query {
9
- constructor(db, coll, apiOptions, transactionId) {
10
- super(db, coll, '', apiOptions, transactionId);
59
+ var CollectionReference = (function (_super) {
60
+ __extends(CollectionReference, _super);
61
+ function CollectionReference(db, coll, apiOptions, transactionId) {
62
+ var _this = _super.call(this, db, coll, '', apiOptions, transactionId) || this;
11
63
  if (transactionId) {
12
- this._transactionId = transactionId;
64
+ _this._transactionId = transactionId;
13
65
  }
66
+ return _this;
14
67
  }
15
- get name() {
16
- return this._coll;
17
- }
18
- doc(docID) {
68
+ Object.defineProperty(CollectionReference.prototype, "name", {
69
+ get: function () {
70
+ return this._coll;
71
+ },
72
+ enumerable: false,
73
+ configurable: true
74
+ });
75
+ CollectionReference.prototype.doc = function (docID) {
19
76
  if (typeof docID !== 'string' && typeof docID !== 'number') {
20
77
  throw new Error('docId必须为字符串或数字');
21
78
  }
22
79
  return new DocumentReference(this._db, this._coll, this._apiOptions, docID, this._transactionId);
23
- }
24
- async add(data) {
25
- let transformData = data;
26
- if (!isArray(data)) {
27
- transformData = [data];
28
- }
29
- transformData = transformData.map(item => {
30
- return stringifyByEJSON(serialize(item));
80
+ };
81
+ CollectionReference.prototype.add = function (data, callback) {
82
+ return withCallback(this._add(data), callback);
83
+ };
84
+ CollectionReference.prototype._add = function (data) {
85
+ return __awaiter(this, void 0, void 0, function () {
86
+ var transformData, params, res;
87
+ return __generator(this, function (_a) {
88
+ switch (_a.label) {
89
+ case 0:
90
+ transformData = data;
91
+ if (!isArray(data)) {
92
+ transformData = [data];
93
+ }
94
+ transformData = transformData.map(function (item) {
95
+ return stringifyByEJSON(serialize(item));
96
+ });
97
+ params = {
98
+ collectionName: this._coll,
99
+ data: transformData
100
+ };
101
+ if (this._transactionId) {
102
+ params.transactionId = this._transactionId;
103
+ }
104
+ return [4, this._request.send('database.insertDocument', params, getReqOpts(this._apiOptions))];
105
+ case 1:
106
+ res = _a.sent();
107
+ if (res.code) {
108
+ return [2, res];
109
+ }
110
+ if (!isArray(data)) {
111
+ if (this._transactionId) {
112
+ return [2, {
113
+ inserted: 1,
114
+ ok: 1,
115
+ id: res.data.insertedIds[0],
116
+ requestId: res.requestId
117
+ }];
118
+ }
119
+ return [2, {
120
+ id: res.data.insertedIds[0],
121
+ requestId: res.requestId
122
+ }];
123
+ }
124
+ return [2, {
125
+ ids: res.data.insertedIds,
126
+ requestId: res.requestId
127
+ }];
128
+ }
129
+ });
31
130
  });
32
- let params = {
33
- collectionName: this._coll,
34
- data: transformData
35
- };
36
- if (this._transactionId) {
37
- params.transactionId = this._transactionId;
38
- }
39
- const res = await this._request.send('database.insertDocument', params, getReqOpts(this._apiOptions));
40
- if (res.code) {
41
- return res;
42
- }
43
- if (!isArray(data)) {
44
- if (this._transactionId) {
45
- return {
46
- inserted: 1,
47
- ok: 1,
48
- id: res.data.insertedIds[0],
49
- requestId: res.requestId
50
- };
51
- }
52
- return {
53
- id: res.data.insertedIds[0],
54
- requestId: res.requestId
55
- };
56
- }
57
- return {
58
- ids: res.data.insertedIds,
59
- requestId: res.requestId
60
- };
61
- }
62
- aggregate(rawPipeline = []) {
131
+ };
132
+ CollectionReference.prototype.aggregate = function (rawPipeline) {
133
+ if (rawPipeline === void 0) { rawPipeline = []; }
63
134
  return new Aggregation(this._db, this._coll, (this._apiOptions.raw || false) ? rawPipeline : []);
64
- }
65
- options(apiOptions) {
135
+ };
136
+ CollectionReference.prototype.options = function (apiOptions) {
66
137
  Validate.isValidOptions(apiOptions);
67
138
  return new CollectionReference(this._db, this._coll, apiOptions, this._transactionId);
68
- }
69
- }
139
+ };
140
+ return CollectionReference;
141
+ }(Query));
142
+ export { 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;