UNPKG

@configurator/ravendb

Version:
246 lines 11.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityToJson = void 0; const TypeUtil_1 = require("../../Utility/TypeUtil"); const Constants_1 = require("../../Constants"); const Exceptions_1 = require("../../Exceptions"); const SetupDocumentBase_1 = require("../SetupDocumentBase"); class EntityToJson { constructor(session) { this._missingDictionary = new Map(); this._session = session; } get missingDictionary() { return this._missingDictionary; } convertEntityToJson(entity, documentInfo) { const { conventions } = this._session; const entityMapper = conventions.objectMapper; if (documentInfo) { this._session.onBeforeConversionToDocumentInvoke(documentInfo.id, entity); } let document = EntityToJson._convertEntityToJsonInternal(entity, this._session.conventions, documentInfo); if (documentInfo) { const documentReference = { value: document }; this._session.onAfterConversionToDocumentInvoke(documentInfo.id, entity, documentReference); document = documentReference.value; } return document; } static _convertEntityToJsonInternal(entity, conventions, documentInfo, removeIdentityProperty = true) { const entityMapper = conventions.objectMapper; let typeInfo; let jsonNode = entityMapper.toObjectLiteral(entity, (_typeInfo) => { typeInfo = _typeInfo; }, conventions.knownEntityTypesByName); if (entity instanceof SetupDocumentBase_1.SetupDocumentBase) { jsonNode = entity.toRemoteFieldNames(); } else { jsonNode = conventions.transformObjectKeysToRemoteFieldNameConvention(jsonNode); } EntityToJson._writeMetadata(jsonNode, typeInfo, documentInfo); const type = TypeUtil_1.TypeUtil.findType(entity, conventions.knownEntityTypes); if (removeIdentityProperty) { EntityToJson._tryRemoveIdentityProperty(jsonNode, type, conventions); } return jsonNode; } static convertEntityToJson(entity, conventions, documentInfo, removeIdentityProperty = true) { let typeInfo; const jsonNode = conventions.objectMapper.toObjectLiteral(entity, (_typeInfo) => { typeInfo = _typeInfo; }); EntityToJson._writeMetadata(jsonNode, typeInfo, documentInfo); if (removeIdentityProperty) { EntityToJson._tryRemoveIdentityProperty(jsonNode, typeInfo.typeName, conventions); } return jsonNode; } static _writeMetadata(jsonNode, typeInfo, documentInfo) { if (!documentInfo) { return; } if (documentInfo.metadata) { documentInfo.metadata[Constants_1.CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES] = typeInfo.nestedTypes; documentInfo.metadata[Constants_1.CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] = documentInfo.metadata[Constants_1.CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] || typeInfo.typeName; } function differentNestedTypes() { const existing = documentInfo.metadataInstance[Constants_1.CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES]; if (!existing) { return true; } if (Object.keys(existing).length !== Object.keys(typeInfo.nestedTypes).length) { return true; } for (const key in typeInfo.nestedTypes) { if (typeInfo.nestedTypes[key] !== existing[key]) { return true; } } return false; } if (documentInfo.metadataInstance) { if (differentNestedTypes()) { documentInfo.metadataInstance[Constants_1.CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES] = typeInfo.nestedTypes; } const nodeType = documentInfo.metadataInstance[Constants_1.CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] || typeInfo.typeName; if (documentInfo.metadataInstance[Constants_1.CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] !== nodeType) { documentInfo.metadataInstance[Constants_1.CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE] = nodeType; } } let setMetadata = false; const metadataNode = {}; if (documentInfo.metadata && Object.keys(documentInfo.metadata).length > 0) { setMetadata = true; Object.assign(metadataNode, documentInfo.metadata); const entityMeta = documentInfo.entity[Constants_1.CONSTANTS.Documents.Metadata.KEY]; for (const metadataItem in entityMeta) { if (entityMeta.hasOwnProperty(metadataItem)) { setMetadata = true; metadataNode[metadataItem] = entityMeta[metadataItem]; } } } else if (documentInfo.metadataInstance) { setMetadata = true; Object.assign(metadataNode, documentInfo.metadataInstance); } if (documentInfo.collection) { setMetadata = true; metadataNode["@collection"] = documentInfo.collection; } if (setMetadata) { jsonNode[Constants_1.CONSTANTS.Documents.Metadata.KEY] = metadataNode; } } convertToEntity(targetEntityType, id, document, trackEntity) { const conventions = this._session.conventions; const entityType = conventions.getJsTypeByDocumentType(targetEntityType); try { if (TypeUtil_1.TypeUtil.isType(document, targetEntityType)) { return document; } const documentRef = { value: document }; this._session.onBeforeConversionToEntityInvoke(id, entityType, documentRef); document = documentRef.value; let entity; const documentTypeFromConventions = conventions.getJsType(id, document); const entityTypeInfoFromMetadata = EntityToJson._getEntityTypeInfoFromMetadata(document); if (documentTypeFromConventions) { const passedEntityTypeIsAssignableFromConventionsDocType = entityType && ((entityType.name === documentTypeFromConventions.name) || TypeUtil_1.TypeUtil.isInstanceOf(entityType, documentTypeFromConventions)); if (passedEntityTypeIsAssignableFromConventionsDocType) { const mapper = conventions.objectMapper; entity = mapper.fromObjectLiteral(document, entityTypeInfoFromMetadata); } } if (!entity) { const mapper = conventions.objectMapper; let passedTypeInfo = entityTypeInfoFromMetadata; if (entityType) { passedTypeInfo = Object.assign(passedTypeInfo, { typeName: entityType.name }); } entity = mapper.fromObjectLiteral(document, passedTypeInfo); } const isProjection = !!document[Constants_1.CONSTANTS.Documents.Metadata.PROJECTION]; if (id) { this._session.generateEntityIdOnTheClient.trySetIdentity(entity, id, isProjection); } return entity; } catch (err) { (0, Exceptions_1.throwError)("InvalidOperationException", `Could not convert document ${id} to entity of type ` + `${entityType ? entityType.name : entityType}: ${err.stack}`, err); } } static _getEntityTypeInfoFromMetadata(document) { const metadata = document[Constants_1.CONSTANTS.Documents.Metadata.KEY]; if (!metadata) { return {}; } return { typeName: metadata[Constants_1.CONSTANTS.Documents.Metadata.RAVEN_JS_TYPE], nestedTypes: metadata[Constants_1.CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES] }; } populateEntity(entity, id, document) { if (!id) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Id cannot be null."); } EntityToJson.populateEntity(entity, document, this._session.conventions.objectMapper); this._session.generateEntityIdOnTheClient.trySetIdentity(entity, id); } static populateEntity(entity, document, objectMapper) { if (!entity) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Entity cannot be null"); } if (!document) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Document cannot be null"); } if (!objectMapper) { (0, Exceptions_1.throwError)("InvalidArgumentException", "ObjectMapper cannot be null"); } try { const entityValue = objectMapper.fromObjectLiteral(document); Object.assign(entity, entityValue); } catch (e) { (0, Exceptions_1.throwError)("InvalidOperationException", "Could not populate entity.", e); } } static _tryRemoveIdentityProperty(document, entityType, conventions) { const identityProperty = conventions.getIdentityProperty(entityType); if (!identityProperty) { return false; } delete document[identityProperty]; return true; } static convertToEntity(entityClass, id, document, conventions) { const entityType = conventions.getJsTypeByDocumentType(entityClass); try { let entity; const documentTypeFromConventions = conventions.getJsType(id, document); const entityTypeInfoFromMetadata = EntityToJson._getEntityTypeInfoFromMetadata(document); if (documentTypeFromConventions) { const passedEntityTypeIsAssignableFromConventionsDocType = entityType && ((entityType.name === documentTypeFromConventions.name) || TypeUtil_1.TypeUtil.isInstanceOf(entityType, documentTypeFromConventions)); if (passedEntityTypeIsAssignableFromConventionsDocType) { const mapper = conventions.objectMapper; entity = mapper.fromObjectLiteral(document, entityTypeInfoFromMetadata); } } if (!entity) { const mapper = conventions.objectMapper; let passedTypeInfo = entityTypeInfoFromMetadata; if (entityType) { passedTypeInfo = Object.assign(passedTypeInfo, { typeName: entityType.name }); } entity = mapper.fromObjectLiteral(document, passedTypeInfo); } return entity; } catch (err) { (0, Exceptions_1.throwError)("InvalidOperationException", `Could not convert document ${id} to entity of type ` + `${entityType ? entityType.name : entityType}: ${err.stack}`, err); } } removeFromMissing(entity) { this._missingDictionary.delete(entity); } clear() { this._missingDictionary.clear(); } } exports.EntityToJson = EntityToJson; //# sourceMappingURL=EntityToJson.js.map