ravendb
Version:
RavenDB client for Node.js
90 lines • 3.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PutCompareExchangeValueCommand = exports.PutCompareExchangeValueOperation = void 0;
const CompareExchangeResult_js_1 = require("./CompareExchangeResult.js");
const RavenCommand_js_1 = require("../../../Http/RavenCommand.js");
const DocumentConventions_js_1 = require("../../Conventions/DocumentConventions.js");
const index_js_1 = require("../../../Exceptions/index.js");
const Serializer_js_1 = require("../../../Mapping/Json/Serializer.js");
const TypeUtil_js_1 = require("../../../Utility/TypeUtil.js");
const RaftIdGenerator_js_1 = require("../../../Utility/RaftIdGenerator.js");
const Constants_js_1 = require("../../../Constants.js");
const CompareExchangeSessionValue_js_1 = require("./CompareExchangeSessionValue.js");
class PutCompareExchangeValueOperation {
_key;
_value;
_index;
_metadata;
constructor(key, value, index, metadata) {
this._key = key;
this._value = value;
this._index = index;
this._metadata = metadata;
}
getCommand(store, conventions, cache) {
return new PutCompareExchangeValueCommand(this._key, this._value, this._index, this._metadata, conventions);
}
get resultType() {
return "CommandResult";
}
}
exports.PutCompareExchangeValueOperation = PutCompareExchangeValueOperation;
class PutCompareExchangeValueCommand extends RavenCommand_js_1.RavenCommand {
_key;
_value;
_index;
_conventions;
_metadata;
constructor(key, value, index, metadata, conventions) {
super();
if (!key) {
(0, index_js_1.throwError)("InvalidArgumentException", "The key argument must have value");
}
if (index < 0) {
(0, index_js_1.throwError)("InvalidArgumentException", "Index must be a non-negative number");
}
this._key = key;
this._value = value;
this._index = index;
this._metadata = metadata;
this._conventions = conventions || DocumentConventions_js_1.DocumentConventions.defaultConventions;
}
get isReadRequest() {
return false;
}
createRequest(node) {
const uri = node.url + "/databases/" + node.database + "/cmpxchg?key=" + encodeURIComponent(this._key) + "&index=" + this._index;
const tuple = {};
tuple[Constants_js_1.COMPARE_EXCHANGE.OBJECT_FIELD_NAME] = TypeUtil_js_1.TypeUtil.isPrimitive(this._value)
? this._value
: this._conventions.transformObjectKeysToRemoteFieldNameConvention(this._value);
if (this._metadata) {
const metadata = CompareExchangeSessionValue_js_1.CompareExchangeSessionValue.prepareMetadataForPut(this._key, this._metadata, this._conventions);
tuple[Constants_js_1.CONSTANTS.Documents.Metadata.KEY] = metadata;
}
return {
method: "PUT",
uri,
body: Serializer_js_1.JsonSerializer.getDefault().serialize(tuple),
headers: this._headers().typeAppJson().build()
};
}
async setResponseAsync(bodyStream, fromCache) {
let body = null;
const resObj = await this._pipeline()
.collectBody(_ => body = _)
.parseJsonSync()
.process(bodyStream);
const type = !TypeUtil_js_1.TypeUtil.isPrimitive(this._value)
? this._conventions.getTypeDescriptorByEntity(this._value)
: null;
const clazz = TypeUtil_js_1.TypeUtil.isClass(type) ? type : null;
this.result = CompareExchangeResult_js_1.CompareExchangeResult.parseFromObject(resObj, this._conventions, clazz);
return body;
}
getRaftUniqueRequestId() {
return RaftIdGenerator_js_1.RaftIdGenerator.newId();
}
}
exports.PutCompareExchangeValueCommand = PutCompareExchangeValueCommand;
//# sourceMappingURL=PutCompareExchangeValueOperation.js.map