Specifying specific fields with Sequelize (NodeJS) instead of * Specifying specific fields with Sequelize (NodeJS) instead of * mysql mysql

Specifying specific fields with Sequelize (NodeJS) instead of *


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


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);})


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 = ...;