UNPKG

ravendb

Version:
191 lines 8.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompareExchangeSessionValue = void 0; const CompareExchangeValue_js_1 = require("./CompareExchangeValue.js"); const index_js_1 = require("../../../Exceptions/index.js"); const TypeUtil_js_1 = require("../../../Utility/TypeUtil.js"); const CompareExchangeValueJsonConverter_js_1 = require("./CompareExchangeValueJsonConverter.js"); const Constants_js_1 = require("../../../Constants.js"); const StringUtil_js_1 = require("../../../Utility/StringUtil.js"); const EntityToJson_js_1 = require("../../Session/EntityToJson.js"); const PutCompareExchangeCommandData_js_1 = require("../../Commands/Batches/PutCompareExchangeCommandData.js"); const DeleteCompareExchangeCommandData_js_1 = require("../../Commands/Batches/DeleteCompareExchangeCommandData.js"); const CompareExchangeValueResultParser_js_1 = require("./CompareExchangeValueResultParser.js"); class CompareExchangeSessionValue { _key; _index; _originalValue; _value; _state; constructor(keyOrValue, index, state) { if (!keyOrValue) { (0, index_js_1.throwError)("InvalidArgumentException", "Key cannot be null"); } if (TypeUtil_js_1.TypeUtil.isString(keyOrValue)) { this._key = keyOrValue; this._index = index; this._state = state; } else { this._key = keyOrValue.key; this._index = keyOrValue.index; this._state = keyOrValue.index >= 0 ? "None" : "Missing"; if (keyOrValue.index > 0) { this._originalValue = keyOrValue; } } } getValue(clazz, conventions) { switch (this._state) { case "None": case "Created": { if (this._value instanceof CompareExchangeValue_js_1.CompareExchangeValue) { return this._value; } if (this._value) { (0, index_js_1.throwError)("InvalidOperationException", "Value cannot be null"); } let entity; if (this._originalValue && !TypeUtil_js_1.TypeUtil.isNullOrUndefined(this._originalValue.value)) { entity = CompareExchangeValueResultParser_js_1.CompareExchangeValueResultParser.deserializeObject(this._originalValue.value, conventions, clazz); } const value = new CompareExchangeValue_js_1.CompareExchangeValue(this._key, this._index, entity, null); this._value = value; return value; } case "Missing": case "Deleted": { return null; } default: { (0, index_js_1.throwError)("NotSupportedException", "Not supported state: " + this._state); } } } create(item) { this._assertState(); if (this._value) { (0, index_js_1.throwError)("InvalidOperationException", "The compare exchange value with key '" + this._key + "' is already tracked."); } this._index = 0; const value = new CompareExchangeValue_js_1.CompareExchangeValue(this._key, this._index, item, null); this._value = value; this._state = "Created"; return value; } delete(index) { this._assertState(); this._index = index; this._state = "Deleted"; } _assertState() { switch (this._state) { case "None": case "Missing": { return; } case "Created": { (0, index_js_1.throwError)("InvalidOperationException", "The compare exchange value with key '" + this._key + "' was already stored."); break; } case "Deleted": { (0, index_js_1.throwError)("InvalidOperationException", "The compare exchange value with key '" + this._key + "' was already deleted."); } } } getCommand(conventions) { switch (this._state) { case "None": case "Created": { if (!this._value) { return null; } const entity = CompareExchangeValueJsonConverter_js_1.CompareExchangeValueJsonConverter.convertToJson(this._value.value, conventions); let entityJson = TypeUtil_js_1.TypeUtil.isObject(entity) ? entity : null; let metadata; if (this._value.hasMetadata() && Object.keys(this._value.metadata)) { metadata = CompareExchangeSessionValue.prepareMetadataForPut(this._key, this._value.metadata, conventions); } let entityToInsert = null; if (TypeUtil_js_1.TypeUtil.isNullOrUndefined(entityJson)) { entityJson = entityToInsert = this._convertEntity(this._key, entity, conventions.objectMapper, metadata); } const newValue = new CompareExchangeValue_js_1.CompareExchangeValue(this._key, this._index, entityJson, null); const hasChanged = TypeUtil_js_1.TypeUtil.isNullOrUndefined(this._originalValue) || this.hasChanged(this._originalValue, newValue); this._originalValue = newValue; if (!hasChanged) { return null; } if (TypeUtil_js_1.TypeUtil.isNullOrUndefined(entityToInsert)) { entityToInsert = this._convertEntity(this._key, entity, conventions.objectMapper, metadata); } return new PutCompareExchangeCommandData_js_1.PutCompareExchangeCommandData(newValue.key, entityToInsert, newValue.index); } case "Deleted": { return new DeleteCompareExchangeCommandData_js_1.DeleteCompareExchangeCommandData(this._key, this._index); } case "Missing": { return null; } default: { (0, index_js_1.throwError)("InvalidOperationException", "Not supported state: " + this._state); } } } _convertEntity(key, entity, objectMapper, metadata) { return { [Constants_js_1.COMPARE_EXCHANGE.OBJECT_FIELD_NAME]: entity, [Constants_js_1.CONSTANTS.Documents.Metadata.KEY]: metadata ?? undefined }; } hasChanged(originalValue, newValue) { if (originalValue === newValue) { return false; } if (!StringUtil_js_1.StringUtil.equalsIgnoreCase(originalValue.key, newValue.key)) { (0, index_js_1.throwError)("InvalidOperationException", "Keys do not match. Expected '" + originalValue.key + " but was: " + newValue.key); } if (originalValue.index !== newValue.index) { return true; } return JSON.stringify(originalValue.value) !== JSON.stringify(newValue.value); } updateState(index) { this._index = index; this._state = "None"; if (this._originalValue) { this._originalValue.index = index; } if (this._value) { this._value.index = index; } } updateValue(value, mapper) { this._index = value.index; this._state = value.index >= 0 ? "None" : "Missing"; this._originalValue = value; if (this._value) { this._value.index = this._index; if (!TypeUtil_js_1.TypeUtil.isNullOrUndefined(this._value.value)) { EntityToJson_js_1.EntityToJson.populateEntity(this._value.value, value.value, mapper); } } } static prepareMetadataForPut(key, metadataDictionary, conventions) { if (Constants_js_1.CONSTANTS.Documents.Metadata.EXPIRES in metadataDictionary) { const obj = metadataDictionary[Constants_js_1.CONSTANTS.Documents.Metadata.EXPIRES]; if (!obj) { CompareExchangeSessionValue._throwInvalidExpiresMetadata("The value of " + Constants_js_1.CONSTANTS.Documents.Metadata.EXPIRES + " metadata for compare exchange '" + key + " is null."); } if (!TypeUtil_js_1.TypeUtil.isDate(obj) && !TypeUtil_js_1.TypeUtil.isString(obj)) { CompareExchangeSessionValue._throwInvalidExpiresMetadata("The class of " + Constants_js_1.CONSTANTS.Documents.Metadata.EXPIRES + " metadata for compare exchange '" + key + "' is not valid. Use the following type: Date or string"); } } return conventions.objectMapper.toObjectLiteral(metadataDictionary); } static _throwInvalidExpiresMetadata(message) { (0, index_js_1.throwError)("InvalidArgumentException", message); } } exports.CompareExchangeSessionValue = CompareExchangeSessionValue; //# sourceMappingURL=CompareExchangeSessionValue.js.map