title: Sequalize limit by unique values in column
body: <p>I have the following database structure:</p>
  
  <p>id (int) | user_id (int) | product_id (int) | data (jsonb)</p>
  
  <p>A combination of the id, user_id and product_id make the primary key. So there can be multiple rows with the same product_id and user_id.</p>
  
  <p>The data column has some JSON containing the following
  <code>{ "item": boolean }</code></p>
  
  <p>The query I need is to select all rows where user_id = x and data-->item = true. This part I can do, but I need to apply a limit. The limit should not restrict the number of rows that are returned, but instead restrict the number of DISTINCT product_ids that are returned. So if I apply a limit of 10 I could have 50 rows returned if each of the unique products have 5 rows belonging to the user_id and and item true.</p>
  
  <p>This is what I have so far but it makes no attempt at this limit. I believe I may need a subquery or GROUPBY but I'm not sure how to achieve this in Sequalize.</p>
  
  <pre><code>return this.myModel.findAll({
    where: {
      user_id: userId,
      'data.item': true,
    },
  });
  </code></pre>
  
  <p>Any guidance will be much appreciated! Thanks</p>