ravendb
Version:
RavenDB client for Node.js
106 lines • 3.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoadStartingWithOperation = void 0;
const GetDocumentsCommand_js_1 = require("../../Commands/GetDocumentsCommand.js");
const DocumentInfo_js_1 = require("../DocumentInfo.js");
const TypeUtil_js_1 = require("../../../Utility/TypeUtil.js");
const index_js_1 = require("../../../Exceptions/index.js");
class LoadStartingWithOperation {
static DEFAULT = {
start: 0,
pageSize: 25,
exclude: "",
startAfter: "",
matches: ""
};
_session;
_startWith;
_matches;
_start;
_pageSize;
_exclude;
_startAfter;
_returnedIds = [];
_resultsSet;
_results;
constructor(session) {
this._session = session;
}
createRequest() {
this._session.incrementRequestCount();
return new GetDocumentsCommand_js_1.GetDocumentsCommand({
startsWith: this._startWith,
startsAfter: this._startAfter,
matches: this._matches,
exclude: this._exclude,
start: this._start,
pageSize: this._pageSize,
metadataOnly: false,
conventions: this._session.conventions
});
}
withStartWith(idPrefix, opts) {
const optsToUse = Object.keys(LoadStartingWithOperation.DEFAULT)
.reduce((result, next) => {
result[next] = TypeUtil_js_1.TypeUtil.isNullOrUndefined(opts[next])
? LoadStartingWithOperation.DEFAULT[next]
: opts[next];
return result;
}, {});
this._startWith = idPrefix;
this._matches = optsToUse.matches;
this._start = optsToUse.start;
this._pageSize = optsToUse.pageSize;
this._exclude = optsToUse.exclude;
this._startAfter = optsToUse.startAfter;
}
setResult(result) {
this._resultsSet = true;
if (this._session.noTracking) {
this._results = result;
return;
}
for (const document of result.results) {
if (!document) {
continue;
}
const newDocumentInfo = DocumentInfo_js_1.DocumentInfo.getNewDocumentInfo(document);
this._session.documentsById.add(newDocumentInfo);
this._returnedIds.push(newDocumentInfo.id);
}
}
getDocuments(docType) {
const entityType = this._session.conventions.getJsTypeByDocumentType(docType);
if (this._session.noTracking) {
if (!this._results) {
(0, index_js_1.throwError)("InvalidOperationException", "Cannot execute getDocuments before operation execution.");
}
if (!this._results || !this._results.results || !this._results.results.length) {
return [];
}
return this._results.results
.map(doc => {
const newDocumentInfo = DocumentInfo_js_1.DocumentInfo.getNewDocumentInfo(doc);
return this._session.trackEntity(entityType, newDocumentInfo);
});
}
return this._returnedIds.map(id => {
return this._getDocument(entityType, id);
});
}
_getDocument(entityType, id) {
if (!id) {
return null;
}
if (this._session.isDeleted(id)) {
return null;
}
const doc = this._session.documentsById.getValue(id);
if (doc) {
return this._session.trackEntity(entityType, doc);
}
return null;
}
}
exports.LoadStartingWithOperation = LoadStartingWithOperation;
//# sourceMappingURL=LoadStartingWithOperation.js.map