Skip to content

Conversation

@nour-aldin
Copy link

@nour-aldin nour-aldin commented Sep 25, 2025

Enhanced Prisma Operations Support

🎯 Summary

This PR adds support for 6 additional Prisma operations to PrismaVault, expanding the repository pattern coverage while maintaining type safety and consistency.

✨ New Operations Added

Error-Throwing Find Operations

  • findUniqueOrThrow - Throws error instead of returning null when record not found
  • findFirstOrThrow - Throws error instead of returning null when no matching record found

Enhanced Bulk Operations

  • updateManyAndReturn - Update multiple records and return the updated data

Data Analysis Operations

  • count - Count records with optional filtering
  • aggregate - Perform mathematical operations (sum, avg, min, max, count)
  • groupBy - Group records and aggregate by groups (EncapsulatedRepository only)

🏗 Implementation

  • Both Repository Types: Available in ExposedPrismaRepository (public) and EncapsulatedPrismaRepository (protected)
  • Type Safety: Full TypeScript support with proper generics and inference
  • Comprehensive Tests: 12+ new test cases covering success and error scenarios
  • Zero Breaking Changes: Fully backward compatible

📋 Files Changed

Generator Templates

  • ExposedPrismaRepository.ts.hbs - Added public method signatures
  • EncapsulatedPrismaRepository.ts.hbs - Added protected method signatures
  • PrismaProxy.ts.hbs - Added core implementations
  • IPrismaDelegate.ts.hbs - Added interface definitions

Test Files

  • ExposedPrismaRepository.test.ts - 6 new comprehensive test cases
  • PrismaProxy.test.ts - 6 new comprehensive test cases

Infrastructure

  • IndexGeneration.ts - Fixed export path resolution

🧪 Test Coverage

All new operations tested for:

  • ✅ Success cases with data validation
  • ✅ Error cases (for *OrThrow methods)
  • ✅ Bulk operations with multiple records
  • ✅ Aggregation accuracy
  • ✅ Type safety and IntelliSense

💡 Usage Examples

// Error-safe operations
const user = await repository.findUniqueOrThrow({ where: { id: 1 } })
// Throws PrismaClientKnownRequestError instead of returning null

// Bulk operations with results
const updatedUsers = await repository.updateManyAndReturn({
  where: { status: "pending" },
  data: { status: "active" },
})

// Data analysis
const userCount = await repository.count({ where: { isActive: true } })
const stats = await repository.aggregate({
  _count: { id: true },
  _avg: { age: true },
})

🎯 Benefits

  1. Better Error Handling - Explicit exceptions vs null checking
  2. Enhanced Developer Experience - More intuitive APIs for common operations
  3. Performance - Native Prisma operations without workarounds
  4. Consistency - Unified pattern across all Prisma operations
  5. Production Ready - Comprehensive testing and type safety

    - findUniqueOrThrow
    - findFirstOrThrow
    - updateManyAndReturn
    - count
    - aggregate
    - groupBy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant