@cloudbase/database 0.11.0 → 0.12.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.
- package/dist/commonjs/collection.d.ts +1 -1
- package/dist/commonjs/document.js +16 -6
- package/dist/commonjs/transaction/query.d.ts +39 -1
- package/dist/commonjs/transaction/query.js +251 -1
- package/dist/esm/collection.d.ts +1 -1
- package/dist/esm/document.js +16 -6
- package/dist/esm/transaction/query.d.ts +39 -1
- package/dist/esm/transaction/query.js +251 -1
- package/package.json +1 -1
- package/src/collection.ts +4 -3
- package/src/document.ts +17 -9
- package/src/transaction/query.ts +355 -2
- package/tests/transaction-query.test.ts +657 -0
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -101
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -210
- package/coverage/lcov.info +0 -0
- package/dist/esm/aggregate.d.ts.map +0 -1
- package/dist/esm/collection.d.ts.map +0 -1
- package/dist/esm/command.d.ts.map +0 -1
- package/dist/esm/commands/logic.d.ts.map +0 -1
- package/dist/esm/commands/query.d.ts.map +0 -1
- package/dist/esm/commands/update.d.ts.map +0 -1
- package/dist/esm/config/app.config.d.ts.map +0 -1
- package/dist/esm/config/error.config.d.ts.map +0 -1
- package/dist/esm/const/code.d.ts.map +0 -1
- package/dist/esm/constant.d.ts.map +0 -1
- package/dist/esm/document.d.ts.map +0 -1
- package/dist/esm/geo/index.d.ts.map +0 -1
- package/dist/esm/geo/interface.d.ts.map +0 -1
- package/dist/esm/geo/lineString.d.ts.map +0 -1
- package/dist/esm/geo/multiLineString.d.ts.map +0 -1
- package/dist/esm/geo/multiPoint.d.ts.map +0 -1
- package/dist/esm/geo/multiPolygon.d.ts.map +0 -1
- package/dist/esm/geo/point.d.ts.map +0 -1
- package/dist/esm/geo/polygon.d.ts.map +0 -1
- package/dist/esm/helper/symbol.d.ts.map +0 -1
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/lib/util.d.ts.map +0 -1
- package/dist/esm/operator-map.d.ts.map +0 -1
- package/dist/esm/query.d.ts.map +0 -1
- package/dist/esm/regexp/index.d.ts.map +0 -1
- package/dist/esm/serializer/common.d.ts.map +0 -1
- package/dist/esm/serializer/datatype.d.ts.map +0 -1
- package/dist/esm/serializer/query.d.ts.map +0 -1
- package/dist/esm/serializer/update.d.ts.map +0 -1
- package/dist/esm/serverDate/index.d.ts.map +0 -1
- package/dist/esm/transaction/collection.d.ts.map +0 -1
- package/dist/esm/transaction/document.d.ts.map +0 -1
- package/dist/esm/transaction/index.d.ts.map +0 -1
- package/dist/esm/transaction/query.d.ts.map +0 -1
- package/dist/esm/typings/index.d.ts.map +0 -1
- package/dist/esm/typings/realtime.d.ts.map +0 -1
- package/dist/esm/util.d.ts.map +0 -1
- package/dist/esm/utils/error.d.ts.map +0 -1
- package/dist/esm/utils/msg.d.ts.map +0 -1
- package/dist/esm/utils/symbol.d.ts.map +0 -1
- package/dist/esm/utils/type.d.ts.map +0 -1
- package/dist/esm/utils/utils.d.ts.map +0 -1
- package/dist/esm/validate.d.ts.map +0 -1
|
@@ -4,6 +4,6 @@ import Aggregation from './aggregate';
|
|
|
4
4
|
export declare class CollectionReference extends Query {
|
|
5
5
|
get name(): string;
|
|
6
6
|
doc(docID?: string | number): DocumentReference;
|
|
7
|
-
add(data: Object, callback?: any): Promise<any>;
|
|
7
|
+
add(data: Object | Object[], callback?: any): Promise<any>;
|
|
8
8
|
aggregate(): Aggregation;
|
|
9
9
|
}
|
|
@@ -38,24 +38,34 @@ var DocumentReference = (function () {
|
|
|
38
38
|
}
|
|
39
39
|
DocumentReference.prototype.create = function (data, callback) {
|
|
40
40
|
callback = callback || (0, util_1.createPromiseCallback)();
|
|
41
|
+
var isBatch = Array.isArray(data);
|
|
41
42
|
var params = {
|
|
42
43
|
collectionName: this._coll,
|
|
43
|
-
data: (0, datatype_1.serialize)(data)
|
|
44
|
+
data: isBatch ? data.map(function (item) { return (0, datatype_1.serialize)(item); }) : (0, datatype_1.serialize)(data)
|
|
44
45
|
};
|
|
45
|
-
if (this.id) {
|
|
46
|
+
if (!isBatch && this.id) {
|
|
46
47
|
params['_id'] = this.id;
|
|
47
48
|
}
|
|
48
49
|
this.request
|
|
49
50
|
.send('database.addDocument', params)
|
|
50
51
|
.then(function (res) {
|
|
52
|
+
var _a;
|
|
51
53
|
if (res.code) {
|
|
52
54
|
callback(0, res);
|
|
53
55
|
}
|
|
54
56
|
else {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
if (isBatch) {
|
|
58
|
+
callback(0, {
|
|
59
|
+
ids: ((_a = res.data) === null || _a === void 0 ? void 0 : _a.insertedIds) || [],
|
|
60
|
+
requestId: res.requestId
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
callback(0, {
|
|
65
|
+
id: res.data._id,
|
|
66
|
+
requestId: res.requestId
|
|
67
|
+
});
|
|
68
|
+
}
|
|
59
69
|
}
|
|
60
70
|
})
|
|
61
71
|
.catch(function (err) {
|
|
@@ -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;
|
package/dist/esm/collection.d.ts
CHANGED
|
@@ -4,6 +4,6 @@ import Aggregation from './aggregate';
|
|
|
4
4
|
export declare class CollectionReference extends Query {
|
|
5
5
|
get name(): string;
|
|
6
6
|
doc(docID?: string | number): DocumentReference;
|
|
7
|
-
add(data: Object, callback?: any): Promise<any>;
|
|
7
|
+
add(data: Object | Object[], callback?: any): Promise<any>;
|
|
8
8
|
aggregate(): Aggregation;
|
|
9
9
|
}
|
package/dist/esm/document.js
CHANGED
|
@@ -35,24 +35,34 @@ var DocumentReference = (function () {
|
|
|
35
35
|
}
|
|
36
36
|
DocumentReference.prototype.create = function (data, callback) {
|
|
37
37
|
callback = callback || createPromiseCallback();
|
|
38
|
+
var isBatch = Array.isArray(data);
|
|
38
39
|
var params = {
|
|
39
40
|
collectionName: this._coll,
|
|
40
|
-
data: serialize(data)
|
|
41
|
+
data: isBatch ? data.map(function (item) { return serialize(item); }) : serialize(data)
|
|
41
42
|
};
|
|
42
|
-
if (this.id) {
|
|
43
|
+
if (!isBatch && this.id) {
|
|
43
44
|
params['_id'] = this.id;
|
|
44
45
|
}
|
|
45
46
|
this.request
|
|
46
47
|
.send('database.addDocument', params)
|
|
47
48
|
.then(function (res) {
|
|
49
|
+
var _a;
|
|
48
50
|
if (res.code) {
|
|
49
51
|
callback(0, res);
|
|
50
52
|
}
|
|
51
53
|
else {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
if (isBatch) {
|
|
55
|
+
callback(0, {
|
|
56
|
+
ids: ((_a = res.data) === null || _a === void 0 ? void 0 : _a.insertedIds) || [],
|
|
57
|
+
requestId: res.requestId
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
callback(0, {
|
|
62
|
+
id: res.data._id,
|
|
63
|
+
requestId: res.requestId
|
|
64
|
+
});
|
|
65
|
+
}
|
|
56
66
|
}
|
|
57
67
|
})
|
|
58
68
|
.catch(function (err) {
|
|
@@ -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 {};
|