{"id":17821525,"url":"https://github.com/a179346/knex-tree","last_synced_at":"2025-06-20T04:06:03.049Z","repository":{"id":43778010,"uuid":"395268815","full_name":"a179346/knex-tree","owner":"a179346","description":"Query hierarchical data structures in sql with knex","archived":false,"fork":false,"pushed_at":"2021-09-11T01:33:02.000Z","size":333,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-19T18:09:28.963Z","etag":null,"topics":["hierarchical","hierarchical-data","knex","mssql","mysql","nodejs","oracle","postgresql","query","sql","sqlite3","tree","tree-structure","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/a179346.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":"2021-08-12T09:42:33.000Z","updated_at":"2024-03-06T02:43:12.000Z","dependencies_parsed_at":"2022-09-09T06:01:44.676Z","dependency_job_id":null,"html_url":"https://github.com/a179346/knex-tree","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/a179346/knex-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a179346%2Fknex-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a179346%2Fknex-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a179346%2Fknex-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a179346%2Fknex-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a179346","download_url":"https://codeload.github.com/a179346/knex-tree/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a179346%2Fknex-tree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260878327,"owners_count":23075959,"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":["hierarchical","hierarchical-data","knex","mssql","mysql","nodejs","oracle","postgresql","query","sql","sqlite3","tree","tree-structure","typescript"],"created_at":"2024-10-27T17:17:59.373Z","updated_at":"2025-06-20T04:05:58.032Z","avatar_url":"https://github.com/a179346.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eknex-tree 👋\u003c/h1\u003e\n\u003cp\u003e\n  \u003ca href=\"https://github.com/a179346/knex-tree/actions/workflows/build.yml\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Documentation\" src=\"https://github.com/a179346/knex-tree/actions/workflows/build.yml/badge.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/knex-tree\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Documentation\" src=\"https://img.shields.io/npm/v/knex-tree?maxAge=3600)\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/a179346/knex-tree#readme\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Documentation\" src=\"https://img.shields.io/badge/documentation-yes-brightgreen.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/a179346/knex-tree/graphs/commit-activity\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Maintenance\" src=\"https://img.shields.io/badge/Maintained%3F-yes-green.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/a179346/knex-tree/blob/master/LICENSE\" target=\"_blank\"\u003e\n    \u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/github/license/a179346/knex-tree\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003e Query hierarchical data structures in sql with knex\n\n## 🔗 Link\n+ [Github](https://github.com/a179346/knex-tree#readme)\n+ [npm](https://www.npmjs.com/package/knex-tree)\n\n## 📥 Install\n\n```sh\nnpm install knex-tree\n```\n\n## 📖 Usage\n- ### Example data\n| id | parentId | desc |\n|---|---|---|\n| 1 | null | I am 1 |\n| 2 | 1 | I am 2 |\n| 3 | 1 | I am 3 |\n| a | 2 | I am a |\n| b | 2 | I am b |\n- ### KnexTree\n##### `KnexTree.Constructor()` =\u003e `KnexTree`\n```js\nconst { KnexTree } = require('knex-tree');\nconst knex = require('knex');\n\nconst tree = new KnexTree({\n  db: knex({ ... }),\n  table: 'mytree',\n  idColumn: 'id',\n  parentIdColumn: 'parentId',\n});\n```\n##### `KnexTree.node(id)` =\u003e `KnexNode`\n```js\nconst node = tree.node(5);\n```\n##### `KnexTree.getAllData()` =\u003e `Promise\u003cModel[]\u003e`\n```js\nconst data = await tree.getAllData();\n```\n- ### KnexNode\n##### `KnexNode.id` =\u003e `id`\n```js\nconst id = node.id;\n```\n##### `KnexNode.isExist()` =\u003e `Promise\u003cboolean\u003e`\n```js\nconst isExist = await node.isExist();\n```\n##### `KnexNode.getData()` =\u003e `Promise\u003cModel | null\u003e`\n```js\n// data is null if the id doesn't exist in the table\nconst data = await node.getData();\n```\n##### `KnexNode.getParentData()` =\u003e `Promise\u003cModel | null\u003e`\n```js\n// data is null if parent dosen't exist in the table\nconst data = await node.getParentData();\n```\n##### `KnexNode.getChildrenData(where?)` =\u003e `Promise\u003cModel[]\u003e`\n```js\nconst data = await node.getChildrenData();\n```\n##### `KnexNode.isRoot()` =\u003e `Promise\u003cboolean\u003e`\n```js\n// return true if node is root\nconst data = await node.isRoot();\n```\n##### `KnexNode.getPath()` =\u003e `Promise\u003c(Model \u0026 ITreeLv)[] | null\u003e`\n```js\n// return the path from root to this node\nconst data = await node.getPath();\n```\n##### `KnexNode.hasChild(id)` =\u003e `Promise\u003cModel | null\u003e`\n```js\n// data is null if there is no data which match \"id = 7\" \u0026 \"parentId = this.id\"\nconst data = await node.hasChild(7);\n```\n##### `KnexNode.hasParent(id)` =\u003e `Promise\u003cModel | null\u003e`\n```js\n// data is null if there is no data which match \"id = 7\" \u0026 \"hasChild(this.id)\"\nconst data = await node.hasParent(7);\n```\n##### `KnexNode.hasAncestor(id, maxLevel?)` =\u003e `Promise\u003c(Model \u0026 ITreeLv) | null\u003e`\n```js\n// data is null if this is no ancestor whose id = 7\nconst data = await node.hasAncestor(7);\n```\n##### `KnexNode.hasDescendant(id, maxLevel?)` =\u003e `Promise\u003c(Model \u0026 ITreeLv) | null\u003e`\n```js\n// data is null if this is no descendant whose id = 7\nconst data = await node.hasDescendant(7);\n```\n##### `KnexNode.getPathUpTo(id, maxLevel?)` =\u003e `Promise\u003c(Model \u0026 ITreeLv)[] | null\u003e`\n```js\n// data is null if this is no ancestor whose id = 7\n// return the path from this node to the ancestor(id = 7)\nconst data = await node.getPathUpTo(7);\n```\n##### `KnexNode.getPathDownTo(id, maxLevel?)` =\u003e `Promise\u003c(Model \u0026 ITreeLv)[] | null\u003e`\n```js\n// data is null if this is no descendant whose id = 7\n// return the path from this node to the descendant(id = 7)\nconst data = await node.getPathDownTo(7);\n```\n##### `KnexNode.getDescendants(maxLevel?, where?, limit?, offset?)` =\u003e `Promise\u003c(Model \u0026 ITreeLv)[]\u003e`\n```js\n// return all descendants\nlet data = await node.getDescendants();\n\n// return all descendants whose TreeLv \u003c= 2\ndata = await node.getDescendants(2);\n```\n\n## 🙋‍♂️ Author\n\n\n* Github: [@a179346](https://github.com/a179346)\n\n## 🤝 Contributing\n\nContributions, issues and feature requests are welcome!\u003cbr /\u003eFeel free to check [issues page](https://github.com/a179346/knex-tree/issues).\n\n## 🌟 Show your support\n\nGive a ⭐️ if this project helped you!\n\n## 📝 License\n\nCopyright © 2021 [a179346](https://github.com/a179346).\u003cbr /\u003e\nThis project is [MIT](https://github.com/a179346/knex-tree/blob/master/LICENSE) licensed.\n\n***\n_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa179346%2Fknex-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa179346%2Fknex-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa179346%2Fknex-tree/lists"}