How to load tree using Materialized Path tree structure with typeorm

1.4k views Asked by At

I have an entity with 'materialized-path' tree structure.

I am trying to load the children. which gives an empty child object Below is the entity

 @Entity({ name: 'sector' })
    @Tree('materialized-path')
    export class SectorEntity {
    constructor(data?: Partial<SectorEntity>) {
    Object.assign(this, data);
      }
    @ApiProperty({ type: 'string' })
    @PrimaryGeneratedColumn()
    id: number;

    @ApiProperty()
    @Column()
  name: string;

  @ApiProperty()
  @CreateDateColumn({ type: 'timestamp' })
  createdAt?: Date;

  @ApiProperty()
  @UpdateDateColumn({ type: 'timestamp' })
  updatedAt?: Date;

  @TreeChildren()
  children: SectorEntity[];

  @TreeParent()
  parent: SectorEntity;
}

Code for getting tree elements

return typeorm.getTreeRepository(SectorEntity).findTrees();

The result I am getting after loading, where there is no data inside children object

[
    {
        "id": 1,
        "name": "Abc",
        "createdAt": "2021-04-29T18:50:19.922Z",
        "updatedAt": "2021-04-29T18:50:19.000Z",        
        "children": []
    }
]

Please let me know where I am going wrong.

1

There are 1 answers

0
mondyfy On

havu hope your problem had already been solved. I have also faced a similar issue, where I did wrong was while saving new records, I have only saved the parent Id in the parent column but if we look at our entity we have: parent: SectorEntity; so updating create function at my end with actual parent entity worked for me.