@cloudbase/database 0.11.0 → 0.12.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 (59) hide show
  1. package/dist/commonjs/transaction/query.d.ts +39 -1
  2. package/dist/commonjs/transaction/query.js +251 -1
  3. package/dist/esm/transaction/query.d.ts +39 -1
  4. package/dist/esm/transaction/query.js +251 -1
  5. package/package.json +1 -1
  6. package/src/transaction/query.ts +355 -2
  7. package/tests/transaction-query.test.ts +657 -0
  8. package/coverage/lcov-report/base.css +0 -224
  9. package/coverage/lcov-report/block-navigation.js +0 -87
  10. package/coverage/lcov-report/favicon.png +0 -0
  11. package/coverage/lcov-report/index.html +0 -101
  12. package/coverage/lcov-report/prettify.css +0 -1
  13. package/coverage/lcov-report/prettify.js +0 -2
  14. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/coverage/lcov-report/sorter.js +0 -210
  16. package/coverage/lcov.info +0 -0
  17. package/dist/esm/aggregate.d.ts.map +0 -1
  18. package/dist/esm/collection.d.ts.map +0 -1
  19. package/dist/esm/command.d.ts.map +0 -1
  20. package/dist/esm/commands/logic.d.ts.map +0 -1
  21. package/dist/esm/commands/query.d.ts.map +0 -1
  22. package/dist/esm/commands/update.d.ts.map +0 -1
  23. package/dist/esm/config/app.config.d.ts.map +0 -1
  24. package/dist/esm/config/error.config.d.ts.map +0 -1
  25. package/dist/esm/const/code.d.ts.map +0 -1
  26. package/dist/esm/constant.d.ts.map +0 -1
  27. package/dist/esm/document.d.ts.map +0 -1
  28. package/dist/esm/geo/index.d.ts.map +0 -1
  29. package/dist/esm/geo/interface.d.ts.map +0 -1
  30. package/dist/esm/geo/lineString.d.ts.map +0 -1
  31. package/dist/esm/geo/multiLineString.d.ts.map +0 -1
  32. package/dist/esm/geo/multiPoint.d.ts.map +0 -1
  33. package/dist/esm/geo/multiPolygon.d.ts.map +0 -1
  34. package/dist/esm/geo/point.d.ts.map +0 -1
  35. package/dist/esm/geo/polygon.d.ts.map +0 -1
  36. package/dist/esm/helper/symbol.d.ts.map +0 -1
  37. package/dist/esm/index.d.ts.map +0 -1
  38. package/dist/esm/lib/util.d.ts.map +0 -1
  39. package/dist/esm/operator-map.d.ts.map +0 -1
  40. package/dist/esm/query.d.ts.map +0 -1
  41. package/dist/esm/regexp/index.d.ts.map +0 -1
  42. package/dist/esm/serializer/common.d.ts.map +0 -1
  43. package/dist/esm/serializer/datatype.d.ts.map +0 -1
  44. package/dist/esm/serializer/query.d.ts.map +0 -1
  45. package/dist/esm/serializer/update.d.ts.map +0 -1
  46. package/dist/esm/serverDate/index.d.ts.map +0 -1
  47. package/dist/esm/transaction/collection.d.ts.map +0 -1
  48. package/dist/esm/transaction/document.d.ts.map +0 -1
  49. package/dist/esm/transaction/index.d.ts.map +0 -1
  50. package/dist/esm/transaction/query.d.ts.map +0 -1
  51. package/dist/esm/typings/index.d.ts.map +0 -1
  52. package/dist/esm/typings/realtime.d.ts.map +0 -1
  53. package/dist/esm/util.d.ts.map +0 -1
  54. package/dist/esm/utils/error.d.ts.map +0 -1
  55. package/dist/esm/utils/msg.d.ts.map +0 -1
  56. package/dist/esm/utils/symbol.d.ts.map +0 -1
  57. package/dist/esm/utils/type.d.ts.map +0 -1
  58. package/dist/esm/utils/utils.d.ts.map +0 -1
  59. package/dist/esm/validate.d.ts.map +0 -1
@@ -1,5 +1,43 @@
1
1
  import { Transaction } from './index';
2
+ import { OrderByDirection } from '../constant';
3
+ interface QueryOrder {
4
+ field?: string;
5
+ direction?: 'asc' | 'desc';
6
+ }
7
+ interface QueryOption {
8
+ limit?: number;
9
+ offset?: number;
10
+ projection?: Object;
11
+ }
12
+ interface GetResult {
13
+ data: any[];
14
+ requestId: string;
15
+ total?: number;
16
+ limit?: number;
17
+ offset?: number;
18
+ }
19
+ interface UpdateResult {
20
+ requestId: string;
21
+ updated: number;
22
+ }
23
+ interface DeleteResult {
24
+ requestId: string;
25
+ deleted: number | string;
26
+ }
2
27
  export declare class Query {
3
28
  protected _transaction: Transaction;
4
- constructor(transaction: Transaction, coll: string);
29
+ constructor(transaction: Transaction, coll: string, fieldFilters?: Object, fieldOrders?: QueryOrder[], queryOptions?: QueryOption);
30
+ where(query: object): Query;
31
+ orderBy(fieldPath: string, directionStr: OrderByDirection): Query;
32
+ limit(limit: number): Query;
33
+ skip(offset: number): Query;
34
+ field(projection: any): Query;
35
+ get(): Promise<GetResult>;
36
+ count(): Promise<{
37
+ requestId: string;
38
+ total: number;
39
+ }>;
40
+ update(data: Object): Promise<UpdateResult>;
41
+ remove(): Promise<DeleteResult>;
5
42
  }
43
+ export {};
@@ -1,11 +1,261 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
2
49
  Object.defineProperty(exports, "__esModule", { value: true });
3
50
  exports.Query = void 0;
51
+ var bson_1 = require("bson");
52
+ var constant_1 = require("../constant");
53
+ var validate_1 = require("../validate");
54
+ var query_1 = require("../serializer/query");
55
+ var update_1 = require("../serializer/update");
56
+ var util_1 = require("../util");
57
+ var GET_DOC = 'database.getInTransaction';
58
+ var UPDATE_DOC = 'database.updateDocInTransaction';
59
+ var DELETE_DOC = 'database.deleteDocInTransaction';
4
60
  var Query = (function () {
5
- function Query(transaction, coll) {
61
+ function Query(transaction, coll, fieldFilters, fieldOrders, queryOptions) {
6
62
  this._coll = coll;
7
63
  this._transaction = transaction;
64
+ this._fieldFilters = fieldFilters;
65
+ this._fieldOrders = fieldOrders || [];
66
+ this._queryOptions = queryOptions || {};
67
+ this._request = this._transaction.getRequestMethod();
68
+ this._transactionId = this._transaction.getTransactionId();
8
69
  }
70
+ Query.prototype.where = function (query) {
71
+ if (Object.prototype.toString.call(query).slice(8, -1) !== 'Object') {
72
+ throw Error(constant_1.ErrorCode.QueryParamTypeError);
73
+ }
74
+ var keys = Object.keys(query);
75
+ var checkFlag = keys.some(function (item) {
76
+ return query[item] !== undefined;
77
+ });
78
+ if (keys.length && !checkFlag) {
79
+ throw Error(constant_1.ErrorCode.QueryParamValueError);
80
+ }
81
+ return new Query(this._transaction, this._coll, query_1.QuerySerializer.encode(query), this._fieldOrders, this._queryOptions);
82
+ };
83
+ Query.prototype.orderBy = function (fieldPath, directionStr) {
84
+ validate_1.Validate.isFieldPath(fieldPath);
85
+ validate_1.Validate.isFieldOrder(directionStr);
86
+ var newOrder = {
87
+ field: fieldPath,
88
+ direction: directionStr
89
+ };
90
+ var combinedOrders = this._fieldOrders.concat(newOrder);
91
+ return new Query(this._transaction, this._coll, this._fieldFilters, combinedOrders, this._queryOptions);
92
+ };
93
+ Query.prototype.limit = function (limit) {
94
+ validate_1.Validate.isInteger('limit', limit);
95
+ var option = __assign({}, this._queryOptions);
96
+ option.limit = limit;
97
+ return new Query(this._transaction, this._coll, this._fieldFilters, this._fieldOrders, option);
98
+ };
99
+ Query.prototype.skip = function (offset) {
100
+ validate_1.Validate.isInteger('offset', offset);
101
+ var option = __assign({}, this._queryOptions);
102
+ option.offset = offset;
103
+ return new Query(this._transaction, this._coll, this._fieldFilters, this._fieldOrders, option);
104
+ };
105
+ Query.prototype.field = function (projection) {
106
+ for (var k in projection) {
107
+ if (projection[k]) {
108
+ if (typeof projection[k] !== 'object') {
109
+ projection[k] = 1;
110
+ }
111
+ }
112
+ else {
113
+ projection[k] = 0;
114
+ }
115
+ }
116
+ var option = __assign({}, this._queryOptions);
117
+ option.projection = projection;
118
+ return new Query(this._transaction, this._coll, this._fieldFilters, this._fieldOrders, option);
119
+ };
120
+ Query.prototype.get = function () {
121
+ return __awaiter(this, void 0, void 0, function () {
122
+ var newOrder, param, res, data, documents, result;
123
+ return __generator(this, function (_a) {
124
+ switch (_a.label) {
125
+ case 0:
126
+ newOrder = [];
127
+ if (this._fieldOrders) {
128
+ this._fieldOrders.forEach(function (order) {
129
+ newOrder.push(order);
130
+ });
131
+ }
132
+ param = {
133
+ collectionName: this._coll,
134
+ transactionId: this._transactionId
135
+ };
136
+ if (this._fieldFilters) {
137
+ param.query = this._fieldFilters;
138
+ }
139
+ if (newOrder.length > 0) {
140
+ param.order = newOrder;
141
+ }
142
+ if (this._queryOptions.offset) {
143
+ param.offset = this._queryOptions.offset;
144
+ }
145
+ if (this._queryOptions.limit) {
146
+ param.limit = this._queryOptions.limit < 1000 ? this._queryOptions.limit : 1000;
147
+ }
148
+ else {
149
+ param.limit = 100;
150
+ }
151
+ if (this._queryOptions.projection) {
152
+ param.projection = this._queryOptions.projection;
153
+ }
154
+ return [4, this._request.send(GET_DOC, param)];
155
+ case 1:
156
+ res = _a.sent();
157
+ if (res.code)
158
+ throw res;
159
+ data = res.data !== 'null' ? bson_1.EJSON.parse(res.data) : null;
160
+ documents = data ? (Array.isArray(data) ? util_1.Util.formatResDocumentData(data) : [util_1.Util.formatField(data)]) : [];
161
+ result = {
162
+ data: documents,
163
+ requestId: res.requestId
164
+ };
165
+ if (res.total !== undefined)
166
+ result.total = res.total;
167
+ if (res.limit !== undefined)
168
+ result.limit = res.limit;
169
+ if (res.offset !== undefined)
170
+ result.offset = res.offset;
171
+ return [2, result];
172
+ }
173
+ });
174
+ });
175
+ };
176
+ Query.prototype.count = function () {
177
+ return __awaiter(this, void 0, void 0, function () {
178
+ var param, res, data, total;
179
+ return __generator(this, function (_a) {
180
+ switch (_a.label) {
181
+ case 0:
182
+ param = {
183
+ collectionName: this._coll,
184
+ transactionId: this._transactionId
185
+ };
186
+ if (this._fieldFilters) {
187
+ param.query = this._fieldFilters;
188
+ }
189
+ return [4, this._request.send(GET_DOC, param)];
190
+ case 1:
191
+ res = _a.sent();
192
+ if (res.code)
193
+ throw res;
194
+ data = res.data !== 'null' ? bson_1.EJSON.parse(res.data) : null;
195
+ total = Array.isArray(data) ? data.length : (data ? 1 : 0);
196
+ return [2, {
197
+ requestId: res.requestId,
198
+ total: res.total !== undefined ? res.total : total
199
+ }];
200
+ }
201
+ });
202
+ });
203
+ };
204
+ Query.prototype.update = function (data) {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ var param, res;
207
+ return __generator(this, function (_a) {
208
+ switch (_a.label) {
209
+ case 0:
210
+ if (!data || typeof data !== 'object') {
211
+ throw new Error('参数必需是非空对象');
212
+ }
213
+ if (data.hasOwnProperty('_id')) {
214
+ throw new Error('不能更新_id的值');
215
+ }
216
+ param = {
217
+ collectionName: this._coll,
218
+ transactionId: this._transactionId,
219
+ query: this._fieldFilters,
220
+ data: bson_1.EJSON.stringify(update_1.UpdateSerializer.encode(data), { relaxed: false })
221
+ };
222
+ return [4, this._request.send(UPDATE_DOC, param)];
223
+ case 1:
224
+ res = _a.sent();
225
+ if (res.code)
226
+ throw res;
227
+ return [2, __assign(__assign({}, res), { updated: bson_1.EJSON.parse(res.updated) })];
228
+ }
229
+ });
230
+ });
231
+ };
232
+ Query.prototype.remove = function () {
233
+ return __awaiter(this, void 0, void 0, function () {
234
+ var param, res;
235
+ return __generator(this, function (_a) {
236
+ switch (_a.label) {
237
+ case 0:
238
+ if (Object.keys(this._queryOptions).length > 0) {
239
+ console.warn('`offset`, `limit` and `projection` are not supported in remove() operation');
240
+ }
241
+ if (this._fieldOrders.length > 0) {
242
+ console.warn('`orderBy` is not supported in remove() operation');
243
+ }
244
+ param = {
245
+ collectionName: this._coll,
246
+ transactionId: this._transactionId,
247
+ query: this._fieldFilters
248
+ };
249
+ return [4, this._request.send(DELETE_DOC, param)];
250
+ case 1:
251
+ res = _a.sent();
252
+ if (res.code)
253
+ throw res;
254
+ return [2, __assign(__assign({}, res), { deleted: bson_1.EJSON.parse(res.deleted) })];
255
+ }
256
+ });
257
+ });
258
+ };
9
259
  return Query;
10
260
  }());
11
261
  exports.Query = Query;
@@ -1,5 +1,43 @@
1
1
  import { Transaction } from './index';
2
+ import { OrderByDirection } from '../constant';
3
+ interface QueryOrder {
4
+ field?: string;
5
+ direction?: 'asc' | 'desc';
6
+ }
7
+ interface QueryOption {
8
+ limit?: number;
9
+ offset?: number;
10
+ projection?: Object;
11
+ }
12
+ interface GetResult {
13
+ data: any[];
14
+ requestId: string;
15
+ total?: number;
16
+ limit?: number;
17
+ offset?: number;
18
+ }
19
+ interface UpdateResult {
20
+ requestId: string;
21
+ updated: number;
22
+ }
23
+ interface DeleteResult {
24
+ requestId: string;
25
+ deleted: number | string;
26
+ }
2
27
  export declare class Query {
3
28
  protected _transaction: Transaction;
4
- constructor(transaction: Transaction, coll: string);
29
+ constructor(transaction: Transaction, coll: string, fieldFilters?: Object, fieldOrders?: QueryOrder[], queryOptions?: QueryOption);
30
+ where(query: object): Query;
31
+ orderBy(fieldPath: string, directionStr: OrderByDirection): Query;
32
+ limit(limit: number): Query;
33
+ skip(offset: number): Query;
34
+ field(projection: any): Query;
35
+ get(): Promise<GetResult>;
36
+ count(): Promise<{
37
+ requestId: string;
38
+ total: number;
39
+ }>;
40
+ update(data: Object): Promise<UpdateResult>;
41
+ remove(): Promise<DeleteResult>;
5
42
  }
43
+ export {};
@@ -1,8 +1,258 @@
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
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
+ 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;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ import { EJSON } from 'bson';
49
+ import { ErrorCode } from '../constant';
50
+ import { Validate } from '../validate';
51
+ import { QuerySerializer } from '../serializer/query';
52
+ import { UpdateSerializer } from '../serializer/update';
53
+ import { Util } from '../util';
54
+ var GET_DOC = 'database.getInTransaction';
55
+ var UPDATE_DOC = 'database.updateDocInTransaction';
56
+ var DELETE_DOC = 'database.deleteDocInTransaction';
1
57
  var Query = (function () {
2
- function Query(transaction, coll) {
58
+ function Query(transaction, coll, fieldFilters, fieldOrders, queryOptions) {
3
59
  this._coll = coll;
4
60
  this._transaction = transaction;
61
+ this._fieldFilters = fieldFilters;
62
+ this._fieldOrders = fieldOrders || [];
63
+ this._queryOptions = queryOptions || {};
64
+ this._request = this._transaction.getRequestMethod();
65
+ this._transactionId = this._transaction.getTransactionId();
5
66
  }
67
+ Query.prototype.where = function (query) {
68
+ if (Object.prototype.toString.call(query).slice(8, -1) !== 'Object') {
69
+ throw Error(ErrorCode.QueryParamTypeError);
70
+ }
71
+ var keys = Object.keys(query);
72
+ var checkFlag = keys.some(function (item) {
73
+ return query[item] !== undefined;
74
+ });
75
+ if (keys.length && !checkFlag) {
76
+ throw Error(ErrorCode.QueryParamValueError);
77
+ }
78
+ return new Query(this._transaction, this._coll, QuerySerializer.encode(query), this._fieldOrders, this._queryOptions);
79
+ };
80
+ Query.prototype.orderBy = function (fieldPath, directionStr) {
81
+ Validate.isFieldPath(fieldPath);
82
+ Validate.isFieldOrder(directionStr);
83
+ var newOrder = {
84
+ field: fieldPath,
85
+ direction: directionStr
86
+ };
87
+ var combinedOrders = this._fieldOrders.concat(newOrder);
88
+ return new Query(this._transaction, this._coll, this._fieldFilters, combinedOrders, this._queryOptions);
89
+ };
90
+ Query.prototype.limit = function (limit) {
91
+ Validate.isInteger('limit', limit);
92
+ var option = __assign({}, this._queryOptions);
93
+ option.limit = limit;
94
+ return new Query(this._transaction, this._coll, this._fieldFilters, this._fieldOrders, option);
95
+ };
96
+ Query.prototype.skip = function (offset) {
97
+ Validate.isInteger('offset', offset);
98
+ var option = __assign({}, this._queryOptions);
99
+ option.offset = offset;
100
+ return new Query(this._transaction, this._coll, this._fieldFilters, this._fieldOrders, option);
101
+ };
102
+ Query.prototype.field = function (projection) {
103
+ for (var k in projection) {
104
+ if (projection[k]) {
105
+ if (typeof projection[k] !== 'object') {
106
+ projection[k] = 1;
107
+ }
108
+ }
109
+ else {
110
+ projection[k] = 0;
111
+ }
112
+ }
113
+ var option = __assign({}, this._queryOptions);
114
+ option.projection = projection;
115
+ return new Query(this._transaction, this._coll, this._fieldFilters, this._fieldOrders, option);
116
+ };
117
+ Query.prototype.get = function () {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var newOrder, param, res, data, documents, result;
120
+ return __generator(this, function (_a) {
121
+ switch (_a.label) {
122
+ case 0:
123
+ newOrder = [];
124
+ if (this._fieldOrders) {
125
+ this._fieldOrders.forEach(function (order) {
126
+ newOrder.push(order);
127
+ });
128
+ }
129
+ param = {
130
+ collectionName: this._coll,
131
+ transactionId: this._transactionId
132
+ };
133
+ if (this._fieldFilters) {
134
+ param.query = this._fieldFilters;
135
+ }
136
+ if (newOrder.length > 0) {
137
+ param.order = newOrder;
138
+ }
139
+ if (this._queryOptions.offset) {
140
+ param.offset = this._queryOptions.offset;
141
+ }
142
+ if (this._queryOptions.limit) {
143
+ param.limit = this._queryOptions.limit < 1000 ? this._queryOptions.limit : 1000;
144
+ }
145
+ else {
146
+ param.limit = 100;
147
+ }
148
+ if (this._queryOptions.projection) {
149
+ param.projection = this._queryOptions.projection;
150
+ }
151
+ return [4, this._request.send(GET_DOC, param)];
152
+ case 1:
153
+ res = _a.sent();
154
+ if (res.code)
155
+ throw res;
156
+ data = res.data !== 'null' ? EJSON.parse(res.data) : null;
157
+ documents = data ? (Array.isArray(data) ? Util.formatResDocumentData(data) : [Util.formatField(data)]) : [];
158
+ result = {
159
+ data: documents,
160
+ requestId: res.requestId
161
+ };
162
+ if (res.total !== undefined)
163
+ result.total = res.total;
164
+ if (res.limit !== undefined)
165
+ result.limit = res.limit;
166
+ if (res.offset !== undefined)
167
+ result.offset = res.offset;
168
+ return [2, result];
169
+ }
170
+ });
171
+ });
172
+ };
173
+ Query.prototype.count = function () {
174
+ return __awaiter(this, void 0, void 0, function () {
175
+ var param, res, data, total;
176
+ return __generator(this, function (_a) {
177
+ switch (_a.label) {
178
+ case 0:
179
+ param = {
180
+ collectionName: this._coll,
181
+ transactionId: this._transactionId
182
+ };
183
+ if (this._fieldFilters) {
184
+ param.query = this._fieldFilters;
185
+ }
186
+ return [4, this._request.send(GET_DOC, param)];
187
+ case 1:
188
+ res = _a.sent();
189
+ if (res.code)
190
+ throw res;
191
+ data = res.data !== 'null' ? EJSON.parse(res.data) : null;
192
+ total = Array.isArray(data) ? data.length : (data ? 1 : 0);
193
+ return [2, {
194
+ requestId: res.requestId,
195
+ total: res.total !== undefined ? res.total : total
196
+ }];
197
+ }
198
+ });
199
+ });
200
+ };
201
+ Query.prototype.update = function (data) {
202
+ return __awaiter(this, void 0, void 0, function () {
203
+ var param, res;
204
+ return __generator(this, function (_a) {
205
+ switch (_a.label) {
206
+ case 0:
207
+ if (!data || typeof data !== 'object') {
208
+ throw new Error('参数必需是非空对象');
209
+ }
210
+ if (data.hasOwnProperty('_id')) {
211
+ throw new Error('不能更新_id的值');
212
+ }
213
+ param = {
214
+ collectionName: this._coll,
215
+ transactionId: this._transactionId,
216
+ query: this._fieldFilters,
217
+ data: EJSON.stringify(UpdateSerializer.encode(data), { relaxed: false })
218
+ };
219
+ return [4, this._request.send(UPDATE_DOC, param)];
220
+ case 1:
221
+ res = _a.sent();
222
+ if (res.code)
223
+ throw res;
224
+ return [2, __assign(__assign({}, res), { updated: EJSON.parse(res.updated) })];
225
+ }
226
+ });
227
+ });
228
+ };
229
+ Query.prototype.remove = function () {
230
+ return __awaiter(this, void 0, void 0, function () {
231
+ var param, res;
232
+ return __generator(this, function (_a) {
233
+ switch (_a.label) {
234
+ case 0:
235
+ if (Object.keys(this._queryOptions).length > 0) {
236
+ console.warn('`offset`, `limit` and `projection` are not supported in remove() operation');
237
+ }
238
+ if (this._fieldOrders.length > 0) {
239
+ console.warn('`orderBy` is not supported in remove() operation');
240
+ }
241
+ param = {
242
+ collectionName: this._coll,
243
+ transactionId: this._transactionId,
244
+ query: this._fieldFilters
245
+ };
246
+ return [4, this._request.send(DELETE_DOC, param)];
247
+ case 1:
248
+ res = _a.sent();
249
+ if (res.code)
250
+ throw res;
251
+ return [2, __assign(__assign({}, res), { deleted: EJSON.parse(res.deleted) })];
252
+ }
253
+ });
254
+ });
255
+ };
6
256
  return Query;
7
257
  }());
8
258
  export { Query };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/database",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "database for (node.)js sdk ",
5
5
  "main": "dist/commonjs/index.js",
6
6
  "module": "dist/esm/index.js",