@configurator/ravendb
Version:
RavenDB client for Node.js
106 lines • 5.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionCountersBase = void 0;
const StringUtil_1 = require("../../Utility/StringUtil");
const Exceptions_1 = require("../../Exceptions");
const TypeUtil_1 = require("../../Utility/TypeUtil");
const IdTypeAndName_1 = require("../IdTypeAndName");
const CountersBatchCommandData_1 = require("../Commands/Batches/CountersBatchCommandData");
const CounterOperation_1 = require("../Operations/Counters/CounterOperation");
class SessionCountersBase {
constructor(session, entityOrId) {
if (TypeUtil_1.TypeUtil.isObject(entityOrId)) {
const document = session.documentsByEntity.get(entityOrId);
if (!document) {
this._throwEntityNotInSession(entityOrId);
return;
}
this._docId = document.id;
}
else if (TypeUtil_1.TypeUtil.isString(entityOrId)) {
if (!entityOrId) {
(0, Exceptions_1.throwError)("InvalidArgumentException", "DocumentId cannot be empty.");
}
this._docId = entityOrId;
}
else {
(0, Exceptions_1.throwError)("InvalidArgumentException", "Document ID or entity argument is neither string nor entity.");
}
this._session = session;
}
increment(counter, delta = 1) {
if (StringUtil_1.StringUtil.isNullOrWhitespace(counter)) {
(0, Exceptions_1.throwError)("InvalidArgumentException", "Counter cannot be empty.");
}
const counterOp = new CounterOperation_1.CounterOperation();
counterOp.type = "Increment";
counterOp.counterName = counter;
counterOp.delta = delta;
const documentInfo = this._session.documentsById.getValue(this._docId);
if (documentInfo && this._session.deletedEntities.contains(documentInfo.entity)) {
SessionCountersBase._throwDocumentAlreadyDeletedInSession(this._docId, counter);
}
const command = this._session.deferredCommandsMap.get(IdTypeAndName_1.IdTypeAndName.keyFor(this._docId, "Counters", null));
if (command) {
const countersBatchCommandData = command;
if (countersBatchCommandData.hasDelete(counter)) {
SessionCountersBase._throwIncrementCounterAfterDeleteAttempt(this._docId, counter);
}
countersBatchCommandData.counters.operations.push(counterOp);
}
else {
this._session.defer(new CountersBatchCommandData_1.CountersBatchCommandData(this._docId, counterOp));
}
}
delete(counter) {
if (StringUtil_1.StringUtil.isNullOrWhitespace(counter)) {
(0, Exceptions_1.throwError)("InvalidArgumentException", "Counter is required.");
}
if (this._session.deferredCommandsMap.has(IdTypeAndName_1.IdTypeAndName.keyFor(this._docId, "DELETE", null))) {
return;
}
const documentInfo = this._session.documentsById.getValue(this._docId);
if (documentInfo && this._session.deletedEntities.contains(documentInfo.entity)) {
return;
}
const counterOp = new CounterOperation_1.CounterOperation();
counterOp.type = "Delete";
counterOp.counterName = counter;
const command = this._session.deferredCommandsMap.get(IdTypeAndName_1.IdTypeAndName.keyFor(this._docId, "Counters", null));
if (command) {
const countersBatchCommandData = command;
if (countersBatchCommandData.hasIncrement(counter)) {
SessionCountersBase._throwDeleteCounterAfterIncrementAttempt(this._docId, counter);
}
countersBatchCommandData.counters.operations.push(counterOp);
}
else {
this._session.defer(new CountersBatchCommandData_1.CountersBatchCommandData(this._docId, counterOp));
}
const cache = this._session.countersByDocId.get(this._docId);
if (cache) {
cache.data.delete(counter);
}
}
_throwEntityNotInSession(entity) {
(0, Exceptions_1.throwError)("InvalidArgumentException", `Entity is not associated with the session, cannot add counter to it. ` +
"Use documentId instead of track the entity in the session");
}
static _throwIncrementCounterAfterDeleteAttempt(documentId, counter) {
(0, Exceptions_1.throwError)("InvalidOperationException", "Can't increment counter " + counter
+ " of document " + documentId
+ ", there is a deferred command registered to delete a counter with the same name.");
}
static _throwDeleteCounterAfterIncrementAttempt(documentId, counter) {
(0, Exceptions_1.throwError)("InvalidOperationException", "Can't delete counter " + counter
+ " of document " + documentId
+ ", there is a deferred command registered to increment a counter with the same name.");
}
static _throwDocumentAlreadyDeletedInSession(documentId, counter) {
(0, Exceptions_1.throwError)("InvalidOperationException", "Can't increment counter " + counter
+ " of document " + documentId
+ ", the document was already deleted in this session.");
}
}
exports.SessionCountersBase = SessionCountersBase;
//# sourceMappingURL=SessionCountersBase.js.map