8
8
waitForCollection ,
9
9
waitForQueryIndex ,
10
10
waitForScope ,
11
+ waitForSearchIndex ,
11
12
waitForUser ,
12
13
whoami ,
13
14
} from '@cbjsdev/http-client' ;
@@ -19,18 +20,21 @@ import {
19
20
CouchbaseClusterChangeCreateCollection ,
20
21
CouchbaseClusterChangeCreateIndex ,
21
22
CouchbaseClusterChangeCreateScope ,
23
+ CouchbaseClusterChangeCreateSearchIndex ,
22
24
CouchbaseClusterChangeCreateUser ,
23
25
CouchbaseClusterChangeDropBucket ,
24
26
CouchbaseClusterChangeDropCollection ,
25
27
CouchbaseClusterChangeDropIndex ,
26
28
CouchbaseClusterChangeDropScope ,
29
+ CouchbaseClusterChangeDropSearchIndex ,
27
30
CouchbaseClusterChangeDropUser ,
28
31
CouchbaseClusterChangeRecreateBucket ,
29
32
CouchbaseClusterChangeRecreateIndex ,
30
33
CouchbaseClusterChangeRecreateUser ,
31
34
CouchbaseClusterChangeUpdateBucket ,
32
35
CouchbaseClusterChangeUpdateCollection ,
33
36
CouchbaseClusterChangeUpdateIndex ,
37
+ CouchbaseClusterChangeUpdateSearchIndex ,
34
38
CouchbaseClusterChangeUpdateUser ,
35
39
CouchbaseClusterChangeUpdateUserPassword ,
36
40
} from './types.js' ;
@@ -44,6 +48,11 @@ export type ChangeOptions = {
44
48
timeout ?: number ;
45
49
} ;
46
50
51
+ function getTimePrefix ( ) {
52
+ const d = new Date ( ) ;
53
+ return `[${ d . getHours ( ) } :${ d . getMinutes ( ) } :${ d . getSeconds ( ) } ]` ;
54
+ }
55
+
47
56
export async function applyCouchbaseClusterChanges (
48
57
cluster : Cluster ,
49
58
apiConfig : CouchbaseHttpApiConfig ,
@@ -80,6 +89,9 @@ export async function applyCouchbaseClusterChanges(
80
89
dropUser : applyDropUser ,
81
90
recreateUser : applyRecreateUser ,
82
91
updateUserPassword : applyUpdateUserPassword ,
92
+ createSearchIndex : applyUpsertSearchIndex ,
93
+ updateSearchIndex : applyUpsertSearchIndex ,
94
+ dropSearchIndex : applyDropSearchIndex ,
83
95
} ;
84
96
85
97
for ( const change of changes ) {
@@ -611,7 +623,89 @@ async function applyDropUser(
611
623
console . log ( `${ getTimePrefix ( ) } User "${ change . user . username } " dropped` ) ;
612
624
}
613
625
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
+ ) ;
617
711
}
0 commit comments