Skip to content

Feat homework 10/ Loading CSV Files #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lesson_10/libraries/src/loaders/dylan_lafferty_loaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import csv from 'csv-parser';
import fs from 'fs';
import { Credit, MediaItem } from '../models/index.js';
import { Loader } from './loader.js';

export class DylanLaffertysLoader implements Loader {
getLoaderName(): string {
return 'dylanlafferty';
}
async loadData(): Promise<MediaItem[]> {
const credits = await this.loadCredits(); //loads credits from the csv file
const mediaItems = await this.loadMediaItems(); //loads media items from csv file

//Creates a map where the key is a string and the value is MediaItem Object
const mapIndex = new Map<string, MediaItem>();

//Loops through the CSV file and gets the id of the mediaItem that is specified.
for (const mediaItem of mediaItems) {
mapIndex.set(mediaItem.getId(), mediaItem);

}

for (const credit of credits) {
const mediaItem = mapIndex.get(credit.getMediaItemId()); //Finds the media item by getting media ID
if (mediaItem) {
mediaItem.addCredit(credit);
}
}

console.log(
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
);
//Returns a newly created array by converting the map values
return Array.from(mapIndex.values());
}

async loadMediaItems(): Promise<MediaItem[]> {
const mediaItem = [];
const readable = fs
.createReadStream('data/media_items.csv', 'utf-8')
.pipe(csv());

for await (const row of readable) {
const { id, title, type, year } = row;
mediaItem.push(new MediaItem(id, title, type, year, []));
}
return mediaItem;
}

async loadCredits(): Promise<Credit[]> {
const credits = [];
const readable = fs
.createReadStream('data/credits.csv', 'utf-8')
.pipe(csv());
for await (const row of readable) {
const { media_item_id, role, name } = row;
credits.push(new Credit(media_item_id, name, role));
}
return credits;
}
}
//# sourceMappingURL=dylan_lafferty_loaders.js.map
3 changes: 2 additions & 1 deletion lesson_10/libraries/src/loaders/loaders.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';

import { AnthonyMaysLoader } from './anthony_mays_loader.js';
import { DylanLaffertysLoader } from './dylan_lafferty_loaders.js';

export const Loaders = Symbol.for('Loaders');

const LOADER_PROVIDERS = [AnthonyMaysLoader];
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader];

@Module({
providers: [
Expand Down