## MONGOOSE

TypeScript MongoDB abstraction library with repository pattern, multi-tenancy support, connection pooling, and comprehensive error handling.

### INSTALL
```bash
npm install @hiennc24/mongoose
```

### SETUP

#### 1. Environment Variables

Create a `.env` file in your project root (copy from `.env.example`):

```bash
cp .env.example .env
```

Set your MongoDB connection string:

```env
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database?retryWrites=true&w=majority
```

**Security Note:** Never commit `.env` file with real credentials to version control. The `.env` file is already in `.gitignore`.

#### 2. Basic Usage

```typescript
import { MongoDB, BaseRepository, Schema } from '@hiennc24/mongoose';

// Initialize MongoDB connection
const mongodb = new MongoDB({
  // connectionString is automatically loaded from process.env.MONGODB_URI
  options: {
    maxPoolSize: 10,
    minPoolSize: 2
  }
});

// Connect to database
await mongodb.connect();

// Define your schema
interface UserEntity {
  id: string;
  name: string;
  email: string;
  createdAt: Date;
  updatedAt: Date;
}

const userSchema = new Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true }
});

// Create repository
class UserRepository extends BaseRepository<UserEntity> {
  constructor() {
    super('user', userSchema, 'users');
  }
}

const userRepo = new UserRepository();

// Use repository methods
const user = await userRepo.create({
  name: 'John Doe',
  email: 'john@example.com'
});

const users = await userRepo.findAll({}, { page: 1, limit: 10 });
```

### FEATURES

- ✅ Generic repository pattern with TypeScript support
- ✅ Multi-tenancy with orgIds filtering
- ✅ Automatic connection retry with exponential backoff
- ✅ Connection pooling and validation
- ✅ Comprehensive error handling
- ✅ CRUD operations + aggregation/populate
- ✅ Pagination support
- ✅ Transaction support (via session parameter)

### TESTING

```bash
# Run all tests
npm test

# Run load tests
npm run test:load
```

### LICENSE

ISC
