Gocqlx with interfaces.
igocqlx
has (almost, to be continued) functions as gocqlx
but provides interfaces to wrap functions from the package.
This is designed to make it easier to mock calls.
You can check it out gocqlxmock
, a simple package to mock your igocqlx
calls.
go get github.com/Guilospanck/igocqlx
The best way to teach how to use something is by examples. Here you can see examples showing the differences between a normal gocqlx
and a igocqlx
calls.
package models
import "github.com/scylladb/gocqlx/v2/table"
type TrackingDataTable struct {
Table *table.Table
}
func NewTrackingDataTable() *TrackingDataTable {
trackingDataMetadata := table.Metadata{
Name: "tracking_data",
Columns: []string{
"first_name", "last_name", "timestamp", "heat",
"location", "speed", "telepathy_powers",
},
PartKey: []string{"first_name", "last_name"},
SortKey: []string{"timestamp"},
}
trackingDataTable := table.New(trackingDataMetadata)
return &TrackingDataTable{
Table: trackingDataTable,
}
}
// using igocqlx
package models
import (
igocqlxtable "github.com/Guilospanck/igocqlx/table"
"github.com/scylladb/gocqlx/v2/table"
)
type TrackingDataTable struct {
Table igocqlxtable.ITable
}
func NewTrackingDataTable() *TrackingDataTable {
trackingDataMetadata := igocqlxtable.Metadata{
M: &table.Metadata{
Name: "tracking_data",
Columns: []string{
"first_name", "last_name", "timestamp", "heat",
"location", "speed", "telepathy_powers",
},
PartKey: []string{"first_name", "last_name"},
SortKey: []string{"timestamp"},
},
}
trackingDataTable := igocqlxtable.New(*trackingDataMetadata.M)
return &TrackingDataTable{
Table: trackingDataTable,
}
}
// using gocqlx
import (
"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/v2"
)
func (conn *scyllaDBConnection) createSession(cluster *gocql.ClusterConfig) (*gocqlx.Session, error) {
session, err := gocqlx.WrapSession(cluster.CreateSession())
if err != nil {
conn.logger.Error("An error occurred while creating DB session: ", zap.Error(err))
return nil, err
}
return &session, nil
}
// using igocqlx
import (
"github.com/Guilospanck/igocqlx"
"github.com/gocql/gocql"
)
func (conn *scyllaDBConnection) createSession(cluster *gocql.ClusterConfig) (igocqlx.ISessionx, error) {
session, err := igocqlx.WrapSession(cluster.CreateSession())
if err != nil {
conn.logger.Error(fmt.Sprintf("An error occurred while creating DB session: %s", err.Error()))
return nil, err
}
return session, nil
}