@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
@@ -1,14 +1,83 @@
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 __assign = (this && this.__assign) || function () {
17
+ __assign = Object.assign || function(t) {
18
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
19
+ s = arguments[i];
20
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
21
+ t[p] = s[p];
22
+ }
23
+ return t;
24
+ };
25
+ return __assign.apply(this, arguments);
26
+ };
27
+ var __values = (this && this.__values) || function(o) {
28
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
29
+ if (m) return m.call(o);
30
+ if (o && typeof o.length === "number") return {
31
+ next: function () {
32
+ if (o && i >= o.length) o = void 0;
33
+ return { value: o && o[i++], done: !o };
34
+ }
35
+ };
36
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
37
+ };
38
+ import { Db } from '../';
1
39
  import { EJSON } from 'bson';
2
40
  import { isObject } from './type';
3
- export const sleep = (ms = 0) => new Promise(r => setTimeout(r, ms));
4
- const counters = {};
5
- export const autoCount = (domain = 'any') => {
41
+ export var sleep = function (ms) {
42
+ if (ms === void 0) { ms = 0; }
43
+ return new Promise(function (r) { return setTimeout(r, ms); });
44
+ };
45
+ export function withCallback(promise, callback) {
46
+ if (typeof callback === 'function') {
47
+ promise.then(function (res) { return callback(0, res); }, function (err) { return callback(err); });
48
+ return undefined;
49
+ }
50
+ return promise;
51
+ }
52
+ var counters = {};
53
+ export var autoCount = function (domain) {
54
+ if (domain === void 0) { domain = 'any'; }
6
55
  if (!counters[domain]) {
7
56
  counters[domain] = 0;
8
57
  }
9
58
  return counters[domain]++;
10
59
  };
11
- export const getReqOpts = (apiOptions) => {
60
+ var wsList = {};
61
+ export function getWsInstance(db) {
62
+ if (!Db.wsClientClass) {
63
+ throw new Error('to use realtime you must import realtime module first');
64
+ }
65
+ var env = db.config.env;
66
+ if (!wsList[env]) {
67
+ wsList[env] = new Db.wsClientClass({
68
+ context: {
69
+ appConfig: {
70
+ docSizeLimit: 1000,
71
+ realtimePingInterval: 10000,
72
+ realtimePongWaitTimeout: 5000,
73
+ request: Db.createRequest(db.config)
74
+ }
75
+ }
76
+ });
77
+ }
78
+ return wsList[env];
79
+ }
80
+ export var getReqOpts = function (apiOptions) {
12
81
  if (apiOptions.timeout !== undefined) {
13
82
  return {
14
83
  timeout: apiOptions.timeout
@@ -16,11 +85,11 @@ export const getReqOpts = (apiOptions) => {
16
85
  }
17
86
  return {};
18
87
  };
19
- export const filterUndefined = o => {
88
+ export var filterUndefined = function (o) {
20
89
  if (!isObject(o)) {
21
90
  return o;
22
91
  }
23
- for (let key in o) {
92
+ for (var key in o) {
24
93
  if (o[key] === undefined) {
25
94
  delete o[key];
26
95
  }
@@ -30,26 +99,78 @@ export const filterUndefined = o => {
30
99
  }
31
100
  return o;
32
101
  };
33
- export const stringifyByEJSON = params => {
102
+ export var stringifyByEJSON = function (params) {
34
103
  params = filterUndefined(params);
35
104
  return EJSON.stringify(params, { relaxed: false });
36
105
  };
37
- export const parseByEJSON = params => {
106
+ export var parseByEJSON = function (params) {
38
107
  return EJSON.parse(params);
39
108
  };
40
- export class TcbError extends Error {
41
- constructor(error) {
42
- super(error.message);
43
- this.code = error.code;
44
- this.message = error.message;
109
+ var toWatchLegacyValue = function (val) {
110
+ var e_1, _a;
111
+ if (val instanceof Date) {
112
+ return { $date: +val };
45
113
  }
46
- }
47
- export const E = (errObj) => {
114
+ if (val instanceof RegExp) {
115
+ return { $regex: val.source, $options: val.flags };
116
+ }
117
+ if (val && typeof val === 'object') {
118
+ if (val._bsontype === 'ObjectId') {
119
+ return { $oid: val.toHexString() };
120
+ }
121
+ if (val._bsontype === 'BSONRegExp') {
122
+ return { $regex: val.pattern, $options: val.options };
123
+ }
124
+ if (Array.isArray(val)) {
125
+ return val.map(toWatchLegacyValue);
126
+ }
127
+ var ret = {};
128
+ try {
129
+ for (var _b = __values(Object.keys(val)), _c = _b.next(); !_c.done; _c = _b.next()) {
130
+ var key = _c.value;
131
+ ret[key] = toWatchLegacyValue(val[key]);
132
+ }
133
+ }
134
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
135
+ finally {
136
+ try {
137
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
138
+ }
139
+ finally { if (e_1) throw e_1.error; }
140
+ }
141
+ return ret;
142
+ }
143
+ return val;
144
+ };
145
+ export var toWatchQuery = function (ejsonStr) {
146
+ if (!ejsonStr) {
147
+ return '{}';
148
+ }
149
+ try {
150
+ var parsed = EJSON.parse(ejsonStr);
151
+ return JSON.stringify(toWatchLegacyValue(parsed));
152
+ }
153
+ catch (e) {
154
+ return ejsonStr;
155
+ }
156
+ };
157
+ var TcbError = (function (_super) {
158
+ __extends(TcbError, _super);
159
+ function TcbError(error) {
160
+ var _this = _super.call(this, error.message) || this;
161
+ _this.code = error.code;
162
+ _this.message = error.message;
163
+ return _this;
164
+ }
165
+ return TcbError;
166
+ }(Error));
167
+ export { TcbError };
168
+ export var E = function (errObj) {
48
169
  return new TcbError(errObj);
49
170
  };
50
171
  export function processReturn(throwOnCode, res) {
51
172
  if (throwOnCode === false) {
52
173
  return res;
53
174
  }
54
- throw E(Object.assign({}, res));
175
+ throw E(__assign({}, res));
55
176
  }
@@ -1,16 +1,29 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
1
12
  import { ErrorCode, WhereFilterOpList, OrderDirectionList, FieldType } from './constant';
2
13
  import { Util } from './util';
3
14
  import { ERRORS } from './const/code';
4
15
  import { E } from './utils/utils';
5
16
  import { getType } from './utils/type';
6
17
  import { SYMBOL_GEO_POINT } from './helper/symbol';
7
- const validOptionsKeys = ['limit', 'offset', 'projection', 'order', 'multiple', 'timeout', 'raw'];
8
- export class Validate {
9
- static isGeopoint(point, degree) {
18
+ var validOptionsKeys = ['limit', 'offset', 'projection', 'order', 'multiple', 'timeout', 'raw'];
19
+ var Validate = (function () {
20
+ function Validate() {
21
+ }
22
+ Validate.isGeopoint = function (point, degree) {
10
23
  if (Util.whichType(degree) !== FieldType.Number) {
11
24
  throw new Error('Geo Point must be number type');
12
25
  }
13
- const degreeAbs = Math.abs(degree);
26
+ var degreeAbs = Math.abs(degree);
14
27
  if (point === 'latitude' && degreeAbs > 90) {
15
28
  throw new Error('latitude should be a number ranges from -90 to 90');
16
29
  }
@@ -18,92 +31,93 @@ export class Validate {
18
31
  throw new Error('longitude should be a number ranges from -180 to 180');
19
32
  }
20
33
  return true;
21
- }
22
- static isInteger(param, num) {
34
+ };
35
+ Validate.isInteger = function (param, num) {
23
36
  if (!Number.isInteger(num)) {
24
37
  throw new Error(param + ErrorCode.IntergerError);
25
38
  }
26
39
  return true;
27
- }
28
- static mustBeBoolean(param, bool) {
40
+ };
41
+ Validate.mustBeBoolean = function (param, bool) {
29
42
  if (typeof bool !== 'boolean') {
30
43
  throw new Error(param + ErrorCode.BooleanError);
31
44
  }
32
45
  return true;
33
- }
34
- static isProjection(param, value) {
46
+ };
47
+ Validate.isProjection = function (param, value) {
35
48
  if (getType(value) !== 'object') {
36
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `${param} projection must be an object` }));
49
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "".concat(param, " projection must be an object") }));
37
50
  }
38
- for (const key in value) {
39
- const subValue = value[key];
51
+ for (var key in value) {
52
+ var subValue = value[key];
40
53
  if (getType(subValue) === 'number') {
41
54
  if (subValue !== 0 && subValue !== 1) {
42
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `if the value in projection is of number, it must be 0 or 1` }));
55
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "if the value in projection is of number, it must be 0 or 1" }));
43
56
  }
44
57
  }
45
58
  else if (getType(subValue) === 'object') {
46
59
  }
47
60
  else {
48
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: 'invalid projection' }));
61
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: 'invalid projection' }));
49
62
  }
50
63
  }
51
64
  return true;
52
- }
53
- static isOrder(param, value) {
65
+ };
66
+ Validate.isOrder = function (param, value) {
54
67
  if (getType(value) !== 'object') {
55
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `${param} order must be an object` }));
68
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "".concat(param, " order must be an object") }));
56
69
  }
57
- for (let key in value) {
58
- const subValue = value[key];
70
+ for (var key in value) {
71
+ var subValue = value[key];
59
72
  if (subValue !== 1 && subValue !== -1) {
60
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `order value must be 1 or -1` }));
73
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "order value must be 1 or -1" }));
61
74
  }
62
75
  }
63
76
  return true;
64
- }
65
- static isFieldOrder(direction) {
77
+ };
78
+ Validate.isFieldOrder = function (direction) {
66
79
  if (OrderDirectionList.indexOf(direction) === -1) {
67
80
  throw new Error(ErrorCode.DirectionError);
68
81
  }
69
82
  return true;
70
- }
71
- static isFieldPath(path) {
83
+ };
84
+ Validate.isFieldPath = function (path) {
72
85
  if (!/^[a-zA-Z0-9-_\.]/.test(path)) {
73
86
  throw new Error();
74
87
  }
75
88
  return true;
76
- }
77
- static isOperator(op) {
89
+ };
90
+ Validate.isOperator = function (op) {
78
91
  if (WhereFilterOpList.indexOf(op) === -1) {
79
92
  throw new Error(ErrorCode.OpStrError);
80
93
  }
81
94
  return true;
82
- }
83
- static isCollName(name) {
95
+ };
96
+ Validate.isCollName = function (name) {
84
97
  if (!/^[a-zA-Z0-9]([a-zA-Z0-9-_]){1,32}$/.test(name)) {
85
98
  throw new Error(ErrorCode.CollNameError);
86
99
  }
87
100
  return true;
88
- }
89
- static isDocID(docId) {
101
+ };
102
+ Validate.isDocID = function (docId) {
90
103
  if (!/^([a-fA-F0-9]){24}$/.test(docId)) {
91
104
  throw new Error(ErrorCode.DocIDError);
92
105
  }
93
106
  return true;
94
- }
95
- static isValidOptions(options = {}) {
107
+ };
108
+ Validate.isValidOptions = function (options) {
109
+ if (options === void 0) { options = {}; }
96
110
  if (getType(options) !== 'object') {
97
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `options must be an object` }));
111
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "options must be an object" }));
98
112
  }
99
- const keys = Object.keys(options);
100
- for (const index in keys) {
113
+ var keys = Object.keys(options);
114
+ for (var index in keys) {
101
115
  if (validOptionsKeys.indexOf(keys[index]) < 0) {
102
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `${keys[index]} is invalid options key` }));
116
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "".concat(keys[index], " is invalid options key") }));
103
117
  }
104
118
  }
105
- const { limit, offset, projection, order } = options;
106
- const { multiple } = options;
119
+ var _a = options, limit = _a.limit, offset = _a.offset, projection = _a.projection, order = _a.order;
120
+ var multiple = options.multiple;
107
121
  if (limit !== undefined) {
108
122
  Validate.isInteger('limit', limit);
109
123
  }
@@ -123,14 +137,14 @@ export class Validate {
123
137
  Validate.isInteger('timeout', options.timeout);
124
138
  }
125
139
  return true;
126
- }
127
- static isValidAggregation(stage) {
140
+ };
141
+ Validate.isValidAggregation = function (stage) {
128
142
  if (Object.keys(stage).length !== 1) {
129
- throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `aggregation stage must have one key` }));
143
+ throw E(__assign(__assign({}, ERRORS.INVALID_PARAM), { message: "aggregation stage must have one key" }));
130
144
  }
131
145
  return true;
132
- }
133
- static isCentersPhere(param) {
146
+ };
147
+ Validate.isCentersPhere = function (param) {
134
148
  if (Array.isArray(param) && param.length === 2) {
135
149
  if (getType(param[0]) === 'object' &&
136
150
  param[0]._internalType === SYMBOL_GEO_POINT &&
@@ -138,8 +152,8 @@ export class Validate {
138
152
  return true;
139
153
  }
140
154
  if (Array.isArray(param[0]) && param[0].length === 2) {
141
- const longitude = param[0][0];
142
- const latitude = param[0][1];
155
+ var longitude = param[0][0];
156
+ var latitude = param[0][1];
143
157
  Validate.isGeopoint('longitude', longitude);
144
158
  Validate.isGeopoint('latitude', latitude);
145
159
  if (typeof param[1] === 'number') {
@@ -147,6 +161,8 @@ export class Validate {
147
161
  }
148
162
  }
149
163
  }
150
- throw new Error(`${ErrorCode.CentersPhereError}`);
151
- }
152
- }
164
+ throw new Error("".concat(ErrorCode.CentersPhereError));
165
+ };
166
+ return Validate;
167
+ }());
168
+ export { Validate };
package/jest.config.js ADDED
@@ -0,0 +1,14 @@
1
+ module.exports = {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ testMatch: ['**/tests/**/*.ts'],
5
+ moduleFileExtensions: ['ts', 'js'],
6
+ transform: {
7
+ '^.+\\.ts$': ['ts-jest', {
8
+ tsconfig: 'tsconfig.test.json'
9
+ }]
10
+ },
11
+ transformIgnorePatterns: [
12
+ '/node_modules/(?!@types)/'
13
+ ]
14
+ };
package/package.json CHANGED
@@ -1,32 +1,36 @@
1
1
  {
2
2
  "name": "@cloudbase/database",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
4
4
  "description": "database for (node.)js sdk ",
5
5
  "main": "dist/commonjs/index.js",
6
6
  "module": "dist/esm/index.js",
7
+ "publishConfig": {
8
+ "tag": "next"
9
+ },
7
10
  "scripts": {
8
11
  "eslint": "eslint \"./**/*.ts\"",
9
12
  "eslint-fix": "eslint --fix \"./**/*.ts\"",
10
- "test": "echo \"Error: no test specified\" && exit 1",
13
+ "test": "jest",
11
14
  "build": "tsc -p tsconfig.json && tsc -p tsconfig.esm.json",
12
15
  "prepublishOnly": "npm run build"
13
16
  },
14
17
  "author": "",
15
18
  "license": "ISC",
16
19
  "devDependencies": {
20
+ "@types/jest": "26.0.24",
21
+ "@types/node": "14.14.31",
17
22
  "eslint": "^5.15.3",
18
23
  "eslint-config-prettier": "^4.1.0",
19
24
  "eslint-plugin-prettier": "^3.0.1",
20
25
  "eslint-plugin-typescript": "^0.14.0",
21
- "typescript": "^3.6.2",
22
- "typescript-eslint-parser": "^22.0.0",
23
- "@types/node": "^13.13.4"
26
+ "jest": "26.6.3",
27
+ "ts-jest": "26.5.6",
28
+ "typescript": "^4.1.0",
29
+ "typescript-eslint-parser": "^22.0.0"
24
30
  },
25
31
  "sideEffects": false,
26
32
  "dependencies": {
27
- "bson": "^4.0.3",
28
- "lodash.clonedeep": "4.5.0",
29
- "lodash.set": "4.3.2",
30
- "lodash.unset": "4.5.2"
31
- }
33
+ "bson": "^6.10.4"
34
+ },
35
+ "packageManager": "yarn@1.22.22"
32
36
  }
package/src/aggregate.ts CHANGED
@@ -5,9 +5,6 @@ import { stringifyByEJSON } from './utils/utils'
5
5
  import { getType } from './utils/type'
6
6
  import { Validate } from './validate'
7
7
  import { Point } from './geo/point'
8
-
9
- // const EARTH_RADIUS = 6378100
10
-
11
8
  export default class Aggregation {
12
9
  _db: any
13
10
  _request: any
@@ -18,7 +15,7 @@ export default class Aggregation {
18
15
 
19
16
  if (db && collectionName) {
20
17
  this._db = db
21
- this._request = new Db.reqClass(this._db.config)
18
+ this._request = Db.createRequest(this._db.config)
22
19
  this._collectionName = collectionName
23
20
 
24
21
  if (rawPipeline && rawPipeline.length > 0) {
@@ -98,7 +95,6 @@ export default class Aggregation {
98
95
  if (param.query) {
99
96
  param.query = QuerySerializer.encode(param.query)
100
97
  }
101
-
102
98
  // 判断是否有 distanceMultiplier 参数
103
99
  if (param.distanceMultiplier && typeof (param.distanceMultiplier) === 'number') {
104
100
  param.distanceMultiplier = param.distanceMultiplier
package/src/collection.ts CHANGED
@@ -3,7 +3,7 @@ import { DocumentReference } from './document'
3
3
  import { Query, QueryOption, UpdateOption } from './query'
4
4
  import Aggregation from './aggregate'
5
5
  import { serialize } from './serializer/datatype'
6
- import { getReqOpts, stringifyByEJSON } from './utils/utils'
6
+ import { getReqOpts, stringifyByEJSON, withCallback } from './utils/utils'
7
7
  import { Validate } from './validate'
8
8
  import { isArray } from './utils/type'
9
9
 
@@ -63,7 +63,20 @@ export class CollectionReference extends Query {
63
63
  * @param data - 数据
64
64
  * @param opts - 可选配置项
65
65
  */
66
- async add(
66
+ add(
67
+ data: any,
68
+ callback?: any
69
+ ): Promise<{
70
+ ids?: string[]
71
+ id?: string
72
+ inserted?: number // 事务调用返回
73
+ ok?: number // 事务调用返回
74
+ requestId: string
75
+ }> {
76
+ return withCallback(this._add(data), callback) as any
77
+ }
78
+
79
+ private async _add(
67
80
  data: any
68
81
  ): Promise<{
69
82
  ids?: string[]