-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
findDescendants method returns all direct subcategories (with its nested categories) #2764
Comments
Same issue with 0.2.13 & PG10 |
Here is complete test case. tree_bug.zip import { Entity, PrimaryGeneratedColumn, BaseEntity, Tree, TreeParent, TreeChildren, getTreeRepository, Column, createConnection } from 'typeorm';
@Entity()
@Tree('materialized-path')
export class NestingDoll extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column('varchar')
name: string;
@Column('uuid', { name: 'parentId', nullable: true })
parentId: string;
@TreeParent()
parent: NestingDoll;
@TreeChildren()
children: NestingDoll[];
async childrenItems(targetItem?: NestingDoll): Promise<NestingDoll[]> {
if (targetItem) {
const childItems = await getTreeRepository(NestingDoll).findDescendants(targetItem);
return Promise.resolve(childItems);
} else {
const result = await getTreeRepository(NestingDoll).findRoots();
return Promise.resolve(result);
}
}
}
async function initDefault() {
const mainDoll: NestingDoll = NestingDoll.create({ name: 'mainDoll' });
await mainDoll.save();
const middleDoll: NestingDoll = NestingDoll.create({ name: 'middleDoll', parent: mainDoll });
await middleDoll.save();
const smallDoll: NestingDoll = NestingDoll.create({ name: 'smallDoll', parent: middleDoll });
await smallDoll.save();
const childItems = await mainDoll.childrenItems(mainDoll);
for (const itm of childItems) {
console.log(itm);
}
};
(async () => {
try {
const connection = await createConnection();
await connection.dropDatabase();
await connection.synchronize();
await initDefault();
} catch (error) {
console.log(error);
}
})(); output:
|
Alternative way to get child items without tree repository: async childrenItems(targetItem ?: NestingDoll): Promise < NestingDoll[] > {
let result: NestingDoll[] = [];
if(targetItem) {
const targetItemEntity = await NestingDoll.findOne(targetItem.id, { relations: ['children'] });
if (targetItemEntity) {
result = targetItemEntity.children;
}
} else {
result = await NestingDoll.find({ where: { parentId: null } });
};
return Promise.resolve(result);
} |
is there any update on this? |
Is there any update on this? |
#8556 this can be related? |
my db is oracle, typeorm version is 0.2.7
i create an entity us ts like this:
@entity()
@Tree("materialized-path")
export class Organization {
}
i convert ts to js,
then i save some entities:
var org1 = {
"id": 1,
"name": 'a'
};
then i use the findDescendantsTree:
connection.getTreeRepository("Organization").findDescendants({ id: 1 }).then(function (aaa) {
console.log(aaa)
}).catch(function (err) {
console.log(err)
})
it will returns all 5 entities , did i miss some options? or my way is wrong?
The text was updated successfully, but these errors were encountered: