-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportScript.cjs
More file actions
53 lines (43 loc) · 1.39 KB
/
importScript.cjs
File metadata and controls
53 lines (43 loc) · 1.39 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
const fs = require("fs")
const CONVEX_URL = "https://confident-aardvark-526.convex.cloud"
const MUTATION_NAME = "teoria/mutate:importQuestionsWithAnswers"
async function runImport() {
try {
console.log("1. Wczytuję plik data.json...")
if (!fs.existsSync("data.json")) {
throw new Error(
"Błąd: Nie znaleziono pliku 'data.json'! Upewnij się, że nazwa jest poprawna.",
)
}
const rawData = fs.readFileSync("data.json", "utf8")
const questionsData = JSON.parse(rawData)
console.log(`Wczytano ${questionsData.length} pytań. Przygotowuję dane...`)
// @ts-ignore
const validatedData = questionsData.map((q) => ({
...q,
year: Number(q.year) || 0,
}))
console.log("2. Wysyłam dane do Convexa (fetch mutation)...")
const response = await fetch(`${CONVEX_URL}/api/mutation`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
path: MUTATION_NAME,
args: {
data: validatedData,
},
format: "json",
}),
})
const result = await response.json()
if (result.status === "success") {
console.log("Wszystkie pytania i odpowiedzi są już w bazie.")
console.log("Zwrócone dane:", result.value)
} else {
console.error("err", JSON.stringify(result, null, 2))
}
} catch (error) {}
}
runImport()