From c17768e5bc289ef6560fccd042a2863a15b74188 Mon Sep 17 00:00:00 2001 From: Brandon Egger Date: Wed, 15 Mar 2023 17:45:04 -0500 Subject: [PATCH] add script for reading in the data csv to mongodb --- data/{at_resources.xlsx => at_overview.xlsx} | Bin data/at_resources.csv | 4 ++ prisma/schema.prisma | 28 +++++--- prisma/seed.ts | 71 ++++++++++++++++++- 4 files changed, 91 insertions(+), 12 deletions(-) rename data/{at_resources.xlsx => at_overview.xlsx} (100%) create mode 100644 data/at_resources.csv diff --git a/data/at_resources.xlsx b/data/at_overview.xlsx similarity index 100% rename from data/at_resources.xlsx rename to data/at_overview.xlsx diff --git a/data/at_resources.csv b/data/at_resources.csv new file mode 100644 index 0000000..8d62c36 --- /dev/null +++ b/data/at_resources.csv @@ -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 diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2c81c0a..c589fb2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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 diff --git a/prisma/seed.ts b/prisma/seed.ts index 4d22cca..9fa17dd 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -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 => { + 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 () => {