UNPKG

@cloudbase/js-sdk

Version:
261 lines (260 loc) 8.69 kB
"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.DocumentReference = void 0; var util_1 = require("./lib/util"); var index_1 = require("./index"); var util_2 = require("./util"); var update_1 = require("./serializer/update"); var datatype_1 = require("./serializer/datatype"); var update_2 = require("./commands/update"); var constant_1 = require("./constant"); var utils_1 = require("./utils/utils"); var DocumentReference = (function () { function DocumentReference(db, coll, docID, projection) { if (projection === void 0) { projection = {}; } 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({ _id: _this.id }) })); }; this._db = db; this._coll = coll; this.id = docID; this.request = index_1.Db.createRequest(this._db.config); this.projection = projection; } DocumentReference.prototype.create = function (data, callback) { callback = callback || (0, util_1.createPromiseCallback)(); var isBatch = Array.isArray(data); var params = { collectionName: this._coll, data: isBatch ? data.map(function (item) { return (0, datatype_1.serialize)(item); }) : (0, datatype_1.serialize)(data) }; if (!isBatch && this.id) { params['_id'] = this.id; } this.request .send('database.addDocument', params) .then(function (res) { var _a; if (res.code) { callback(0, res); } else { if (isBatch) { callback(0, { ids: ((_a = res.data) === null || _a === void 0 ? void 0 : _a.insertedIds) || [], requestId: res.requestId }); } else { callback(0, { id: res.data._id, requestId: res.requestId }); } } }) .catch(function (err) { callback(err); }); return callback.promise; }; DocumentReference.prototype.set = function (data, callback) { callback = callback || (0, util_1.createPromiseCallback)(); if (!this.id) { return Promise.resolve({ code: 'INVALID_PARAM', message: 'docId不能为空' }); } 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 hasOperator = false; var checkMixed = function (objs) { if (typeof objs === 'object') { for (var key in objs) { if (objs[key] instanceof update_2.UpdateCommand) { hasOperator = true; } else if (typeof objs[key] === 'object') { checkMixed(objs[key]); } } } }; checkMixed(data); if (hasOperator) { return Promise.resolve({ code: 'DATABASE_REQUEST_FAILED', message: 'update operator complicit' }); } var merge = false; var param = { collectionName: this._coll, queryType: constant_1.QueryType.DOC, data: (0, datatype_1.serialize)(data), multi: false, merge: merge, upsert: true }; if (this.id) { param['query'] = { _id: this.id }; } this.request .send('database.updateDocument', param) .then(function (res) { if (res.code) { callback(0, res); } else { callback(0, { updated: res.data.updated, upsertedId: res.data.upserted_id, requestId: res.requestId }); } }) .catch(function (err) { callback(err); }); return callback.promise; }; DocumentReference.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 query = { _id: this.id }; var merge = true; var param = { collectionName: this._coll, data: update_1.UpdateSerializer.encode(data), query: query, queryType: constant_1.QueryType.DOC, multi: false, merge: merge, upsert: false }; this.request .send('database.updateDocument', param) .then(function (res) { if (res.code) { callback(0, res); } else { callback(0, { updated: res.data.updated, upsertedId: res.data.upserted_id, requestId: res.requestId }); } }) .catch(function (err) { callback(err); }); return callback.promise; }; DocumentReference.prototype.remove = function (callback) { callback = callback || (0, util_1.createPromiseCallback)(); var query = { _id: this.id }; var param = { collectionName: this._coll, query: query, queryType: constant_1.QueryType.DOC, multi: false }; this.request .send('database.deleteDocument', param) .then(function (res) { if (res.code) { callback(0, res); } else { callback(0, { deleted: res.data.deleted, requestId: res.requestId }); } }) .catch(function (err) { callback(err); }); return callback.promise; }; DocumentReference.prototype.get = function (callback) { callback = callback || (0, util_1.createPromiseCallback)(); var query = { _id: this.id }; var param = { collectionName: this._coll, query: query, queryType: constant_1.QueryType.DOC, multi: false, projection: this.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); callback(0, { data: documents, requestId: res.requestId }); } }) .catch(function (err) { callback(err); }); return callback.promise; }; DocumentReference.prototype.field = function (projection) { for (var k in projection) { if (projection[k]) { projection[k] = 1; } else { projection[k] = 0; } } return new DocumentReference(this._db, this._coll, this.id, projection); }; return DocumentReference; }()); exports.DocumentReference = DocumentReference;