UNPKG

@cloudbase/js-sdk

Version:
250 lines (249 loc) 8.89 kB
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); }; import { createPromiseCallback } from './lib/util'; import { QueryType } from './constant'; import { Db } from './index'; import { Validate } from './validate'; import { Util } from './util'; import { QuerySerializer } from './serializer/query'; import { UpdateSerializer } from './serializer/update'; import { ErrorCode } from './constant'; import { getWsInstance } from './utils/utils'; var Query = (function () { function Query(db, coll, fieldFilters, fieldOrders, queryOptions) { var _this = this; this.watch = function (options) { var ws = 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 = Db.createRequest(this._db.config); } Query.prototype.get = function (callback) { callback = callback || createPromiseCallback(); var newOder = []; if (this._fieldOrders) { this._fieldOrders.forEach(function (order) { newOder.push(order); }); } var param = { collectionName: this._coll, queryType: 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.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 || createPromiseCallback(); var param = { collectionName: this._coll, queryType: 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(ErrorCode.QueryParamTypeError); } var keys = Object.keys(query); var checkFlag = keys.some(function (item) { return query[item] !== undefined; }); if (keys.length && !checkFlag) { throw Error(ErrorCode.QueryParamValueError); } return new Query(this._db, this._coll, QuerySerializer.encode(query), this._fieldOrders, this._queryOptions); }; Query.prototype.orderBy = function (fieldPath, directionStr) { Validate.isFieldPath(fieldPath); 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.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.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 || 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: QueryType.WHERE, multi: true, merge: true, upsert: false, data: 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 || 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: QuerySerializer.encode(this._fieldFilters), queryType: 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; }()); export { Query };