A comprehensive TypeScript project demonstrating TypeORM integration with PostgreSQL, featuring robust testing with Jest and pg-mem for in-memory database testing.
- 🎯 TypeORM - Modern TypeScript ORM for database operations
- 🐘 PostgreSQL - Production-ready database support
- 🧪 Jest Testing - Comprehensive test suite with in-memory database
- 📝 TypeScript - Full type safety and modern JavaScript features
- 🔄 Repository Pattern - Clean architecture with separation of concerns
- 🛡️ Service Layer - Business logic encapsulation
src/
├── entities/ # TypeORM entities
│ └── User.ts
├── repositories/ # Data access layer
│ └── UserRepository.ts
├── services/ # Business logic layer
│ └── UserService.ts
├── __tests__/ # Test suites
│ └── UserService.test.ts
└── index.ts # Application entry point
- Node.js (v16 or higher)
- npm or yarn
- PostgreSQL (for production)
- Clone the repository:
git clone <repository-url>
cd test_type_orm- Install dependencies:
npm install- Build the project:
npm run buildThis project uses Jest with pg-mem for lightning-fast in-memory PostgreSQL testing.
npm testnpm test -- --watchnpm test -- UserService.test.tsnpm test -- --coverage| Command | Description |
|---|---|
npm run build |
Compile TypeScript to JavaScript |
npm start |
Run the compiled application |
npm run dev |
Run development server with ts-node |
npm test |
Execute test suite |
- User: Core entity with id, name, and email fields
- UserRepository: Handles all database operations for User entity
- Provides methods for CRUD operations and filtering
- UserService: Contains business logic and orchestrates repository calls
- Methods include:
createUser(name, email)- Create new usergetAllUsers()- Retrieve all usersgetUserByEmail(email)- Find user by emailfilterUsers(options)- Advanced filtering with multiple criteriadeleteUser(id)- Remove user by ID
The project includes comprehensive tests covering:
- ✅ User creation and validation
- ✅ Retrieving all users (including empty database scenarios)
- ✅ Finding users by email
- ✅ Advanced filtering capabilities
- ✅ User deletion
- ✅ Edge cases and error handling
- Empty Database: Ensures proper handling when no data exists
- Data Persistence: Verifies data integrity across operations
- Filtering Logic: Tests various search and filter combinations
- Error Handling: Validates proper error responses
The project uses strict TypeScript settings with:
- Target: ES2020
- Module: CommonJS
- Strict type checking enabled
- Decorator support for TypeORM
- TypeScript support via ts-jest
- In-memory PostgreSQL with pg-mem
- Automatic test discovery
- Coverage reporting
- typeorm - TypeScript ORM
- pg - PostgreSQL client
- reflect-metadata - Decorator metadata reflection
- jest - Testing framework
- ts-jest - TypeScript preprocessor for Jest
- pg-mem - In-memory PostgreSQL for testing
- typescript - TypeScript compiler
- ts-node - TypeScript execution engine
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the ISC License.
⭐ Made with TypeScript and TypeORM ⭐