import { EntitySchemaDatatype } from "../schema/entity-schema-datatype" ; import { EntitySchemaField } from "../schema/entity-schema-field" ; import { EntitySchemaService } from "../schema/entity-schema.service" ; import { generateSubSchemaField } from "./datatype-array" ; /**
 * Datatype for the EntitySchemaService transforming values of a javascript `Map` recursively.
 *
 * (De)serialize each value according to the given Datatype transformer, similar to how arrays are handled.
 *
 * For example:
 *
 * `@DatabaseField({ innerDataType: 'month' }) dateMap: Map<string, Date>;`
 * will ensure that in the database this property is saved as "month" date string for each key
 * using the  monthEntitySchemaDatatype (e.g. resulting in `[['a', '2020-01'], ['b', '2020-04']]` in the database).
  export const mapEntitySchemaDatatype = { name : "map" , transformToDatabaseFormat : ( value , schemaField , schemaService , parent ) => { if ( ! ( value instanceof Map ) ) { console . warn ( 'property to be saved with "map" EntitySchema is not of expected type' , value ) ; return value ; } const innerElementDatatype = schemaService . getDatatypeOrDefault ( schemaField . innerDataType ) ; const result = [ ] ; value . forEach ( ( item , key ) => { result . push ( [ key , innerElementDatatype . transformToDatabaseFormat ( item , generateSubSchemaField ( schemaField ) , schemaService , parent ) , ] ) ; } ) ; return result ; } , transformToObjectFormat : ( value , schemaField , schemaService , parent ) => { if ( value instanceof Map ) { return value ; } if ( ! Array . isArray ( value ) || value === null ) { console . warn ( 'property to be loaded with "map" EntitySchema is not valid' , value ) ; return value ; } const innerElementType = schemaService . getDatatypeOrDefault ( schemaField . innerDataType ) ; const result = new Map ( ) ; for ( const keyValue of value ) { const transformedElement = innerElementType . transformToObjectFormat ( keyValue [ 1 ] , generateSubSchemaField ( schemaField ) , schemaService , parent ) ; result . set ( keyValue [ 0 ] , transformedElement ) ; } return result ; } , } ;