Skip to content

Commit c979cc7

Browse files
committed
feat: adds chanel_hutt_loader.ts
1 parent 89614e9 commit c979cc7

File tree

2 files changed

+50
-2
lines changed

2 files changed

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

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

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

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
4+
import { ChanelHuttLoader } from './chanel_hutt_loader.js';
45
import { DylanLaffertysLoader } from './dylan_lafferty_loaders.js';
5-
66
export const Loaders = Symbol.for('Loaders');
77

8-
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader];
8+
const LOADER_PROVIDERS = [
9+
AnthonyMaysLoader,
10+
DylanLaffertysLoader,
11+
ChanelHuttLoader,
12+
];
913

1014
@Module({
1115
providers: [

0 commit comments

Comments
 (0)