ravendb
Version:
RavenDB client for Node.js
97 lines • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PutIndexesCommand = exports.PutIndexesOperation = void 0;
const Serializer_js_1 = require("../../../Mapping/Json/Serializer.js");
const index_js_1 = require("../../../Exceptions/index.js");
const RavenCommand_js_1 = require("../../../Http/RavenCommand.js");
const HttpUtil_js_1 = require("../../../Utility/HttpUtil.js");
const IndexTypeExtensions_js_1 = require("../../Indexes/IndexTypeExtensions.js");
const RaftIdGenerator_js_1 = require("../../../Utility/RaftIdGenerator.js");
const ObjectUtil_js_1 = require("../../../Utility/ObjectUtil.js");
class PutIndexesOperation {
get resultType() {
return "CommandResult";
}
_indexToAdd;
constructor(...indexToAdd) {
if (!indexToAdd || !indexToAdd.length) {
(0, index_js_1.throwError)("InvalidArgumentException", "indexToAdd cannot be null");
}
this._indexToAdd = indexToAdd;
}
getCommand(conventions) {
return new PutIndexesCommand(conventions, this._indexToAdd);
}
}
exports.PutIndexesOperation = PutIndexesOperation;
class PutIndexesCommand extends RavenCommand_js_1.RavenCommand {
_indexToAdd;
_allJavaScriptIndexes;
_conventions;
constructor(conventions, indexesToAdd) {
super();
if (!conventions) {
(0, index_js_1.throwError)("InvalidArgumentException", "conventions cannot be null or undefined.");
}
if (!indexesToAdd) {
(0, index_js_1.throwError)("InvalidArgumentException", "indexesToAdd cannot be null or undefined.");
}
this._conventions = conventions;
this._allJavaScriptIndexes = true;
this._indexToAdd = indexesToAdd.reduce((result, next) => {
// We validate on the server that it is indeed a javascript index.
if (!IndexTypeExtensions_js_1.IndexTypeExtensions.isJavaScript(next.type)) {
this._allJavaScriptIndexes = false;
}
if (!next.name) {
(0, index_js_1.throwError)("InvalidArgumentException", "Index name cannot be null.");
}
result.push(this._conventions.objectMapper.toObjectLiteral(next));
return result;
}, []);
}
createRequest(node) {
const uri = node.url + "/databases/" + node.database
+ (this._allJavaScriptIndexes ? "/indexes" : "/admin/indexes");
const INDEX_DEF_FIELDS_REGEX = /^Indexes\.\[]\.Fields$/;
const bodyJson = ObjectUtil_js_1.ObjectUtil.transformObjectKeys({
Indexes: this._indexToAdd
}, {
recursive: true,
defaultTransform: ObjectUtil_js_1.ObjectUtil.pascal,
paths: [
{
path: INDEX_DEF_FIELDS_REGEX,
transform: x => x
}
]
});
const body = Serializer_js_1.JsonSerializer.getDefault()
.serialize(bodyJson);
const headers = HttpUtil_js_1.HeadersBuilder
.create()
.typeAppJson()
.build();
return {
method: "PUT",
uri,
body,
headers
};
}
async setResponseAsync(bodyStream, fromCache) {
let body = null;
const results = await this._defaultPipeline(x => body = x)
.process(bodyStream);
this.result = results["results"];
return body;
}
get isReadRequest() {
return false;
}
getRaftUniqueRequestId() {
return RaftIdGenerator_js_1.RaftIdGenerator.newId();
}
}
exports.PutIndexesCommand = PutIndexesCommand;
//# sourceMappingURL=PutIndexesOperation.js.map