author Jeremy Daly <jeremy@jeremydaly.com>
 *  license import parseEntityAttributes from './parseEntityAttributes' ; import { EntityConstructor } from '../classes/Entity' ; import { error } from './utils' ; export interface TrackingInfo { fields : string [ ] ; defaults : any ; required : any ; linked : Linked ; keys : any ; } export interface Linked { [ key ] : string [ ] ; } export interface TrackingInfoKeys { partitionKey ? : string ; sortKey ? : string ; } export type ParsedEntity = ReturnType < typeof parseEntity > ; export function parseEntity ( entity ) { let { name , timestamps , created , createdAlias , modified , modifiedAlias , typeAlias , attributes , autoExecute , autoParse , table , ... args } = entity ; if ( Object . keys ( args ) . length > 0 ) error ( `Invalid Entity configuration options: ${ Object . keys ( args ) . join ( ', ' ) }` ) ; name = typeof name === 'string' && name . trim ( ) . length > 0 ? name . trim ( ) : error ( `'name' must be defined` ) ; timestamps = typeof timestamps === 'boolean' ? timestamps : true ; created = typeof created === 'string' && created . trim ( ) . length > 0 ? created . trim ( ) : '_ct' ; createdAlias = typeof createdAlias === 'string' && createdAlias . trim ( ) . length > 0 ? createdAlias . trim ( ) : 'created' ; modified = typeof modified === 'string' && modified . trim ( ) . length > 0 ? modified . trim ( ) : '_md' ; modifiedAlias = typeof modifiedAlias === 'string' && modifiedAlias . trim ( ) . length > 0 ? modifiedAlias . trim ( ) : 'modified' ; typeAlias = typeof typeAlias === 'string' && typeAlias . trim ( ) . length > 0 ? typeAlias . trim ( ) : 'entity' ; attributes = typeof attributes === 'object' && ! Array . isArray ( attributes ) ? attributes : error ( `Please provide a valid 'attributes' object` ) ; if ( timestamps ) { attributes [ created ] = { type : 'string' , alias : createdAlias , default : ( ) => new Date ( ) . toISOString ( ) } ; attributes [ modified ] = { type : 'string' , alias : modifiedAlias , default : ( ) => new Date ( ) . toISOString ( ) , onUpdate : true } ; } let track = { fields : Object . keys ( attributes ) , defaults : { } , required : { } , linked : { } , keys : { } } ; return Object . assign ( { name , schema : parseEntityAttributes ( attributes , track ) , defaults : track . defaults , required : track . required , linked : track . linked , autoExecute , autoParse , _etAlias : typeAlias } , table ? { table } : { } ) ; } export default parseEntity ;