@configurator/ravendb
Version:
RavenDB client for Node.js
81 lines • 3.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PutCompareExchangeValueCommand = exports.PutCompareExchangeValueOperation = void 0;
const CompareExchangeResult_1 = require("./CompareExchangeResult");
const RavenCommand_1 = require("../../../Http/RavenCommand");
const DocumentConventions_1 = require("../../Conventions/DocumentConventions");
const Exceptions_1 = require("../../../Exceptions");
const Serializer_1 = require("../../../Mapping/Json/Serializer");
const TypeUtil_1 = require("../../../Utility/TypeUtil");
const RaftIdGenerator_1 = require("../../../Utility/RaftIdGenerator");
const Constants_1 = require("../../../Constants");
const CompareExchangeSessionValue_1 = require("./CompareExchangeSessionValue");
class PutCompareExchangeValueOperation {
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_1.RavenCommand {
constructor(key, value, index, metadata, conventions) {
super();
if (!key) {
(0, Exceptions_1.throwError)("InvalidArgumentException", "The key argument must have value");
}
if (index < 0) {
(0, Exceptions_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_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_1.COMPARE_EXCHANGE.OBJECT_FIELD_NAME] = TypeUtil_1.TypeUtil.isPrimitive(this._value)
? this._value
: this._conventions.transformObjectKeysToRemoteFieldNameConvention(this._value);
if (this._metadata) {
const metadata = CompareExchangeSessionValue_1.CompareExchangeSessionValue.prepareMetadataForPut(this._key, this._metadata, this._conventions);
tuple[Constants_1.CONSTANTS.Documents.Metadata.KEY] = metadata;
}
return {
method: "PUT",
uri,
body: Serializer_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_1.TypeUtil.isPrimitive(this._value)
? this._conventions.getTypeDescriptorByEntity(this._value)
: null;
const clazz = TypeUtil_1.TypeUtil.isClass(type) ? type : null;
this.result = CompareExchangeResult_1.CompareExchangeResult.parseFromObject(resObj, this._conventions, clazz);
return body;
}
getRaftUniqueRequestId() {
return RaftIdGenerator_1.RaftIdGenerator.newId();
}
}
exports.PutCompareExchangeValueCommand = PutCompareExchangeValueCommand;
//# sourceMappingURL=PutCompareExchangeValueOperation.js.map