import { CodeGenDirective , CodeGenField , CodeGenModel , CodeGenModelMap } from '../visitors/appsync-visitor' ; import { CodeGenConnectionType , CodeGenFieldConnection , flattenFieldDirectives , makeConnectionAttributeName , } from './process-connections' ; import { getConnectedFieldV2 } from './process-connections-v2' ; export function processBelongsToConnection ( field , model , modelMap , connectionDirective ) { const otherSide = modelMap [ field . type ] ; const otherSideField = getConnectedFieldV2 ( field , model , otherSide , connectionDirective . name ) ; const connectionFields = connectionDirective . arguments . fields || [ ] ; if ( connectionFields . length > 1 ) { throw new Error ( 'DataStore only support one key in field' ) ; } if ( field . isList ) { throw new Error ( `A list field does not support the 'belongsTo' relation` ) ; } let validOtherSideField = false ; otherSideField . directives . forEach ( dir => { if ( dir . name === 'hasOne' || dir . name === 'hasMany' ) { validOtherSideField = true ; } } ) ; if ( ! validOtherSideField ) { throw new Error ( `A 'belongsTo' field should match to a corresponding 'hasMany' or 'hasOne' field` ) ; } const otherSideHasMany = otherSideField . isList ; const isConnectingFieldAutoCreated = false ; return { kind : CodeGenConnectionType . BELONGS_TO , connectedModel : otherSide , isConnectingFieldAutoCreated , targetName : connectionFields [ 0 ] || ( otherSideHasMany ? makeConnectionAttributeName ( otherSide . name , otherSideField . name ) : makeConnectionAttributeName ( model . name , field . name ) ) , } ; } export function getBelongsToConnectedField ( field , model , connectedModel ) { let otherSideDirectives = flattenFieldDirectives ( connectedModel ) . filter ( dir => { const connectedField = connectedModel . fields . find ( connField => { return connField . name === dir . fieldName ; } ) ; const fieldType = connectedField ?. type ; return ( ( dir . name === 'hasOne' && ! connectedField ?. isList ) || ( dir . name === 'hasMany' && connectedField ?. isList ) ) && model . name === fieldType ; } ) ; if ( otherSideDirectives ?. length === 1 ) { return connectedModel . fields . find ( connField => { return connField . name === otherSideDirectives [ 0 ] . fieldName ; } ) ; } }