@cloudbase/js-sdk
Version:
cloudbase javascript sdk
253 lines (252 loc) • 9.17 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Query = void 0;
var util_1 = require("./lib/util");
var constant_1 = require("./constant");
var index_1 = require("./index");
var validate_1 = require("./validate");
var util_2 = require("./util");
var query_1 = require("./serializer/query");
var update_1 = require("./serializer/update");
var constant_2 = require("./constant");
var utils_1 = require("./utils/utils");
var Query = (function () {
function Query(db, coll, fieldFilters, fieldOrders, queryOptions) {
var _this = this;
this.watch = function (options) {
var ws = (0, utils_1.getWsInstance)(_this._db);
return ws.watch(__assign(__assign({}, options), { envId: _this._db.config.env, collectionName: _this._coll, query: JSON.stringify(_this._fieldFilters || {}), limit: _this._queryOptions.limit, orderBy: _this._fieldOrders
? _this._fieldOrders.reduce(function (acc, cur) {
acc[cur.field] = cur.direction;
return acc;
}, {})
: undefined }));
};
this._db = db;
this._coll = coll;
this._fieldFilters = fieldFilters;
this._fieldOrders = fieldOrders || [];
this._queryOptions = queryOptions || {};
this._request = index_1.Db.createRequest(this._db.config);
}
Query.prototype.get = function (callback) {
callback = callback || (0, util_1.createPromiseCallback)();
var newOder = [];
if (this._fieldOrders) {
this._fieldOrders.forEach(function (order) {
newOder.push(order);
});
}
var param = {
collectionName: this._coll,
queryType: constant_1.QueryType.WHERE
};
if (this._fieldFilters) {
param.query = this._fieldFilters;
}
if (newOder.length > 0) {
param.order = newOder;
}
if (this._queryOptions.offset) {
param.offset = this._queryOptions.offset;
}
if (this._queryOptions.limit) {
param.limit = this._queryOptions.limit < 1000 ? this._queryOptions.limit : 1000;
}
else {
param.limit = 100;
}
if (this._queryOptions.projection) {
param.projection = this._queryOptions.projection;
}
this._request
.send('database.queryDocument', param)
.then(function (res) {
if (res.code) {
callback(0, res);
}
else {
var documents = util_2.Util.formatResDocumentData(res.data.list);
var result = {
data: documents,
requestId: res.requestId
};
if (res.total !== undefined)
result.total = res.total;
if (res.limit !== undefined)
result.limit = res.limit;
if (res.offset !== undefined)
result.offset = res.offset;
callback(0, result);
}
})
.catch(function (err) {
callback(err);
});
return callback.promise;
};
Query.prototype.count = function (callback) {
callback = callback || (0, util_1.createPromiseCallback)();
var param = {
collectionName: this._coll,
queryType: constant_1.QueryType.WHERE
};
if (this._fieldFilters) {
param.query = this._fieldFilters;
}
this._request
.send('database.countDocument', param)
.then(function (res) {
if (res.code) {
callback(0, res);
}
else {
callback(0, {
requestId: res.requestId,
total: res.data.total
});
}
})
.catch(function (e) {
callback(e);
});
return callback.promise;
};
Query.prototype.where = function (query) {
if (Object.prototype.toString.call(query).slice(8, -1) !== 'Object') {
throw Error(constant_2.ErrorCode.QueryParamTypeError);
}
var keys = Object.keys(query);
var checkFlag = keys.some(function (item) {
return query[item] !== undefined;
});
if (keys.length && !checkFlag) {
throw Error(constant_2.ErrorCode.QueryParamValueError);
}
return new Query(this._db, this._coll, query_1.QuerySerializer.encode(query), this._fieldOrders, this._queryOptions);
};
Query.prototype.orderBy = function (fieldPath, directionStr) {
validate_1.Validate.isFieldPath(fieldPath);
validate_1.Validate.isFieldOrder(directionStr);
var newOrder = {
field: fieldPath,
direction: directionStr
};
var combinedOrders = this._fieldOrders.concat(newOrder);
return new Query(this._db, this._coll, this._fieldFilters, combinedOrders, this._queryOptions);
};
Query.prototype.limit = function (limit) {
validate_1.Validate.isInteger('limit', limit);
var option = __assign({}, this._queryOptions);
option.limit = limit;
return new Query(this._db, this._coll, this._fieldFilters, this._fieldOrders, option);
};
Query.prototype.skip = function (offset) {
validate_1.Validate.isInteger('offset', offset);
var option = __assign({}, this._queryOptions);
option.offset = offset;
return new Query(this._db, this._coll, this._fieldFilters, this._fieldOrders, option);
};
Query.prototype.update = function (data, callback) {
callback = callback || (0, util_1.createPromiseCallback)();
if (!data || typeof data !== 'object') {
return Promise.resolve({
code: 'INVALID_PARAM',
message: '参数必需是非空对象'
});
}
if (data.hasOwnProperty('_id')) {
return Promise.resolve({
code: 'INVALID_PARAM',
message: '不能更新_id的值'
});
}
var param = {
collectionName: this._coll,
query: this._fieldFilters,
queryType: constant_1.QueryType.WHERE,
multi: true,
merge: true,
upsert: false,
data: update_1.UpdateSerializer.encode(data)
};
this._request
.send('database.updateDocument', param)
.then(function (res) {
if (res.code) {
callback(0, res);
}
else {
callback(0, {
requestId: res.requestId,
updated: res.data.updated,
upsertId: res.data.upsert_id
});
}
})
.catch(function (e) {
callback(e);
});
return callback.promise;
};
Query.prototype.field = function (projection) {
for (var k in projection) {
if (projection[k]) {
if (typeof projection[k] !== 'object') {
projection[k] = 1;
}
}
else {
projection[k] = 0;
}
}
var option = __assign({}, this._queryOptions);
option.projection = projection;
return new Query(this._db, this._coll, this._fieldFilters, this._fieldOrders, option);
};
Query.prototype.remove = function (callback) {
callback = callback || (0, util_1.createPromiseCallback)();
if (Object.keys(this._queryOptions).length > 0) {
console.warn('`offset`, `limit` and `projection` are not supported in remove() operation');
}
if (this._fieldOrders.length > 0) {
console.warn('`orderBy` is not supported in remove() operation');
}
var param = {
collectionName: this._coll,
query: query_1.QuerySerializer.encode(this._fieldFilters),
queryType: constant_1.QueryType.WHERE,
multi: true
};
this._request
.send('database.deleteDocument', param)
.then(function (res) {
if (res.code) {
callback(0, res);
}
else {
callback(0, {
requestId: res.requestId,
deleted: res.data.deleted
});
}
})
.catch(function (e) {
callback(e);
});
return callback.promise;
};
return Query;
}());
exports.Query = Query;