Go Nested Set is an implementation of the Nested set model for Gorm.
This project is the Go version of awesome_nested_set, which uses the same data structure design, so it uses the same data together with awesome_nested_set.
Actually the original design is for this, the content managed by awesome_nested_set in our Rails application, the front-end Go API also needs to be maintained.
This is a Go version of the awesome_nested_set, and it built for compatible with awesome_nested_set, so you can use
go get github.com/griffinqiu/go-nested-set
You must use nestedset
Stuct tag to define your Gorm model like this:
Support struct tags:
id
- int64 - Primary key of the nodeparent_id
- int64 - ParentID column, 0 is rootlft
- intrgt
- intdepth
- int - Depth of the nodechildren_count
- Number of children
Example:
import "github.com/griffinqiu/go-nested-set"
// Category
type Category struct {
ID int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" nestedset:"id"`
Title string
ParentID int64 `nestedset:"parent_id"`
UserType string `nestedset:"scope"`
UserID int64 `nestedset:"scope"`
Rgt int `nestedset:"rgt"`
Lft int `nestedset:"lft"`
Depth int `nestedset:"depth"`
ChildrenCount int `nestedset:"children_count"`
}
import nestedset "github.com/griffinqiu/go-nested-set"
// nestedset.MoveDirectionLeft
// nestedset.MoveDirectionRight
// nestedset.MoveDirectionInner
nestedset.MoveTo(tx, node, to, nestedset.MoveDirectionLeft)
// With scope, limit tree in a scope
tx := db.Model(&Category{}).Where("user_type = ? AND user_id = ?", "User", 100)
// Get all nodes
categories, _ := tx.Order("lft asc").Error
// Get root nodes
categories, _ := tx.Where("parent_id IS NULL").Order("lft asc").Error
// Get childrens
categories, _ := tx.Where("parent_id = ?", parentCategory.ID).Order("lft asc").Error
$ createdb nested-set-test
$ go test ./...
MIT