# @algochad/prisma-core

A powerful NestJS library that brings Entity Framework Core-like operations to Prisma ORM and GraphQL, enhanced with C# LINQ-style data manipulation capabilities. This library provides a unified, type-safe API for database interactions while offering advanced query building, data transformation, and performance optimization features.

---

## 🚀 Features

- **🏗️ EF-Core-like Operations**: Intuitive query building and data manipulation using Prisma ORM
- **🌐 GraphQL Data Source Support**: Full LINQ-style queries with GraphQL APIs (requires schema matching)
- **🔗 LINQ Operations**: Basic port of C# LINQ operations for advanced data queries and transformations
- **🔧 Extensible API**: Unified, type-safe API for seamless database and GraphQL interactions
- **⚡ Performance Optimized**: Built-in benchmarking and performance optimization tools
- **🏢 NestJS Integration**: Native integration designed specifically for NestJS applications
- **📊 Universal Repository Pattern**: Dynamic model access with full type safety across data sources
- **🔄 Transaction Support**: Comprehensive transaction management capabilities (Prisma ORM)

---

## 📚 Quick Navigation

### 🚀 Getting Started

- **[📦 Installation Guide](./docs/installation.md)** - Set up the library in your project
- **[🛠️ Quick Setup Guide](./docs/quick-setup.md)** - Complete setup with examples
- **[⭐ Quick Start Patterns](./docs/quick-start-patterns.md)** - Most commonly used Select & OrderBy patterns

### 📖 Core Documentation

- **[📖 Usage Guide](./docs/usage-guide.md)** - Basic query operations and patterns
- **[🔗 LINQ Operations](./docs/linq-operations.md)** - Synchronous and asynchronous LINQ operations
- **[🏛️ Universal Repository Pattern](./docs/repository-pattern.md)** - Three ways to access models
- **[⚡ Performance Benchmarks](./docs/performance.md)** - Comprehensive benchmarking and optimization guide

### 📚 Reference & Help

- **[🛠️ API Reference](./docs/api-reference.md)** - Complete API documentation
- **[🔧 Troubleshooting](./docs/troubleshooting.md)** - Common issues and solutions
- **[📚 Examples & Tutorials](./docs/examples.md)** - Real-world examples and tutorials

### 🤝 Community

- **[🤝 Contributing](./docs/contributing.md)** - How to contribute to the project
- **[📄 License](./docs/LICENSE.md)** - MIT License details

---

## ⚡ Quick Example

```typescript
// Simple query with field selection and sorting
const results = await repository.test
    .Where({ isActive: true })
    .Select({
        id: true,
        name: true,
        description: true,
    })
    .OrderBy({ name: 'asc' })
    .ToArray();

// LINQ-style transformations
const transformed = await repository.test
    .Where({ isActive: true })
    .ToEnumerable()
    .Where((test) => test.description != null)
    .Select((test) => ({
        id: test.id,
        displayName: test.name.toUpperCase(),
        summary: `${test.name}: ${test.description}`,
    }))
    .OrderBy((item) => item.displayName)
    .ToArray();

// Async performance optimization for large datasets
const optimized = await AsyncEnumerable.from(largeDataset)
    .Where(async (item) => await validateAsync(item))
    .Select(async (item) => await transformAsync(item))
    .ToArrayAsync();
```

---

## 📊 Performance Highlights

- **⚡ 76% Performance Improvement** with AsyncEnumerable for complex operations
- **💾 7% Memory Efficiency** gains with async processing
- **🎯 2.8x Faster Sorting** operations with async implementations
- **📈 Scales to 100K+ items** with optimized processing

### When to Use What

| Operation Type          | Dataset Size  | Recommendation    | Reason                      |
| ----------------------- | ------------- | ----------------- | --------------------------- |
| Simple filtering        | < 1,000 items | `Enumerable`      | Lower overhead              |
| Complex transformations | > 1,000 items | `AsyncEnumerable` | Parallel processing         |
| Database queries        | Any size      | `AsQueryable()`   | Database-level optimization |
| Sorting operations      | > 1,000 items | `AsyncEnumerable` | 2.8x performance gain       |

---

## 🏗️ Repository Setup

```typescript
@Injectable()
export class AppRepository extends PrismaRepository {
    constructor(databaseService: PrismaCoreService) {
        super(databaseService);
    }

    // Typed accessor for User model
    get user(): PrismaUnifiedBuilder<User> {
        return this.model<User>('user') as any;
    }

    // Typed accessor for Post model
    get post(): PrismaUnifiedBuilder<Post> {
        return this.model<Post>('post') as any;
    }
}
```

---

## 🔗 Links and Resources

- **📖 Full Documentation**: [Complete Documentation](./docs/README.md)
- **📦 NPM Package**: [@algochad/prisma-core](https://www.npmjs.com/package/@algochad/prisma-core)
- **🐛 GitHub Issues**: [Report Issues](https://github.com/algochad/prisma-core/issues)
- **💬 GitHub Discussions**: [Community Discussions](https://github.com/algochad/prisma-core/discussions)
- **🏗️ Prisma Documentation**: [Prisma Docs](https://www.prisma.io/docs)
- **🏢 NestJS Documentation**: [NestJS Docs](https://docs.nestjs.com)

---

<div align="center">

**Made with ❤️ for the NestJS and Prisma community**

_Star ⭐ this project if you find it helpful!_

**[📚 Start with the Installation Guide →](./docs/installation.md)**

</div>
