Skip to content

Instantly share code, notes, and snippets.

@mattatcha
Created July 28, 2014 14:35
Show Gist options
  • Save mattatcha/2d5aa8c744cf63332f99 to your computer and use it in GitHub Desktop.
Save mattatcha/2d5aa8c744cf63332f99 to your computer and use it in GitHub Desktop.
package handler
import (
"github.com/gin-gonic/gin"
db "github.com/lmdtfy/lmdtfy/pkg/store"
)
func CreateRepo(c *gin.Context) {
query := db.Repos().Insert(map[string]string{"Test": "test"})
store.Store.RunWrite(query)
c.JSON(200, "")
return
}
package store
import (
"time"
r "github.com/dancannon/gorethink"
)
var (
sess *r.Session
dbName string
)
// Store represents a connection to the DB and allows you to run queries.
type Store struct {
}
// RunWrite will run a query for the current session.
func (s *Store) RunWrite(term r.Term) (r.WriteResponse, error) {
return term.RunWrite(sess)
}
// Collection represents a table in rethinkdb.
type Collection struct {
r.Term
}
// Repos stores all user repositories.
func Repos() Collection {
return Collection{r.Table("repos")}
}
// Apps stores all apps for a uder.
func Apps() Collection {
return Collection{r.Table("apps")}
}
// Builds stores all builds for a an app.
func Builds() Collection {
return Collection{r.Table("builds")}
}
// Commits stores all commits for a repo/app.
func Commits() Collection {
return Collection{r.Table("commits")}
}
// Settings stores all app (lmdtfy) settings.
func Settings() Collection {
return Collection{r.Table("settings")}
}
// Users stores all app users.
func Users() Collection {
return Collection{r.Table("users")}
}
// Connect establishes connection with rethinkDB
func Connect(address, database string) error {
dbName = database
var err error
sess, err = r.Connect(r.ConnectOpts{
Address: address,
Database: dbName,
MaxIdle: 10,
IdleTimeout: time.Second * 10,
})
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment