Documentation ¶
Index ¶
Constants ¶
View Source
const ( TableName = "[table_tree]" ColumnIDAttr = "[id]" ColumnParentIDAttr = "[parent_id]" ColumnTreeIDAttr = "[tree_id]" ColumnLeftAttr = "[left]" ColumnRightAttr = "[right]" ColumnLevelAttr = "[level]" DefaultIDColumn = "ID" DefaultParentIDColumn = "ParentID" DefaultTreeIDColumn = "TreeID" DefaultLeftColumn = "Lft" DefaultRightColumn = "Rght" DefaultLevelColumn = "Lvl" )
MPTT Table consts
Variables ¶
View Source
var ( UnsupportedPositionError = errors.New("unsupported position error") ModelTypeError = errors.New("tree node data should be a pointer") )
Functions ¶
Types ¶
type KeyColumnFields ¶
type KeyColumnFields struct { IDFieldName string ParentFieldName string TreeIDFieldName string LeftFieldName string RightFieldName string LevelFieldName string }
KeyColumnFields for custom mptt model without embedded struct ModelBase
type KeyField ¶
KeyField mptt has id parent_id tree_id left right level fields id parent_id tree_id only support positive integer or string left right level only support positive integer
type ModelBase ¶
type ModelBase struct { ID int `gorm:"primaryKey" json:"id"` ParentID int `gorm:"default:0;index" json:"parent_id"` TreeID int `gorm:"index" json:"tree_id"` Lvl int `gorm:"index" json:"lvl"` Lft int `gorm:"index" json:"lft"` Rght int `gorm:"index" json:"rght"` }
ModelBase default mptt base model for user to embedded
type Option ¶
type Option func(options *treeOptions)
Option ...
func WithAttrs ¶
func WithAttrs(fields KeyColumnFields) Option
WithAttrs custom model without Embedded Struct ModelBase
type PositionEnum ¶
type PositionEnum string
const ( LastChild PositionEnum = "last-child" FirstChild PositionEnum = "first-child" Left PositionEnum = "left" Right PositionEnum = "right" )
type TreeManager ¶
type TreeManager interface { GormDB() *gorm.DB CreateNode(node interface{}) error InsertNode(node, target interface{}, position PositionEnum) error MoveNode(node, target interface{}, position PositionEnum, refreshTarget ...bool) (bool, error) MoveNodeByID(nodeID, targetID interface{}, position PositionEnum) (bool, error) DeleteNode(n interface{}, doNotRefresh ...bool) error DeleteNodeByID(nodeID interface{}) error Rebuild() error PartialRebuild(treeID int) error RefreshNode(node interface{}) error Node(node interface{}) TreeNode }
TreeManager ...
func NewTreeManager ¶
func NewTreeManager(db *gorm.DB, modelPtr interface{}, opts ...Option) (TreeManager, error)
NewTreeManager create mptt tree manager
type TreeNode ¶
type TreeNode interface { GetAncestors(outListPtr interface{}, ascending, includeSelf bool) error GetDescendants(outListPtr interface{}, includeSelf bool) error GetFamily(outListPtr interface{}) error // my ancestors and my descendants GetChildren(outListPtr interface{}) error GetLeafNodes(outListPtr interface{}) error GetSiblings(outListPtr interface{}, includeSelf bool) error GetNextSibling(outPtr interface{}, conds ...interface{}) error GetPreviousSibling(outPtr interface{}, conds ...interface{}) error GetRoot(outPtr interface{}) error GetDescendantCount() int GetLevel() int IsChildNode() bool IsLeafNode() bool IsRootNode() bool IsDescendantOf(other interface{}, includeSelf bool) bool IsAncestorOf(other interface{}, includeSelf bool) bool }
Source Files ¶
Click to show internal directories.
Click to hide internal directories.