node.jssequelize.js

Get Resultset From Query without Meta Information Sequelize


I am new to Node.js/Sequelize.js. I have following piece of code for query:

var agent_list =  models.agent.findAll({
                                      subQuery: false,
                                      where:   qry_filter,
                                      attributes: select_attributes,
                                      include:include_models,
                                      group: ['agent_id'],
                                      order: agent_data.sort || appConfig.DEFAULT_AGENT_SORT,
                                      limit: agent_data.num_results || appConfig.DEFAULT_RESPONSE_SIZE

                                    })

                                   .then(function(agent_list){

                                      console.log(agent_list);

                                   });

The statement "console.log(agent_list)" prints the data retrieved from db plus the meta information like options:{...} , modelOptions: {...} etc. dataValues object contains data that i want. The resultset is nested js objects, each has the same structure so it would be very difficult to loop through the resultset and get only the dataValues.

I have experience working with PHP where something like this $db -> Execute("$qry") would return resultset with meta and to get rows $db -> Execute("$qry")->getRows() can be used. How to achieve this in sequelize?


Solution

  • There is a npm package called sequelize-values which you can use.

    So in your case, your code would be

    models.agent.findAll({
        subQuery: false,
        where: qry_filter,
        attributes: select_attributes,
        include: include_models,
        group: ['agent_id'],
        order: agent_data.sort || appConfig.DEFAULT_AGENT_SORT,
        limit: agent_data.num_results || appConfig.DEFAULT_RESPONSE_SIZE
    }).then(function(agent_list) {
        return agent_list.map(function(agent) {
            return agent.getValues();
        });
    }).then(function(agent_list) {
        console.log(agent_list);
    });