UNPKG

ravendb

Version:
64 lines 3.13 kB
import { OperationCompletionAwaiter } from "./OperationCompletionAwaiter.js"; import { throwError } from "../../Exceptions/index.js"; import { PatchOperationResult } from "./PatchOperation.js"; import { StatusCodes } from "../../Http/StatusCode.js"; import { StringUtil } from "../../Utility/StringUtil.js"; export class OperationExecutor { _store; _databaseName; _requestExecutor; constructor(store, databaseName) { this._store = store; this._databaseName = databaseName ?? store.database; if (!StringUtil.isNullOrWhitespace(this._databaseName)) { this._requestExecutor = store.getRequestExecutor(this._databaseName); } else { 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) { 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(this.getRequestExecutor(), this.getRequestExecutor().conventions, idResult.operationId, command.selectedNodeTag || idResult.operationNodeTag); } else if (operation.resultType === "PatchResult") { const patchOperationResult = new PatchOperationResult(); if (command.statusCode === StatusCodes.NotModified) { patchOperationResult.status = "NotModified"; return patchOperationResult; } if (command.statusCode === 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; } } //# sourceMappingURL=OperationExecutor.js.map