UNPKG

ravendb

Version:
111 lines 5.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionCountersBase = void 0; const StringUtil_js_1 = require("../../Utility/StringUtil.js"); const index_js_1 = require("../../Exceptions/index.js"); const TypeUtil_js_1 = require("../../Utility/TypeUtil.js"); const IdTypeAndName_js_1 = require("../IdTypeAndName.js"); const CountersBatchCommandData_js_1 = require("../Commands/Batches/CountersBatchCommandData.js"); const CounterOperation_js_1 = require("../Operations/Counters/CounterOperation.js"); /** * Abstract implementation for in memory session operations */ class SessionCountersBase { _docId; _session; constructor(session, entityOrId) { if (TypeUtil_js_1.TypeUtil.isObject(entityOrId)) { const document = session.documentsByEntity.get(entityOrId); if (!document) { this._throwEntityNotInSession(entityOrId); return; } this._docId = document.id; } else if (TypeUtil_js_1.TypeUtil.isString(entityOrId)) { if (!entityOrId) { (0, index_js_1.throwError)("InvalidArgumentException", "DocumentId cannot be empty."); } this._docId = entityOrId; } else { (0, index_js_1.throwError)("InvalidArgumentException", "Document ID or entity argument is neither string nor entity."); } this._session = session; } increment(counter, delta = 1) { if (StringUtil_js_1.StringUtil.isNullOrWhitespace(counter)) { (0, index_js_1.throwError)("InvalidArgumentException", "Counter cannot be empty."); } const counterOp = new CounterOperation_js_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_js_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_js_1.CountersBatchCommandData(this._docId, counterOp)); } } delete(counter) { if (StringUtil_js_1.StringUtil.isNullOrWhitespace(counter)) { (0, index_js_1.throwError)("InvalidArgumentException", "Counter is required."); } if (this._session.deferredCommandsMap.has(IdTypeAndName_js_1.IdTypeAndName.keyFor(this._docId, "DELETE", null))) { return; // no-op } const documentInfo = this._session.documentsById.getValue(this._docId); if (documentInfo && this._session.deletedEntities.contains(documentInfo.entity)) { return; //no-op } const counterOp = new CounterOperation_js_1.CounterOperation(); counterOp.type = "Delete"; counterOp.counterName = counter; const command = this._session.deferredCommandsMap.get(IdTypeAndName_js_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_js_1.CountersBatchCommandData(this._docId, counterOp)); } const cache = this._session.countersByDocId.get(this._docId); if (cache) { cache.data.delete(counter); } } _throwEntityNotInSession(entity) { (0, index_js_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, index_js_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, index_js_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, index_js_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