11import { readFileSync } from "node:fs" ;
22import { globSync } from "glob" ;
3+ import { OramaCloud } from "@orama/core" ;
34import { generalPurposeCrawler } from "@orama/crawly" ;
45import "dotenv/config" ;
56
67const ORAMA_PRIVATE_API_KEY = process . env . ORAMA_PRIVATE_API_KEY ;
7- const ORAMA_PRIVATE_INDEX_ID = process . env . ORAMA_PRIVATE_INDEX_ID ;
8+ const ORAMA_DATASOURCE_ID = process . env . ORAMA_DATASOURCE_ID ;
9+ const ORAMA_PROJECT_ID = process . env . ORAMA_PROJECT_ID ;
810
911const baseURL = new URL ( "../dist" , import . meta. url ) . pathname ;
1012const HTMLFiles = globSync ( "**/*.html" , { cwd : baseURL } ) ;
@@ -18,71 +20,41 @@ const pagesToIndex = HTMLFiles.flatMap((file) => {
1820
1921 const productionDocsURL = `https://docs.solidjs.com/${ path } ` ;
2022
21- return {
22- ...generalPurposeCrawler ( productionDocsURL , pageContent , {
23- parseCodeBlocks : false ,
24- } ) [ 0 ] ,
25- contentWithCode : generalPurposeCrawler ( productionDocsURL , pageContent ) ?. [ 0 ]
26- ?. content ,
27- } ;
28- } ) ;
23+ const content = generalPurposeCrawler ( productionDocsURL , pageContent , { parseCodeBlocks : false } ) [ 0 ] ;
24+ const contentWithCode = generalPurposeCrawler ( productionDocsURL , pageContent , { parseCodeBlocks : true } ) [ 0 ] ;
2925
30- async function emptyIndex ( ) {
31- await fetch (
32- `https://api.oramasearch.com/api/v1/webhooks/${ ORAMA_PRIVATE_INDEX_ID } /snapshot` ,
33- {
34- method : "POST" ,
35- headers : {
36- "Content-Type" : "application/json" ,
37- authorization : `Bearer ${ ORAMA_PRIVATE_API_KEY } ` ,
38- } ,
39- body : JSON . stringify ( [ ] ) ,
40- }
41- ) ;
42- }
26+ const fullContent = {
27+ title : content . title ,
28+ path : content . path ,
29+ content : content . content ,
30+ contentWithCode : contentWithCode . content ,
31+ }
4332
44- async function upsertFreshData ( ) {
45- const batches = [ ] ;
46- const batchesSize = 25 ;
33+ if ( content ?. category ) {
34+ fullContent . category = `enum(' ${ content . category } ')`
35+ }
4736
48- for ( let i = 0 ; i < pagesToIndex . length ; i += batchesSize ) {
49- const batch = pagesToIndex . slice ( i , i + batchesSize ) ;
50- batches . push ( batch ) ;
37+ if ( content ?. section ) {
38+ fullContent . section = `enum('${ content . section } ')`
5139 }
5240
53- for ( let i = 0 ; i < batches . length ; i ++ ) {
54- const batch = batches [ i ] ;
41+ return fullContent
42+ } ) ;
5543
56- await fetch (
57- `https://api.oramasearch.com/api/v1/webhooks/${ ORAMA_PRIVATE_INDEX_ID } /notify` ,
58- {
59- method : "POST" ,
60- headers : {
61- "Content-Type" : "application/json" ,
62- authorization : `Bearer ${ ORAMA_PRIVATE_API_KEY } ` ,
63- } ,
64- body : JSON . stringify ( {
65- upsert : batch ,
66- } ) ,
67- }
68- ) ;
69- }
70- }
44+ const orama = new OramaCloud ( {
45+ apiKey : ORAMA_PRIVATE_API_KEY ,
46+ projectId : ORAMA_PROJECT_ID
47+ } )
7148
72- async function deployIndex ( ) {
73- await fetch (
74- `https://api.oramasearch.com/api/v1/webhooks/${ ORAMA_PRIVATE_INDEX_ID } /deploy` ,
75- {
76- method : "POST" ,
77- headers : {
78- authorization : `Bearer ${ ORAMA_PRIVATE_API_KEY } ` ,
79- } ,
80- }
81- ) ;
49+ const index = orama . index . set ( ORAMA_DATASOURCE_ID )
50+
51+ console . log ( `[Orama] - Indexing ${ pagesToIndex . length } documents to Orama...` )
8252
83- console . log ( "Index deployed" ) ;
84- }
53+ const tempIndexId = `tempIndex-${ Date . now ( ) } `
54+ await index . createTemporaryIndex ( tempIndexId )
55+ const tempIdx = orama . index . set ( tempIndexId )
56+ await tempIdx . insertDocuments ( pagesToIndex )
57+ await index . swapTemporaryIndex ( ORAMA_DATASOURCE_ID , tempIndexId )
58+ await orama . index . delete ( tempIndexId )
8559
86- await emptyIndex ( ) ;
87- await upsertFreshData ( ) ;
88- await deployIndex ( ) ;
60+ console . log ( `[Orama] - Indexed ${ pagesToIndex . length } documents to Orama.` )
0 commit comments