88 waitForCollection ,
99 waitForQueryIndex ,
1010 waitForScope ,
11+ waitForSearchIndex ,
1112 waitForUser ,
1213 whoami ,
1314} from '@cbjsdev/http-client' ;
@@ -19,18 +20,21 @@ import {
1920 CouchbaseClusterChangeCreateCollection ,
2021 CouchbaseClusterChangeCreateIndex ,
2122 CouchbaseClusterChangeCreateScope ,
23+ CouchbaseClusterChangeCreateSearchIndex ,
2224 CouchbaseClusterChangeCreateUser ,
2325 CouchbaseClusterChangeDropBucket ,
2426 CouchbaseClusterChangeDropCollection ,
2527 CouchbaseClusterChangeDropIndex ,
2628 CouchbaseClusterChangeDropScope ,
29+ CouchbaseClusterChangeDropSearchIndex ,
2730 CouchbaseClusterChangeDropUser ,
2831 CouchbaseClusterChangeRecreateBucket ,
2932 CouchbaseClusterChangeRecreateIndex ,
3033 CouchbaseClusterChangeRecreateUser ,
3134 CouchbaseClusterChangeUpdateBucket ,
3235 CouchbaseClusterChangeUpdateCollection ,
3336 CouchbaseClusterChangeUpdateIndex ,
37+ CouchbaseClusterChangeUpdateSearchIndex ,
3438 CouchbaseClusterChangeUpdateUser ,
3539 CouchbaseClusterChangeUpdateUserPassword ,
3640} from './types.js' ;
@@ -44,6 +48,11 @@ export type ChangeOptions = {
4448 timeout ?: number ;
4549} ;
4650
51+ function getTimePrefix ( ) {
52+ const d = new Date ( ) ;
53+ return `[${ d . getHours ( ) } :${ d . getMinutes ( ) } :${ d . getSeconds ( ) } ]` ;
54+ }
55+
4756export async function applyCouchbaseClusterChanges (
4857 cluster : Cluster ,
4958 apiConfig : CouchbaseHttpApiConfig ,
@@ -80,6 +89,9 @@ export async function applyCouchbaseClusterChanges(
8089 dropUser : applyDropUser ,
8190 recreateUser : applyRecreateUser ,
8291 updateUserPassword : applyUpdateUserPassword ,
92+ createSearchIndex : applyUpsertSearchIndex ,
93+ updateSearchIndex : applyUpsertSearchIndex ,
94+ dropSearchIndex : applyDropSearchIndex ,
8395 } ;
8496
8597 for ( const change of changes ) {
@@ -611,7 +623,89 @@ async function applyDropUser(
611623 console . log ( `${ getTimePrefix ( ) } User "${ change . user . username } " dropped` ) ;
612624}
613625
614- function getTimePrefix ( ) {
615- const d = new Date ( ) ;
616- return `[${ d . getHours ( ) } :${ d . getMinutes ( ) } :${ d . getSeconds ( ) } ]` ;
626+ async function applyUpsertSearchIndex (
627+ cluster : Cluster ,
628+ apiConfig : CouchbaseHttpApiConfig ,
629+ change :
630+ | CouchbaseClusterChangeCreateSearchIndex
631+ | CouchbaseClusterChangeUpdateSearchIndex ,
632+ opts : ChangeOptions
633+ ) {
634+ const config = change . configFn ( {
635+ sourceName : change . bucket ,
636+ bucketName : change . bucket ,
637+ scopeName : change . scope ,
638+ } ) ;
639+
640+ if ( change . type === 'updateSearchIndex' ) {
641+ const searchIndex = await cluster
642+ . bucket ( change . bucket )
643+ . scope ( change . scope )
644+ . searchIndexes ( )
645+ . getIndex ( change . name ) ;
646+
647+ config . uuid = searchIndex . uuid ;
648+ config . sourceUUID = searchIndex . sourceUuid ;
649+ }
650+
651+ console . log (
652+ `${ getTimePrefix ( ) } Requesting creation of search index "${ change . bucket } # ${ change . name } "`
653+ ) ;
654+
655+ await cluster
656+ . bucket ( change . bucket )
657+ . scope ( change . scope )
658+ . searchIndexes ( )
659+ . upsertIndex ( config , opts ) ;
660+
661+ console . log (
662+ `${ getTimePrefix ( ) } Waiting for search index "${ change . bucket } # ${ change . name } " to be created`
663+ ) ;
664+ await waitForSearchIndex (
665+ apiConfig ,
666+ change . name ,
667+ {
668+ bucket : change . bucket ,
669+ scope : change . scope ,
670+ } ,
671+ opts
672+ ) ;
673+ console . log (
674+ `${ getTimePrefix ( ) } Search index "${ change . bucket } # ${ change . name } " created`
675+ ) ;
676+ }
677+
678+ async function applyDropSearchIndex (
679+ cluster : Cluster ,
680+ apiConfig : CouchbaseHttpApiConfig ,
681+ change : CouchbaseClusterChangeDropSearchIndex ,
682+ opts : ChangeOptions
683+ ) {
684+ console . log (
685+ `${ getTimePrefix ( ) } Requesting deletion of search index "${ change . bucket } # ${ change . name } "`
686+ ) ;
687+
688+ await cluster
689+ . bucket ( change . bucket )
690+ . scope ( change . scope )
691+ . searchIndexes ( )
692+ . dropIndex ( change . name ) ;
693+
694+ console . log (
695+ `${ getTimePrefix ( ) } Waiting for search index "${ change . bucket } # ${ change . name } " to be deleted`
696+ ) ;
697+ await waitForSearchIndex (
698+ apiConfig ,
699+ change . name ,
700+ {
701+ bucket : change . bucket ,
702+ } ,
703+ {
704+ ...opts ,
705+ expectMissing : true ,
706+ }
707+ ) ;
708+ console . log (
709+ `${ getTimePrefix ( ) } Search index "${ change . bucket } # ${ change . name } " deleted`
710+ ) ;
617711}
0 commit comments