Wednesday, 10 May 2017

Sequelize select only rows with more than one record

I have this query:

Time_Sheet_Details.findAll({
      include: [
          {
              model: timesheetNotesSubcon,
              required: false,
              attributes:["note","file_name", "id", "working_hrs", "timestamp", "has_screenshot", "notes_category", "userid"],
          },
          {
              model: Timesheet,
              attributes:["id","leads_id","userid","subcontractors_id"],
              where:  clientWhere, // Client
              include:[
                  {
                      model: Lead_Info, attributes:["id","fname","lname","email","hiring_coordinator_id","status"],
                      where:  scWhere, // SC
                      include:[{
                          model: adminInfoSchema,
                          required: false,
                          attributes:["admin_id","admin_fname", "admin_lname", "admin_email", "signature_contact_nos", "signature_company"],
                      }]

                  },
                  {
                      model:Personal_Info,attributes:["userid","fname","lname","email"],
                  }
              ]
          }],
      where: {
          reference_date: filters.reference_date
      },
      order:[
          ["id","DESC"]
      ],
      offset:((page-1)*limit),
      limit : limit,
      subQuery:false

  }).then(function(foundObject){
      willFulfillDeferred.resolve(foundObject);
  });

My setup is I have a Timesheet has a TimesheetDetails has many timesheetNotesSubcon . I am fetching TimesheetDetails together will all the timesheetNotesSubcon with it.

It works fine, but what if I want to fetch only those with timesheetNotesSubcon which has equal or more than 1 record of timesheetNotesSubcon so all TimesheetDetails with zero result in timesheetNotesSubcon will not be fetched.



via Yassi

No comments:

Post a Comment