-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimportResources.js
More file actions
58 lines (53 loc) · 1.86 KB
/
importResources.js
File metadata and controls
58 lines (53 loc) · 1.86 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
import fs from 'fs';
import path from 'path';
import { PrismaClient } from '@prisma/client';
// Upload Resources to the Database, and update current ones
const prisma = new PrismaClient();
async function importResources() {
const filePath = path.join(process.cwd(), 'public', 'gcse_log_json.json');
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
for (const item of data) {
const existing = await prisma.resource.findUnique({
where: { resourceId: String(item["Resource ID"]) },
});
if (existing) {
// Update all fields except averageRating
await prisma.resource.update({
where: { resourceId: String(item["Resource ID"]) },
data: {
type: item["Resource Type"],
title: item["Resource Title"],
level: item["Study Level"],
subject: item["Subject"],
examBoard: item["Exam Board"],
link: item["Link"],
author: item["Resource Author"],
description: item["Resource Description"],
// Skip averageRating on purpose, prevents database from overriding current rating
},
});
console.log(`Updated resource ID ${item["Resource ID"]}`);
} else {
await prisma.resource.create({
data: {
resourceId: String(item["Resource ID"]),
type: item["Resource Type"],
title: item["Resource Title"],
level: item["Study Level"],
subject: item["Subject"],
examBoard: item["Exam Board"],
link: item["Link"],
author: item["Resource Author"],
description: item["Resource Description"],
averageRating: item["Average Rating"] || 0,
},
});
console.log(`Imported new resource ID ${item["Resource ID"]}`);
}
}
await prisma.$disconnect();
}
importResources().catch((e) => {
console.error(e);
prisma.$disconnect();
});