royib / clean-architecture-nestJS

An in-depth implementation of Clean Architecture using NestJS and type-script
795 stars 192 forks source link

Incompatible types #2

Open icodefish opened 2 years ago

icodefish commented 2 years ago

Hi,

great article but here's the problem I came across while looking through your code. In your MongoDataServices class you import your Author, Book and Genre from your mongo models but the abstract class IDataServices makes use of Author, Book and Genre that come from entites of the core directory. And these classes are different so my linter cries with errors.

osan15 commented 2 years ago

Hi,

I agree with you, if we take the BookServices, in the createBook() method the ORM generates a Book from the file "frameworks/data-services/mongo/model/book.model.ts" (of type BookDocument). On the other hand, the createBook() method returns a Book from the file "core/entities/book.entity.ts" which is not at all related to the Mongoose entity.

How to solve this problem ? Should we have an abstract method in the
"frameworks/data-services/mongo/model/book.model.ts" class which allows to generate a book based on a mongoose book entity?

royibenita commented 2 years ago

Hey, TypeScript is checking for computability between src/frameworks/data-services/mongo/model/book.model.ts and src/core/entities/book.entity.ts if you try to change or omit one of the properties in the model, typescript will give an error. for example I have omitted publishDate from the model and gut an error: image

other approach is to use a different ORM like typeorm and use generic annotations in your entities - https://docs.nestjs.com/techniques/database it is still not clean as you would want it to be but it is a good compromise, you are coupling to an ORM and not for a DB. I prefer to stay with the approach I have implemented