-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-thought-to-highlight.js
61 lines (46 loc) · 1.71 KB
/
add-thought-to-highlight.js
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
import { getEmbedding } from "./embed-into-supabase.js"
import { createClient } from '@supabase/supabase-js'
import { v4 as uuidv4 } from 'uuid';
import dotenv from 'dotenv'
dotenv.config()
const supabaseUrl = process.env.SUPABASE_URL
const supabaseKey = process.env.SUPABASE_KEY
const supabase = createClient(supabaseUrl, supabaseKey, {
auth: {
persistSession: false
}
})
// add thought to highlight.thought JSONB column
// schema == [{ "thoughtId": uuid, "thought": string, "userId": int, "createdAt": timestamp }]
export const addThoughtToHighlight = async (highlightId, thought, userId) => {
const { data: highlight, error } = await supabase.from('highlights').select('*').eq('id', highlightId)
if (error) {
console.log(error)
return
}
const thoughts = highlight[0].thoughts
const thoughtObj = {
thoughtId: uuidv4(),
thought: thought,
userId: userId,
createdAt: new Date().toISOString()
}
if (!thoughts) {
const newThoughts = [thoughtObj]
const { data: updatedHighlight, error: updateError } = await supabase.from('highlights').update({ thoughts: newThoughts }).eq('id', highlightId)
if (updateError) {
console.log(updateError)
return
}
return updatedHighlight
}
const newThoughts = [...thoughts, thoughtObj]
const { data: updatedHighlight, error: updateError } = await supabase.from('highlights').update({ thoughts: newThoughts }).eq('id', highlightId)
if (updateError) {
console.log(updateError)
return
}
return updatedHighlight
}
// test
// addThoughtToHighlight('592371723', 'this is a test thought', 1).then(res => console.log(res))