【问题标题】:How to handle User/Group relationships in SailsJS using MongoDB?如何使用 MongoDB 处理 SailsJS 中的用户/组关系?
【发布时间】:2014-08-29 19:06:53
【问题描述】:

我是 SailsJS(使用 v0.10)和 MongoDB 的新手,并试图实现用户/组关系的功能。我无法理解它。

这是我尝试实现的:

  • 用户可以是一个或多个的成员。
  • 可以有一个或多个成员(用户)。
  • 中的每个成员都可以有一个角色,AdminNormal
  • 在获取 User 时,还会将用户所属的所有 Groups 作为属性返回。
  • 在获取 Group 时,还会将组中的所有 Users 作为属性返回。

我有以下模型。

User.js

module.exports = {

    attributes: {

        // Email, this is used as login credentials 
        email: {
            type: 'email', // Email type will get validated by the ORM
            required: true,
            unique: true
        },

        // The name of the person, used to simplify identification
        name: {
            type: 'string',
            required: true
        },

    }
};

Groups.js

module.exports = {

    attributes: {

        name: {
            type: 'string',
            required: true
        },

        description: {
            type: 'string'
        },

    }

};

我需要一些关于如何实现它的帮助。

谢谢

【问题讨论】:

  • 对于 mongodb 中的模式,请查看 these blog post 它是三个部分。我想他们会帮助你。
  • 谢谢!重做它,并尝试从我得到的 ide 中制作一些原型。

标签: mongodb sails.js waterline


【解决方案1】:

您可以在 Sails/Waterline 中使用多对多关联来实现这一点。更多信息请见:http://sailsjs.org/#/documentation/concepts/ORM/Associations/ManytoMany.html

User.js

module.exports = {

    attributes: {

        // Email, this is used as login credentials 
        email: {
            type: 'email', // Email type will get validated by the ORM
            required: true,
            unique: true
        },

        // The name of the person, used to simplify identification
        name: {
            type: 'string',
            required: true
        },

        // Groups
        groups: {
            collection: 'groups',
            via: 'members'
        }

    }
};

Groups.js

module.exports = {

    attributes: {

        name: {
            type: 'string',
            required: true
        },

        description: {
            type: 'string'
        },

        // Members
        members: {
            collection: 'user',
            via: 'groups'
        }

    }

};

【讨论】:

  • 这并不完全奏效,因为我无法使用此技术指定每个用户在组中的角色。
猜你喜欢
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
相关资源
最近更新 更多