UNPKG

ravendb

Version:
133 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PatchCommand = exports.PatchOperation = exports.PatchOperationResult = void 0; const RavenCommand_js_1 = require("../../Http/RavenCommand.js"); const index_js_1 = require("../../Exceptions/index.js"); const ObjectUtil_js_1 = require("../../Utility/ObjectUtil.js"); const DateUtil_js_1 = require("../../Utility/DateUtil.js"); class PatchOperationResult { status; document; } exports.PatchOperationResult = PatchOperationResult; class PatchOperation { _id; _changeVector; _patch; _patchIfMissing; _skipPatchIfChangeVectorMismatch; get resultType() { return "PatchResult"; } constructor(id, changeVector, patch, patchIfMissing = null, skipPatchIfChangeVectorMismatch = false) { if (!patch) { (0, index_js_1.throwError)("InvalidArgumentException", "Patch cannot be null"); } if (!patch.script || !patch.script.trim()) { (0, index_js_1.throwError)("InvalidArgumentException", "Patch script cannot be null"); } if (patchIfMissing && !patchIfMissing.script.trim()) { (0, index_js_1.throwError)("InvalidArgumentException", "PatchIfMissing script cannot be null"); } this._id = id; this._changeVector = changeVector; this._patch = patch; this._patchIfMissing = patchIfMissing; this._skipPatchIfChangeVectorMismatch = skipPatchIfChangeVectorMismatch; } getCommand(store, conventions, cache, returnDebugInformation = false, test = false) { return new PatchCommand(conventions, this._id, this._changeVector, this._patch, this._patchIfMissing, this._skipPatchIfChangeVectorMismatch, returnDebugInformation, test); } } exports.PatchOperation = PatchOperation; class PatchCommand extends RavenCommand_js_1.RavenCommand { _id; _changeVector; _patch; _skipPatchIfChangeVectorMismatch; _returnDebugInformation; _test; _conventions; constructor(conventions, id, changeVector, patch, patchIfMissing, skipPatchIfChangeVectorMismatch, returnDebugInformation, test) { super(); if (!conventions) { (0, index_js_1.throwError)("InvalidArgumentException", "Conventions cannot be null"); } if (!patch) { (0, index_js_1.throwError)("InvalidArgumentException", "Patch cannot be null"); } if (!patch.script.trim()) { (0, index_js_1.throwError)("InvalidArgumentException", "Patch.Script cannot be null"); } if (patchIfMissing && !patchIfMissing.script.trim()) { (0, index_js_1.throwError)("InvalidArgumentException", "PatchIfMissing.Script cannot be null"); } if (!id) { (0, index_js_1.throwError)("InvalidArgumentException", "Id cannot be null"); } this._id = id; this._changeVector = changeVector; this._patch = ObjectUtil_js_1.ObjectUtil.transformObjectKeys(conventions.objectMapper.toObjectLiteral({ patch, patchIfMissing }), { defaultTransform: ObjectUtil_js_1.ObjectUtil.pascal, paths: [ { transform: conventions.localToServerFieldNameConverter, path: /Values/i } ] }); this._skipPatchIfChangeVectorMismatch = skipPatchIfChangeVectorMismatch; this._returnDebugInformation = returnDebugInformation; this._test = test; this._conventions = conventions; } get isReadRequest() { return false; } createRequest(node) { let uri = node.url + "/databases/" + node.database + "/docs?id=" + encodeURIComponent(this._id); if (this._skipPatchIfChangeVectorMismatch) { uri += "&skipPatchIfChangeVectorMismatch=true"; } if (this._returnDebugInformation) { uri += "&debug=true"; } if (this._test) { uri += "&test=true"; } const body = JSON.stringify(this._patch); const req = { method: "PATCH", uri, headers: this._headers().typeAppJson().build(), body }; this._addChangeVectorIfNotNull(this._changeVector, req); return req; } async setResponseAsync(bodyStream, fromCache) { if (!bodyStream) { return; } let body; const results = await this._pipeline() .collectBody(_ => body = _) .parseJsonSync() .process(bodyStream); this.result = PatchCommand._mapToLocalObject(results, this._conventions); return body; } static _mapToLocalObject(json, conventions) { return { changeVector: json.ChangeVector, collection: json.Collection, debug: json.Debug, lastModified: DateUtil_js_1.DateUtil.utc.parse(json.LastModified), status: json.Status, modifiedDocument: ObjectUtil_js_1.ObjectUtil.transformDocumentKeys(json.ModifiedDocument, conventions), originalDocument: ObjectUtil_js_1.ObjectUtil.transformDocumentKeys(json.OriginalDocument, conventions) }; } } exports.PatchCommand = PatchCommand; //# sourceMappingURL=PatchOperation.js.map