Skip to content

Instantly share code, notes, and snippets.

@Gurpartap
Last active August 29, 2015 14:06
Show Gist options
  • Save Gurpartap/e6e1420931f6becf67b2 to your computer and use it in GitHub Desktop.
Save Gurpartap/e6e1420931f6becf67b2 to your computer and use it in GitHub Desktop.
A user model based on zoom (a lightweight redis orm)
package models
import "github.com/albrow/zoom"
func Init() {
if err := zoom.Register(&User{}); err != nil {
panic(err)
}
}
package main
import (
"./app/models"
"github.com/albrow/zoom"
)
func main() {
zoom.Init(nil)
defer zoom.Close()
models.Init()
...
}
package models
import (
"github.com/albrow/zoom"
)
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
zoom.DefaultData
zoom.Sync
}
func (m User) FindAll() []*User {
var users []*User
if err := zoom.NewQuery("User").Scan(&users); err != nil {
panic(err)
}
for _, u := range users {
u.Unlock()
}
return users
}
func (m User) FindById(id string) *User {
u := &User{}
if err := zoom.ScanById(id, u); err != nil {
panic(err)
}
// defer u.Unlock()
return u
}
func (u User) DeleteById(id string) {
if err := zoom.DeleteById("User", id); err != nil {
panic(err)
}
defer u.Unlock()
}
func (u *User) Save() {
if err := zoom.Save(u); err != nil {
panic(err)
}
defer u.Unlock()
}
func (u *User) Delete() {
if err := zoom.Delete(u); err != nil {
panic(err)
}
defer u.Unlock()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment