diff --git a/database/cb.go b/database/cb.go index aaaab4d..b9d1bb1 100644 --- a/database/cb.go +++ b/database/cb.go @@ -7,12 +7,15 @@ import ( "github.com/couchbase/gocb/v2" "github.com/ghanithan/goBilling/config" + "github.com/ghanithan/goBilling/instrumentation" ) // Module for couchbase db wrapper type couchbaseDatabase struct { - Db *gocb.Cluster + Db *gocb.Cluster + Bucket *gocb.Bucket + logger instrumentation.GoLogger } var ( @@ -20,7 +23,8 @@ var ( dbInstance couchbaseDatabase ) -func InitCouchbaseDb(config *config.Config) couchbaseDatabase { +func InitCouchbaseDb(config *config.Config, logger instrumentation.GoLogger) couchbaseDatabase { + defer logger.TimeTheFunction(time.Now(), "InitCouchbaseDb") once.Do(func() { options := gocb.ClusterOptions{ Authenticator: gocb.PasswordAuthenticator{ @@ -49,18 +53,50 @@ func InitCouchbaseDb(config *config.Config) couchbaseDatabase { log.Fatal(err) } + bucket := cluster.Bucket(config.Db.Reponame) + err = bucket.WaitUntilReady(5*time.Second, nil) + if err != nil { + log.Fatal(err) + } + dbInstance = couchbaseDatabase{ - Db: cluster, + Db: cluster, + Bucket: bucket, + logger: logger, } }) return dbInstance } -func (cb *couchbaseDatabase) GetDb() *gocb.Cluster { +func (cb *couchbaseDatabase) GetDbInstance() *gocb.Cluster { return cb.Db } +func (cb *couchbaseDatabase) GetDb() *gocb.Bucket { + return cb.Bucket +} + func (cb *couchbaseDatabase) Close() { closeOpt := gocb.ClusterCloseOptions{} cb.Db.Close(&closeOpt) } + +func (cb *couchbaseDatabase) FetchById(collection string, id string) (*gocb.GetResult, error) { + getResult, err := cb.Bucket.Collection(collection).Get(id, &gocb.GetOptions{}) + if err != nil { + cb.logger.Error("Issue in fetching from collection: %q", err) + return nil, err + } + + return getResult, nil +} + +func ExtractContent[T any](getResult *gocb.GetResult, extractedOutput *T, logger instrumentation.GoLogger) error { + err := getResult.Content(getResult) + if err != nil { + logger.Error("Issue in unmarsheling: %q", err) + return err + } + return nil + +} diff --git a/database/cb_test.go b/database/cb_test.go index 4029ebc..957b125 100644 --- a/database/cb_test.go +++ b/database/cb_test.go @@ -3,12 +3,27 @@ package database import ( "fmt" "testing" + "time" "github.com/couchbase/gocb/v2" "github.com/ghanithan/goBilling/config" + "github.com/ghanithan/goBilling/instrumentation" ) +type userType struct { + Id string `json:custid` + Name string `json: name` + Address struct { + Street string `json: street` + City string `json: city` + Zipcode string `json: zipcode` + } `json: address` +} + func TestInitCouchbaseDb(t *testing.T) { + logger := instrumentation.InitInstruments() + defer logger.TimeTheFunction(time.Now(), "TestInitCouchbaseDb") + if testing.Short() { t.Skip("skipping test in short mode.") } @@ -17,13 +32,13 @@ func TestInitCouchbaseDb(t *testing.T) { t.Fatalf("Error in reading config: %s", err) } - db := InitCouchbaseDb(config) + db := InitCouchbaseDb(config, logger) - cluster := db.GetDb() + cluster := db.GetDbInstance() // col := bucket.Scope("_default").Collection("customers") - // // Get the document back + // Get the document back // getResult, err := col.Get("custid:C1", nil) // if err != nil { // t.Errorf("Error fetching: %s", err) @@ -52,15 +67,31 @@ func TestInitCouchbaseDb(t *testing.T) { fmt.Printf("interface result: %v\n", intfc) } - // type UserType struct { - // Id string `json:custid` - // Name string `json: name` - // Address struct { - // Street string `json: street` - // City string `json: city` - // Zipcode string `json: zipcode` - // } `json: address` - // } + getResult, _ := db.GetDb().Collection("customers").Get("C1", &gocb.GetOptions{}) + + user := &userType{} + + err = getResult.Content(&user) + if err != nil { + t.Errorf("Issue in unmarsheling: %q", err) + + } + + fmt.Println("output: ", user) + + user1 := &userType{} + + getResult, err = db.FetchById("customers", "C1") + + if err != nil { + t.Errorf("Error in FetchById: %s", err) + } + + if err := ExtractContent(getResult, user1, logger); err != nil { + t.Errorf("Error in extracting content: %s", err) + + } + t.Errorf("output: %s", user1) // user := &UserType{} // err = getResult.Content(&user) diff --git a/database/database.go b/database/database.go index 83c37f0..1105b66 100644 --- a/database/database.go +++ b/database/database.go @@ -6,5 +6,5 @@ type DbService interface { } type DbStub interface { - FetchById(...string) any + FetchById(string, string) (any, error) } diff --git a/instrumentation/logger.go b/instrumentation/logger.go index af05728..85f0f87 100644 --- a/instrumentation/logger.go +++ b/instrumentation/logger.go @@ -32,7 +32,7 @@ func (l *GoLogger) Error(msg string, args ...any) { l.logger.Error(msg, args...) } -func TimeTheFunction(start time.Time, functionName string) { +func (l *GoLogger) TimeTheFunction(start time.Time, functionName string) { elapsed := time.Since(start) log.Printf("%s took %dms to complete", functionName, elapsed.Nanoseconds()/1000) }