All Projects → longbridgeapp → nested-set

longbridgeapp / nested-set

Licence: MIT license
Nested Set is an Go implementation of the Nested set model for Gorm.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to nested-set

stefano-tree
Framework agnostic Nested Set (MPTT) implementation for PHP
Stars: ✭ 24 (-45.45%)
Mutual labels:  tree, nested-set
yii2-jstree-widget
jsTree tree widget for yii2
Stars: ✭ 16 (-63.64%)
Mutual labels:  tree, nested-set
radixdb
Static Radix Tree (Patricia trie) implementation in C
Stars: ✭ 34 (-22.73%)
Mutual labels:  tree
ofxSpaceColonization
Space Colonization algorithm implementation in openFrameworks
Stars: ✭ 62 (+40.91%)
Mutual labels:  tree
leaflet-layer-tree-plugin
No description or website provided.
Stars: ✭ 31 (-29.55%)
Mutual labels:  tree
markdown-explorer
Easily explore, view and edit markdown documentation of a file tree
Stars: ✭ 58 (+31.82%)
Mutual labels:  tree
kdtree-rs
K-dimensional tree in Rust for fast geospatial indexing and lookup
Stars: ✭ 137 (+211.36%)
Mutual labels:  tree
react-binary-tree
Binary Tree Traversal Visualisation
Stars: ✭ 25 (-43.18%)
Mutual labels:  tree
ofxLSystem
3D turtle graphics interpretation of L-Systems
Stars: ✭ 39 (-11.36%)
Mutual labels:  tree
ego
Go微服务.A simple and component-based microservice kit for go.
Stars: ✭ 765 (+1638.64%)
Mutual labels:  gorm
clean-gin
Implementation of clean architecture in Go, Gin with dependency injection.
Stars: ✭ 181 (+311.36%)
Mutual labels:  gorm
dslib
🌿 A library of "connected" data structures
Stars: ✭ 122 (+177.27%)
Mutual labels:  tree
kuu
Modular Go Web Framework based on GORM and Gin.
Stars: ✭ 15 (-65.91%)
Mutual labels:  gorm
tbcnn
Efficient tree-based convolutional neural networks in TensorFlow
Stars: ✭ 121 (+175%)
Mutual labels:  tree
tree-tree
No description or website provided.
Stars: ✭ 15 (-65.91%)
Mutual labels:  tree
extraction
Tree Extraction for JavaScript Object Graphs
Stars: ✭ 70 (+59.09%)
Mutual labels:  tree
go-paginate
Cursor-based go paginator
Stars: ✭ 48 (+9.09%)
Mutual labels:  gorm
qverse
Traverse any data with DPML commands.
Stars: ✭ 25 (-43.18%)
Mutual labels:  tree
gradle-dependencies-viewer
A simple web UI to analyze dependencies for your project based on the text data generated from "gradle dependencies" command.
Stars: ✭ 70 (+59.09%)
Mutual labels:  tree
cp-react-tree-table
A fast, efficient tree table component for ReactJS.
Stars: ✭ 83 (+88.64%)
Mutual labels:  tree

Nested Set for Go

build

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.

What Go Nested Set can do?

For manage a nested tree node like this:

Showcase

Video taken from BlueDoc, used by awesome_nested_set + react-dnd.

Installation

go get github.com/longbridgeapp/nested-set

Usage

Define the model

You must use nestedset Stuct tag to define your Gorm model like this:

Support struct tags:

  • id - int64 - Primary key of the node
  • parent_id - sql.NullInt64 - ParentID column, null is root
  • lft - int
  • rgt - int
  • depth - int - Depth of the node
  • children_count - Number of children

Optional:

  • scope - restricts what is to be considered a list. You can also setup scope by multiple attributes.

Example:

import (
	"database/sql"
	"github.com/longbridgeapp/nested-set"
)

// Category
type Category struct {
	ID            int64         `gorm:"PRIMARY_KEY;AUTO_INCREMENT" nestedset:"id"`
	ParentID      sql.NullInt64 `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"`
	Title         string
}

Move Node

import nestedset "github.com/longbridgeapp/nested-set"

// create a new node root level last child
nestedset.Create(tx, &node, nil)

// create a new node as parent first child
nestedset.Create(tx, &node, &parent)

// nestedset.MoveDirectionLeft
// nestedset.MoveDirectionRight
// nestedset.MoveDirectionInner
nestedset.MoveTo(tx, node, to, nestedset.MoveDirectionLeft)

Get Nodes with tree order

// 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

Testing

$ createdb nested-set-test
$ go test ./...
-- some useful sql to check status
SELECT n.id,
CONCAT(REPEAT('. . ', (COUNT(p.id) - 1)::int), n.title) AS t,
n.title, n.lft, n.rgt, n.depth, n.children_count
FROM categories AS n, categories AS p
WHERE (n.lft BETWEEN p.lft AND p.rgt)
GROUP BY n.id ORDER BY n.lft;

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].