- Introduced hook decorators for lifecycle methods in models. - Documented @Model decorator for model configuration. - Added relationship decorators for defining model relationships. - Created MigrationBuilder and MigrationManager documentation for schema migrations. - Added QueryExecutor and RelationshipManager documentation for query execution and relationship handling. - Included examples for complex queries and migration scenarios. - Developed a social platform example showcasing model relationships and application structure. - Enhanced contributing guidelines with community, documentation, and release processes. - Established testing guidelines for unit and integration tests.
1.8 KiB
1.8 KiB
sidebar_position
sidebar_position |
---|
2 |
Relationships
DebrosFramework provides powerful relationship management capabilities for connecting models.
Overview
Relationships define how models are connected to each other and provide mechanisms for loading related data efficiently.
Relationship Types
BelongsTo
A many-to-one relationship where the current model belongs to another model.
class Post extends BaseModel {
@Field({ type: 'string', required: true })
userId: string;
@BelongsTo(() => User, 'userId')
user: User;
}
HasMany
A one-to-many relationship where the current model has many related models.
class User extends BaseModel {
@HasMany(() => Post, 'userId')
posts: Post[];
}
HasOne
A one-to-one relationship where the current model has one related model.
class User extends BaseModel {
@HasOne(() => UserProfile, 'userId')
profile: UserProfile;
}
ManyToMany
A many-to-many relationship using a pivot table.
class User extends BaseModel {
@ManyToMany(() => Role, 'user_roles')
roles: Role[];
}
Loading Relationships
Eager Loading
Load relationships when querying:
const usersWithPosts = await User.query()
.with(['posts'])
.find();
Lazy Loading
Load relationships on-demand:
const user = await User.findById('user123');
const posts = await user.loadRelationship('posts');
Nested Relationships
Load nested relationships:
const usersWithPostsAndComments = await User.query()
.with(['posts.comments', 'posts.comments.user'])
.find();
Related Classes
RelationshipManager
- Manages relationshipsBaseModel
- Base model class