I came across a bit of a design problem with Mongoose - but maybe I am following the wrong approach?
In good old OOP style I want to have a class called User. The class has several member such as username, firstname, lastname, salt and hash, and it has a method called authenticate which takes a hashed password as an argument. However to fill my User object with data, I need to write code like this, as far as I am aware:
var userSchema = mongoose.Schema({
firstName: String,
lastName: String,
userName: String,
salt: String,
hashed_pwd:String
});
userSchema.methods = {
authenticate: function(passwordToMatch) {
return crypto.hashPwd(this.salt, passwordToMatch) === this.hashed_pwd;
}
}
This code is cluttered with Mongoose specific code - calling mongoose.Schema, and to add the method I have to use userSchema.methods. When I tried to declare my User class separately and tried to create the Mongoose Schema out of it, I encountered all kinds of errors, and the member method was not recognised.
So when I want to use a different database using the same schema, I have to redeclare everything. But I want to declare all my schemas separately as they will reoccure all over the place in my application.
Is there any elegant solution to this, is there a different library I can use, or am I following a completely wrong approach?
thanks in advance..