本文介绍了Node.js sequelize embed hasMany IDs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 ember-data 的 Ember 应用程序和使用 Sequelize.js 从 MySQL 提供数据的 node.js 后端.
I am experimenting with a Ember app using ember-data and a node.js backend serving data from MySQL using Sequelize.js.
我的问题:如果我通过 hasMany 将 Comment 模型关联到 Post 模型,则 ember-data 的预期 JSON 看起来像
My problem: If I have a Comment model associated to a Post model through hasMany the expected JSON by ember-data looks like
{ "post": { "comment_ids": [1, 2, 3] } }使用 sequelize 在没有昂贵循环等的情况下查询/生成此 JSON 的最佳方法是什么?
What would be the best way to query / generate this JSON without expensive loops etc. using sequelize?
Comment 模型有一个带有 post_id 的外键.
The Comment model has a foreign key with the post_id.
推荐答案我去实现它是这样的:
// include comments Post.all({ where: "..", include: [Comment] }).success(function(posts) { // collect IDs _.each(posts, function(element) { element["comments"] = _.pluck(elementments, 'id'); }); });更多推荐
Node.js sequelize embed hasMany IDs
发布评论