@cloudbase/database 1.4.3 → 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 (196) 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 +8 -4
  38. package/dist/commonjs/query.js +308 -201
  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 +4 -1
  67. package/dist/commonjs/utils/utils.js +93 -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 +8 -4
  106. package/dist/esm/query.js +299 -192
  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 +4 -1
  137. package/dist/esm/utils/utils.js +78 -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 +33 -29
  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 +46 -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/tsconfig.esm.json +4 -2
  155. package/tsconfig.json +16 -6
  156. package/tsconfig.test.json +15 -0
  157. package/dist/commonjs/realtime/error.d.ts +0 -7
  158. package/dist/commonjs/realtime/error.js +0 -11
  159. package/dist/commonjs/realtime/listener.d.ts +0 -14
  160. package/dist/commonjs/realtime/listener.js +0 -17
  161. package/dist/commonjs/realtime/message.d.ts +0 -3
  162. package/dist/commonjs/realtime/message.js +0 -10
  163. package/dist/commonjs/realtime/snapshot.d.ts +0 -16
  164. package/dist/commonjs/realtime/snapshot.js +0 -42
  165. package/dist/commonjs/realtime/virtual-websocket-client.d.ts +0 -63
  166. package/dist/commonjs/realtime/virtual-websocket-client.js +0 -736
  167. package/dist/commonjs/realtime/websocket-client.d.ts +0 -78
  168. package/dist/commonjs/realtime/websocket-client.js +0 -567
  169. package/dist/commonjs/realtime/ws-event.d.ts +0 -126
  170. package/dist/commonjs/realtime/ws-event.js +0 -139
  171. package/dist/commonjs/transaction.d.ts +0 -42
  172. package/dist/commonjs/transaction.js +0 -126
  173. package/dist/esm/realtime/error.d.ts +0 -7
  174. package/dist/esm/realtime/error.js +0 -8
  175. package/dist/esm/realtime/listener.d.ts +0 -14
  176. package/dist/esm/realtime/listener.js +0 -14
  177. package/dist/esm/realtime/message.d.ts +0 -3
  178. package/dist/esm/realtime/message.js +0 -6
  179. package/dist/esm/realtime/snapshot.d.ts +0 -16
  180. package/dist/esm/realtime/snapshot.js +0 -39
  181. package/dist/esm/realtime/virtual-websocket-client.d.ts +0 -63
  182. package/dist/esm/realtime/virtual-websocket-client.js +0 -733
  183. package/dist/esm/realtime/websocket-client.d.ts +0 -78
  184. package/dist/esm/realtime/websocket-client.js +0 -564
  185. package/dist/esm/realtime/ws-event.d.ts +0 -126
  186. package/dist/esm/realtime/ws-event.js +0 -137
  187. package/dist/esm/transaction.d.ts +0 -42
  188. package/dist/esm/transaction.js +0 -121
  189. package/src/realtime/error.ts +0 -19
  190. package/src/realtime/listener.ts +0 -35
  191. package/src/realtime/message.ts +0 -14
  192. package/src/realtime/snapshot.ts +0 -60
  193. package/src/realtime/virtual-websocket-client.ts +0 -1195
  194. package/src/realtime/websocket-client.ts +0 -995
  195. package/src/realtime/ws-event.ts +0 -163
  196. package/src/transaction.ts +0 -190
@@ -1,14 +1,72 @@
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
+ import { Db } from '../';
1
28
  import { EJSON } from 'bson';
2
29
  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') => {
30
+ export var sleep = function (ms) {
31
+ if (ms === void 0) { ms = 0; }
32
+ return new Promise(function (r) { return setTimeout(r, ms); });
33
+ };
34
+ export function withCallback(promise, callback) {
35
+ if (typeof callback === 'function') {
36
+ promise.then(function (res) { return callback(0, res); }, function (err) { return callback(err); });
37
+ return undefined;
38
+ }
39
+ return promise;
40
+ }
41
+ var counters = {};
42
+ export var autoCount = function (domain) {
43
+ if (domain === void 0) { domain = 'any'; }
6
44
  if (!counters[domain]) {
7
45
  counters[domain] = 0;
8
46
  }
9
47
  return counters[domain]++;
10
48
  };
11
- export const getReqOpts = (apiOptions) => {
49
+ var wsList = {};
50
+ export function getWsInstance(db) {
51
+ if (!Db.wsClientClass) {
52
+ throw new Error('to use realtime you must import realtime module first');
53
+ }
54
+ var env = db.config.env;
55
+ if (!wsList[env]) {
56
+ wsList[env] = new Db.wsClientClass({
57
+ context: {
58
+ appConfig: {
59
+ docSizeLimit: 1000,
60
+ realtimePingInterval: 10000,
61
+ realtimePongWaitTimeout: 5000,
62
+ request: Db.createRequest(db.config)
63
+ }
64
+ }
65
+ });
66
+ }
67
+ return wsList[env];
68
+ }
69
+ export var getReqOpts = function (apiOptions) {
12
70
  if (apiOptions.timeout !== undefined) {
13
71
  return {
14
72
  timeout: apiOptions.timeout
@@ -16,11 +74,11 @@ export const getReqOpts = (apiOptions) => {
16
74
  }
17
75
  return {};
18
76
  };
19
- export const filterUndefined = o => {
77
+ export var filterUndefined = function (o) {
20
78
  if (!isObject(o)) {
21
79
  return o;
22
80
  }
23
- for (let key in o) {
81
+ for (var key in o) {
24
82
  if (o[key] === undefined) {
25
83
  delete o[key];
26
84
  }
@@ -30,26 +88,30 @@ export const filterUndefined = o => {
30
88
  }
31
89
  return o;
32
90
  };
33
- export const stringifyByEJSON = params => {
91
+ export var stringifyByEJSON = function (params) {
34
92
  params = filterUndefined(params);
35
93
  return EJSON.stringify(params, { relaxed: false });
36
94
  };
37
- export const parseByEJSON = params => {
95
+ export var parseByEJSON = function (params) {
38
96
  return EJSON.parse(params);
39
97
  };
40
- export class TcbError extends Error {
41
- constructor(error) {
42
- super(error.message);
43
- this.code = error.code;
44
- this.message = error.message;
98
+ var TcbError = (function (_super) {
99
+ __extends(TcbError, _super);
100
+ function TcbError(error) {
101
+ var _this = _super.call(this, error.message) || this;
102
+ _this.code = error.code;
103
+ _this.message = error.message;
104
+ return _this;
45
105
  }
46
- }
47
- export const E = (errObj) => {
106
+ return TcbError;
107
+ }(Error));
108
+ export { TcbError };
109
+ export var E = function (errObj) {
48
110
  return new TcbError(errObj);
49
111
  };
50
112
  export function processReturn(throwOnCode, res) {
51
113
  if (throwOnCode === false) {
52
114
  return res;
53
115
  }
54
- throw E(Object.assign({}, res));
116
+ throw E(__assign({}, res));
55
117
  }
@@ -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.0",
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[]
package/src/document.ts CHANGED
@@ -4,9 +4,8 @@ import { UpdateSerializer } from './serializer/update'
4
4
  import { serialize } from './serializer/datatype'
5
5
  import { UpdateCommand } from './commands/update'
6
6
  import { IWatchOptions, DBRealtimeListener } from './typings/index'
7
- import { RealtimeWebSocketClient } from './realtime/websocket-client'
8
7
  import { QueryType } from './constant'
9
- import { getReqOpts, stringifyByEJSON, processReturn } from './utils/utils'
8
+ import { getWsInstance, getReqOpts, stringifyByEJSON, processReturn, withCallback } from './utils/utils'
10
9
  import { ERRORS } from './const/code'
11
10
  import { EJSON } from 'bson'
12
11
  import { QueryOption, UpdateOption } from './query'
@@ -24,11 +23,6 @@ export class DocumentReference {
24
23
 
25
24
  readonly _transactionId: string
26
25
 
27
- /**
28
- *
29
- */
30
- readonly projection: Object
31
-
32
26
  /**
33
27
  * 数据库引用
34
28
  *
@@ -73,7 +67,7 @@ export class DocumentReference {
73
67
  this.id = docID
74
68
  this._transactionId = transactionId
75
69
  /* eslint-disable new-cap*/
76
- this.request = new Db.reqClass(this._db.config)
70
+ this.request = Db.createRequest(this._db.config)
77
71
  this._apiOptions = apiOptions
78
72
  }
79
73
 
@@ -83,16 +77,23 @@ export class DocumentReference {
83
77
  * @param data - 文档数据
84
78
  * @internal
85
79
  */
86
- async create(data: any): Promise<any> {
80
+ create(data: any, callback?: any): Promise<any> {
81
+ return withCallback(this._create(data), callback)
82
+ }
83
+
84
+ private async _create(data: any): Promise<any> {
85
+ const isBatch = Array.isArray(data)
86
+
87
87
  // 存在docid 则放入data
88
- if (this.id) {
88
+ if (!isBatch && this.id) {
89
89
  data['_id'] = this.id
90
90
  }
91
91
 
92
92
  let params = {
93
93
  collectionName: this._coll,
94
- // data: Util.encodeDocumentDataForReq(data, false, false)
95
- data: [stringifyByEJSON(serialize(data))],
94
+ data: isBatch
95
+ ? data.map((item: any) => stringifyByEJSON(serialize(item)))
96
+ : [stringifyByEJSON(serialize(data))],
96
97
  transactionId: this._transactionId
97
98
  }
98
99
 
@@ -116,11 +117,18 @@ export class DocumentReference {
116
117
  }
117
118
  }
118
119
 
120
+ // 批量创建,返回 ids
121
+ if (isBatch) {
122
+ return {
123
+ ids: (res.data && res.data.insertedIds) || [],
124
+ requestId: res.requestId
125
+ }
126
+ }
127
+
119
128
  return {
120
129
  id: res.data.insertedIds[0],
121
130
  requestId: res.requestId
122
131
  }
123
- // }
124
132
  }
125
133
 
126
134
  /**
@@ -132,7 +140,11 @@ export class DocumentReference {
132
140
  * @param data - 文档数据
133
141
  * @param opts - 可选项
134
142
  */
135
- async set(data: Object): Promise<any> {
143
+ set(data: Object, callback?: any): Promise<any> {
144
+ return withCallback(this._set(data), callback)
145
+ }
146
+
147
+ private async _set(data: Object): Promise<any> {
136
148
  if (!this.id) {
137
149
  return processReturn(this._db.config.throwOnCode, {
138
150
  ...ERRORS.INVALID_PARAM,
@@ -226,7 +238,11 @@ export class DocumentReference {
226
238
  * @param data - 文档数据
227
239
  * @param opts - 可选项
228
240
  */
229
- async update(data: Object): Promise<any> {
241
+ update(data: Object, callback?: any): Promise<any> {
242
+ return withCallback(this._update(data), callback)
243
+ }
244
+
245
+ private async _update(data: Object): Promise<any> {
230
246
  if (!data || typeof data !== 'object') {
231
247
  return processReturn(this._db.config.throwOnCode, {
232
248
  ...ERRORS.INVALID_PARAM,
@@ -277,14 +293,18 @@ export class DocumentReference {
277
293
  /**
278
294
  * 删除文档(事务兼容delete接口)
279
295
  */
280
- async delete(): Promise<any> {
281
- return this.remove()
296
+ delete(callback?: any): Promise<any> {
297
+ return this.remove(callback)
282
298
  }
283
299
 
284
300
  /**
285
301
  * 删除文档
286
302
  */
287
- async remove(): Promise<any> {
303
+ remove(callback?: any): Promise<any> {
304
+ return withCallback(this._remove(), callback)
305
+ }
306
+
307
+ private async _remove(): Promise<any> {
288
308
  const query = stringifyByEJSON({ _id: this.id })
289
309
  const param = {
290
310
  collectionName: this._coll,
@@ -317,7 +337,11 @@ export class DocumentReference {
317
337
  /**
318
338
  * 返回选中的文档(_id)
319
339
  */
320
- async get(): Promise<any> {
340
+ get(callback?: any): Promise<any> {
341
+ return withCallback(this._get(), callback)
342
+ }
343
+
344
+ private async _get(): Promise<any> {
321
345
  const query = stringifyByEJSON({ _id: this.id })
322
346
  const { projection } = this._apiOptions as QueryOption
323
347
  const param: any = {
@@ -327,7 +351,6 @@ export class DocumentReference {
327
351
  queryType: QueryType.DOC,
328
352
  multi: false
329
353
  }
330
-
331
354
  if (projection) {
332
355
  param.projection = stringifyByEJSON(projection)
333
356
  }
@@ -387,20 +410,9 @@ export class DocumentReference {
387
410
  * 监听单个文档
388
411
  */
389
412
  watch = (options: IWatchOptions): DBRealtimeListener => {
390
- if (!Db.ws) {
391
- Db.ws = new RealtimeWebSocketClient({
392
- context: {
393
- appConfig: {
394
- docSizeLimit: 1000,
395
- realtimePingInterval: 10000,
396
- realtimePongWaitTimeout: 5000,
397
- request: this.request
398
- }
399
- }
400
- })
401
- }
413
+ const ws = getWsInstance(this._db)
402
414
 
403
- return (Db.ws as RealtimeWebSocketClient).watch({
415
+ return ws.watch({
404
416
  ...options,
405
417
  envId: this._db.config.env,
406
418
  collectionName: this._coll,
@@ -409,6 +421,5 @@ export class DocumentReference {
409
421
  _id: this.id
410
422
  })
411
423
  })
412
- // })
413
424
  }
414
425
  }