UNPKG

@configurator/ravendb

Version:
182 lines 8.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompareExchangeSessionValue = void 0; const CompareExchangeValue_1 = require("./CompareExchangeValue"); const Exceptions_1 = require("../../../Exceptions"); const TypeUtil_1 = require("../../../Utility/TypeUtil"); const CompareExchangeValueJsonConverter_1 = require("./CompareExchangeValueJsonConverter"); const Constants_1 = require("../../../Constants"); const StringUtil_1 = require("../../../Utility/StringUtil"); const EntityToJson_1 = require("../../Session/EntityToJson"); const PutCompareExchangeCommandData_1 = require("../../Commands/Batches/PutCompareExchangeCommandData"); const DeleteCompareExchangeCommandData_1 = require("../../Commands/Batches/DeleteCompareExchangeCommandData"); class CompareExchangeSessionValue { constructor(keyOrValue, index, state) { if (!keyOrValue) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Key cannot be null"); } if (TypeUtil_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_1.CompareExchangeValue) { return this._value; } if (this._value) { (0, Exceptions_1.throwError)("InvalidOperationException", "Value cannot be null"); } let entity; if (this._originalValue && !TypeUtil_1.TypeUtil.isNullOrUndefined(this._originalValue.value)) { if (TypeUtil_1.TypeUtil.isPrimitive(clazz) || !clazz) { entity = this._originalValue.value; } else { entity = EntityToJson_1.EntityToJson.convertToEntity(clazz, this._key, this._originalValue.value, conventions); } } const value = new CompareExchangeValue_1.CompareExchangeValue(this._key, this._index, entity, null); this._value = value; return value; } case "Missing": case "Deleted": return null; default: (0, Exceptions_1.throwError)("NotSupportedException", "Not supported state: " + this._state); } } create(item) { this._assertState(); if (this._value) { (0, Exceptions_1.throwError)("InvalidOperationException", "The compare exchange value with key '" + this._key + "' is already tracked."); } this._index = 0; const value = new CompareExchangeValue_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, Exceptions_1.throwError)("InvalidOperationException", "The compare exchange value with key '" + this._key + "' was already stored."); break; case "Deleted": (0, Exceptions_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_1.CompareExchangeValueJsonConverter.convertToJson(this._value.value, conventions); let entityJson = TypeUtil_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_1.TypeUtil.isNullOrUndefined(entityJson)) { entityJson = entityToInsert = this._convertEntity(this._key, entity, conventions.objectMapper, metadata); } const newValue = new CompareExchangeValue_1.CompareExchangeValue(this._key, this._index, entityJson, null); const hasChanged = TypeUtil_1.TypeUtil.isNullOrUndefined(this._originalValue) || this.hasChanged(this._originalValue, newValue); this._originalValue = newValue; if (!hasChanged) { return null; } if (TypeUtil_1.TypeUtil.isNullOrUndefined(entityToInsert)) { entityToInsert = this._convertEntity(this._key, entity, conventions.objectMapper, metadata); } return new PutCompareExchangeCommandData_1.PutCompareExchangeCommandData(newValue.key, entityToInsert, newValue.index); } case "Deleted": return new DeleteCompareExchangeCommandData_1.DeleteCompareExchangeCommandData(this._key, this._index); case "Missing": return null; default: (0, Exceptions_1.throwError)("InvalidOperationException", "Not supported state: " + this._state); } } _convertEntity(key, entity, objectMapper, metadata) { return { [Constants_1.COMPARE_EXCHANGE.OBJECT_FIELD_NAME]: entity, [Constants_1.CONSTANTS.Documents.Metadata.KEY]: metadata ?? undefined }; } hasChanged(originalValue, newValue) { if (originalValue === newValue) { return false; } if (!StringUtil_1.StringUtil.equalsIgnoreCase(originalValue.key, newValue.key)) { (0, Exceptions_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_1.TypeUtil.isNullOrUndefined(this._value.value)) { EntityToJson_1.EntityToJson.populateEntity(this._value.value, value.value, mapper); } } } static prepareMetadataForPut(key, metadataDictionary, conventions) { if (Constants_1.CONSTANTS.Documents.Metadata.EXPIRES in metadataDictionary) { const obj = metadataDictionary[Constants_1.CONSTANTS.Documents.Metadata.EXPIRES]; if (!obj) { CompareExchangeSessionValue._throwInvalidExpiresMetadata("The value of " + Constants_1.CONSTANTS.Documents.Metadata.EXPIRES + " metadata for compare exchange '" + key + " is null."); } if (!TypeUtil_1.TypeUtil.isDate(obj) && !TypeUtil_1.TypeUtil.isString(obj)) { CompareExchangeSessionValue._throwInvalidExpiresMetadata("The class of " + Constants_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, Exceptions_1.throwError)("InvalidArgumentException", message); } } exports.CompareExchangeSessionValue = CompareExchangeSessionValue; //# sourceMappingURL=CompareExchangeSessionValue.js.map