-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUserRepository.ts
49 lines (38 loc) · 1.35 KB
/
UserRepository.ts
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
import { eq, inArray } from 'drizzle-orm'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import {
type NewUser,
type UpdatedUser,
type User,
user as userTable,
} from '../../../db/schema/user.js'
import type { UsersInjectableDependencies } from '../userDiConfig.js'
export class UserRepository {
private readonly drizzle: PostgresJsDatabase
constructor({ drizzle }: UsersInjectableDependencies) {
this.drizzle = drizzle
}
async getUser(id: string): Promise<User | null> {
const [result] = await this.drizzle.select().from(userTable).where(eq(userTable.id, id))
return result ?? null
}
getUsers(userIds: string[]): Promise<User[]> {
return this.drizzle.select().from(userTable).where(inArray(userTable.id, userIds))
}
async deleteUser(id: string): Promise<User | null> {
const [result] = await this.drizzle.delete(userTable).where(eq(userTable.id, id)).returning()
return result ?? null
}
async updateUser(id: string, updatedUser: UpdatedUser): Promise<User | null> {
const [result] = await this.drizzle
.update(userTable)
.set(updatedUser)
.where(eq(userTable.id, id))
.returning()
return result ?? null
}
async createUser(user: NewUser): Promise<User> {
const [result] = await this.drizzle.insert(userTable).values(user).returning()
return result
}
}