ravendb
Version:
RavenDB client for Node.js
92 lines • 3.29 kB
JavaScript
import { JsonSerializer } from "../../../Mapping/Json/Serializer.js";
import { throwError } from "../../../Exceptions/index.js";
import { RavenCommand } from "../../../Http/RavenCommand.js";
import { HeadersBuilder } from "../../../Utility/HttpUtil.js";
import { IndexTypeExtensions } from "../../Indexes/IndexTypeExtensions.js";
import { RaftIdGenerator } from "../../../Utility/RaftIdGenerator.js";
import { ObjectUtil } from "../../../Utility/ObjectUtil.js";
export class PutIndexesOperation {
get resultType() {
return "CommandResult";
}
_indexToAdd;
constructor(...indexToAdd) {
if (!indexToAdd || !indexToAdd.length) {
throwError("InvalidArgumentException", "indexToAdd cannot be null");
}
this._indexToAdd = indexToAdd;
}
getCommand(conventions) {
return new PutIndexesCommand(conventions, this._indexToAdd);
}
}
export class PutIndexesCommand extends RavenCommand {
_indexToAdd;
_allJavaScriptIndexes;
_conventions;
constructor(conventions, indexesToAdd) {
super();
if (!conventions) {
throwError("InvalidArgumentException", "conventions cannot be null or undefined.");
}
if (!indexesToAdd) {
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.isJavaScript(next.type)) {
this._allJavaScriptIndexes = false;
}
if (!next.name) {
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.transformObjectKeys({
Indexes: this._indexToAdd
}, {
recursive: true,
defaultTransform: ObjectUtil.pascal,
paths: [
{
path: INDEX_DEF_FIELDS_REGEX,
transform: x => x
}
]
});
const body = JsonSerializer.getDefault()
.serialize(bodyJson);
const headers = 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.newId();
}
}
//# sourceMappingURL=PutIndexesOperation.js.map