UNPKG

@configurator/ravendb

Version:
104 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LazyLoadOperation = void 0; const LoadOperation_1 = require("../LoadOperation"); const GetRequest_1 = require("../../../Commands/MultiGet/GetRequest"); const GetDocumentsCommand_1 = require("../../../Commands/GetDocumentsCommand"); const StreamUtil_1 = require("../../../../Utility/StreamUtil"); const StringUtil_1 = require("../../../../Utility/StringUtil"); const StringBuilder_1 = require("../../../../Utility/StringBuilder"); class LazyLoadOperation { constructor(session, loadOperation, clazz) { this._alreadyInSession = []; this._clazz = clazz; this._session = session; this._loadOperation = loadOperation; } createRequest() { const queryBuilder = new StringBuilder_1.StringBuilder("?"); if (this._includes) { for (const include of this._includes) { queryBuilder.append("&include=").append(include); } } let hasItems = false; for (const id of this._ids) { if (this._session.isLoadedOrDeleted(id)) { this._alreadyInSession.push(id); } else { hasItems = true; queryBuilder.append("&id=") .append(encodeURIComponent(id)); } } if (!hasItems) { this._result = this._loadOperation.getDocuments(this._clazz); return null; } const getRequest = new GetRequest_1.GetRequest(); getRequest.url = "/docs"; getRequest.query = queryBuilder.toString(); return getRequest; } byId(id) { if (StringUtil_1.StringUtil.isNullOrEmpty(id)) { return this; } if (!this._ids) { this._ids = [id]; } return this; } byIds(ids) { this._ids = Array.from(new Set(ids.filter(x => !StringUtil_1.StringUtil.isNullOrEmpty(x)))); return this; } withIncludes(includes) { this._includes = includes; return this; } get result() { return this._result; } set result(result) { this._result = result; } get queryResult() { return this._queryResult; } set queryResult(queryResult) { this._queryResult = queryResult; } get requiresRetry() { return this._requiresRetry; } set requiresRetry(result) { this._requiresRetry = result; } async handleResponseAsync(response) { if (response.forceRetry) { this.result = null; this.requiresRetry = true; return; } const multiLoadResult = await GetDocumentsCommand_1.GetDocumentsCommand.parseDocumentsResultResponseAsync((0, StreamUtil_1.stringToReadable)(response.result), this._session.conventions); this._handleResponse(multiLoadResult); } _handleResponse(loadResult) { if (this._alreadyInSession.length) { new LoadOperation_1.LoadOperation(this._session) .byIds(this._alreadyInSession) .getDocuments(this._clazz); } this._loadOperation.setResult(loadResult); if (!this._requiresRetry) { this._result = this._loadOperation.getDocuments(this._clazz); } } getResult() { return null; } } exports.LazyLoadOperation = LazyLoadOperation; //# sourceMappingURL=LazyLoadOperation.js.map