Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda