Feathers-Sequelize Where statement and Query param

1.1k Views Asked by At

I have the following feathers-sequelize query:

context.params.sequelize = {
    raw: false,
    include: [{
      model: organisations,
      where: organisationId ? { id: organisationId } : undefined,
      include: [{
        model: roles,
        where: roleId ? { id: roleId } : undefined,
      }],
    }],
    where: single ? (
      sequelize.and(
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."roleId"'),
          sequelize.col('"organisations->roles"."id"'),
        ),
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."userId"'),
          sequelize.col("users.id")
        )
      )
    ) : undefined
  }

I make the following service request:

await UserService.find({ query: { email: data.email } }))

The query object is left out since in the above sequelize query I am setting my own where statement. If I remove the where statement than the query object works. How do I make use of my own where statement and also include the feathers query object?

Thank you & Regards,

Emir

1

There are 1 best solutions below

0
On

In order to add extra conditions to the existing query object, instead of using the params.sqeuelize you add them directly into the query object (since it's available throuhg context.params.query) and since you use a before hook, the query object gets modified before the execution. In you example should be something like:

if(single) {
    context.params.query.push(
      sequelize.and(
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."roleId"'),
          sequelize.col('"organisations->roles"."id"'),
        ),
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."userId"'),
          sequelize.col("users.id")
        )
      )
   )
}

Probably not the correct syntax, but that's the idea: add extra conditions directly in the feathers query object. Hope this helps.