2020-04-20 18:00:01 +02:00
# 🚀 GocqlX [](http://godoc.org/github.com/scylladb/gocqlx) [](https://goreportcard.com/report/github.com/scylladb/gocqlx) [](https://travis-ci.org/scylladb/gocqlx)
2017-07-24 16:22:30 +02:00
2020-04-20 18:00:01 +02:00
GocqlX makes working with Scylla easy and error prone without sacrificing performance.
It’ s inspired by [Sqlx ](https://github.com/jmoiron/sqlx ), a tool for working with SQL databases, but it goes beyond what Sqlx provides.
2017-07-24 16:22:30 +02:00
2020-04-20 18:00:01 +02:00
## Features
2017-07-24 16:22:30 +02:00
2020-04-20 18:00:01 +02:00
* Binding query parameters from struct fields, map, or both
* Scanning query results into structs based on field names
* Convenient functions for common tasks such as loading a single row into a struct or all rows into a slice (list) of structs
* Making any struct a UDT without implementing marshalling functions
* GocqlX is fast. Its performance is comparable to raw driver. You can find some benchmarks [here ](#performance ).
2017-07-24 16:22:30 +02:00
2020-04-20 18:00:01 +02:00
Subpackages provide additional functionality:
2017-07-25 14:30:13 +02:00
2018-10-26 17:01:00 +02:00
* CQL query builder ([package qb ](https://github.com/scylladb/gocqlx/blob/master/qb ))
2020-04-20 18:00:01 +02:00
* CRUD operations based on table model ([package table ](https://github.com/scylladb/gocqlx/blob/master/table ))
2018-10-26 17:01:00 +02:00
* Database migrations ([package migrate ](https://github.com/scylladb/gocqlx/blob/master/migrate ))
2017-08-03 18:39:16 +02:00
2020-04-20 18:00:01 +02:00
## Installation
go get -u github.com/scylladb/gocqlx
## Getting started
2020-02-09 15:11:34 +02:00
2020-04-20 18:00:01 +02:00
Wrap gocql Session:
2020-02-09 15:11:34 +02:00
2020-04-20 18:00:01 +02:00
```go
// Create gocql cluster.
cluster := gocql.NewCluster(hosts...)
// Wrap session on creation, gocqlx session embeds gocql.Session pointer.
session, err := gocqlx.WrapSession(cluster.CreateSession())
if err != nil {
t.Fatal(err)
}
```
Specify table model:
2017-07-25 14:30:13 +02:00
```go
2020-04-20 18:00:01 +02:00
// metadata specifies table name and columns it must be in sync with schema.
var personMetadata = table.Metadata{
Name: "person",
Columns: []string{"first_name", "last_name", "email"},
PartKey: []string{"first_name"},
SortKey: []string{"last_name"},
}
// personTable allows for simple CRUD operations based on personMetadata.
var personTable = table.New(personMetadata)
2018-10-26 17:01:00 +02:00
// Person represents a row in person table.
// Field names are converted to camel case by default, no need to add special tags.
// If you want to disable a field add `db:"-"` tag, it will not be persisted.
2017-07-28 10:18:38 +02:00
type Person struct {
2020-04-20 18:00:01 +02:00
FirstName string
LastName string
Email []string
2017-07-25 14:30:13 +02:00
}
2020-04-20 18:00:01 +02:00
```
2017-07-25 14:30:13 +02:00
2020-04-20 18:00:01 +02:00
Bind data from a struct and insert a row:
2017-08-02 11:12:31 +02:00
2020-04-20 18:00:01 +02:00
```go
p := Person{
"Michał",
"Matczuk",
[]string{"michal@scylladb .com"},
2017-07-25 14:30:13 +02:00
}
2020-04-20 18:00:01 +02:00
q := session.Query(personTable.Insert()).BindStruct(p)
if err := q.ExecRelease(); err != nil {
t.Fatal(err)
2017-07-25 14:30:13 +02:00
}
2020-04-20 18:00:01 +02:00
```
2018-05-25 08:02:16 +02:00
2020-04-20 18:00:01 +02:00
Load a single row to a struct:
```go
p := Person{
"Michał",
"Matczuk",
nil, // no email
}
q := session.Query(personTable.Get()).BindStruct(p)
if err := q.GetRelease(&p); err != nil {
t.Fatal(err)
2018-10-26 17:01:00 +02:00
}
2020-04-20 18:00:01 +02:00
t.Log(p)
// stdout: {Michał Matczuk [michal@scylladb .com]}
```
2018-10-26 17:01:00 +02:00
2020-04-20 18:00:01 +02:00
Load all rows in to a slice:
2018-10-26 17:01:00 +02:00
2020-04-20 18:00:01 +02:00
```go
var people []Person
q := session.Query(personTable.Select()).BindMap(qb.M{"first_name": "Michał"})
if err := q.SelectRelease(&people); err != nil {
t.Fatal(err)
2018-05-25 08:02:16 +02:00
}
2020-04-20 18:00:01 +02:00
t.Log(people)
// stdout: [{Michał Matczuk [michal@scylladb .com]}]
2017-07-25 14:30:13 +02:00
```
2020-04-20 18:00:01 +02:00
## Examples
You can find lots of other examples in [example_test.go ](https://github.com/scylladb/gocqlx/blob/master/example_test.go ), go and run the examples locally:
```bash
make run-scylla
make run-examples
```
2017-08-30 11:02:55 +02:00
2017-07-28 14:38:19 +02:00
## Performance
2020-04-20 18:00:01 +02:00
GocqlX performance is comparable to the raw `gocql` driver.
2020-04-17 11:55:33 +02:00
Below benchmark results running on my laptop.
2017-07-28 14:38:19 +02:00
```
2020-04-17 11:55:33 +02:00
BenchmarkBaseGocqlInsert 2392 427491 ns/op 7804 B/op 39 allocs/op
BenchmarkGocqlxInsert 2479 435995 ns/op 7803 B/op 39 allocs/op
BenchmarkBaseGocqlGet 2853 452384 ns/op 7309 B/op 35 allocs/op
BenchmarkGocqlxGet 2706 442645 ns/op 7646 B/op 38 allocs/op
BenchmarkBaseGocqlSelect 747 1664365 ns/op 49415 B/op 927 allocs/op
BenchmarkGocqlxSelect 667 1877859 ns/op 42521 B/op 932 allocs/op
2017-07-28 14:38:19 +02:00
```
2020-04-20 18:00:01 +02:00
See the benchmark in [benchmark_test.go ](https://github.com/scylladb/gocqlx/blob/master/benchmark_test.go ), you can run the benchmark locally:
```bash
make run-scylla
make bench
```
2017-08-23 10:20:39 +02:00
2017-09-28 15:32:58 +02:00
## License
2017-08-23 10:20:39 +02:00
Copyright (C) 2017 ScyllaDB
2017-09-28 15:32:58 +02:00
This project is distributed under the Apache 2.0 license. See the [LICENSE ](https://github.com/scylladb/gocqlx/blob/master/LICENSE ) file for details.
It contains software from:
2017-08-23 10:20:39 +02:00
* [gocql project ](https://github.com/gocql/gocql ), licensed under the BSD license
* [sqlx project ](https://github.com/jmoiron/sqlx ), licensed under the MIT license
2018-05-25 08:02:16 +02:00
Apache®, Apache Cassandra® are either registered trademarks or trademarks of
2017-08-23 10:20:39 +02:00
the Apache Software Foundation in the United States and/or other countries.
2017-09-28 15:32:58 +02:00
No endorsement by The Apache Software Foundation is implied by the use of these marks.
2017-10-02 09:35:19 +02:00
GitHub star is always appreciated!