多列引用单个表 - Sails JS API Model

时间:2016-05-11 06:03:09

标签: javascript mysql node.js sails.js

最近我学习了Sails JS,虽然看起来非常有用( 我不需要自己构建api?! )我正在测试帆的当前小项目遇到了一些障碍。

我的主要职业是老师,整个项目的最终目标是列出学生,他们与之合作的同行(friend_id)和学生他们没有(unfriend_id)。使用此信息以及他们当前的GPA,我想通过其他一些算法优化座位表。

第一部分,我需要从Sails数据服务器返回的数据与我达成一致。

我需要帆来做什么(我在一对多的集合中查看了帆文档以及多对多和多对一,但这个问题似乎特别重要)是聚集基于friend_idunfriend_id列的用户的所有项目。

数据

This SqlFiddle具有基本架构设置,其中包含一些虚拟数据,供每个人复制/粘贴并在需要时直接使用。

用户

CREATE TABLE `students` (
  `student_id` int(11) NOT NULL AUTO_INCREMENT,
  `student_first_name` varchar(200) NOT NULL,
  `student_last_name` varchar(255) NOT NULL,
  `student_home_phone` varchar(10) DEFAULT NULL,
  `student_guardian_email` varchar(255) DEFAULT NULL,
  `student_gpa` float NOT NULL DEFAULT '2',
  `class_id` tinyint(4) NOT NULL,
  UNIQUE KEY `student_id` (`student_id`)
);

关系

CREATE TABLE `relations` (
  `relation_id` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL DEFAULT '100000',
  `friend_id` int(11) DEFAULT NULL,
  `unfriend_id` int(11) DEFAULT NULL,
  UNIQUE KEY `relation_id` (`relation_id`)
);

虚拟数据(忽略名称)

INSERT INTO `students` VALUES (1,'Paul','Walker','1112223333','fake@email.com',2,1),(2,'Vin','Diesel','1112223333','fake@email.com',3,1),(3,'That','One\'Guy','1112223333','fake@email.com',4,1),(4,'Not','Yuagin','1112223333','fake@email.com',2,1),(5,'Hei','Yu','1112223333','fake@email.com',2,1);
INSERT INTO `relations` VALUES (1,1,2,NULL),(2,2,1,NULL),(3,1,NULL,4),(4,4,NULL,1),(5,1,5,NULL),(6,5,1,NULL),(7,2,3,NULL),(8,3,2,NULL);

我尝试了类似下面的内容,但是当我运行它时,api为两者返回一个空的json数组(在我至少收到学生/关系列表之前,这取决于我正在寻找的api)。

Students.js

module.exports = {
 connection:'localMysql',
    schema: 'true',
    attributes: {
        student_id: {
            type: "integer",
            required: true,
            unique: true,
            primaryKey:true
        },
        student_first_name:{
            type:'string'
        },
        student_last_name:{
            type:'string'
        },
        student_home_phone:{
            type:'integer'
        },
        student_guardian_email:{
            type: 'email'
        },
        class_id:{
            type:'integer'
        },
        friends:{
            collection:'relationship',
            via:'student_friends'
        }
    },
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false
}

Relationship.js

module.exports = {
    connection:'localMysql',
    tableName:'relations',

    attributes: {
        relation_id:{
            type: 'integer',
            required: true,
            unique: true,
            primaryKey:true
        },
        student_id:{
            type:'integer'
        },
        friend_id:{
            type:'integer'
        },
        unfriend_id:{
            type:'integer'
        },
        student_friends:{
            collection:'students',
            via:'student_id'
        }
    },
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false
}

我现在对所有正在发生的事情都不是全新的,但我对Node和Sails的新意,我似乎只是在摸不着头脑。如果我想要的不能通过集合模型完成,我会在哪里放置代码来进行这些事务?我假设(但你知道他们说的是什么......)它会出现在StudentsController.js文件中吗?

铊;博士

我可以获取此MySQL查询创建的行为:

select 
    s.*, group_concat(r.friend_id) as friends, group_concat(r.unfriend_id) as unfriends 
from 
    students s  
left join 
    relations r 
ON s.student_id = r.student_id
GROUP BY s.student_id;

使用Sails JS中的collection设置进​​行复制?

如果没有,我手动将代码放在哪里? (假设不是 StudentsController.js

更新

根据@Solarflare的建议,我已经设法获得更多内联,但这是一个非常艰难的开始。现在我得到了一个名为“人际关系”的专栏,但其中没有任何内容。哪个比以前更好,我什么都没有(因为所有返回的都是[])。但它是一个空阵列。 (输出如下)。

注意:我从关系模型中删除了student_friends声明。

我该如何解决?

[
  {
    "relationships": [],
    "student_id": 1,
    "student_first_name": "Paul",
    "student_last_name": "Walker",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [],
    "student_id": 2,
    "student_first_name": "Vin",
    "student_last_name": "Diesel",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [],
    "student_id": 3,
    "student_first_name": "That",
    "student_last_name": "One'Guy",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [],
    "student_id": 4,
    "student_first_name": "Not",
    "student_last_name": "Yuagin",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [],
    "student_id": 5,
    "student_first_name": "Hei",
    "student_last_name": "Yu",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  }
]

更新#2

在等待的时候,我只是有点乱,我有一些非常简陋的工作。我进入Relationship.js文件并将student_id的定义更改为model:'students',via:'student_id',我至少可以获得给定学生的所有关系。它给了我正确的收藏,但我仍然想知道是否有更直接的方法来摆弄它以获得我需要的东西。以下是localhost:1337/students的输出:

[
  {
    "relationships": [
      {
        "relation_id": 1,
        "friend_id": 2,
        "unfriend_id": null,
        "student_id": 1
      },
      {
        "relation_id": 3,
        "friend_id": null,
        "unfriend_id": 4,
        "student_id": 1
      },
      {
        "relation_id": 5,
        "friend_id": 5,
        "unfriend_id": null,
        "student_id": 1
      }
    ],
    "student_id": 1,
    "student_first_name": "Paul",
    "student_last_name": "Walker",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [
      {
        "relation_id": 2,
        "friend_id": 1,
        "unfriend_id": null,
        "student_id": 2
      },
      {
        "relation_id": 7,
        "friend_id": 3,
        "unfriend_id": null,
        "student_id": 2
      }
    ],
    "student_id": 2,
    "student_first_name": "Vin",
    "student_last_name": "Diesel",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [
      {
        "relation_id": 8,
        "friend_id": 2,
        "unfriend_id": null,
        "student_id": 3
      }
    ],
    "student_id": 3,
    "student_first_name": "That",
    "student_last_name": "One'Guy",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [
      {
        "relation_id": 4,
        "friend_id": null,
        "unfriend_id": 1,
        "student_id": 4
      }
    ],
    "student_id": 4,
    "student_first_name": "Not",
    "student_last_name": "Yuagin",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "relationships": [
      {
        "relation_id": 6,
        "friend_id": 1,
        "unfriend_id": null,
        "student_id": 5
      }
    ],
    "student_id": 5,
    "student_first_name": "Hei",
    "student_last_name": "Yu",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  }
]

1 个答案:

答案 0 :(得分:-1)

好的,所以这里的全部内容非常简单,我把我的代码放在上面,在这里和那里做了一些改变,说实话,不认为它会起作用,但我得到了某些东西哪个更好而不是我以前得到的一大堆。这不是一个完美的解决方案,但现在它的效果还不错。

这是文件,我做了什么:

Students.js

module.exports = {
    connection: 'localMysql',
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false,
    attributes: {
        student_id: {
            type: "integer",
            required: true,
            unique: true,
            primaryKey: true
        },
        student_first_name: {
            type: 'string'
        },
        student_last_name: {
            type: 'string'
        },
        student_home_phone: {
            type: 'integer'
        },
        student_guardian_email: {
            type: 'email'
        },
        class_id: {
            type: 'integer'
        },
        friends:{
            collection:'relationship',
            via:'student_id'
        },
        unfriends:{
            collection:'relationship',
            via:'student_id'
        }
    }

};

Relationship.js

module.exports = {
    connection: 'localMysql',
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false,
    tableName:'relations',
    attributes: {
        relation_id: {
            type: 'integer',
            required: true,
            unique: true,
            primaryKey: true
        },
        student_id: {
            model:'students',
            via:'student_id'
        },
        friend_id: {
            model:'students',
            via:'student_id',
            through:'friends'
        },
        unfriend_id: {
            model:'students',
            via:'student_id',
            through:'unfriends'
        }

    }
};

然后我将两个新文件添加到api/models/目录Friends.jsUnfriends.js,这两个文件都非常简单:

Friends.js

module.exports = {
    connection: 'localMysql',
    schema: false,
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false,
    attributes: {
        student_id:{
            model:'students',
            via:'student_id'
        },
        friend_id:{
            model:'relationship',
            via:'friend_id'
        }
    }

};

Unfriends.js

module.exports = {
    connection: 'localMysql',
    schema: false,
    autoPK: false,
    autoCreatedAt: false,
    autoUpdatedAt: false,
    attributes: {
        student_id:{
            model:'students',
            via:'student_id'
        },
        unfriend_id:{
            model:'relationship',
            via:'unfriend_id'
        }
    }

};

输出

最重要的是“我能得到正确的输出”,简短的回答是:不,但它可以使用。我没有获得原始查询的Sails表示,但我已经接近了。

localhost:1337 / relationship

的输出
[
  {
    "student_id": {
      "student_id": 1,
      "student_first_name": "Paul",
      "student_last_name": "Walker",
      "student_home_phone": "1112223333",
      "student_guardian_email": "fake@email.com",
      "class_id": 1
    },
    "friend_id": {
      "student_id": 2,
      "student_first_name": "Vin",
      "student_last_name": "Diesel",
      "student_home_phone": "1112223333",
      "student_guardian_email": "fake@email.com",
      "class_id": 1
    },
    "relation_id": 1
  },
  {
    "student_id": {
      "student_id": 1,
      "student_first_name": "Paul",
      "student_last_name": "Walker",
      "student_home_phone": "1112223333",
      "student_guardian_email": "fake@email.com",
      "class_id": 1
    },
    "unfriend_id": {
      "student_id": 4,
      "student_first_name": "Not",
      "student_last_name": "Yuagin",
      "student_home_phone": "1112223333",
      "student_guardian_email": "fake@email.com",
      "class_id": 1
    },
    "relation_id": 3
  }, 
//Truncated for space

localhost:1337 / students /

的输出
[
  {
    "friends": [
      {
        "relation_id": 1,
        "student_id": 1,
        "friend_id": 2,
        "unfriend_id": null
      },
      {
        "relation_id": 3,
        "student_id": 1,
        "friend_id": null,
        "unfriend_id": 4
      },
      {
        "relation_id": 5,
        "student_id": 1,
        "friend_id": 5,
        "unfriend_id": null
      }
    ],
    "unfriends": [
      {
        "relation_id": 1,
        "student_id": 1,
        "friend_id": 2,
        "unfriend_id": null
      },
      {
        "relation_id": 3,
        "student_id": 1,
        "friend_id": null,
        "unfriend_id": 4
      },
      {
        "relation_id": 5,
        "student_id": 1,
        "friend_id": 5,
        "unfriend_id": null
      }
    ],
    "student_id": 1,
    "student_first_name": "Paul",
    "student_last_name": "Walker",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },//Truncated for length

现在,在StudentsController.js我经历了一个方法,它给了我更多我想要的东西。

StudentsController.js

module.exports = {
    get:function(req,res){
        Students.find()
                        .populate('friends',{ 
                                'friend_id':{'!':null}
                        })
                        .populate('unfriends',{
                        'unfriend_id':{'!':null}
        }).exec(function(err,j){
            if(err) res.json(err);
            res.json(j);
        });
    }
};

localhost的输出:1337 / students / get(这比我想要的更接近/学生

[
  {
    "friends": [
      {
        "relation_id": 1,
        "student_id": 1,
        "friend_id": 2,
        "unfriend_id": null
      },
      {
        "relation_id": 5,
        "student_id": 1,
        "friend_id": 5,
        "unfriend_id": null
      }
    ],
    "unfriends": [
      {
        "relation_id": 3,
        "student_id": 1,
        "friend_id": null,
        "unfriend_id": 4
      }
    ],
    "student_id": 1,
    "student_first_name": "Paul",
    "student_last_name": "Walker",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  },
  {
    "friends": [
      {
        "relation_id": 2,
        "student_id": 2,
        "friend_id": 1,
        "unfriend_id": null
      },
      {
        "relation_id": 7,
        "student_id": 2,
        "friend_id": 3,
        "unfriend_id": null
      }
    ],
    "unfriends": [],
    "student_id": 2,
    "student_first_name": "Vin",
    "student_last_name": "Diesel",
    "student_home_phone": "1112223333",
    "student_guardian_email": "fake@email.com",
    "class_id": 1
  }//Truncated for length

现在为什么除了我在控制器中编写的代码之外的任何东西都表现得像现在这样,我不知道。但至少它正在发挥作用。