Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Sequelize - How to create query from non-db Models?

I want to create a query that returns an non-db Model using the Sequelize's DSL. I wrote the raw query as following:

instanceMethods: {
  getHoursReport: function({from, to, companies}) {
    let currentDate = new Date();

    from = from || new Date(
      currentDate.getFullYear(),
      currentDate.getMonth(),
      1
    );

    to = to || new Date(
      currentDate.getFullYear(),
      currentDate.getMonth() + 1,
      0
    );

    let fromString = from.toISOString();
    let toString   = to.toISOString();

    let companiesFilter = 'true';

    if (typeof companies !== 'undefined' && companies.length > 0) {
      companiesFilter = `c.id IN ('${companies.join("','")}')`;
    }

    // @todo: Use Sequelize DSL to make the query
    const query = `
      SELECT p.id as contractor,
             string_agg(DISTINCT pr.name, ';') as projects,
             round(sum(te.hours), 2) as "totalHours",
             round(sum(te.hours * te.rate), 2) as "totalPay"
        FROM person_company_roles pcr
        JOIN companies c
          ON pcr."companyId" = c.id
        JOIN projects pr
          ON pr."clientId" = c.id
        JOIN project_timesheets pt
          ON pt."projectId" = pr.id
        JOIN timesheet_entries te
          ON pt.id = te."timesheetId"
        JOIN people p
          ON p.id = pt."contractorId"
        WHERE pcr."personId" = '${this.id}' AND
              pcr.role = 'owner' AND
              te."entryDate"::date BETWEEN
              '${fromString}'::date AND '${toString}'::date AND
              ${companiesFilter}
        GROUP BY p.id;
      `;

    return sequelize.query(query, {
      model: sequelize.models.hoursReport
    }).then(
      report => report[0]
    );
  }
}

In Sequelize docs the query is built from a Model. So, I don't understand how I could create the query from my hoursReport model.

Any help will be appreciated.

Thanks in advance

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!