@cloudbase/js-sdk
Version:
cloudbase javascript sdk
258 lines (257 loc) • 8.4 kB
JavaScript
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 { Db } from './index';
import { Util } from './util';
import { UpdateSerializer } from './serializer/update';
import { serialize } from './serializer/datatype';
import { UpdateCommand } from './commands/update';
import { QueryType } from './constant';
import { getWsInstance } from './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 = 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 = Db.createRequest(this._db.config);
this.projection = projection;
}
DocumentReference.prototype.create = function (data, callback) {
callback = callback || createPromiseCallback();
var isBatch = Array.isArray(data);
var params = {
collectionName: this._coll,
data: isBatch ? data.map(function (item) { return serialize(item); }) : 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 || 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 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: QueryType.DOC,
data: 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 || 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: UpdateSerializer.encode(data),
query: query,
queryType: 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 || createPromiseCallback();
var query = { _id: this.id };
var param = {
collectionName: this._coll,
query: query,
queryType: 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 || createPromiseCallback();
var query = { _id: this.id };
var param = {
collectionName: this._coll,
query: query,
queryType: 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.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;
}());
export { DocumentReference };