UNPKG

ravendb

Version:
142 lines 6.13 kB
import { GetRevisionsCommand } from "../../Commands/GetRevisionsCommand.js"; import { throwError } from "../../../Exceptions/index.js"; import { TypeUtil } from "../../../Utility/TypeUtil.js"; import { DocumentInfo } from "../DocumentInfo.js"; import { CONSTANTS } from "../../../Constants.js"; export class GetRevisionOperation { _session; _result; _command; constructor(session, changeVectorOrChangeVectorsOrId, startOrDate, pageSize, metadataOnly = false) { if (!session) { throwError("InvalidArgumentException", "Session cannot be null."); } if (!changeVectorOrChangeVectorsOrId) { throwError("InvalidArgumentException", "Id cannot be null."); } this._session = session; if (startOrDate instanceof Date) { this._command = new GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId, startOrDate); } else if (TypeUtil.isArray(changeVectorOrChangeVectorsOrId)) { this._command = new GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId); } else if (TypeUtil.isNumber(startOrDate)) { this._command = new GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId, startOrDate, pageSize, metadataOnly); } else { this._command = new GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId); } } createRequest() { if (this._command.changeVectors) { return this._session.checkIfAllChangeVectorsAreAlreadyIncluded(this._command.changeVectors) ? null : this._command; } if (this._command.changeVector) { return this._session.checkIfAllChangeVectorsAreAlreadyIncluded([this.command.changeVector]) ? null : this._command; } if (this.command.before) { return this._session.checkIfRevisionByDateTimeBeforeAlreadyIncluded(this.command.id, this.command.before) ? null : this._command; } return this._command; } set result(result) { this._result = result; } get command() { return this._command; } _getRevision(documentType, document) { if (!document) { return null; } let id = null; const metadata = document[CONSTANTS.Documents.Metadata.KEY]; if (metadata) { id = metadata[CONSTANTS.Documents.Metadata.ID]; } let changeVector = null; if (metadata) { changeVector = metadata[CONSTANTS.Documents.Metadata.CHANGE_VECTOR]; } const entity = this._session.entityToJson.convertToEntity(documentType, id, document, !this._session.noTracking); const documentInfo = new DocumentInfo(); documentInfo.id = id; documentInfo.changeVector = changeVector; documentInfo.document = document; documentInfo.metadata = metadata; documentInfo.entity = entity; this._session.documentsByEntity.put(entity, documentInfo); this._session.onAfterConversionToEntityInvoke(id, document, entity); return entity; } getRevisionsFor(documentType) { const resultsCount = this._result.results.length; const results = []; for (const document of this._result.results) { results.push(this._getRevision(documentType, document)); } return results; } getRevisionsMetadataFor() { const resultsCount = this._result.results.length; const results = []; for (const document of this._result.results) { const metadata = document[CONSTANTS.Documents.Metadata.KEY]; results.push(metadata); } return results; } getRevision(documentType) { if (!this._result) { let revision; if (this._command.changeVectors) { for (const changeVector of this._command.changeVectors) { revision = this._session.includeRevisionsByChangeVector.get(changeVector); if (revision) { return this._getRevision(documentType, revision.document); } } } if (this.command.changeVector && this._session.includeRevisionsByChangeVector) { revision = this._session.includeRevisionsByChangeVector.get(this._command.changeVector); if (revision) { return this._getRevision(documentType, revision.document); } } if (this._command.before && this._session.includeRevisionsIdByDateTimeBefore) { const dictionaryDateTimeToDocument = this._session.includeRevisionsIdByDateTimeBefore.get(this._command.id); if (dictionaryDateTimeToDocument) { revision = dictionaryDateTimeToDocument.get(this._command.before.getTime()); if (revision) { return this._getRevision(documentType, revision.document); } } } return null; } const document = this._result.results[0]; return this._getRevision(documentType, document); } getRevisions(documentType) { const results = {}; if (!this._result) { for (const changeVector of this._command.changeVectors) { const revision = this._session.includeRevisionsByChangeVector.get(changeVector); if (revision) { results[changeVector] = this._getRevision(documentType, revision.document); } } return results; } for (let i = 0; i < this._command.changeVectors.length; i++) { const changeVector = this._command.changeVectors[i]; if (!changeVector) { continue; } results[changeVector] = this._getRevision(documentType, this._result.results[i]); } return results; } } //# sourceMappingURL=GetRevisionOperation.js.map