import { serialize , deserialize } from 'bson' ; import fs from 'fs' ; import { Course } from '../../domain/Course' ; import { CourseRepository } from '../../domain/CourseRepository' ; export class FileCourseRepository implements CourseRepository { private FILE_PATH = `${ __dirname }/courses` ; async save ( course ) { fs . promises . writeFile ( this . filePath ( course . id ) , serialize ( course ) ) ; } async search ( courseId ) { const courseData = await fs . promises . readFile ( this . filePath ( courseId ) ) ; const { id , name , duration } = deserialize ( courseData ) ; return new Course ( { id , name , duration } ) ; } private filePath ( id ) { return `${ this . FILE_PATH }.${ id }.repo` ; } }