Skip to content

Commit 5a555d9

Browse files
committed
feat: add Lesson 10 Loader CSV Files
1 parent 89614e9 commit 5a555d9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

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

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

33
import { AnthonyMaysLoader } from './anthony_mays_loader.js';
44
import { DylanLaffertysLoader } from './dylan_lafferty_loaders.js';
5+
import { OliviaJamesLoader } from './olivia_james_loader.js';
56

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

8-
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader];
9+
const LOADER_PROVIDERS = [AnthonyMaysLoader, DylanLaffertysLoader, OliviaJamesLoader];
910

1011
@Module({
1112
providers: [
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
7+
export class OliviaJamesLoader implements Loader {
8+
getLoaderName(): string {
9+
return 'oliviajames';
10+
}
11+
12+
13+
async loadData(): Promise<MediaItem[]> {
14+
const credits = await this.loadCredits();
15+
const mediaItems = await this.loadMediaItems();
16+
17+
18+
console.log(
19+
`Loaded ${credits.length} credits and ${mediaItems.length} media items`,
20+
);
21+
22+
23+
return [...mediaItems.values()];
24+
}
25+
26+
27+
async loadMediaItems(): Promise<MediaItem[]> {
28+
const Media = [];
29+
const readable = fs
30+
31+
32+
.createReadStream('data/media_items.csv', 'utf-8')
33+
.pipe(csv());
34+
for await (const row of readable) {
35+
const { id, title, type, year } = row;
36+
Media.push(new MediaItem(id, title, type, year, []));
37+
}
38+
return Media;
39+
}
40+
41+
42+
async loadCredits(): Promise<Credit[]> {
43+
const credits = [];
44+
const readable = fs
45+
.createReadStream('data/credits.csv', 'utf-8')
46+
.pipe(csv());
47+
for await (const row of readable) {
48+
const { media_item_id, role, name } = row;
49+
credits.push(new Credit(media_item_id, name, role));
50+
}
51+
return credits;
52+
}
53+
}

0 commit comments

Comments
 (0)