Gocassa is a high-level library on top of gocql.
Compared to gocql it provides query building, adds data binding, and provides easy-to-use "recipe" tables for common query use-cases. Unlike cqlc, it does not use code generation.
The raw CQL table pretty much lets you write any CQL query. Here is an example:
package main
import(
"fmt"
"time"
"github.com/hailocab/gocassa"
)
type Sale struct {
Id string
CustomerId string
SellerId string
Price int
Created time.Time
}
func main() {
keySpace, err := gocassa.ConnectToKeySpace("test", []string{"127.0.0.1"}, "", "")
if err != nil {
panic(err)
}
sales := keySpace.Table("sale", Sale{}, gocassa.Keys{
PartitionKeys: []string{"Id"},
})
err = sales.Set(Sale{
Id: "sale-1",
CustomerId: "customer-1",
SellerId: "seller-1",
Price: 42,
Created: time.Now(),
})
if err != nil {
panic(err)
}
result := &Sale{}
if err := sales.Where(Eq("Id", "sale-1")).Query().ReadOne(result); err != nil {
panic(err)
}
fmt.Println(*result)
}
OneToOneTable
provides only very simple CRUD functionality:
sales := keySpace.OneToOneTable("sale", "Id", Sale{})
// …
result := &Sale{}
err := sales.Read("sale-1", result)
}
OneToManyTable
can list rows filtered by equality of a single field (eg. list sales based on their sellerId
):
saleTables := keySpace.OneToManyTable("sale", "SellerId", "Id", Sale{})
// …
results := &[]Sale{}
err := sales.List("seller-1", nil, 0, results)
TimeSeriesTable
provides an interface to list rows within a time interval:
salesTable := keySpace.TimeSeriesTable("sale", "Created", "Id", Sale{})
//...
results := &[]Sale{}
err := sales.List(yesterdayTime, todayTime, results)
TimeSeriesBTable
is like a cross between OneToManyTable
and TimeSeriesTable
. It can list rows within a time interval, and filtered by equality of a single field. The following lists sales in a time interval, by a certain seller:
salesTable := keySpace.TimeSeriesBTable("sale", "SellerId", "Created", "Id", Sale{})
//...
results := &[]Sale{}
err := sales.List("seller-1", yesterdayTime, todayTime, results)