# TypeORM Best Practices
TypeORM is an ORM that can run in Node.js and can be used with TypeScript and JavaScript. Here are some best practices to follow.
## Use Repository Pattern
Always use repositories for database operations:
```typescript
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository,
) {}
}
```
## Avoid N+1 Queries
Use eager loading or query builder with joins:
```typescript
const users = await this.userRepository.find({
relations: ['posts', 'profile'],
});
```
## Use Transactions
For operations that need atomicity:
```typescript
await this.dataSource.transaction(async (manager) => {
await manager.save(user);
await manager.save(profile);
});
```