-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnote.server.ts
More file actions
69 lines (63 loc) · 1.4 KB
/
note.server.ts
File metadata and controls
69 lines (63 loc) · 1.4 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { User } from '@supabase/supabase-js';
import type { definitions } from 'types/supabase';
import supabase, { supabaseAdmin } from '~/supabase.server';
import { v4 as uuid } from 'uuid';
export type Note = definitions['notes'];
export async function getNote({
id,
userId
}: Pick<Note, 'id'> & {
userId: User['id'];
}) {
const { data, error } = await supabase
.from<Note>('notes')
.select('*')
.eq('user_id', userId)
.eq('id', id);
if (error) throw error;
return data[0];
}
export async function getNoteListItems({ userId }: { userId: User['id'] }) {
const { data, error } = await supabase
.from<Note>('notes')
.select('*')
.eq('user_id', userId);
if (error) throw error;
return data;
}
export async function createNote({
body,
title,
userId
}: Pick<Note, 'body' | 'title'> & {
userId: User['id'];
}) {
const newNoteId = uuid();
const { error } = await supabaseAdmin.from<Note>('notes').insert(
[
{
id: newNoteId,
body,
title,
user_id: userId
}
],
{
returning: 'minimal'
}
);
if (error) throw error;
return newNoteId;
}
export async function deleteNote({
id,
userId
}: Pick<Note, 'id'> & { userId: User['id'] }) {
const { data, error } = await supabaseAdmin
.from<Note>('notes')
.delete()
.eq('id', id)
.eq('user_id', userId);
if (error) throw error;
return data;
}