import { Entity } from "../persistence/ITableMetadataStore" ; import { ODATA_TYPE } from "../utils/constants" ; import { getTimestampString } from "../utils/utils" ; import { EdmString } from "./EdmString" ; import { EntityProperty , parseEntityProperty } from "./EntityProperty" ; export class NormalizedEntity { public ref : Entity ; public properties : EntityProperty [ ] = [ ] ; public propertiesMap : { [ property ] : EntityProperty ; } = { } ; public constructor ( entity ) { this . ref = entity ; const partitionKeyProperty = new EntityProperty ( "PartitionKey" , entity . PartitionKey , new EdmString ( entity . PartitionKey ) , true ) ; this . properties . push ( partitionKeyProperty ) ; this . propertiesMap . PartitionKey = partitionKeyProperty ; const rowKeyProperty = new EntityProperty ( "RowKey" , entity . RowKey , new EdmString ( entity . RowKey ) , true ) ; this . properties . push ( rowKeyProperty ) ; this . propertiesMap . RowKey = rowKeyProperty ; entity . properties . Timestamp = getTimestampString ( typeof entity . lastModifiedTime === "string" ? new Date ( entity . lastModifiedTime ) : entity . lastModifiedTime ) ; entity . properties [ "Timestamp@odata.type" ] = "Edm.DateTime" ; for ( const key in entity . properties ) { if ( Object . prototype . hasOwnProperty . call ( entity . properties , key ) ) { const element = entity . properties [ key ] ; if ( this . propertiesMap [ key ] !== undefined ) { continue ; } if ( key . endsWith ( ODATA_TYPE ) ) { continue ; } else { const type = entity . properties [ `${ key }${ ODATA_TYPE }` ] ; if ( type !== undefined && typeof type !== "string" ) { throw RangeError ( `Invalid EdmType value:${ type } for key:${ key }${ ODATA_TYPE }` ) ; } const property = parseEntityProperty ( key , element , type , false ) ; this . properties . push ( property ) ; this . propertiesMap [ key ] = property ; } } } } public toResponseString ( annotationLevel , injections , includes ? ) { const pairs = [ ] ; for ( const key in injections ) { if ( Object . prototype . hasOwnProperty . call ( injections , key ) ) { const value = injections [ key ] ; pairs . push ( `"${ key }":${ JSON . stringify ( value ) }` ) ; } } for ( const pair of this . properties ) { if ( ! includes || includes . has ( pair . name ) ) { const str = pair . toResponseString ( annotationLevel ) ; if ( str ) { pairs . push ( str ) ; } } } return `{${ pairs . join ( "," ) }}` ; } public normalize ( ) { this . ref . properties = { } ; for ( const entity of this . properties ) { entity . normalize ( this . ref ) ; } return this . ref ; } }