ravendb
Version:
RavenDB client for Node.js
88 lines • 3.74 kB
JavaScript
import { SubscriptionBatchBase } from "./SubscriptionBatchBase.js";
import { GenerateEntityIdOnTheClient } from "../Identity/GenerateEntityIdOnTheClient.js";
import { throwError } from "../../Exceptions/index.js";
import { DocumentInfo } from "../Session/DocumentInfo.js";
export class SubscriptionBatch extends SubscriptionBatchBase {
_store;
_generateEntityIdOnTheClient;
_sessionOpened = false;
constructor(documentType, revisions, requestExecutor, store, dbName) {
super(documentType, revisions, requestExecutor, dbName);
this._store = store;
this._generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(this._requestExecutor.conventions, () => throwError("InvalidOperationException", "Shouldn't be generating new ids here"));
}
initialize(batch) {
this._sessionOpened = false;
super.initialize(batch);
}
openSession(options) {
if (options) {
SubscriptionBatch._validateSessionOptions(options);
}
options = options || {};
options.database = this._dbName;
options.requestExecutor = this._requestExecutor;
return this._openSessionInternal(options);
}
_openSessionInternal(options) {
if (this._sessionOpened) {
this.throwSessionCanBeOpenedOnlyOnce();
}
this._sessionOpened = true;
const s = this._store.openSession(options);
this._loadDataToSession(s);
return s;
}
throwSessionCanBeOpenedOnlyOnce() {
throwError("InvalidOperationException", "Session can only be opened once per each Subscription batch");
}
static _validateSessionOptions(options) {
if (options.database) {
throwError("InvalidOperationException", "Cannot set Database when session is opened in subscription.");
}
if (options.requestExecutor) {
throwError("InvalidOperationException", "Cannot set RequestExecutor when session is opened in subscription.");
}
if (options.transactionMode !== "SingleNode") {
throwError("InvalidOperationException", "Cannot set TransactionMode when session is opened in subscription. Only 'SingleNode' is supported.");
}
}
_loadDataToSession(session) {
if (session.noTracking) {
return;
}
if (this._includes && this._includes.length) {
for (const item of this._includes) {
session.registerIncludes(item);
}
}
if (this._counterIncludes && this._counterIncludes.length) {
for (const item of this._counterIncludes) {
session.registerCounters(item.includes, item.counterIncludes);
}
}
if (this._timeSeriesIncludes && this._timeSeriesIncludes.length > 0) {
for (const item of this._timeSeriesIncludes) {
session.registerTimeSeries(item);
}
}
for (const item of this.items) {
if (item.projection || item.revision) {
continue;
}
const documentInfo = new DocumentInfo();
documentInfo.id = item.id;
documentInfo.document = item.rawResult;
documentInfo.metadata = item.rawMetadata;
documentInfo.metadataInstance = item.metadata;
documentInfo.changeVector = item.changeVector;
documentInfo.entity = item.result;
documentInfo.newDocument = false;
session.registerExternalLoadedIntoTheSession(documentInfo);
}
}
ensureDocumentId(item, id) {
this._generateEntityIdOnTheClient.trySetIdentity(item, id);
}
}
//# sourceMappingURL=SubscriptionBatch.js.map