Giter Club home page Giter Club logo

go-nested-set's Introduction

Go Nested Set

build

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.

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/griffinqiu/go-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/griffinqiu/go-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/griffinqiu/go-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 ./...

License

MIT

go-nested-set's People

Contributors

calebx avatar cxiang03 avatar griffinqiu avatar huacnlee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.