{"id":18317630,"url":"https://github.com/shimohq/ioredis-tree","last_synced_at":"2025-04-05T21:32:26.428Z","repository":{"id":40639706,"uuid":"46102794","full_name":"shimohq/ioredis-tree","owner":"shimohq","description":"🌲 A robust tree structure implementation for Redis","archived":false,"fork":false,"pushed_at":"2023-04-16T20:49:07.000Z","size":79,"stargazers_count":86,"open_issues_count":6,"forks_count":12,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-21T12:07:48.809Z","etag":null,"topics":["ioredis","redis","tree"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/shimohq.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":"2015-11-13T05:52:05.000Z","updated_at":"2024-12-04T23:31:43.000Z","dependencies_parsed_at":"2022-09-15T19:10:41.319Z","dependency_job_id":null,"html_url":"https://github.com/shimohq/ioredis-tree","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimohq%2Fioredis-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimohq%2Fioredis-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimohq%2Fioredis-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shimohq%2Fioredis-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shimohq","download_url":"https://codeload.github.com/shimohq/ioredis-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247406082,"owners_count":20933803,"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":["ioredis","redis","tree"],"created_at":"2024-11-05T18:06:57.768Z","updated_at":"2025-04-05T21:32:26.076Z","avatar_url":"https://github.com/shimohq.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ioredis-tree\nA robust tree structure implementation for Redis\n\n[![Build Status](https://travis-ci.org/shimohq/ioredis-tree.svg?branch=master)](https://travis-ci.org/shimohq/ioredis-tree)\n[![Dependency Status](https://david-dm.org/shimohq/ioredis-tree.svg)](https://david-dm.org/shimohq/ioredis-tree)\n\n## Install\n\n```shell\nnpm install ioredis-tree\n```\n\n## Usage\n\n```javascript\nvar Redis = require('ioredis');\nvar redis = require('ioredis-tree')(new Redis());\n\nredis.tinsert('files', 'parent', 'node');\n```\n\n## NOTE\n\nStarting from v1.0.0, ioredis-tree assumes that nodes containing char ':' are the root of the tree for performance reasons.\n\n## API\n\n### TINSERT key parent node\n\nInsert `node` to `parent`. If `parent` does not exist, a new tree with root of `parent` is created.\n\n#### Example\n\n```javascript\nredis.tinsert('mytree', '1', '2');\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+-----+\n   |\n+--+--+\n|  2  |\n+-----+\n```\n\n```javascript\nredis.tinsert('mytree', '1', '4');\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+-----+----+\n   |               |\n+--+--+         +--+--+\n|  2  |         |  4  |\n+-----+         +-----+\n```\n\n`TINSERT` accepts one of the three optional options to specified where to insert the node into:\n\n1. `INDEX`: Insert the node to the specified index. Index starts with `0`, and it can also be negative numbers indicating offsets starting at the end of the list. For example, `-1` is the last element of the list, `-2` the penultimate, and so on. If the index is out of the range, the node will insert into the tail (when positive) or head (when negative).\n2. `BEFORE`: Insert the node before the specified node. Insert to the head when the specified node is not found.\n3. `AFTER`: Insert the node after the specified node. Insert to the tail when the specified node is not found.\n\nContinue with our example:\n\n```javascript\nredis.tinsert('mytree', '1', '3', { before: '4' });\n\n// Or:\n// redis.tinsert('mytree', '1', '3', { after: '2' });\n// redis.tinsert('mytree', '1', '3', { index: 1 });\n// redis.tinsert('mytree', '1', '3', { index: -2 });\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |       |       |\n+--+--+ +--+--+ +--+--+\n|  2  | |  3  | |  4  |\n+-----+ +-----+ +-----+\n```\n\n```javascript\nredis.tinsert('mytree', '2', '5', { index: 1000 });\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |       |       |\n+--+--+ +--+--+ +--+--+\n|  2  | |  3  | |  4  |\n+--+--+ +-----+ +-----+\n   |\n+--+--+\n|  5  |\n+-----+\n```\n\nA node can have multiple parents, say we insert `4` into `5`:\n\n```javascript\nredis.tinsert('mytree', '5', '4');\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |       |       |\n+--+--+ +--+--+ +--+--+\n|  2  | |  3  | |  4  |\n+--+--+ +-----+ +-----+\n   |\n+--+--+\n|  5  |\n+--+--+\n   |\n+--+--+\n|  4  |\n+-----+\n```\n\nIt's not allowed to move a node into its posterity, which will lead an error:\n\n```javascript\nredis.tinsert('mytree', '3', '1');\n// ERR parent node cannot be the posterity of new node\n```\n\n### TPARENTS key node\n\nGet the parents of the node. Returns an empty array when doesn't have parent.\n\n```javascript\nredis.tparents('mytree', '5'); // ['2']\nredis.tparents('mytree', '1'); // []\nredis.tparents('mytree', '4'); // ['5', '1']\nredis.tparents('non-exists tree', '1'); // []\nredis.tparents('mytree', 'non-exists node'); // []\n```\n\nThe order of parents is random.\n\n### TPATH key from to\n\nGet the path between `from` and `to`. The depth of `from` must lower than `to`. Return `null` if `from` isn't a ancestor of `to`.\n\n```javascript\nredis.tpath('mytree', '1', '5'); // ['2']\nredis.tpath('mytree', '1', '3'); // []\nredis.tpath('mytree', '1', '7'); // null\n```\n\nIf there's more than one path between the two nodes, the shorter path will be returned:\n\n```javascript\nredis.tpath('mytree', '1', '4'); // []\n```\n\n### TCHILDREN key node\n\nGet the children of the node. Each node has at least two properties:\n\n1. `node`: The node itself.\n2. `hasChild`: `true` or `false`, whether the node has at least one child.\n\nIf the `hasChild` is `true`, there will be an additional `children` property, which is an array containing the children of the node.\n\n\n```javascript\nredis.tchildren('mytree', '1');\n// [\n//   { node: '2', hasChild: true, children: [{ node: '5', hasChild: false }] },\n//   { node: '3', hasChild: false },\n//   { node: '4', hasChild: false }\n// ]\nredis.tchildren('mytree', '5'); // []\nredis.tchildren('non-exists tree', '1'); // []\nredis.tchildren('mytree', 'non-exists node'); // []\n```\n\n`TCHILDREN` accepts a `LEVEL` option to specified how many levels of children to fetch:\n\n```javascript\nredis.tchildren('mytree', '1', { level: 1 });\n// [\n//   { node: '2', hasChild: true },\n//   { node: '3', hasChild: false },\n//   { node: '4', hasChild: false }\n// ]\n```\n\nNotice that although node '2' has a child (its `hasChild` is `true`), it doesn't has the `children` property since we are only insterested in the first level children by specifying `{ level: 1 }`.\n\n### TREM key parent count node\n\nRemove the reference of a node from a parent.\n\n```javascript\nredis.trem('mytree', '5', 0, '4');\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |       |       |\n+--+--+ +--+--+ +--+--+\n|  2  | |  3  | |  4  |\n+--+--+ +-----+ +-----+\n   |\n+--+--+\n|  5  |\n+-----+\n```\n\nThe `count` argument influences the operation in the following ways:\n\n* count \u003e 0: Remove nodes moving from head to tail.\n* count \u003c 0: Remove nodes moving from tail to head.\n* count = 0: Remove all nodes.\n\n`TREM` returns the remaining nodes in the parent.\n\n### TMREM key node\n\nRemove the node from all parents. Use `not` option to exclude a parent.\n\n```javascript\nredis.tmrem('mytree', '2', { not: '3' });\n```\n\n### TDESTROY key node\n\nDestroy a node recursively and remove all references of it. Returns the count of nodes being deleted.\n\n```javascript\nredis.tdestroy('mytree', '2'); // returns 2, since \"2\" and \"5\" are deleted.\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |               |\n+--+--+         +--+--+\n|  3  |         |  4  |\n+-----+         +-----+\n```\n\n### TMOVECHILDREN key sourceNode targetNode [APPEND|PREPEND]\n\nMove all the children of the `sourceNode` to `targetNode`. By default the new nodes will be\nappended to the target node, if `PREPEND` option is passed, the new nodes will be prepended\nto the target node.\n\n```javascript\nredis.tmovechildren('mytree', 'source', 'target', 'PREPEND');\n```\n\n### TEXISTS key node\n\nReturns if node exists.\n\n```javascript\nredis.texists('mytree', '2'); // 0\nredis.texists('mytree', '1'); // 1\n```\n\n### TRENAME key node name\n\nRename a node.\n\n```javascript\nredis.trename('mytree', '2', '5');\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |               |\n+--+--+         +--+--+\n|  5  |         |  4  |\n+-----+         +-----+\n```\n\n\n### TPRUNE key node\n\nPrune a tree to remove its children from parents which don't belong to the node.\n\nGiven the following two trees:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |               |\n+--+--+         +--+--+\n|  5  |         |  4  |\n+-----+         +-----+\n\n        +-----+\n        |  6  |\n   +----+--+--+\n   |\n+--+--+\n|  5  |\n+-----+\n```\n\n```javascript\nredis.tprune('mytree', '1');\n```\n\nCreates:\n\n```\n        +-----+\n        |  1  |\n   +----+--+--+----+\n   |               |\n+--+--+         +--+--+\n|  5  |         |  4  |\n+-----+         +-----+\n\n        +-----+\n        |  6  |\n        +--+--+\n```\n\n## Cluster Compatibility\n\nThis module supports Redis Cluster by ensuring all nodes that are belong to a tree have a same slot.\n\n## Performance\n\nBenefiting from the high performance of Redis, modifying a tree is very fast. For instance, getting all children of a tree with the level of 100 recursively in a iMac 5k costs 4ms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshimohq%2Fioredis-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshimohq%2Fioredis-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshimohq%2Fioredis-tree/lists"}