import { EntitySchemaDatatype } from "../schema/entity-schema-datatype" ; /**
 * Datatype for the EntitySchemaService transforming Date values to/from a short string month format ("YYYY-mm").
 *
 * Throws an exception if the property is set to something that is not a Date instance and cannot be cast to Date either.
 *
 * For example:
 *
 * `@DatabaseField({dataType: 'month'}) myMonth: Date = new Date('2020-01-15'); // will be "2020-01" in the database`
 */ export const monthEntitySchemaDatatype = { name : "month" , transformToDatabaseFormat : ( value ) => { if ( ! ( value instanceof Date ) ) { value = new Date ( value ) ; } return ( value . getFullYear ( ) . toString ( ) + "-" + ( value . getMonth ( ) + 1 ) . toString ( ) . replace ( /^(\d)$/g , "0$1" ) ) ; } , transformToObjectFormat : ( value ) => { value = value . toString ( ) . replace ( /-(\d)-/g , "-0$1-" ) . replace ( /-(\d)$/g , "-0$1" ) ; const date = new Date ( value ) ; if ( Number . isNaN ( date . getTime ( ) ) ) { throw new Error ( "failed to convert data to Date object: " + value ) ; } return date ; } , } ;