@@ -3,14 +3,17 @@ import { OramaCloud } from '@orama/core';
33import { getDocuments } from './get-documents.mjs' ;
44import { ORAMA_SYNC_BATCH_SIZE } from '../../next.constants.mjs' ;
55
6- // The following follows the instructions at https://docs.orama .com/cloud/data-sources/custom-integrations/webhooks
6+ // The following follows the instructions at https://docs.oramasearch .com/docs/ cloud/data-sources/rest-APIs/using-rest-apis
77
88const orama = new OramaCloud ( {
99 projectId : process . env . NEW_ORAMA_PROJECT_ID || '' ,
1010 apiKey : process . env . NEW_ORAMA_PRIVATE_API_KEY || '' ,
1111} ) ;
1212
1313const datasource = orama . dataSource ( process . env . NEW_ORAMA_DATASOURCE_ID || '' ) ;
14+
15+ // Create a temporary index to perform the insertions
16+ const temporary = await datasource . createTemporaryIndex ( ) ;
1417const documents = await getDocuments ( ) ;
1518
1619console . log ( `Syncing ${ documents . length } documents to Orama Cloud index` ) ;
@@ -27,16 +30,15 @@ const runUpdate = async () => {
2730
2831 console . log ( `Sending ${ batches . length } batches of ${ batchSize } documents` ) ;
2932
33+ // Insert documents batch by batch into the temporary index
3034 for ( const batch of batches ) {
31- await datasource . insertDocuments ( batch ) ;
35+ await temporary . insertDocuments ( batch ) ;
3236 }
37+
38+ // Once all documents are inserted into the temporary index, we swap it with the live one atomically.
39+ await temporary . swap ( ) ;
3340} ;
3441
35- // Now we proceed to call the APIs in order.
36- // The previous implementation used to empty the index before inserting new documents
37- // to remove documents that are no longer in the source.
38- // The new API from @orama /core might have a different approach for full sync.
39- // Based on the provided examples, we are now only running the update.
4042await runUpdate ( ) ;
4143
4244console . log ( 'Orama Cloud sync completed successfully!' ) ;
0 commit comments