add script for reading in the data csv to mongodb

This commit is contained in:
Brandon Egger 2023-03-15 17:45:04 -05:00
parent 3ae346a807
commit c17768e5bc
4 changed files with 91 additions and 12 deletions

4
data/at_resources.csv Normal file
View File

@ -0,0 +1,4 @@
Name,Icon,Platforms,Ages,Skills,Skill Level,Manufacturer,Payment Type,Download Link,Description
AB Listening Adventures,,APP_IOS,0-10,WORDS;SENTENCES;DISCOURSE,BEGINNER;INTERMEDIATE,n/a,FREE,,N/A
Adult CI Home-Based AT,,PDF,10-100,PHONEMES;WORDS,BEGINNER;INTERMEDIATE;ADVANCED,n/a,FREE,,N/A
Amptify,,WEBSITE,10-100,PHONEMES;WORDS,BEGINNER;INTERMEDIATE;ADVANCED,n/a,SUBSCRIPTION_WEEKLY,123,testing
1 Name Icon Platforms Ages Skills Skill Level Manufacturer Payment Type Download Link Description
2 AB Listening Adventures APP_IOS 0-10 WORDS;SENTENCES;DISCOURSE BEGINNER;INTERMEDIATE n/a FREE N/A
3 Adult CI Home-Based AT PDF 10-100 PHONEMES;WORDS BEGINNER;INTERMEDIATE;ADVANCED n/a FREE N/A
4 Amptify WEBSITE 10-100 PHONEMES;WORDS BEGINNER;INTERMEDIATE;ADVANCED n/a SUBSCRIPTION_WEEKLY 123 testing

View File

@ -37,23 +37,29 @@ enum SkillLevel {
ADVANCED
}
enum PaymentType {
FREE
SUBSCRIPTION_WEEKLY
SUBSCRIPTION_MONTHLY
}
type RangeInput {
min Int
max Int
}
model AuditoryResource {
id String @id @default(auto()) @map("_id") @db.ObjectId
icon String
name String
description String
manufacturer String
platforms Platform[]
ages RangeInput
skills Skill[]
skill_levels SkillLevel[]
cost Float
download_link String
id String @id @default(auto()) @map("_id") @db.ObjectId
icon String
name String
description String
manufacturer String
platforms Platform[]
ages RangeInput
skills Skill[]
skill_levels SkillLevel[]
payment_options PaymentType[]
download_link String
}
// Necessary for Next auth

View File

@ -1,7 +1,75 @@
import { PrismaClient } from "@prisma/client";
import { type Platform, PrismaClient, type RangeInput, type Skill, type PaymentType, type SkillLevel, type Prisma } from "@prisma/client";
import * as path from 'path';
import fs from "fs";
import readline from "readline";
const filePath = path.resolve(__dirname, '../data/at_resources.csv');
const prisma = new PrismaClient();
const readCSVData = async (path: string): Promise<Prisma.AuditoryResourceCreateInput[]> => {
const stream = fs.createReadStream(path);
const reader = readline.createInterface({ input: stream });
return await new Promise((resolve, _reject) => {
const auditoryResources: Prisma.AuditoryResourceCreateInput[] = [];
reader.on("line", row => {
const rowCells = row.split(",");
if (rowCells[0] === "Name") {
//skip first row
return;
}
const platforms: Platform[] = (rowCells[2]?.split(";") ?? []) as Platform[];
const skills: Skill[] = (rowCells[4]?.split(";") ?? []) as Skill[];
const skill_levels: SkillLevel[] = (rowCells[5]?.split(";") ?? []) as SkillLevel[];
const payment_options: PaymentType[] = (rowCells[7]?.split(";") ?? []) as PaymentType[];
const splitAgeString = rowCells[3]?.split('-') ?? ['0', '10'];
const ages: RangeInput = {
min: parseInt(splitAgeString[0] ?? '0'),
max: parseInt(splitAgeString[1] ?? '10'),
}
const data: Prisma.AuditoryResourceCreateInput = {
name: rowCells[0] ?? 'n/a',
icon: rowCells[1] ?? '',
description: rowCells[9] ?? 'n/a',
download_link: rowCells[8] ?? '',
manufacturer: rowCells[6] ?? 'n/a',
platforms,
ages,
skills,
skill_levels,
payment_options
}
auditoryResources.push(data);
});
reader.on("close", () => {
// Reached the end of file
resolve(auditoryResources)
console.log("Loading CSV completed!");
});
});
}
async function main() {
const resources = await readCSVData(filePath);
console.log(resources);
for (const resource of resources) {
console.log(resource)
await prisma.auditoryResource.create({
data: resource,
});
}
}
/*
async function main() {
await prisma.auditoryResource.createMany({
data: [
@ -30,6 +98,7 @@ async function main() {
],
});
}
*/
main()
.then(async () => {