import { Type } from './Type' ; import type { Platform } from '../platforms' ; import type { EntityProperty } from '../typings' ; import { ValidationError } from '../errors' ; export class DateType extends Type < Date , string > { convertToDatabaseValue ( value , platform ) { if ( value instanceof Date ) { return value . toISOString ( ) . substr ( 0 , 10 ) ; } if ( ! value || value . toString ( ) . match ( /^\d{4}-\d{2}-\d{2}$/ ) ) { return value as string ; } throw ValidationError . invalidType ( DateType , value , 'JS' ) ; } convertToJSValue ( value , platform ) { if ( ! value || value instanceof Date ) { return value as Date ; } const date = new Date ( value ) ; if ( date . toString ( ) === 'Invalid Date' ) { throw ValidationError . invalidType ( DateType , value , 'database' ) ; } return date ; } compareAsType ( ) { return 'string' ; } getColumnType ( prop , platform ) { return platform . getDateTypeDeclarationSQL ( prop . length ) ; } toJSON ( value , platform ) { return this . convertToDatabaseValue ( value , platform ) ; } }