gohm

package module
v0.0.0-...-4d19ff1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2015 License: MIT Imports: 7 Imported by: 0

README

Gohm ॐ build

Gohm is a Go port of the popular Ohm Ruby library, it provides a simple interface to store and retrieve your model data in a Redis database.

Ohm compatibility

Gohm implements nothing but the basic usage right now, but expect all or most features in Ohm to be implemented into Gohm as time goes by, contributions are very welcome. :)

Both Ohm and Gohm are powered by ohm-scripts, a set of Lua scripts that bundle common operations and make it easy to write a port such as this one, it also means that by adhering to the ohm standard models stored with Gohm can be loaded from Ohm, and vice-versa.

Gohm Models

Gohm models are simple Go structs which just need to adhere certain criteria, this is how a Gohm model might look like in code:

type User struct{
	ID    string `ohm:"id"`
	Name  string `ohm:"name"`
	Email string `ohm:"email"`
}

As you can see, when you describe your Go struct you specify the fields that Gohm will be persisting by identifying them with tags, just as you would describe JSON serialization, for example.

The two special considerations you need to keep in your struct for it to be Gohm-compliant are:

  • It must have a field tagged as id.
  • All fields tagged with ohm need to be public (capitalized)*

(*) Only necessary because of how Go's reflection works, Gohms need to be able to manipulate the attribute and it's value.

Setting up the Gohm connection.

You can set up a new connection easily:

import(
  "github.com/pote/gohm"
)

// Set up using the `REDIS_URL` env variable.
Gohm, _ := gohm.NewConnection()

// Or use your own Redis pool.
import(
  "github.com/pote/gohm"
  "github.com/pote/redisurl"
)

pool := redisurl.NewPoolWithUrl("redis://:password@localhost:6379", 3, 200, "240s")
Gohm, _ := gohm.NewConnection(pool)
Saving a model
u := User{
  Name: "Marty",
  Email: "marty@mcfly.com",
}

Gohm.Save(&u)

u.ID //=> "1"
Retrieving a model by id
u := User{ID: "1"}
Gohm.Load(&u)

u.Name //=> "Marty"

Documentation

Index

Constants

View Source
const LUA_SAVE string = `` /* 3173-byte string literal not displayed */

Variables

View Source
var NoIDError error = errors.New(`model does not have an ohm:"id" tagged field`)
View Source
var NoStructError error = errors.New(`model is not a struct`)
View Source
var NonExportedAttrError error = errors.New(`can't put ohm tags in unexported fields`)
View Source
var NonStringIDError error = errors.New(`model's ohm:"id" field is not a string`)

Functions

This section is empty.

Types

type Connection

type Connection struct {
	RedisPool *redis.Pool
	// contains filtered or unexported fields
}

func NewConnection

func NewConnection(r ...*redis.Pool) (*Connection, error)

func NewConnectionWithPool

func NewConnectionWithPool(pool *redis.Pool) *Connection

func (*Connection) Load

func (c *Connection) Load(model interface{}) (err error)

func (*Connection) Save

func (c *Connection) Save(model interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL