Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/a179346/knex-tree
Query hierarchical data structures in sql with knex
https://github.com/a179346/knex-tree
hierarchical hierarchical-data knex mssql mysql nodejs oracle postgresql query sql sqlite3 tree tree-structure typescript
Last synced: 2 months ago
JSON representation
Query hierarchical data structures in sql with knex
- Host: GitHub
- URL: https://github.com/a179346/knex-tree
- Owner: a179346
- License: mit
- Created: 2021-08-12T09:42:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-11T01:33:02.000Z (over 3 years ago)
- Last Synced: 2024-09-16T01:31:28.417Z (4 months ago)
- Topics: hierarchical, hierarchical-data, knex, mssql, mysql, nodejs, oracle, postgresql, query, sql, sqlite3, tree, tree-structure, typescript
- Language: TypeScript
- Homepage:
- Size: 325 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
knex-tree 👋
> Query hierarchical data structures in sql with knex
## 🔗 Link
+ [Github](https://github.com/a179346/knex-tree#readme)
+ [npm](https://www.npmjs.com/package/knex-tree)## 📥 Install
```sh
npm install knex-tree
```## 📖 Usage
- ### Example data
| id | parentId | desc |
|---|---|---|
| 1 | null | I am 1 |
| 2 | 1 | I am 2 |
| 3 | 1 | I am 3 |
| a | 2 | I am a |
| b | 2 | I am b |
- ### KnexTree
##### `KnexTree.Constructor()` => `KnexTree`
```js
const { KnexTree } = require('knex-tree');
const knex = require('knex');const tree = new KnexTree({
db: knex({ ... }),
table: 'mytree',
idColumn: 'id',
parentIdColumn: 'parentId',
});
```
##### `KnexTree.node(id)` => `KnexNode`
```js
const node = tree.node(5);
```
##### `KnexTree.getAllData()` => `Promise`
```js
const data = await tree.getAllData();
```
- ### KnexNode
##### `KnexNode.id` => `id`
```js
const id = node.id;
```
##### `KnexNode.isExist()` => `Promise`
```js
const isExist = await node.isExist();
```
##### `KnexNode.getData()` => `Promise`
```js
// data is null if the id doesn't exist in the table
const data = await node.getData();
```
##### `KnexNode.getParentData()` => `Promise`
```js
// data is null if parent dosen't exist in the table
const data = await node.getParentData();
```
##### `KnexNode.getChildrenData(where?)` => `Promise`
```js
const data = await node.getChildrenData();
```
##### `KnexNode.isRoot()` => `Promise`
```js
// return true if node is root
const data = await node.isRoot();
```
##### `KnexNode.getPath()` => `Promise<(Model & ITreeLv)[] | null>`
```js
// return the path from root to this node
const data = await node.getPath();
```
##### `KnexNode.hasChild(id)` => `Promise`
```js
// data is null if there is no data which match "id = 7" & "parentId = this.id"
const data = await node.hasChild(7);
```
##### `KnexNode.hasParent(id)` => `Promise`
```js
// data is null if there is no data which match "id = 7" & "hasChild(this.id)"
const data = await node.hasParent(7);
```
##### `KnexNode.hasAncestor(id, maxLevel?)` => `Promise<(Model & ITreeLv) | null>`
```js
// data is null if this is no ancestor whose id = 7
const data = await node.hasAncestor(7);
```
##### `KnexNode.hasDescendant(id, maxLevel?)` => `Promise<(Model & ITreeLv) | null>`
```js
// data is null if this is no descendant whose id = 7
const data = await node.hasDescendant(7);
```
##### `KnexNode.getPathUpTo(id, maxLevel?)` => `Promise<(Model & ITreeLv)[] | null>`
```js
// data is null if this is no ancestor whose id = 7
// return the path from this node to the ancestor(id = 7)
const data = await node.getPathUpTo(7);
```
##### `KnexNode.getPathDownTo(id, maxLevel?)` => `Promise<(Model & ITreeLv)[] | null>`
```js
// data is null if this is no descendant whose id = 7
// return the path from this node to the descendant(id = 7)
const data = await node.getPathDownTo(7);
```
##### `KnexNode.getDescendants(maxLevel?, where?, limit?, offset?)` => `Promise<(Model & ITreeLv)[]>`
```js
// return all descendants
let data = await node.getDescendants();// return all descendants whose TreeLv <= 2
data = await node.getDescendants(2);
```## 🙋♂️ Author
* Github: [@a179346](https://github.com/a179346)
## 🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/a179346/knex-tree/issues).## 🌟 Show your support
Give a ⭐️ if this project helped you!
## 📝 License
Copyright © 2021 [a179346](https://github.com/a179346).
This project is [MIT](https://github.com/a179346/knex-tree/blob/master/LICENSE) licensed.***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_