christopherhesse / rethinkgo

OBSOLETE Go language driver for RethinkDB
http://www.rethinkdb.com
BSD 3-Clause "New" or "Revised" License
137 stars 6 forks source link

rethinkgo

NOTE: No future work is planned on this driver. See @dancannon's gorethink instead.

Go language driver for RethinkDB made by Christopher Hesse

Current supported RethinkDB version: 1.7.1

Installation

go get -u github.com/christopherhesse/rethinkgo

If you do not have the goprotobuf runtime installed, it is required:

brew install mercurial  # if you do not have mercurial installed
go get code.google.com/p/goprotobuf/{proto,protoc-gen-go}

Example

package main

import (
    "fmt"
    r "github.com/christopherhesse/rethinkgo"
)

type Employee struct {
    FirstName string
    LastName  string
    Job       string
    Id        string `json:"id,omitempty"` // (will appear in json as "id", and not be sent if empty)
}

func main() {
    // To access a RethinkDB database, you connect to it with the Connect function
    session, err := r.Connect("localhost:28015", "company_info")
    if err != nil {
        fmt.Println("error connecting:", err)
        return
    }

    var response []Employee
    // Using .All(), we can read the entire response into a slice, without iteration
    err = r.Table("employees").Run(session).All(&response)
    if err != nil {
        fmt.Println("err:", err)
    } else {
        fmt.Println("response:", response)
    }

    // If we want to iterate over each result individually, we can use the rows
    // object as an iterator
    rows := r.Table("employees").Run(session)
    for rows.Next() {
        var row Employee
        if err = rows.Scan(&row); err != nil {
            fmt.Println("err:", err)
            break
        }
        fmt.Println("row:", row)
    }
    if err = rows.Err(); err != nil {
        fmt.Println("err:", err)
    }
}

Overview

The Go driver is most similar to the official Javascript driver.

Most of the functions have the same names as in the Javascript driver, only with the first letter capitalized. See Go Driver Documentation for examples and documentation for each function.

To use RethinkDB with this driver, you build a query using r.Table(), for example, and then call query.Run(session) to execute the query on the server and return an iterator for the results.

There are 3 convenience functions on the iterator if you don't want to iterate over the results, .One(&result) for a query that returns a single result, .All(&results) for multiple results, and .Exec() for a query that returns an empty response (for instance, r.TableCreate(string)) or to ignore the response.

The important types are r.Exp (for RethinkDB expressions), r.Query (interface for all queries, including expressions), r.List (used for Arrays, an alias for []interface{}), and r.Map (used for Objects, an alias for map[string]interface{}).

The function r.Expr() can take arbitrary structs and uses the "json" module to serialize them. This means that structs can use the json.Marshaler interface (define a method MarshalJSON() on the struct). Also, struct fields can also be annotated to specify their JSON equivalents:

type MyStruct struct {
    MyField int `json:"my_field"` // (will appear in json as my_field)
    OtherField int // (will appear in json as OtherField)
}

See the json docs for more information.

Changelog

Changes for RethinkDB 1.7.1:

Changes for RethinkDB 1.6.1:

Differences from official RethinkDB drivers