{"id":19674089,"url":"https://github.com/longbridge/nested-set","last_synced_at":"2025-04-29T01:31:05.429Z","repository":{"id":40484701,"uuid":"318485141","full_name":"longbridge/nested-set","owner":"longbridge","description":"Nested Set is an Go implementation of the Nested set model for Gorm.","archived":false,"fork":false,"pushed_at":"2023-01-10T02:53:07.000Z","size":95,"stargazers_count":69,"open_issues_count":4,"forks_count":19,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-25T00:58:40.531Z","etag":null,"topics":["gorm","nested","nested-set","tree"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/longbridge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-04T10:43:08.000Z","updated_at":"2025-03-27T15:11:40.000Z","dependencies_parsed_at":"2023-02-08T16:45:33.785Z","dependency_job_id":null,"html_url":"https://github.com/longbridge/nested-set","commit_stats":null,"previous_names":["griffinqiu/go-nested-set","longbridge/nested-set","longbridgeapp/nested-set"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longbridge%2Fnested-set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longbridge%2Fnested-set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longbridge%2Fnested-set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longbridge%2Fnested-set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/longbridge","download_url":"https://codeload.github.com/longbridge/nested-set/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251415706,"owners_count":21585876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gorm","nested","nested-set","tree"],"created_at":"2024-11-11T17:17:11.603Z","updated_at":"2025-04-29T01:31:04.384Z","avatar_url":"https://github.com/longbridge.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nested Set for Go\n\n[![build](https://github.com/longbridgeapp/nested-set/workflows/build/badge.svg)](https://github.com/longbridgeapp/nested-set/actions?query=workflow%3Abuild)\n\nNested Set is an implementation of the [Nested set model](https://en.wikipedia.org/wiki/Nested_set_model) for [Gorm](https://gorm.io/index.html).\n\nThis project is the Go version of [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set), which uses the same data structure design, so it uses the same data together with [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set).\n\n\u003e Actually the original design is for this, the content managed by [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set) in our Rails application, the front-end Go API also needs to be maintained.\n\nThis is a Go version of the [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set), and it built for compatible with [awesome_nested_set](https://github.com/collectiveidea/awesome_nested_set).\n\n## What Go Nested Set can do?\n\nFor manage a nested tree node like this:\n\n![Showcase](https://user-images.githubusercontent.com/5518/103117256-8717d900-46a4-11eb-9079-743051b59104.gif)\n\n\u003e Video taken from [BlueDoc](https://github.com/huacnlee/bluedoc), used by awesome_nested_set + [react-dnd](https://react-dnd.github.io/react-dnd/examples/sortable/simple).\n\n## Installation\n\n```\ngo get github.com/longbridgeapp/nested-set\n```\n\n## Usage\n\n### Define the model\n\nYou must use `nestedset` Stuct tag to define your Gorm model like this:\n\nSupport struct tags:\n\n- `id` - int64 - Primary key of the node\n- `parent_id` - sql.NullInt64 - ParentID column, null is root\n- `lft` - int\n- `rgt` - int\n- `depth` - int - Depth of the node\n- `children_count` - Number of children\n\nOptional:\n\n- `scope` - restricts what is to be considered a list. You can also setup scope by multiple attributes.\n\nExample:\n\n```go\nimport (\n\t\"database/sql\"\n\t\"github.com/longbridgeapp/nested-set\"\n)\n\n// Category\ntype Category struct {\n\tID            int64         `gorm:\"PRIMARY_KEY;AUTO_INCREMENT\" nestedset:\"id\"`\n\tParentID      sql.NullInt64 `nestedset:\"parent_id\"`\n\tUserType      string        `nestedset:\"scope\"`\n\tUserID        int64         `nestedset:\"scope\"`\n\tRgt           int           `nestedset:\"rgt\"`\n\tLft           int           `nestedset:\"lft\"`\n\tDepth         int           `nestedset:\"depth\"`\n\tChildrenCount int           `nestedset:\"children_count\"`\n\tTitle         string\n}\n```\n\n### Move Node\n\n```go\nimport nestedset \"github.com/longbridgeapp/nested-set\"\n\n// create a new node root level last child\nnestedset.Create(tx, \u0026node, nil)\n\n// create a new node as parent first child\nnestedset.Create(tx, \u0026node, \u0026parent)\n\n// nestedset.MoveDirectionLeft\n// nestedset.MoveDirectionRight\n// nestedset.MoveDirectionInner\nnestedset.MoveTo(tx, node, to, nestedset.MoveDirectionLeft)\n```\n\n### Get Nodes with tree order\n\n```go\n// With scope, limit tree in a scope\ntx := db.Model(\u0026Category{}).Where(\"user_type = ? AND user_id = ?\", \"User\", 100)\n\n// Get all nodes\ncategories, _ := tx.Order(\"lft asc\").Error\n\n// Get root nodes\ncategories, _ := tx.Where(\"parent_id IS NULL\").Order(\"lft asc\").Error\n\n// Get childrens\ncategories, _ := tx.Where(\"parent_id = ?\", parentCategory.ID).Order(\"lft asc\").Error\n```\n\n## Testing\n\n```bash\n$ createdb nested-set-test\n$ go test ./...\n```\n\n```SQL\n-- some useful sql to check status\nSELECT n.id,\nCONCAT(REPEAT('. . ', (COUNT(p.id) - 1)::int), n.title) AS t,\nn.title, n.lft, n.rgt, n.depth, n.children_count\nFROM categories AS n, categories AS p\nWHERE (n.lft BETWEEN p.lft AND p.rgt)\nGROUP BY n.id ORDER BY n.lft;\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flongbridge%2Fnested-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flongbridge%2Fnested-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flongbridge%2Fnested-set/lists"}