-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusersRepository.ts
More file actions
52 lines (43 loc) · 1.31 KB
/
usersRepository.ts
File metadata and controls
52 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { eq } from 'drizzle-orm';
import { DBProvider } from '../db/provider';
import { User } from './userEntity';
import { usersTable } from './usersTable';
export interface UsersRepository {
findById(id: string): Promise<User | null>;
findByEmail(email: string): Promise<User | null>;
create(user: User): Promise<void>;
update(user: User): Promise<void>;
delete(id: string): Promise<void>;
}
export class SqlUsersRepository implements UsersRepository {
constructor(private readonly db: DBProvider) {}
async findById(id: string): Promise<User | null> {
const [user] = await this.db
.get()
.select()
.from(usersTable)
.where(eq(usersTable.id, id));
return user ?? null;
}
async findByEmail(email: string): Promise<User | null> {
const [user] = await this.db
.get()
.select()
.from(usersTable)
.where(eq(usersTable.email, email));
return user ?? null;
}
async create(user: User): Promise<void> {
await this.db.get().insert(usersTable).values(user);
}
async update(user: User): Promise<void> {
await this.db
.get()
.update(usersTable)
.set(user)
.where(eq(usersTable.id, user.id));
}
async delete(id: string): Promise<void> {
await this.db.get().delete(usersTable).where(eq(usersTable.id, id));
}
}