-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnote.server.ts
43 lines (37 loc) · 992 Bytes
/
note.server.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
import { and, desc, eq } from 'drizzle-orm'
import { db } from '~/db/db.server'
import { notes, type Note } from '~/db/schema'
export type { Note }
export function getNoteListItems({
userId,
}: {
userId: Note['userId']
}): Promise<Note[]> {
return db.query.notes.findMany({
where: eq(notes.userId, userId),
orderBy: [desc(notes.updatedAt)],
})
}
export function getNote({
id,
userId,
}: Pick<Note, 'id' | 'userId'>): Promise<Note | undefined> {
return db.query.notes.findFirst({
where: and(eq(notes.id, id), eq(notes.userId, userId)),
})
}
export async function deleteNote({
id,
userId,
}: Pick<Note, 'id' | 'userId'>): Promise<{ id: Note['id'] } | undefined> {
return db
.delete(notes)
.where(and(eq(notes.id, id), eq(notes.userId, userId)))
.returning({ id: notes.id })
.get()
}
export function createNote(
data: Pick<Note, 'body' | 'title' | 'userId'>,
): Promise<Note> {
return db.insert(notes).values(data).returning().get()
}