Files
gocqlx/README.md

273 lines
8.4 KiB
Markdown
Raw Permalink Normal View History

# 🚀 GocqlX [![GoDoc](https://pkg.go.dev/badge/github.com/scylladb/gocqlx/v3.svg)](https://pkg.go.dev/github.com/scylladb/gocqlx/v3) [![Go Report Card](https://goreportcard.com/badge/github.com/scylladb/gocqlx)](https://goreportcard.com/report/github.com/scylladb/gocqlx) [![Build Status](https://travis-ci.org/scylladb/gocqlx.svg?branch=master)](https://travis-ci.org/scylladb/gocqlx)
2017-07-24 16:22:30 +02:00
2020-06-09 09:19:13 +02:00
GocqlX makes working with Scylla easy and less error-prone.
Its 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
## Compatibility
Versions of GocqlX prior to v3.0.0 are compatible with both [Apache Cassandras gocql](https://github.com/apache/cassandra-gocql-driver) and [ScyllaDBs fork](https://github.com/scylladb/gocql).
However, starting with v3.0.0, GocqlX exclusively supports the scylladb/gocql driver.
If you are using GocqlX v3.0.0 or newer, you must ensure your `go.mod` includes a replace directive to point to ScyllaDBs fork:
```go
// Use the latest version of scylladb/gocql; check for updates at https://github.com/scylladb/gocql/releases
replace github.com/gocql/gocql => github.com/scylladb/gocql v1.16.0
```
This is required because GocqlX relies on ScyllaDB-specific extensions and bug fixes introduced in the gocql fork. Attempting to use the standard gocql driver with GocqlX v3.0.0+ may lead to build or runtime issues.
## Features
2017-07-24 16:22:30 +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
Subpackages provide additional functionality:
2017-07-25 14:30:13 +02:00
* CQL query builder ([package qb](https://github.com/scylladb/gocqlx/blob/master/qb))
* CRUD operations based on table model ([package table](https://github.com/scylladb/gocqlx/blob/master/table))
* Database migrations ([package migrate](https://github.com/scylladb/gocqlx/blob/master/migrate))
2017-08-03 18:39:16 +02:00
## Installation GocqlX
Add GocqlX to your Go module:
```bash
go get github.com/scylladb/gocqlx/v3
```
## Installation schemagen
Unfortunately you can't install it via `go install`, since `go.mod` contains `replace` directive.
So, you have to check it out and install manually:
2020-04-29 09:35:08 +02:00
```bash
git clone git@github.com:scylladb/gocqlx.git
cd gocqlx/cmd/schemagen/
go install .
2020-04-29 09:35:08 +02:00
```
## Getting started
First, import the required packages:
```go
import (
"fmt"
"log"
"github.com/gocql/gocql"
"github.com/scylladb/gocqlx/v3"
"github.com/scylladb/gocqlx/v3/qb"
"github.com/scylladb/gocqlx/v3/table"
)
```
Wrap gocql Session:
```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 {
log.Fatal(err)
}
defer session.Close()
```
Specify table model:
2017-07-25 14:30:13 +02:00
```go
// 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)
// Person represents a row in person table.
// Field names are converted to snake case by default, no need to add special tags.
// A field will not be persisted by adding the `db:"-"` tag or making it unexported.
2017-07-28 10:18:38 +02:00
type Person struct {
FirstName string
LastName string
Email []string
HairColor string `db:"-"` // exported and skipped
eyeColor string // unexported also skipped
2017-07-25 14:30:13 +02:00
}
```
2017-07-25 14:30:13 +02:00
Bind data from a struct and insert a row:
```go
p := Person{
"Michał",
"Matczuk",
[]string{"michal@scylladb.com"},
"red", // not persisted
"hazel" // not persisted
2017-07-25 14:30:13 +02:00
}
q := session.Query(personTable.Insert()).BindStruct(p)
if err := q.ExecRelease(); err != nil {
log.Fatal(err)
2017-07-25 14:30:13 +02:00
}
```
2018-05-25 08:02:16 +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 {
log.Fatal(err)
}
fmt.Println(p)
// stdout: {Michał Matczuk [michal@scylladb.com]}
```
Load all rows in to a slice:
```go
var people []Person
q := session.Query(personTable.Select()).BindMap(qb.M{"first_name": "Michał"})
if err := q.SelectRelease(&people); err != nil {
log.Fatal(err)
2018-05-25 08:02:16 +02:00
}
fmt.Println(people)
// stdout: [{Michał Matczuk [michal@scylladb.com]}]
2017-07-25 14:30:13 +02:00
```
2021-11-13 13:55:44 +02:00
## Generating table metadata with schemagen
Installation
```bash
go get -u "github.com/scylladb/gocqlx/v3/cmd/schemagen"
2021-11-13 13:55:44 +02:00
```
Usage:
```bash
schemagen [flags]
2021-11-13 13:55:44 +02:00
Flags:
-cluster string
a comma-separated list of host:port tuples (default "127.0.0.1")
-keyspace string
keyspace to inspect (required)
-output string
the name of the folder to output to (default "models")
-pkgname string
the name you wish to assign to your generated package (default "models")
```
Example:
Running the following command for `examples` keyspace:
```bash
schemagen -cluster="127.0.0.1:9042" -keyspace="examples" -output="models" -pkgname="models"
2021-11-13 13:55:44 +02:00
```
Generates `models/models.go` as follows:
```go
// Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT.
package models
import "github.com/scylladb/gocqlx/v3/table"
2021-11-13 13:55:44 +02:00
// Table models.
var (
Playlists = table.New(table.Metadata{
Name: "playlists",
Columns: []string{
"album",
"artist",
"id",
"song_id",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{
"title",
"album",
"artist",
},
})
Songs = table.New(table.Metadata{
Name: "songs",
Columns: []string{
"album",
"artist",
"data",
"id",
"tags",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{},
})
)
2021-11-13 13:55:44 +02:00
```
## Examples
2020-04-23 17:44:01 +02:00
You can find lots of examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
Go and run them locally:
```bash
make run-scylla
make run-examples
```
2017-08-30 11:02:55 +02:00
## Training
The course [Using Scylla Drivers](https://university.scylladb.com/courses/using-scylla-drivers) in Scylla University explains how to use drivers in different languages to interact with a Scylla cluster. The lesson, [Golang and Scylla Part 3 - GoCQLX](https://university.scylladb.com/courses/using-scylla-drivers/lessons/golang-and-scylla-part-3-gocqlx/), goes over a sample application that, using GoCQLX, interacts with a three-node Scylla cluster. It connects to a Scylla cluster, displays the contents of a table, inserts and deletes data, and shows the contents of the table after each action. [Scylla University](https://university.scylladb.com/) includes other training material and online courses which will help you become a Scylla NoSQL database expert.
2017-07-28 14:38:19 +02:00
## Performance
GocqlX performance is comparable to the raw `gocql` driver.
Below benchmark results running on my laptop.
2017-07-28 14:38:19 +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-23 17:44:01 +02:00
See the benchmark in [benchmark_test.go](https://github.com/scylladb/gocqlx/blob/master/benchmark_test.go).
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!