Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving multiple rows with Sequelizejs

I am beginning to learn SequelizeJs however I encounter a small issue: I have a model which is defined as such:

var ex_table= sequelize.define("ex_table", {
   concept1: Sequelize.STRING(5),
   concept2: Sequelize.STRING(80),
   concept3: Sequelize.STRING,
   weight: Sequelize.INTEGER,
   value: Sequelize.DECIMAL(20,2)
}, {
   tableName: "samble_table"});

Retrieving a single row from the table works, I do it like this:

ex_table
  .find({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
      console.log(result.values);
    }
  })

This gives me one row of data, which is as expected. However when I'm trying to retrieve multiple rows like this:

ex_table
  .findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
      console.log(result.values);
    }
  })

Here I get underfined, any ideas how to get the multiple rows?

like image 663
Bondifrench Avatar asked Dec 30 '25 12:12

Bondifrench


1 Answers

I found out, that there are at least 2 ways to get all the rows: As commented by @barry-johnson, result is an array so we go over the array like any other:

ex_table
  .findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
      for (var i=0; i<result.length; i++) {
           console.log(result[i].values);
           }
    }
  })

or there is this interesting line on raw queries in the Models part of Sequelize documentation:

Sometimes you might be expecting a massive dataset that you just want to display, without manipulation. For each row you select, Sequelize creates a DAO, with functions for update, delete, get associations etc. If you have thousands of rows, this might take some time. If you only need the raw data and don't want to update anything, you can do like this to get the raw data.

As I wanted to see the raw data, this is what we need, so we can get the multiple rows like this:

ex_table
  .findAll({where: Sequelize.and({weight: 320193}, {concept1: 'AGLOK'}, {concept2: 'lambda'}, {concept2: 'Industry Group'})}, {raw:true})
  .complete(function (err, result) {
    if (!!err) {
      console.log("An error occurred while creating the table:", err);
    } else {
           console.log(result);
    }
  })

Hopefully this will help someone else as well.

like image 133
Bondifrench Avatar answered Jan 01 '26 02:01

Bondifrench



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!