ravendb
Version:
RavenDB client for Node.js
84 lines • 2.77 kB
JavaScript
import { CaseInsensitiveStringSet } from "../../../Primitives/CaseInsensitiveStringSet.js";
import { throwError } from "../../../Exceptions/index.js";
import { TypeUtil } from "../../../Utility/TypeUtil.js";
import { StringUtil } from "../../../Utility/StringUtil.js";
/**
* Commands that patches multiple documents using same patch script
* CAUTION: This command does not update session state after .saveChanges() call
*/
export class BatchPatchCommandData {
_seenIds = CaseInsensitiveStringSet.create();
_ids = [];
_name = null;
_patch;
_patchIfMissing;
constructor(patch, patchIfMissing, ...ids) {
if (!patch) {
throwError("InvalidArgumentException", "Patch cannot be null.");
}
if (arguments.length >= 3) {
if (!ids) {
throwError("InvalidArgumentException", "Ids cannot be null.");
}
if (ids.length === 0) {
throwError("InvalidArgumentException", "Value cannot be an empty collection.");
}
for (const idEntry of ids) {
if (TypeUtil.isObject(idEntry)) {
const { id, changeVector } = idEntry;
this._add(id, changeVector);
}
else {
this._add(idEntry);
}
}
}
this._patch = patch;
this._patchIfMissing = patchIfMissing;
}
_add(id, changeVector) {
if (StringUtil.isNullOrWhitespace(id)) {
throwError("InvalidArgumentException", "Value cannot be null or whitespace.");
}
if (!this._seenIds.add(id)) {
throwError("InvalidOperationException", "Could not add ID '" + id + "' because item with the same ID was already added");
}
this._ids.push({ id, changeVector });
}
get ids() {
return this._ids;
}
get id() {
return throwError("NotSupportedException");
}
get name() {
return this._name;
}
get patch() {
return this._patch;
}
patchIfMissing() {
return this._patchIfMissing;
}
get changeVector() {
return throwError("NotSupportedException");
}
get type() {
return "BatchPATCH";
}
serialize(conventions) {
const ids = this._ids.map(x => ({
Id: x.id,
ChangeVector: x.changeVector || undefined
}));
return {
Ids: ids,
Patch: this.patch.serialize(conventions),
Type: "BatchPATCH",
PatchIfMissing: this._patchIfMissing
? this._patchIfMissing.serialize(conventions)
: undefined
};
}
}
//# sourceMappingURL=BatchPatchCommandData.js.map