使用 Sequelize (NodeJS) 代替 * 指定特定字段

发布于 2024-12-14 01:17:14 字数 253 浏览 0 评论 0原文

好吧,我在 NodeJS 中有一个项目,我正在其中使用 Sequelize 来实现 MySQL ORM。这件事工作得非常好,但是我试图弄清楚是否有一种方法可以指定在查询的基础上返回哪些字段,或者是否有一种方法可以在某处执行 .query() 。

例如,在我们的用户数据库中,可能存在大量的记录和列。在本例中,我只需要返回三列,因此仅获取这些列会更快。然而,Sequelize 只是在表中查询所有“*”,以尽可能满足完整的对象模型。这是我想在应用程序的这个特定区域中绕过的功能。

Alright so I have a project in NodeJS where I'm utilizing Sequelize for a MySQL ORM. The thing works fantastically however I'm trying to figure out if there is a way to specify what fields are being returned on a query basis or if there's even a way just to do a .query() somewhere.

For example in our user database there can be ridiculous amounts of records and columns. In this case I need to return three columns only so it would be faster to get just those columns. However, Sequelize just queries the table for everything "*" to fulfill the full object model as much as possible. This is the functionality I'd like to bypass in this particular area of the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆离笙 2024-12-21 01:17:14

您必须将属性指定为传递给 findAll() 的对象中的属性:

Project.findAll({attributes: ['name', 'age']}).on('success', function (projects) {
  console.log(projects);
});

我是如何发现这一点的:

首先在此处调用查询: https://github.com/sdepold/sequelize/blob/master/lib/model-definition.js#L131
然后在这里构建: https://github.com /sdepold/sequelize/blob/master/lib/connectors/mysql/query-generator.js#L56-59

You have to specify the attributes as a property in the object that you pass to findAll():

Project.findAll({attributes: ['name', 'age']}).on('success', function (projects) {
  console.log(projects);
});

How I found this:

The query is first called here: https://github.com/sdepold/sequelize/blob/master/lib/model-definition.js#L131
Then gets constructed here: https://github.com/sdepold/sequelize/blob/master/lib/connectors/mysql/query-generator.js#L56-59

゛清羽墨安 2024-12-21 01:17:14

版本中尝试一下

template.findAll({
    where: {
        user_id: req.params.user_id 
    },
    attributes: ['id', 'template_name'], 
}).then(function (list) {
    res.status(200).json(list);
})

Try this in new version

template.findAll({
    where: {
        user_id: req.params.user_id 
    },
    attributes: ['id', 'template_name'], 
}).then(function (list) {
    res.status(200).json(list);
})
提赋 2024-12-21 01:17:14

所有答案都是正确的,但我们也可以使用 includeexclude 以及

Model.findAll({
  attributes: { include: ['id'] }
});

Model.findAll({
  attributes: { exclude: ['createdAt'] }
});

来源

All Answers are correct but we can also use include and exclude as well

Model.findAll({
  attributes: { include: ['id'] }
});

Model.findAll({
  attributes: { exclude: ['createdAt'] }
});

Source

为你鎻心 2024-12-21 01:17:14

在属性键中使用数组。您可以为别名执行嵌套数组。

Project.findAll({
  attributes: ['id', ['name', 'project_name']],
  where: {id: req.params.id}
})
.then(function(projects) {
  res.json(projects);
})

将产生:

SELECT id, name AS project_name FROM projects WHERE id = ...;

Use the arrays in the attribute key. You can do nested arrays for aliases.

Project.findAll({
  attributes: ['id', ['name', 'project_name']],
  where: {id: req.params.id}
})
.then(function(projects) {
  res.json(projects);
})

Will yield:

SELECT id, name AS project_name FROM projects WHERE id = ...;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文