UNPKG

ravendb

Version:
102 lines 3.41 kB
import { GetDocumentsCommand } from "../../Commands/GetDocumentsCommand.js"; import { DocumentInfo } from "../DocumentInfo.js"; import { TypeUtil } from "../../../Utility/TypeUtil.js"; import { throwError } from "../../../Exceptions/index.js"; export 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({ 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.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.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) { 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.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; } } //# sourceMappingURL=LoadStartingWithOperation.js.map