sequelize.js关联访问器



我用Node.js 制作了API服务器

此外,我使用sequelize.js(版本4(与MySQL进行通信。

我的桌子结构在这里。

[文章]

  • no(PK(
  • 主题
  • 内容
  • 创建日期
  • 更新日期

[注释]

  • no(PK(
  • 内容
  • 创建日期
  • 更新日期
  • article_no(FK到文章(

[index.controller.js]

import { Article, Comment } from '../model/model';
export const index = (req, res) => {
res.send('controller index');
};

export const getArticle = (req, res) => {
try {
Article.all()
.then(article => {
res.status(200).json({status: true, result: article});
});
} catch(e) {
res.status(500).json({status: false, result: "get article fail"});
}
}
export const addArticle = (req, res) => {
const { subject, content } = req.body;
try {
Article.create({
subject: subject,
content: content
})
res.status(200).json({status: true, result: "article write success"});
} catch(e) {
res.status(500).json({status: false, result: "article fail"});
}
}
export const getComment = (req, res) => {
try {
Comment.all()
.then(comment => {
res.status(200).json({status: true, result: comment})
});
} catch(e) {
res.status(500).json({status: false, result: "get comment fail"});
}
}
export const addComment = (req, res) => {
const { content, article_no } = req.body;
try {
Comment.create({
content: content,
article_no: article_no
})
.then(() => res.status(200).json({status: true, result: "comment write success"}))
} catch(e) {
console.log(e);
res.status(500).json({status: false, result: "comment fail"});
}
}

[index.js]

import express from 'express';
import { index, getArticle, getComment,addArticle, addComment } from './index.controller';
const router = express.Router();
router.get('/', index);
router.get('/article', getArticle);
router.post('/article', addArticle);
router.get('/comment', getComment);
router.post('/comment', addComment);
export default router;

[model.js]

import Sequelize from 'sequelize';
const sequelize = new Sequelize('db', 'id', 'pw', {
host: '127.0.0.1',
dialect: 'mysql'
})
export const Article = sequelize.define('article', {
no: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
subject: {
type: Sequelize.STRING,
allowNull: false
},
content: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true,
underscored: true
})
export const Comment = sequelize.define('comment', {
no: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
content: {
type: Sequelize.STRING,
allowNull: false
}
}, {
freezeTableName: true,
underscored: true
})
Article.hasMany(Comment, {as: 'Comments'}); // association
Comment.belongsTo(Article); // association
sequelize.sync({
force: false
});

由于关联(hasManybelongsTo(,article_no列将添加到Comment表中。

参考本文件,http://docs.sequelizejs.com/manual/tutorial/associations.html#one-到许多协会hasmany-

上面说Instances of Project will get the accessors getWorkers and setWorkers.

在我的情况下,它将是getCommentssetComments

但我不知道如何使用accessor获取所有与评论相关的文章。

电流输出在这里。(如果我连接到GET /article(

{  
"status":true,
"result":[  
{  
"no":1,
"content":"comment test",
"created_at":"2018-07-18T05:00:45.000Z",
"updated_at":"2018-07-18T05:00:45.000Z",
"article_no":1
}
]
}

此处为所需输出

{  
"status":true,
"result":[  
{  
"no":1,
"content":"comment test",
"created_at":"2018-07-18T05:00:45.000Z",
"updated_at":"2018-07-18T05:00:45.000Z",
"article_no":1,
"comments": [
// related comments here!
]
}
]
}

谢谢。

当您想要加入另一个模型时,您应该在查询中使用include

User.findAll({
include: [
{ model: Profile, required: true // inner join }
],
limit: 3
});

查看Sequelize模型使用文档。

要访问访问者的评论,你需要这样做:

const articles = await Article.all();
articles.forEach(article => {
const comments = await article.getComments();
})

背后的想法是,每个article序列化对象都将具有访问器getComments,但在内部执行getComments时,它会向数据库发出新的请求,并在注释where查询中预填充articleId。这被称为懒惰加载,因为你可以在需要的时候加载数据。但事实并非如此。

对于所需的输出,我建议使用include方法,因为它将向数据库发出单个请求。

最新更新