@@ -64,6 +64,34 @@ async function indexData() {
6464 apiKey : process . env . TYPESENSE_API_KEY ,
6565 } ) ;
6666
67+ let collectionExists = false ;
68+ try {
69+ await client . collections ( process . env . TYPESENSE_COLLECTION ) . retrieve ( ) ;
70+ collectionExists = true ;
71+ } catch ( error ) {
72+ if ( error . httpStatus !== 404 ) {
73+ throw error ;
74+ }
75+ }
76+
77+ if ( collectionExists ) {
78+ await client . collections ( process . env . TYPESENSE_COLLECTION ) . delete ( ) ;
79+ console . log ( `Collection ${ process . env . TYPESENSE_COLLECTION } deleted successfully.` ) ;
80+ }
81+
82+ await client . collections ( ) . create ( {
83+ name : process . env . TYPESENSE_COLLECTION ,
84+ fields : [
85+ { name : 'objectID' , type : 'string' } ,
86+ { name : 'title' , type : 'string' } ,
87+ { name : 'search_keyword' , type : 'string' } ,
88+ { name : 'slug' , type : 'string' } ,
89+ { name : 'excerpt' , type : 'string' } ,
90+ { name : 'headings' , type : 'string[]' , facet : false }
91+ ]
92+ } ) ;
93+ console . log ( `Collection ${ process . env . TYPESENSE_COLLECTION } created successfully.` ) ;
94+
6795 const response = await request ( 'http://localhost:8001/___graphql' , pageQuery ) ;
6896 console . log ( 'response' , response ) ;
6997 const data = await response ;
0 commit comments