@@ -12,6 +12,8 @@ const oramaHeaders = {
1212 Authorization : `Bearer ${ API_KEY } ` ,
1313} ;
1414
15+ console . log ( `Syncing ${ siteContent . length } documents to Orama Cloud index` ) ;
16+
1517// Orama allows to send several documents at once, so we batch them in groups of 50.
1618// This is not strictly necessary, but it makes the process faster.
1719const runUpdate = async ( ) => {
@@ -22,38 +24,55 @@ const runUpdate = async () => {
2224 batches . push ( siteContent . slice ( i , i + batchSize ) ) ;
2325 }
2426
27+ console . log ( `Sending ${ batches . length } batches of ${ batchSize } documents` ) ;
28+
2529 await Promise . all ( batches . map ( insertBatch ) ) ;
2630} ;
2731
2832// We call the "notify" API to upsert the documents in the index.
2933// Orama will keep a queue of all the documents we send, and will process them once we call the "deploy" API.
3034// Full docs on the "notify" API: https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks#updating-removing-inserting-elements-in-a-live-index
31- const insertBatch = async batch =>
32- await fetch ( `${ ORAMA_API_BASE_URL } /notify` , {
35+ const insertBatch = async batch => {
36+ const { ok , statusText } = await fetch ( `${ ORAMA_API_BASE_URL } /notify` , {
3337 method : 'POST' ,
3438 headers : oramaHeaders ,
3539 body : JSON . stringify ( { upsert : batch } ) ,
3640 } ) ;
3741
42+ if ( ! ok ) {
43+ throw new Error ( `Request to "/notify" failed with status: ${ statusText } ` ) ;
44+ }
45+ } ;
46+
3847// We call the "deploy" API to trigger a deployment of the index, which will process all the documents in the queue.
3948// Full docs on the "deploy" API: https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks#deploying-the-index
40- const triggerDeployment = async ( ) =>
41- await fetch ( `${ ORAMA_API_BASE_URL } /deploy` , {
49+ const triggerDeployment = async ( ) => {
50+ const { ok , statusText } = await fetch ( `${ ORAMA_API_BASE_URL } /deploy` , {
4251 method : 'POST' ,
4352 headers : oramaHeaders ,
4453 } ) ;
4554
55+ if ( ! ok ) {
56+ throw new Error ( `Request to "/deploy" failed with status: ${ statusText } ` ) ;
57+ }
58+ } ;
59+
4660// We call the "snapshot" API to empty the index before inserting the new documents.
4761// The "snapshot" API is typically used to replace the entire index with a fresh set of documents, but we use it here to empty the index.
4862// This operation gets queued, so the live index will still be available until we call the "deploy" API and redeploy the index.
4963// Full docs on the "snapshot" API: https://docs.oramasearch.com/cloud/data-sources/custom-integrations/webhooks#inserting-a-snapshot
50- const emptyOramaIndex = async ( ) =>
51- await fetch ( `${ ORAMA_API_BASE_URL } /snapshot` , {
64+ const emptyOramaIndex = async ( ) => {
65+ const { ok , statusText } = await fetch ( `${ ORAMA_API_BASE_URL } /snapshot` , {
5266 method : 'POST' ,
5367 headers : oramaHeaders ,
5468 body : JSON . stringify ( [ ] ) ,
5569 } ) ;
5670
71+ if ( ! ok ) {
72+ throw new Error ( `Request to "/snapshot" failed with status: ${ statusText } ` ) ;
73+ }
74+ } ;
75+
5776// Now we proceed to call the APIs in order:
5877// 1. Empty the index
5978// 2. Insert the documents
@@ -63,3 +82,5 @@ const emptyOramaIndex = async () =>
6382await emptyOramaIndex ( ) ;
6483await runUpdate ( ) ;
6584await triggerDeployment ( ) ;
85+
86+ console . log ( 'Orama Cloud sync completed successfully!' ) ;
0 commit comments