简体   繁体   English

Node.JS和Mongoose OOP-如何从纯数据对象中分离出Mongoose数据访问代码?

[英]Node.JS with Mongoose OOP - how to separate mongoose data access code from pure data object?

I came across a bit of a design problem with Mongoose - but maybe I am following the wrong approach? 我在使用Mongoose时遇到了一个设计问题-但也许我采用了错误的方法?

In good old OOP style I want to have a class called User. 在旧的OOP风格中,我想要一个名为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. 该类具有多个成员,例如用户名,名字,姓氏,盐和哈希,并且它具有一个称为authenticate的方法,该方法将哈希密码作为参数。 However to fill my User object with data, I need to write code like this, as far as I am aware: 但是,据我所知,要用数据填充User对象,我需要编写如下代码:

    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. 该代码与Mongoose特定代码杂乱无章-调用mongoose.Schema,并添加我必须使用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. 当我尝试分别声明User类并尝试从中创建Mongoose Schema时,遇到各种错误,并且无法识别member方法。

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.. 提前致谢..

This is perhaps a partial answer, but it might help guide you in a useful direction. 这也许是部分答案,但是它可能有助于指导您朝着有用的方向发展。

It's worth understanding what mongoose is and what it does. 值得了解的是猫鼬及其作用。 Mongoose is an ODM (not an ORM— read here for the difference . TL;DR: ODM is to NoSQL as ORM is to SQL; non-relational DB to relational DB.) Mongoose helps abstract some of the boilerplate boring stuff like validations; 猫鼬是ODM(不是ORM ,区别在于此处 。TL; DR:ODM是NoSQL,就像ORM是SQL;非关系DB到关系DB。)Mongoose帮助抽象一些无聊的东西,例如验证; stuff you want when dealing with a data in or being written into a database. 在处理数据或将数据写入数据库时​​需要的东西。 The basis of this validation comes from mongoose.Schema with which you define what each model should contain and what each property on a model should be cast as (ie username should be a string, updatedOn should be a datestamp.) 验证的基础来自mongoose.Schema使用该模式定义每个模型应包含的内容以及模型上的每个属性应转换为的形式(即, 用户名应为字符串, updatedOn应为日期戳 。)

If you want your code to be database-agnostic (that is, not requiring any specific database), Mongoose is not what you're looking for. 如果您希望代码与数据库无关(即不需要任何特定的数据库),那么Mongoose并不是您想要的。 If not being tied to any specific DB is important to you, you should try looking for a database agnostic ORM/ODM. 如果不依赖任何特定的数据库对您很重要,则应尝试查找与数据库无关的ORM / ODM。 At the moment, Waterline comes to mind. 此刻, 水线浮现在脑海。

It should be mentioned that, if you don't care about validations, data casting, etc. that mongoose provides, you could just roll vanilla MongoDB queries and stick whatever JSON-compatible JavaScript object into your (NoSQL) database. 应该提到的是,如果您不关心猫鼬提供的验证,数据转换等,则只需滚动原始的MongoDB查询并将任何兼容JSON的JavaScript对象粘贴到您的(NoSQL)数据库中即可。 But this is not recommended unless you fully understand what you lose / risk in doing so. 但是不建议您这样做,除非您完全了解这样做会导致的损失/风险。

I might also argue that, the authenticate method has no place being in your model and should instead exist in a position previous to any model/database interaction (eg in a controller or the equivalent if you're not following MVC conventions.) 我可能还会争辩说,authenticate方法在您的模型中没有位置,而应该存在于任何模型/数据库交互之前的位置(例如,在控制器中,或者如果您不遵循MVC约定,则在等效方法中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM