Spaces:
Sleeping

File size: 1,208 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { eq } from "drizzle-orm";
import { users, User, NewUser } from "../schema/users";
import { IPgUserRepository } from "./IPgUserRepository";

export class PgUserRepository implements IPgUserRepository {
    constructor(private readonly db: any) { }

    async upsert(input: NewUser): Promise<void> {
        await this.db
            .insert(users)
            .values(input)
            .onConflictDoUpdate({
                target: [users.email],
                set: {
                    displayName: input.displayName,
                    passwordHash: input.passwordHash,
                    role: input.role,
                    updatedAt: new Date(),
                },
            });
    }

    async findById(id: string): Promise<User | null> {
        const [result] = await this.db.select().from(users).where(eq(users.id, id)).limit(1);
        return result || null;
    }

    async findByEmail(email: string): Promise<User | null> {
        const [result] = await this.db.select().from(users).where(eq(users.email, email)).limit(1);
        return result || null;
    }

    async delete(id: string): Promise<void> {
        await this.db.delete(users).where(eq(users.id, id));
    }
}