-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompts.ts
177 lines (145 loc) · 3.43 KB
/
prompts.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { supabase } from "@/lib/supabase/browser-client"
import { TablesInsert, TablesUpdate } from "@/supabase/types"
export const getPromptById = async (promptId: string) => {
const { data: prompt, error } = await supabase
.from("prompts")
.select("*")
.eq("id", promptId)
.single()
if (!prompt) {
throw new Error(error.message)
}
return prompt
}
export const getPromptWorkspacesByWorkspaceId = async (workspaceId: string) => {
const { data: workspace, error } = await supabase
.from("workspaces")
.select(
`
id,
name,
prompts (*)
`
)
.eq("id", workspaceId)
.single()
if (!workspace) {
throw new Error(error.message)
}
return workspace
}
export const getPromptWorkspacesByPromptId = async (promptId: string) => {
const { data: prompt, error } = await supabase
.from("prompts")
.select(
`
id,
name,
workspaces (*)
`
)
.eq("id", promptId)
.single()
if (!prompt) {
throw new Error(error.message)
}
return prompt
}
export const createPrompt = async (
prompt: TablesInsert<"prompts">,
workspace_id: string
) => {
const { data: createdPrompt, error } = await supabase
.from("prompts")
.insert([prompt])
.select("*")
.single()
if (error) {
throw new Error(error.message)
}
await createPromptWorkspace({
user_id: createdPrompt.user_id,
prompt_id: createdPrompt.id,
workspace_id
})
return createdPrompt
}
export const createPrompts = async (
prompts: TablesInsert<"prompts">[],
workspace_id: string
) => {
const { data: createdPrompts, error } = await supabase
.from("prompts")
.insert(prompts)
.select("*")
if (error) {
throw new Error(error.message)
}
await createPromptWorkspaces(
createdPrompts.map(prompt => ({
user_id: prompt.user_id,
prompt_id: prompt.id,
workspace_id
}))
)
return createdPrompts
}
export const createPromptWorkspace = async (item: {
user_id: string
prompt_id: string
workspace_id: string
}) => {
const { data: createdPromptWorkspace, error } = await supabase
.from("prompt_workspaces")
.insert([item])
.select("*")
.single()
if (error) {
throw new Error(error.message)
}
return createdPromptWorkspace
}
export const createPromptWorkspaces = async (
items: { user_id: string; prompt_id: string; workspace_id: string }[]
) => {
const { data: createdPromptWorkspaces, error } = await supabase
.from("prompt_workspaces")
.insert(items)
.select("*")
if (error) throw new Error(error.message)
return createdPromptWorkspaces
}
export const updatePrompt = async (
promptId: string,
prompt: TablesUpdate<"prompts">
) => {
const { data: updatedPrompt, error } = await supabase
.from("prompts")
.update(prompt)
.eq("id", promptId)
.select("*")
.single()
if (error) {
throw new Error(error.message)
}
return updatedPrompt
}
export const deletePrompt = async (promptId: string) => {
const { error } = await supabase.from("prompts").delete().eq("id", promptId)
if (error) {
throw new Error(error.message)
}
return true
}
export const deletePromptWorkspace = async (
promptId: string,
workspaceId: string
) => {
const { error } = await supabase
.from("prompt_workspaces")
.delete()
.eq("prompt_id", promptId)
.eq("workspace_id", workspaceId)
if (error) throw new Error(error.message)
return true
}