-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-random-highlight.js
116 lines (89 loc) · 3.33 KB
/
get-random-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
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
import { createClient } from "@supabase/supabase-js";
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,
},
});
export async function fetchRandomHighlightInBookID(bookIds, amount) {
try {
if (!amount) amount = 1;
if (amount > 10) amount = 10;
console.log(bookIds);
// Fetch a subset of 10 random IDs from the highlights table
const { data: ids_table, error } = await supabase.rpc(
"fetch_random_ids_by_book_ids",
{ book_ids: bookIds }
);
const ids = ids_table.map((id) => id.id);
if (error) throw error;
const highlights = [];
for (let i = 0; i < amount; i++) {
// Pick a random ID from the subset
const randomIndex = Math.floor(Math.random() * ids.length);
const randomId = ids[randomIndex];
// Fetch the highlight with the random ID
const { data: highlight, error: highlightError } = await supabase
.from("highlights")
.select("*")
.eq("id", randomId);
// fetch the book associated with the highlight from book_id
const { data: book, error: bookError } = await supabase
.from("books")
.select("*")
.eq("book_id", highlight[0].book_id);
// add the book to the highlight
highlight[0].book = book[0];
if (highlightError) throw highlightError;
highlights.push(highlight[0]);
// remove the ID from the subset
ids.splice(randomIndex, 1);
}
return highlights;
} catch (err) {
console.error("Error fetching random highlight from books:", err);
}
}
// Function to fetch a random highlight
export const fetchRandomHighlight = async (amount) => {
try {
if (!amount) amount = 1;
if (amount > 10) amount = 10;
// Fetch a subset of 10 random IDs from the highlights table
const { data: ids, error } = await supabase.rpc("fetch_random_ids");
if (error) throw error;
// console.log(ids); // Log the response to see its structure
const highlights = [];
for (let i = 0; i < amount; i++) {
// Pick a random ID from the subset
const randomIndex = Math.floor(Math.random() * ids.length);
const randomId = ids[randomIndex];
// Fetch the highlight with the random ID
const { data: highlight, error: highlightError } = await supabase
.from("highlights")
.select("*")
.eq("id", randomId);
// fetch the book associated with the highlight from book_id
const { data: book, error: bookError } = await supabase
.from("books")
.select("*")
.eq("book_id", highlight[0].book_id);
// add the book to the highlight
highlight[0].book = book[0];
if (highlightError) throw highlightError;
highlights.push(highlight[0]);
// remove the ID from the subset
ids.splice(randomIndex, 1);
}
return highlights;
} catch (error) {
console.error("Error fetching random highlight:", error);
}
};
// Fetch a random highlight
// fetchRandomHighlight(5).then((data) => console.log(data.length));
// Fetch a random highlight in a book
// fetchRandomHighlightInBookID([1474857, 3288110, 6928984, 1474862, 1474858], 5).then((data) => // console.log(data.map((d) => d.book.title)));