UNPKG

ravendb

Version:
68 lines 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OperationExecutor = void 0; const OperationCompletionAwaiter_js_1 = require("./OperationCompletionAwaiter.js"); const index_js_1 = require("../../Exceptions/index.js"); const PatchOperation_js_1 = require("./PatchOperation.js"); const StatusCode_js_1 = require("../../Http/StatusCode.js"); const StringUtil_js_1 = require("../../Utility/StringUtil.js"); class OperationExecutor { _store; _databaseName; _requestExecutor; constructor(store, databaseName) { this._store = store; this._databaseName = databaseName ?? store.database; if (!StringUtil_js_1.StringUtil.isNullOrWhitespace(this._databaseName)) { this._requestExecutor = store.getRequestExecutor(this._databaseName); } else { (0, index_js_1.throwError)("InvalidOperationException", "Cannot use operations without a database defined, did you forget to call forDatabase?"); } } getRequestExecutor() { if (this._requestExecutor) { return this._requestExecutor; } this._requestExecutor = this._databaseName ? this._store.getRequestExecutor(this._databaseName) : null; return this._requestExecutor; } forDatabase(databaseName) { if (!databaseName) { (0, index_js_1.throwError)("InvalidArgumentException", `Argument 'databaseName' is invalid: ${databaseName}.`); } if (this._databaseName.toLowerCase() === databaseName.toLowerCase()) { return this; } return new OperationExecutor(this._store, databaseName); } async send(operation, sessionInfo, documentType) { const command = operation.getCommand(this._store, this.getRequestExecutor().conventions, this.getRequestExecutor().cache); await this.getRequestExecutor().execute(command, sessionInfo); if (operation.resultType === "OperationId") { const idResult = command.result; return new OperationCompletionAwaiter_js_1.OperationCompletionAwaiter(this.getRequestExecutor(), this.getRequestExecutor().conventions, idResult.operationId, command.selectedNodeTag || idResult.operationNodeTag); } else if (operation.resultType === "PatchResult") { const patchOperationResult = new PatchOperation_js_1.PatchOperationResult(); if (command.statusCode === StatusCode_js_1.StatusCodes.NotModified) { patchOperationResult.status = "NotModified"; return patchOperationResult; } if (command.statusCode === StatusCode_js_1.StatusCodes.NotFound) { patchOperationResult.status = "DocumentDoesNotExist"; return patchOperationResult; } const patchResult = command.result; patchOperationResult.status = patchResult.status; const { conventions } = this.getRequestExecutor(); conventions.tryRegisterJsType(documentType); const entityType = conventions.getJsTypeByDocumentType(documentType); patchOperationResult.document = conventions.deserializeEntityFromJson(entityType, patchResult.modifiedDocument); return patchOperationResult; } return command.result; } } exports.OperationExecutor = OperationExecutor; //# sourceMappingURL=OperationExecutor.js.map