Relations API - Relationship Definition
Function: Model layer relationship definition, supports hasOne/hasMany/belongsTo
📖 Overview
Relations define how Model documents point to related collections and provide the configuration used by populate().
Model schema examples on this page use the runtime-scoped s namespace passed by monSQLize. Application code does not need to import the root schema-dsl entry for these examples.
Core Features
- ✅ hasOne - one-to-one relationship (user-profile)
- ✅ hasMany - one-to-many relationship (user-article)
- ✅ belongsTo - many-to-one relationship (article - author)
- ✅ Many to Many - implemented through intermediate tables
- ✅ Self-reference - Supports self-association (tree structure)
- ✅ Flexible Configuration - Custom field mapping
🚀 Quick Start
Basic definition
📚Relationship type
hasOne (one-to-one)
One document corresponds to one document from another collection.
Example: User-Profile
Data structure:
hasMany (one-to-many)
One document corresponds to multiple documents from another collection.
Example: User-Post
Data structure:
belongsTo (many to one)
Multiple documents correspond to one document of another collection (the reverse of hasOne).
Example: Article-Author
Data structure:
🎯 Configuration options
RelationDefinition
Complete configuration example
💡 Usage scenarios
Scenario 1: User-Profile (hasOne)
Requirement: One user only has one profile
Scenario 2: User-Article (hasMany)
Requirement: A user can publish multiple articles
Scenario 3: Article-Author (belongsTo)
Requirement: An article belongs to one author
Scenario 4: Many-to-many (student-course)
Requirements: Students can take multiple courses and courses have multiple students
Option: Use intermediate table
Scenario 5: Self-reference (tree structure)
Requirement: Comment reply to comment (father-son relationship)
🎨 Best Practices
1. Relationship naming
2. Foreign key design
3. Performance considerations
4. Index optimization
🆚 Comparison with SQL
SQL JOIN vs Populate
Example comparison
SQL:
monSQLize:
❓ FAQ
Q1: Can it be many-to-many?
A: ✅ Yes, it can be achieved through an intermediate table.
Q2: Can it be self-referenced?
A: ✅ Yes, self-association is supported.
See Scenario 5: Self-reference
Q3: Can conditional filtering be used?
A: Supported. The current version can use match in the populate option to filter related data.
Q4: Can there be a two-way relationship?
A: ✅ Yes, the relationship can be defined in both directions.
Q5: How to determine localField and foreignField?
A: Look in the direction of the arrow.
Memory method:
- localField: local (own collection) field
- foreignField: foreign (associated collection) field
Q6: Can multiple relationships point to the same collection?
A: ✅ Yes, use different relationship names.
📝 Complete example
E-commerce system example
🔗 Related documents
- Populate API - Detailed explanation of related queries
- Model API - Model layer complete documentation
- Schema API - Schema definition
📌 Design principles
1. One-way vs two-way relationship
Suggestion: Define as needed, not all relationships need to be bidirectional.
2. Foreign key storage location
3. Relationship vs Embedding
Example: