Skip to content

Commit

Permalink
docs: add self relation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou authored and Janny committed Apr 9, 2019
1 parent c6ef653 commit 24545f5
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions docs/site/BelongsTo-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,11 @@ export class Category extends Entity {
})
id?: number;

@hasMany(() => Category, {keyTo: 'parentId'})
categories?: Category[];

@belongsTo(() => Category)
parentId: number;
parentId?: number;

constructor(data?: Partial<Category>) {
super(data);
Expand All @@ -210,10 +213,22 @@ export class CategoryRepository extends DefaultCrudRepository<
Category,
typeof Category.prototype.id
> {
public readonly parent: BelongsToAccessor<Category, number>;
public readonly parent: BelongsToAccessor<
Category,
typeof Category.prototype.id
>;
public readonly categories: HasManyRepositoryFactory<
Category,
typeof Category.prototype.id
>;

constructor(@inject('datasources.db') dataSource: DbDataSource) {
super(Category, dataSource);
this.parent = this._createBelongsToAccessorFor(
this.categories = this.createHasManyRepositoryFactoryFor(
'categories',
Getter.fromValue(this),
);
this.parent = this.createBelongsToAccessorFor(
'parent',
Getter.fromValue(this),
); // for recursive relationship
Expand Down

0 comments on commit 24545f5

Please sign in to comment.