ravendb
Version:
RavenDB client for Node.js
146 lines • 6.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetRevisionOperation = void 0;
const GetRevisionsCommand_js_1 = require("../../Commands/GetRevisionsCommand.js");
const index_js_1 = require("../../../Exceptions/index.js");
const TypeUtil_js_1 = require("../../../Utility/TypeUtil.js");
const DocumentInfo_js_1 = require("../DocumentInfo.js");
const Constants_js_1 = require("../../../Constants.js");
class GetRevisionOperation {
_session;
_result;
_command;
constructor(session, changeVectorOrChangeVectorsOrId, startOrDate, pageSize, metadataOnly = false) {
if (!session) {
(0, index_js_1.throwError)("InvalidArgumentException", "Session cannot be null.");
}
if (!changeVectorOrChangeVectorsOrId) {
(0, index_js_1.throwError)("InvalidArgumentException", "Id cannot be null.");
}
this._session = session;
if (startOrDate instanceof Date) {
this._command = new GetRevisionsCommand_js_1.GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId, startOrDate);
}
else if (TypeUtil_js_1.TypeUtil.isArray(changeVectorOrChangeVectorsOrId)) {
this._command = new GetRevisionsCommand_js_1.GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId);
}
else if (TypeUtil_js_1.TypeUtil.isNumber(startOrDate)) {
this._command = new GetRevisionsCommand_js_1.GetRevisionsCommand(session.conventions, changeVectorOrChangeVectorsOrId, startOrDate, pageSize, metadataOnly);
}
else {
this._command = new GetRevisionsCommand_js_1.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_js_1.CONSTANTS.Documents.Metadata.KEY];
if (metadata) {
id = metadata[Constants_js_1.CONSTANTS.Documents.Metadata.ID];
}
let changeVector = null;
if (metadata) {
changeVector = metadata[Constants_js_1.CONSTANTS.Documents.Metadata.CHANGE_VECTOR];
}
const entity = this._session.entityToJson.convertToEntity(documentType, id, document, !this._session.noTracking);
const documentInfo = new DocumentInfo_js_1.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_js_1.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;
}
}
exports.GetRevisionOperation = GetRevisionOperation;
//# sourceMappingURL=GetRevisionOperation.js.map