I am developing an application in which I need to have some abstraction.
I mean, I would like to "mimic" the behavior of an interface, for example, creating a contract inside my specific classes.
Actually, when dealing with Users, I have a UserMongoRepository class with an implemented contract:
- getAll () returns a complete list of users by promise
- getById (id) returns the interested user by promise
- save (user) saves the user as promised
- ... etc.
I have the same methods implemented inside UserMysqlRepository (allowing me to switch behavior when a change is required.
Problem
My problem is that I am dealing with Mongoose, which does not act as a datamapper, but rather as an active record.
This means that my save (user) implementation will be a bit strange:
save(user){ let mongooseUser = this.convert(user); return user.save(); }
The conversion method allows me to move from a standard model to a specific Mongoose model. This allows me to have an abstraction again and not to rewrite my full access to application data.
My real problem is when I try to unit test my full class:
import MongooseUser from '../../auth/mongooseModel/MongooseUser'; export default class UserMongoRepository{ constructor(){ } convert(user){ return new MongooseUser({email:user.mail,password:user.password,firstname:user.firstName, lastname:user.lastName}); } findById(id){ return MongooseUser.find({id:id}); } save(user){ return user.save(); } }
In a standard way, I would enter my DAO inside my constructor and be able to mock it.
In the case of the mongoose, this is a bit of a concern, because the element that does the task is not an instance of the object (so that I can mock it), but the class definition is imported at the top of the document.
Solutions
Should I pass the MongooseUser class definition as a parameter inside the constructor?
Suppose I have this code inside the conversion method:
let user = new this.MongooseUser({})
Do you have an idea to abstract the behavior of the mongoose in the way data is displayed?
I do not want to use another module, this, in my opinion, is the most advanced with NodeJS ...