- 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.
3.4 KiB
3.4 KiB
sidebar_position
sidebar_position |
---|
2 |
Complex Queries
This example demonstrates advanced query patterns and techniques in DebrosFramework.
Overview
Learn how to build complex queries using DebrosFramework's powerful query system.
Query Examples
Basic Filtering
// Simple where clause
const activeUsers = await User.query()
.where('isActive', true)
.find();
// Multiple conditions
const recentPosts = await Post.query()
.where('createdAt', '>', Date.now() - 7 * 24 * 60 * 60 * 1000)
.where('isPublished', true)
.find();
Advanced Filtering
// OR conditions
const popularPosts = await Post.query()
.where('viewCount', '>', 1000)
.orWhere('likeCount', '>', 100)
.find();
// IN operator
const categorizedPosts = await Post.query()
.where('category', 'in', ['tech', 'science', 'programming'])
.find();
// LIKE operator for text search
const searchResults = await Post.query()
.where('title', 'like', '%javascript%')
.find();
Sorting and Pagination
// Sorting
const sortedPosts = await Post.query()
.orderBy('createdAt', 'desc')
.orderBy('title', 'asc')
.find();
// Pagination
const paginatedPosts = await Post.query()
.orderBy('createdAt', 'desc')
.limit(10)
.offset(20)
.find();
Relationship Loading
// Eager loading
const usersWithPosts = await User.query()
.with(['posts'])
.find();
// Nested relationships
const usersWithPostsAndComments = await User.query()
.with(['posts.comments', 'posts.comments.user'])
.find();
// Conditional relationship loading
const activeUsersWithRecentPosts = await User.query()
.where('isActive', true)
.with(['posts'], (query) =>
query.where('createdAt', '>', Date.now() - 30 * 24 * 60 * 60 * 1000)
)
.find();
Aggregation
// Count
const userCount = await User.query()
.where('isActive', true)
.count();
// Group by
const postsByCategory = await Post.query()
.select('category', 'COUNT(*) as count')
.groupBy('category')
.find();
Complex Joins
// Manual join
const usersWithPostCount = await User.query()
.leftJoin('posts', 'users.id', 'posts.userId')
.select('users.username', 'COUNT(posts.id) as postCount')
.groupBy('users.id', 'users.username')
.find();
Caching
// Cache for 5 minutes
const cachedPosts = await Post.query()
.where('isPublished', true)
.cache(300)
.find();
// Disable caching
const freshPosts = await Post.query()
.where('isPublished', true)
.cache(false)
.find();
Performance Optimization
Query Optimization
// Use indexes
const optimizedQuery = await Post.query()
.where('userId', userId) // Indexed field
.where('createdAt', '>', startDate)
.orderBy('createdAt', 'desc')
.limit(50)
.find();
// Batch operations
const userIds = ['user1', 'user2', 'user3'];
const users = await User.query()
.where('id', 'in', userIds)
.find();
Parallel Queries
// Execute queries in parallel
const [users, posts, comments] = await Promise.all([
User.query().where('isActive', true).find(),
Post.query().where('isPublished', true).find(),
Comment.query().where('isModerated', true).find()
]);
Related Topics
- Query Builder - Query construction
- Relationships - Model relationships
- Performance - Performance optimization