ravendb
Version:
RavenDB client for Node.js
66 lines • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazySessionOperations = void 0;
const LazyMultiLoaderWithInclude_js_1 = require("../../Loaders/LazyMultiLoaderWithInclude.js");
const Lazy_js_1 = require("../../../Lazy.js");
const LazyStartsWithOperation_js_1 = require("./LazyStartsWithOperation.js");
const LoadStartingWithOperation_js_1 = require("../LoadStartingWithOperation.js");
const StringUtil_js_1 = require("../../../../Utility/StringUtil.js");
const index_js_1 = require("../../../../Exceptions/index.js");
const LazyConditionalLoadOperation_js_1 = require("./LazyConditionalLoadOperation.js");
class LazySessionOperations {
_delegate;
constructor(delegate) {
this._delegate = delegate;
}
include(path) {
return new LazyMultiLoaderWithInclude_js_1.LazyMultiLoaderWithInclude(this._delegate).include(path);
}
load(idOrIds, clazz) {
const isMultipleIds = Array.isArray(idOrIds);
if (!isMultipleIds && this._delegate.isLoaded(idOrIds)) {
return new Lazy_js_1.Lazy(() => this._delegate.load(idOrIds, { documentType: clazz }));
}
const ids = isMultipleIds ? idOrIds : [idOrIds];
const result = this._delegate.lazyLoadInternal(ids, [], clazz);
return isMultipleIds
? result
: new Lazy_js_1.Lazy(async () => (await result.getValue())[idOrIds]);
}
loadStartingWith(idPrefix, opts) {
// eslint-disable-next-line unicorn/prefer-default-parameters
opts = opts || null;
opts = Object.assign({}, LoadStartingWithOperation_js_1.LoadStartingWithOperation.DEFAULT, opts);
const operation = new LazyStartsWithOperation_js_1.LazyStartsWithOperation(idPrefix, opts, this._delegate);
return this._delegate.addLazyOperation(operation);
}
conditionalLoad(id, changeVector, clazz) {
if (StringUtil_js_1.StringUtil.isNullOrEmpty(id)) {
(0, index_js_1.throwError)("InvalidArgumentException", "Id cannot be null");
}
if (this._delegate.isLoaded(id)) {
return new Lazy_js_1.Lazy(async () => {
const entity = await this._delegate.load(id, clazz);
if (!entity) {
return {
entity: null,
changeVector: null
};
}
const cv = this._delegate.advanced.getChangeVectorFor(entity);
return {
entity,
changeVector: cv
};
});
}
if (StringUtil_js_1.StringUtil.isNullOrEmpty(changeVector)) {
(0, index_js_1.throwError)("InvalidArgumentException", "The requested document with id '"
+ id + "' is not loaded into the session and could not conditional load when changeVector is null or empty.");
}
const lazyLoadOperation = new LazyConditionalLoadOperation_js_1.LazyConditionalLoadOperation(this._delegate, id, changeVector, clazz);
return this._delegate.addLazyOperation(lazyLoadOperation);
}
}
exports.LazySessionOperations = LazySessionOperations;
//# sourceMappingURL=LazySessionOperations.js.map