Implement repository interfaces to use any database (PostgreSQL, MySQL, DynamoDB, etc.).
All data access goes through four interfaces in src/auth/repositories/interfaces/:
| Interface | Methods |
|---|---|
IUserRepository |
create, findById, findByEmail, findByUsername, update, resetFailedAttempts |
ISessionRepository |
create, findBySessionId, findByUserId, deleteBySessionId, deleteByUserId, touch, updateSessionId, countByUserId, deleteOldestByUserId |
ITokenRepository |
create, findByToken, deleteByToken, deleteByUserId |
ILoginHistoryRepository |
create, findByUserId, deleteOldEntries |
src/auth/repositories/
├── interfaces/ ← Keep these unchanged
├── mongodb/ ← Existing MongoDB implementations
└── postgres/ ← Your new folder
├── user.repository.ts
├── session.repository.ts
├── token.repository.ts
└── login-history.repository.ts
import type { IUserRepository } from '../interfaces/user.repository.interface.js';
export class PostgresUserRepository implements IUserRepository {
async create(data: CreateUserDto): Promise<UserDocument> {
// Your PostgreSQL logic here
}
// Implement all methods...
}In src/auth/index.ts, swap the repository imports:
import { PostgresUserRepository } from './repositories/postgres/user.repository.js';
const userRepository = new PostgresUserRepository();- All repository methods must return the same types as defined in the interfaces
- The
_idfield must be a string-convertible value (toString()) - Timestamps (
createdAt,updatedAt) must beDateobjects