question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to createQuery?

See original GitHub issue

Feathers-sequelize accepts the context.params.query and then in the background builds a sequelize query which gets executed.

I want to use the feathers params still but also add my own feathers where logic.

Context.params.query { email: test@example.com, $limit: 1 }

Looking at the following 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,
  }

Since I have defined my own where statement, it is overriding the one that feathers will make. I want to be able to merge them so my query and the feathers, limits, filters etc… continue to work as expected.

If someone has an alternative solution I am open. If there is a way to move my current sequelize where statements etc…

Thank you & Regards, Emir

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DesignByOnyxcommented, Sep 11, 2018

Sorry I’m just seeing this (I get too many github notifications… some fall through the cracks). Anyhow, inside a before hook, you should be able do something like this:

(context) => {
  if (!context.params.sequelize) context.params.sequelize = {};
  Object.assign(context.params.sequelize, {
    include: [{ /* copy the value for include here */ }]
  });
  // for the WHERE clause, we want to update params.query
  Object.assign(context.params.query, seqClient.and( 
    /* copy code from above here */
  ));
  return context;
}

That should get you close. Hope that helps.

1reaction
DesignByOnyxcommented, Aug 7, 2018

You can achieve what you want by updating the context.params.query. You do not want to pass a top-level “where” property in context.params.sequelize. Here is what the full code would look like:

  context.params.sequelize = {
    /** DO NOT PASS A TOP-LEVEL "where" HERE **/
    raw: false,
    include: [{
      model: organisations,
      where: organisationId ? { id: organisationId } : undefined,
      include: [{
        model: roles,
        where: roleId ? { id: roleId } : undefined,
      }],
    }]
  }
  /** INSTEAD, UPDATE THE PARAMS.QUERY DIRECTLY **/
  if (single) {
    const orgUserRoleId = sequelize.col('"organisations->organisation_users"."roleId"');
    const orgUserUserId = sequelize.col('"organisations->organisation_users"."userId"');
    Object.assign(context.params.query, {
      [orgUserRoleId]: sequelize.col('"organisations->roles"."id"'),
      [orgUserUserId]: sequelize.col("users.id")
    });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating Queries Using the Java Persistence Query Language
The createQuery method is used to create dynamic queries, which are queries ... createQuery( "SELECT c FROM Customer c WHERE c.name LIKE :custName")...
Read more >
Chapter 14. Creating and executing queries
You create queries with the EntityManager#createQuery() method and its variants. You can write the query in the Java Persistence Query Language (JPQL), ...
Read more >
Create a query, form, or report in Access - Microsoft Support
Create a query to focus on specific data. Select Create > Query Wizard . Select Simple Query, and then OK. Select the table...
Read more >
When use createQuery() and find() methods of EntityManager?
In a nutshell, createQuery allows you to retrieve entities in a more dynamic fashion, while find limits you to searching for an entity...
Read more >
JPA Criteria Queries - Baeldung
Learn how to use the @Query annotation in Spring Data JPA to define ... of CriteriaQuery by calling the CriteriaBuilder createQuery() method ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found