Node.js sequelize embed hasMany IDs

编程入门 行业动态 更新时间:2024-10-27 20:37:43
本文介绍了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

本文发布于:2023-10-19 00:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:sequelize   js   Node   IDs   hasMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!