1+ import process from 'node:process' ;
12const { marked } = require ( 'marked' )
23const TerminusClient = require ( "@terminusdb/terminusdb-client" ) ;
34const client = new TerminusClient . WOQLClient ( 'https://cloud.terminusdb.com/TerminatorsX' ,
45 { organization :'TerminatorsX' , db : 'terminusCMS_docs' } )
56client . setApiKey ( process . env . TERMINUSDB_API_TOKEN )
67
8+
9+
10+ function getChangesUrl ( action = 'changes' ) {
11+ const clientCopy = client . copy ( )
12+ clientCopy . connectionConfig . api_extension = 'api/'
13+ if ( clientCopy . connectionConfig . baseServer ) {
14+ clientCopy . connectionConfig . server = clientCopy . connectionConfig . baseServer
15+ }
16+ return clientCopy . connectionConfig . dbBase ( action )
17+ }
18+
19+ async function createCR ( ) {
20+ const payload = {
21+ original_branch : 'main' ,
22+ name : `CR_indexing__${ Date . now ( ) } ` ,
23+ message : 'Create change request for indexing' ,
24+ author : client . user ( )
25+ }
26+
27+ const createURL = getChangesUrl ( )
28+
29+ const result = await client . sendCustomRequest ( 'POST' , createURL , payload )
30+ return result
31+ }
32+
33+ async function mergeChangeRequest ( currentCR ) {
34+ // I need to submit the change request before merge it
35+ await client . sendCustomRequest ( 'PUT' , `${ getChangesUrl ( ) } /${ currentCR } ` , { message : 'Submitted CR' , status : 'Submitted' } )
36+ // I merge the change request, this will trigger the indexing process
37+ return await client . sendCustomRequest ( 'PUT' , `${ getChangesUrl ( ) } /${ currentCR } ` , { message : 'MERGED CR' , status : 'Merged' } )
38+ }
39+
40+ const timeout = ( time ) => new Promise ( ( resolve ) => setTimeout ( resolve , time ) )
41+
42+ async function pollingCall ( commitid , callBack ) {
43+ try {
44+ const pollingUrl = `${ getChangesUrl ( 'indexes' ) } /${ commitid } /check`
45+ const document = await client . sendCustomRequest ( 'GET' , pollingUrl )
46+ if ( document . indexing_status !== 'Assigned' && document . indexing_status !== 'Error' ) {
47+ await timeout ( 10000 )
48+ await pollingCall ( commitid , callBack )
49+ } else {
50+ return callBack ( document )
51+ }
52+ } catch ( err ) {
53+ console . log ( err . message )
54+ clearTimeout ( timeout )
55+ }
56+ }
57+
58+ function callBack ( document ) {
59+ if ( document . indexing_status === 'Error' ) {
60+ console . log ( 'Error' , document . error_message )
61+ } else {
62+ console . log ( 'Finished' )
63+ }
64+ }
65+
766function parseDocument ( document ) {
867 const mdBody = document [ 'body' ] . value
968 const tokens = marked . lexer ( mdBody , { } )
@@ -24,6 +83,8 @@ function parseDocument(document) {
2483}
2584
2685async function main ( ) {
86+ const { changeRequestId, branchName } = await createCR ( )
87+ client . checkout ( branchName )
2788 const docs = await client . getDocument ( { as_list : true , type : 'Page' } )
2889 const newDocs = [ ]
2990 for ( let doc of docs ) {
@@ -33,7 +94,22 @@ async function main() {
3394 newDocs . push ( doc )
3495 }
3596 }
97+ const sections = await client . getDocument ( { as_list : true , type : 'Sections' } )
98+ const sectionIds = sections . map ( x => x [ '@id' ] )
99+ await client . deleteDocument ( { id : sectionIds } )
36100 await client . updateDocument ( docs , { create : true } , "" , "Update sections from script" )
101+ try {
102+ const response = await mergeChangeRequest ( changeRequestId )
103+ if ( response . tracking_branch_last_commit ) {
104+ // or maybe you can put an await
105+ pollingCall ( response . tracking_branch_last_commit , callBack )
106+ }
107+ }
108+ catch ( err ) {
109+ const message = err . data && typeof err . data === 'object' ? JSON . stringify ( err . data , null , 4 ) : err . message
110+ process . exit ( 1 ) ;
111+ console . log ( 'ERROR' , message )
112+ }
37113}
38114
39115main ( )
0 commit comments