import { EntitySchemaDatatype } from "../schema/entity-schema-datatype" ; import { EntitySchemaField } from "../schema/entity-schema-field" ; import { EntitySchemaService } from "../schema/entity-schema.service" ; /**
 * Datatype for the EntitySchemaService transforming values of an array recursively.
 *
 * (De)serialize each item in an array according to the given Datatype transformer.
 *
 * As TypeScript array types are not reliable, you have to explicitly configure the dataType for array items with the annotation.
 * For example:
 *
 * `@DatabaseField({ innerDataType: 'month' }) dateArr: Date[];`
 * will ensure that in the database this property is saved as an array of "month" date strings
 * using the  monthEntitySchemaDatatype (e.g. resulting in `['2020-01', '2020-04']` in the database).
  export const arrayEntitySchemaDatatype = { name : "array" , transformToDatabaseFormat : ( value , schemaField , schemaService , parent ) => { if ( ! Array . isArray ( value ) ) { console . warn ( `property to be transformed with "array" EntitySchema is not an array` , value , parent ) ; return value ; } const arrayElementDatatype = schemaService . getDatatypeOrDefault ( schemaField . innerDataType ) ; return value . map ( ( el ) => arrayElementDatatype . transformToDatabaseFormat ( el , generateSubSchemaField ( schemaField ) , schemaService , parent ) ) ; } , transformToObjectFormat : ( value , schemaField , schemaService , parent ) => { if ( ! Array . isArray ( value ) ) { console . warn ( 'property to be transformed with "array" EntitySchema is not an array' , value , parent ) ; return value ; } const arrayElementDatatype = schemaService . getDatatypeOrDefault ( schemaField . innerDataType ) ; return value . map ( ( el ) => arrayElementDatatype . transformToObjectFormat ( el , generateSubSchemaField ( schemaField ) , schemaService , parent ) ) ; } , } ; ignore param arraySchemaField export function generateSubSchemaField ( arraySchemaField ) { const subSchemaField = Object . assign ( { } , arraySchemaField ) ; subSchemaField . dataType = arraySchemaField . innerDataType ; delete subSchemaField . innerDataType ; return subSchemaField ; }