import { CodeGenDirective , CodeGenModel } from '../visitors/appsync-visitor' ; import { getDirective } from './fieldUtils' ; param model export const processIndex = ( model ) => { const indexMap = model . fields . reduce ( ( acc , field ) => { const indexDirective = getDirective ( field ) ( 'index' ) ; if ( ! indexDirective ) { return acc ; } return { ... acc , [ field . name ] : indexDirective } ; } , { } as Record < string , CodeGenDirective > ) ; const keyList = Object . entries ( indexMap ) . map ( ( [ fieldName , directive ] ) => ( { name : 'key' , arguments : { name : directive . arguments . name , queryField : directive . arguments . queryField , fields : [ fieldName ] . concat ( ( directive . arguments . sortKeyFields as string [ ] ) ?? [ ] ) , } , } ) ) ; const existingIndexNames = model . directives . filter ( directive => directive . name === 'key' && ! ! directive . arguments . name ) . map ( directive => directive . arguments . name ) ; const deDupedKeyList = keyList . filter ( key => ! existingIndexNames . includes ( key . arguments . name ) ) ; model . directives . push ( ... deDupedKeyList ) ; } ;