Skip to content

Commit 0bce1fb

Browse files
committed
FEATURE: Target the media_items.csv file to load data.
1 parent b01aa8a commit 0bce1fb

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import csv from 'csv-parser';
2+
import fs from 'fs';
3+
import { Credit, MediaItem } from '../models/index.js';
4+
import { Loader } from './loader.js';
5+
6+
export class JBeyLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'JBey';
9+
}
10+
11+
async loadData(): Promise<MediaItem[]> {
12+
const credits = await this.loadCredits();
13+
const mediaItems = await this.loadMediaItems();
14+
15+
console.log(
16+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
17+
);
18+
19+
return [...mediaItems.values()];
20+
}
21+
22+
async loadMediaItems(): Promise<MediaItem[]> {
23+
// TODO: Implement this method.
24+
const mediaItems = [];
25+
const readable = fs
26+
.createReadStream('data/media_items.csv', 'utf-8')
27+
.pipe(csv());
28+
for await (const row of readable) {
29+
const { id, title, type, year } = row;
30+
mediaItems.push(new MediaItem(id, title, type, year, []));
31+
}
32+
return mediaItems;
33+
}
34+
35+
async loadCredits(): Promise<Credit[]> {
36+
const credits = [];
37+
const readable = fs
38+
.createReadStream('data/credits.csv', 'utf-8')
39+
.pipe(csv());
40+
for await (const row of readable) {
41+
const { media_item_id, role, name } = row;
42+
credits.push(new Credit(media_item_id, name, role));
43+
}
44+
return credits;
45+
}
46+
}

lesson_10/libraries/src/loaders/loaders.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Module } from '@nestjs/common';
22

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
4+
import { JBeyLoader } from './JBey_loader.js';
45

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

7-
const LOADER_PROVIDERS = [AnthonyMaysLoader];
8+
const LOADER_PROVIDERS = [AnthonyMaysLoader, JBeyLoader];
89

910
@Module({
1011
providers: [

0 commit comments

Comments
 (0)