A comprehensive TypeORM example project demonstrating modern TypeScript patterns, database operations, and the Repository pattern with SQLite.
- ποΈ TypeORM Integration - Full ORM setup with entities, repositories, and data source configuration
- π SQLite Database - Lightweight, file-based database perfect for learning and development
- π§ Repository Pattern - Clean architecture with custom repository implementation
- π― CRUD Operations - Complete Create, Read, Update, Delete functionality
- π TypeScript - Fully typed with modern ES6+ features
- π Pagination Support - Built-in pagination for large datasets
- βοΈ Environment Configuration - Flexible setup with environment variables
- π§ͺ Production Ready - Environment-aware configuration for different deployment stages
- π User-Post Relations - One-to-many relation between users and posts, with full repository support
- Node.js (v16 or higher)
- npm or yarn
-
Clone the repository
git clone <your-repo-url> cd typescript_course
-
Install dependencies
npm install
-
Run the application
npm start
That's it! The application will:
- Initialize the SQLite database
- Create sample users and posts
- Demonstrate various database operations
- Display results in the console
typescript_course/
βββ src/
β βββ entity/
β β βββ User.ts # User entity definition
β β βββ Post.ts # Post entity definition
β βββ repository/
β β βββ UserRepository.ts # Custom repository with CRUD operations for users
β β βββ PostRepository.ts # Custom repository with CRUD operations for posts
β βββ migrations/ # Database migrations (optional)
β βββ data-source.ts # TypeORM data source configuration
β βββ index.ts # Main application entry point
βββ database.sqlite # SQLite database file (auto-generated)
βββ package.json # Dependencies and scripts
βββ tsconfig.json # TypeScript configuration
const userRepository = new UserRepository();
const postRepository = new PostRepository();
const newUser = await userRepository.create({
name: "John Doe",
email: "[email protected]"
});
const post1 = await postRepository.create({
title: "First Post",
content: "This is the first post.",
user: newUser
});
const post2 = await postRepository.create({
title: "Second Post",
content: "This is the second post.",
user: newUser
});// Find all users with their posts
const usersWithPosts = await userRepository.findAll(true);
// Find user by email with posts
const user = await userRepository.findByEmail("[email protected]", true);
// Find all posts with their user
const postsWithUser = await postRepository.findAll(true);
// Find posts by user id
const posts = await postRepository.findByUserId(newUser.id);const updatedUser = await userRepository.update(1, {
name: "Jane Doe"
});
const updatedPost = await postRepository.update(1, {
title: "Updated Title"
});const isUserDeleted = await userRepository.delete(1);
const isPostDeleted = await postRepository.delete(1);| Method | Description | Parameters | Returns |
|---|---|---|---|
create(userData) |
Create a new user | Partial<User> |
Promise<User> |
findAll(withPosts?) |
Get all users, optionally with posts | boolean? |
Promise<User[]> |
findById(id, withPosts?) |
Find user by ID, optionally with posts | number, boolean? |
Promise<User | null> |
findByEmail(email, withPosts?) |
Find user by email, optionally with posts | string, boolean? |
Promise<User | null> |
update(id, userData) |
Update user | number, Partial<User> |
Promise<User | null> |
delete(id) |
Delete user | number |
Promise<boolean> |
existsByEmail(email) |
Check if email exists | string |
Promise<boolean> |
findWithPagination(page, limit, withPosts?) |
Get paginated users, optionally with posts | number, number, boolean? |
Promise<{users: User[], total: number}> |
loadPosts(userId) |
Get posts for a user | number |
Promise<Post[]> |
| Method | Description | Parameters | Returns |
|---|---|---|---|
create(postData) |
Create a new post | Partial<Post> |
Promise<Post> |
findAll(withUser?) |
Get all posts, optionally with user | boolean? |
Promise<Post[]> |
findById(id, withUser?) |
Find post by ID, optionally with user | number, boolean? |
Promise<Post | null> |
update(id, postData) |
Update post | number, Partial<Post> |
Promise<Post | null> |
delete(id) |
Delete post | number |
Promise<boolean> |
findByUserId(userId, withUser?) |
Get posts by user id, optionally with user | number, boolean? |
Promise<Post[]> |
interface User {
id: number; // Auto-generated primary key
name: string; // User's full name
email: string; // Unique email address
posts: Post[]; // User's posts
}interface Post {
id: number; // Auto-generated primary key
title: string; // Post title
content: string; // Post content
user: User; // Author (User)
}Create a .env file in the root directory:
DB_SYNCHRONIZE=true # Auto-sync database schema (disable in production)
NODE_ENV=development # Environment modeThe project uses SQLite by default, but you can easily switch to other databases by modifying src/data-source.ts:
export const AppDataSource = new DataSource({
type: "postgres", // or "mysql", "mariadb", etc.
host: "localhost",
port: 5432,
username: "your_username",
password: "your_password",
database: "your_database",
// ... rest of configuration
});npm start # Compile TypeScript and run the application
npm run typeorm # Run TypeORM CLI commandsContributions are welcome! Here are some ways you can contribute:
- π Report bugs - Found an issue? Let us know!
- π‘ Suggest features - Have ideas for improvements?
- π Improve documentation - Help make the docs better
- π§ Submit pull requests - Fix bugs or add features
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- TypeORM Documentation - Learn about TypeORM features and configuration
- TypeScript Handbook - Master TypeScript fundamentals
- SQLite Documentation - Understand SQLite database features
This project is licensed under the MIT License - see the LICENSE file for details.
If this project helped you learn TypeORM and TypeScript, please consider giving it a β!
Happy Coding! π
Built with β€οΈ using TypeORM and TypeScript