简体   繁体   English

使用 MongoDB 时 Sails.js 不应用模型方案

[英]Sails.js not applying model scheme when using MongoDB

I'm going through the (excellent) Sails.js book , which discusses creating a User model User.js in Chapter 6 like so:我正在阅读(优秀的) Sails.js 书,它在第 6 章中讨论了创建用户模型User.js ,如下所示:

module.exports = {
    connection: "needaword_postgresql",
    migrate: 'drop',
    attributes: {
        email: { 
            type: 'string',
            email: "true",
            unique: 'string'
        },

        username: {
            type: 'string',
            unique: 'string'
        },

        encryptedPassword: { 
            type: 'string'
        },

        gravatarURL: { 
            type: 'string'
        },

        deleted: {
            type: 'boolean'
        },

        admin: {
            type: 'boolean'
        },

        banned: {
            type: 'boolean'
        }
    },
    toJSON: function() { 
      var modelAttributes  = this.toObject();
      delete modelAttributes.password;
      delete modelAttributes.confirmation;
      delete modelAttributes.encryptedPassword;
      return modelAttributes;
    }   
};

Using Postgres, a new record correctly populates the boolean fields not submitted by the login form as null, as the book suggests should be the case:使用 Postgres,新记录正确地将登录表单未提交的布尔字段填充为 null,正如书中所建议的那样:

在此处输入图片说明

But I want to use MongoDB instead of PostgreSQL.但我想使用 MongoDB 而不是 PostgreSQL。 I had no problem switching the adaptor.我切换适配器没有问题。 But now, when I create a new record, it appears to ignore the schema in User.js and just put the literal POST data into the DB:但是现在,当我创建一条新记录时,它似乎忽略了User.js的架构,而只是将文字 POST 数据放入数据库中:

在此处输入图片说明

I understand that MongoDB is NoSQL and can take any parameters, but I was under the impression that using a schema in Users.js would apply to a POST request to the /user endpoint (via the blueprint routes for now) regardless of what database was sitting at the bottom.我知道 MongoDB 是 NoSQL 并且可以接受任何参数,但我的印象是在Users.js中使用模式将应用于对/user端点的 POST 请求(现在通过蓝图路由),而不管数据库是什么坐在底部。 Do I need to somehow explicitly tie the model to the endpoint for NoSQL databases?我是否需要以某种方式明确地将模型绑定到 NoSQL 数据库的端点?

(I've checked the records that are created in Postgres and MongoDB, and they match the responses from localhost:1337/user posted above) (我检查了在 Postgres 和 MongoDB 中创建的记录,它们与上面发布的 localhost:1337/user 的响应匹配)

I understand that MongoDB is NoSQL我知道 MongoDB 是 NoSQL

Good!好! In sails the sails-mongo waterline module is responsible for everything regarding mongodb.在sails 中,sails-mongo waterline 模块负责有关mongodb 的一切。 I think I found the relevant code: https://github.com/balderdashy/sails-mongo/blob/master/lib/document.js#L95 So sails-mongo simply does not care about non existent values.我想我找到了相关代码: https : //github.com/balderdashy/sails-mongo/blob/master/lib/document.js#L95所以sails-mongo 根本不关心不存在的值。 If you think this is bad then feel free to create an issue on the github page.如果您认为这很糟糕,请随时在 github 页面上创建问题。

A possible workaround might be using defaultsTo:一种可能的解决方法可能是使用 defaultsTo:

banned : {
    type : "boolean",
    defaultsTo : false
}

You can configure your model to strictly use the schema with this flag:您可以将模型配置为严格使用带有此标志的架构:

module.exports = {
  schema: true,
  attributes: {
    ...
  }
}

I eventually settled on performing the validations inside my controller.我最终决定在我的控制器中执行验证。

// a signup form
create: async (req, res) => {
    const { name, email, password } = req.body;
    try {
      const userExists = await sails.models.user.findOne({ email });
      if (userExists) {
        throw 'That email address is already in use.';
      }
}

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

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