@algochad/prisma-core
Version:
A comprehensive NestJS library that provides EF-Core-like operations using Prisma and GraphQL. Features LINQ-style query builders, advanced data manipulation, GraphQL integration with genql, and a unified API for both Prisma and GraphQL operations. Includ
156 lines (137 loc) • 4.08 kB
Markdown
# Quick Start - Select & OrderBy Patterns
## 🚀 Quick Start - Select & OrderBy Patterns
These are the most commonly used patterns in the library, featuring object projection with Select and powerful sorting with OrderBy:
### ⭐ Primary Pattern: Prisma-style Select + OrderBy
```typescript
// Prisma-style field selection with sorting (most common pattern)
const customResults = await repository.test
.Where({ isActive: true })
.Select({
id: true,
name: true,
description: true,
})
.OrderBy({ name: 'asc' })
.ToArray();
// AsQueryable uses Prisma query syntax (not LINQ predicates)
const queryableResults = await repository.test
.AsQueryable()
.Where({
isActive: true,
name: { contains: 'Test' },
})
.Select({
id: true,
name: true,
description: true,
})
.OrderBy({ name: 'asc' })
.Take(5)
.ToArrayAsync();
// For LINQ-style predicates, use ToEnumerable() then apply transformations
const linqStyleResults = await repository.test
.Where({ isActive: true })
.ToEnumerable()
.Where((test) => test.name.length > 5)
.Select((test) => ({
id: test.id,
name: test.name,
displayName: `${test.name} - ${test.description || 'N/A'}`,
priority: test.name.length > 10 ? 'High' : 'Low',
}))
.OrderBy((item) => item.priority)
.ToArray();
```
### 🔧 Common OrderBy Variations
```typescript
// Single field sorting with object notation
await repository.test.OrderBy({ name: 'asc' }).ToArray();
await repository.test.OrderBy({ createdAt: 'desc' }).ToArray();
// Multiple field sorting (Note: Use multiple OrderBy calls for complex sorting)
await repository.test.OrderBy({ createdAt: 'desc' }).ToArray();
// Sorting with IQueryable uses Prisma syntax
await repository.test
.AsQueryable()
.Where({ isActive: true })
.OrderBy({ name: 'asc' })
.ToArrayAsync();
// For LINQ-style sorting with computed fields, use ToEnumerable()
const sortedByNameLength = await repository.test
.Where({ isActive: true })
.ToEnumerable()
.Select((test) => ({
id: test.id,
name: test.name,
nameLength: test.name.length,
}))
.OrderBy((item) => item.nameLength)
.ToArray();
```
### 📋 Field Selection Patterns
```typescript
// Prisma-style field selection (efficient for large datasets)
await repository.test
.Select({
id: true,
name: true,
description: true,
})
.OrderBy({ name: 'asc' })
.ToArray();
// 🆕 Advanced Select with nested relations and type safety
const dataWithRelations = await repository.test
.Select<TestWithCategory>({
createdAt: true,
description: true,
id: true,
isActive: true,
name: true,
category: {
select: {
id: true,
name: true,
description: true,
},
},
})
.Where({
isActive: {
equals: true,
},
})
.OrderBy({ createdAt: 'desc' })
.ToArray();
// AsQueryable with Prisma-style field selection
await repository.test
.AsQueryable()
.Where({
isActive: true,
description: { not: null },
})
.Select({
id: true,
name: true,
description: true,
})
.OrderBy({ name: 'asc' })
.ToArrayAsync();
// LINQ-style transformations require ToEnumerable()
const transformedData = await repository.test
.Where({ isActive: true })
.ToEnumerable()
.Where((test) => test.description !== null)
.Select((test) => ({
id: test.id,
displayText: `${test.name}: ${test.description}`,
metadata: {
length: test.name.length,
hasDesc: test.description !== null,
},
}))
.OrderBy((item) => item.displayText)
.ToArray();
```
## Next Steps
- [Learn more about filtering and selection](./usage-guide.md)
- [Explore LINQ operations](./linq-operations.md)
- [See performance benchmarks](./performance.md)