ravendb
Version:
RavenDB client for Node.js
62 lines • 2.81 kB
JavaScript
import { LazyMultiLoaderWithInclude } from "../../Loaders/LazyMultiLoaderWithInclude.js";
import { Lazy } from "../../../Lazy.js";
import { LazyStartsWithOperation } from "./LazyStartsWithOperation.js";
import { LoadStartingWithOperation } from "../LoadStartingWithOperation.js";
import { StringUtil } from "../../../../Utility/StringUtil.js";
import { throwError } from "../../../../Exceptions/index.js";
import { LazyConditionalLoadOperation } from "./LazyConditionalLoadOperation.js";
export class LazySessionOperations {
_delegate;
constructor(delegate) {
this._delegate = delegate;
}
include(path) {
return new LazyMultiLoaderWithInclude(this._delegate).include(path);
}
load(idOrIds, clazz) {
const isMultipleIds = Array.isArray(idOrIds);
if (!isMultipleIds && this._delegate.isLoaded(idOrIds)) {
return new Lazy(() => this._delegate.load(idOrIds, { documentType: clazz }));
}
const ids = isMultipleIds ? idOrIds : [idOrIds];
const result = this._delegate.lazyLoadInternal(ids, [], clazz);
return isMultipleIds
? result
: new Lazy(async () => (await result.getValue())[idOrIds]);
}
loadStartingWith(idPrefix, opts) {
// eslint-disable-next-line unicorn/prefer-default-parameters
opts = opts || null;
opts = Object.assign({}, LoadStartingWithOperation.DEFAULT, opts);
const operation = new LazyStartsWithOperation(idPrefix, opts, this._delegate);
return this._delegate.addLazyOperation(operation);
}
conditionalLoad(id, changeVector, clazz) {
if (StringUtil.isNullOrEmpty(id)) {
throwError("InvalidArgumentException", "Id cannot be null");
}
if (this._delegate.isLoaded(id)) {
return new 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.isNullOrEmpty(changeVector)) {
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(this._delegate, id, changeVector, clazz);
return this._delegate.addLazyOperation(lazyLoadOperation);
}
}
//# sourceMappingURL=LazySessionOperations.js.map