Comparing version 1.0.0 to 1.1.0
@@ -24,3 +24,3 @@ import Knex from 'knex'; | ||
getParentData(): Promise<Model | null>; | ||
getChildrenData(): Promise<Model[] | null>; | ||
getChildrenData(where?: any): Promise<Model[]>; | ||
isRoot(): Promise<boolean>; | ||
@@ -34,4 +34,4 @@ getPath(): Promise<(Model & ITreeLv)[] | null>; | ||
getPathDownTo(id: IdType, maxLevel?: number): Promise<(Model & ITreeLv)[] | null>; | ||
getDescendants(maxLevel?: number): Promise<(Model & ITreeLv)[] | null>; | ||
getDescendants(maxLevel?: number | null, where?: any): Promise<(Model & ITreeLv)[]>; | ||
} | ||
export {}; |
@@ -66,9 +66,10 @@ "use strict"; | ||
} | ||
getChildrenData() { | ||
getChildrenData(where) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const where = {}; | ||
where[this.options.parentIdColumn] = this.id; | ||
const result = yield this.options.db(this.options.table).where(where); | ||
if (result.length === 0) | ||
return null; | ||
const whereParentIsId = {}; | ||
whereParentIsId[this.options.parentIdColumn] = this.id; | ||
const query = this.options.db(this.options.table).where(whereParentIsId); | ||
if (where) | ||
query.andWhere(where); | ||
const result = yield query; | ||
return result; | ||
@@ -82,3 +83,5 @@ }); | ||
.first(); | ||
return (result && (result[this.options.parentIdColumn] === null || result[this.options.parentIdColumn] === this.id)) || false; | ||
if (!result) | ||
return false; | ||
return (result[this.options.parentIdColumn] === null || result[this.options.parentIdColumn] === this.id); | ||
}); | ||
@@ -114,5 +117,3 @@ } | ||
const result = yield this.options.db(this.options.table).where(where).first(); | ||
if (!result) | ||
return null; | ||
return result; | ||
return result || null; | ||
}); | ||
@@ -195,5 +196,5 @@ } | ||
} | ||
getDescendants(maxLevel) { | ||
getDescendants(maxLevel, where) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield this.options.db | ||
const query = this.options.db | ||
.withRecursive('pt', (qb) => { | ||
@@ -212,6 +213,6 @@ qb.select([this.options.table + '.*', this.options.db.raw('0 as `TreeLv`')]) | ||
}) | ||
.select('*').from('pt'); | ||
if (!result || result.length < 1) | ||
return null; | ||
result.shift(); | ||
.select('*').from('pt').where(this.options.idColumn, '!=', this.id); | ||
if (where) | ||
query.andWhere(where); | ||
const result = yield query; | ||
return result; | ||
@@ -218,0 +219,0 @@ }); |
@@ -9,3 +9,3 @@ export interface ITreeLv { | ||
getParentData(): Promise<Model | null>; | ||
getChildrenData(): Promise<Model[] | null>; | ||
getChildrenData(where?: any): Promise<Model[]>; | ||
isRoot(): Promise<boolean>; | ||
@@ -19,3 +19,3 @@ getPath(): Promise<(Model & ITreeLv)[] | null>; | ||
getPathDownTo(id: IdType, maxLevel?: number): Promise<(Model & ITreeLv)[] | null>; | ||
getDescendants(maxLevel?: number): Promise<(Model & ITreeLv)[] | null>; | ||
getDescendants(maxLevel?: number | null, where?: any): Promise<(Model & ITreeLv)[]>; | ||
} | ||
@@ -22,0 +22,0 @@ export interface ITree<IdType, Model> { |
{ | ||
"name": "knex-tree", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Query hierarchical data structures in sql with knex", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -81,3 +81,3 @@ <h1 align="center">knex-tree 👋</h1> | ||
``` | ||
##### `KnexNode.getChildrenData()` => `Promise<Model[] | null>` | ||
##### `KnexNode.getChildrenData(where?)` => `Promise<Model[]>` | ||
```js | ||
@@ -129,5 +129,4 @@ // data is null if no children | ||
``` | ||
##### `KnexNode.getDescendants(maxLevel?)` => `Promise<(Model & ITreeLv)[] | null>` | ||
##### `KnexNode.getDescendants(maxLevel?, where?)` => `Promise<(Model & ITreeLv)[]>` | ||
```js | ||
// data is null if this.id doesn't exist | ||
// return all descendants | ||
@@ -134,0 +133,0 @@ let data = await node.getDescendants(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26985
277
156