import { SchemaOverview } from '@directus/schema/dist/types/overview' ; import { Column } from 'knex-schema-inspector/dist/types/column' ; import getLocalType from './get-local-type' ; import logger from '../logger' ; import env from '../env' ; export default function getDefaultValue ( column ) { const type = getLocalType ( column ) ; let defaultValue = column . default_value ?? null ; if ( defaultValue === null ) return null ; if ( defaultValue === 'null' ) return null ; if ( defaultValue === 'NULL' ) return null ; if ( typeof defaultValue === 'string' && ( ( defaultValue . startsWith ( `'` ) && defaultValue . endsWith ( `'` ) ) || ( defaultValue . startsWith ( `"` ) && defaultValue . endsWith ( `"` ) ) ) ) { defaultValue = defaultValue . slice ( 1 , - 1 ) ; } if ( defaultValue === '0000-00-00 00:00:00' ) return null ; switch ( type ) { case 'bigInteger' : case 'integer' : case 'decimal' : case 'float' : return Number . isNaN ( Number ( defaultValue ) ) === false ? Number ( defaultValue ) : defaultValue ; case 'boolean' : return castToBoolean ( defaultValue ) ; case 'json' : return castToObject ( defaultValue ) ; default : return defaultValue ; } } function castToBoolean ( value ) { if ( typeof value === 'boolean' ) return value ; if ( value === 0 || value === '0' ) return false ; if ( value === 1 || value === '1' ) return true ; if ( value === 'false' || value === false ) return false ; if ( value === 'true' || value === true ) return true ; return Boolean ( value ) ; } function castToObject ( value ) { if ( ! value ) return value ; if ( typeof value === 'object' ) return value ; if ( typeof value === 'string' ) { try { return JSON . parse ( value ) ; } catch ( err ) { if ( env . NODE_ENV === 'development' ) { logger . error ( err ) ; } return value ; } } return { } ; }