Skip to content

Commit 4e59cc4

Browse files
committed
Adds: ananatawa_loader to Lesson10 and modifies loaders.module.ts file
1 parent 7b5f439 commit 4e59cc4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 AnanatawasLoader implements Loader {
7+
getLoaderName(): string {
8+
return 'ananatawa';
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+
const credits = [];
24+
const readable = fs
25+
.createReadStream('data/media_items.csv', 'utf-8')
26+
.pipe(csv());
27+
for await (const row of readable) {
28+
const { media_item_id, role, name } = row;
29+
credits.push(new Credit(media_item_id, name, role));
30+
}
31+
return [];
32+
}
33+
34+
async loadCredits(): Promise<Credit[]> {
35+
const credits = [];
36+
const readable = fs
37+
.createReadStream('data/credits.csv', 'utf-8')
38+
.pipe(csv());
39+
for await (const row of readable) {
40+
const { media_item_id, role, name } = row;
41+
credits.push(new Credit(media_item_id, name, role));
42+
}
43+
return credits;
44+
}
45+
}

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

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

3+
import { AnanatawaLoader } from './ananatawa_loader.js';
34
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
45

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

7-
const LOADER_PROVIDERS = [AnthonyMaysLoader];
8+
const LOADER_PROVIDERS = [AnthonyMaysLoader, AnanatawaLoader];
9+
810

911
@Module({
1012
providers: [

0 commit comments

Comments
 (0)