Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

Distinct #76

Closed
mcnamee opened this issue Oct 2, 2016 · 13 comments
Closed

Distinct #76

mcnamee opened this issue Oct 2, 2016 · 13 comments

Comments

@mcnamee
Copy link

mcnamee commented Oct 2, 2016

Hi, is it possible to get 'distinct' records through a rest call (e.g. With the mongoose distinct())? Is possible to Perhaps add it via a before hook?

E.g. If I have a collection full of books, and I want to get just the names of all distinct Authors

@daffl
Copy link
Member

daffl commented Oct 2, 2016

You could add your own query parameter and make the model call based on that query in the hook, setting hook.result similar to this:

app.service('users').before({
  find(params) {
    if(params.query.$distinct) {
      return this.Model.find().distinct().then(data => {
        hook.result = data;
        return hook;
      });
    }
  }
});

@daffl daffl closed this as completed Oct 2, 2016
@daffl daffl reopened this Oct 2, 2016
@mcnamee
Copy link
Author

mcnamee commented Oct 3, 2016

Thanks so much for the quick response @daffl - By doing it this way, is my understanding correct that it would do 2 DB queries, where the second is unneeded?

@daffl
Copy link
Member

daffl commented Oct 3, 2016

No. If you set the result in a before hook the original database call is not going to happen.

@mcnamee
Copy link
Author

mcnamee commented Oct 3, 2016

Wow, great to know. Thanks again!
I'll give it a go

@mcnamee
Copy link
Author

mcnamee commented Oct 4, 2016

Hi @daffl - I ended up creating a hook like this, which seems to work a treat:

/*
 * Distinct Search - can only be 1 distinct at a time
 * - allows you to URI query like: books?$distinct=author
 * ============================================================
 */
exports.searchDistinct = function () {
  return function (hook) {
    let query = hook.params.query;

    // Must be a before hook
    if (hook.type !== 'before') {
      throw new Error('The \'searchDistinct\' hook should only be used as a \'before\' hook (hooks.searchDistinct).');
    }

    // Throw error when no field is provided - eg. just users?$distinct
    if (query.$distinct === '') {
     throw new Error('Missing $distinct: Which field should be distinct? (hooks.searchDistinct)');
    }

    let distinctValue = query.$distinct || null;
    if (distinctValue == null) return hook;

    // Remove $distinct param from query (preventing errors)
    delete query.$distinct;

    return new Promise((resolve, reject) => {
      var args = [
        { $match: query || {} },
        { $group: {
          _id: "$" + distinctValue,
          total: { $sum: 1 }
        }}
      ];
      this.Model.aggregate(args)
        .then(data => {
          hook.result = {
            total: data.length || 0,
            data,
          };
          resolve();
        }).catch(err => reject(err));
    });
  }
}

This allows you to hit an endpoint like /users?status=active&$distinct=country and returns data similar to:

{
  "total": 25,
  "data": [
    {
      "_id": "Spain",
      "total": 22
    },
    {
      "_id": "Denmark",
      "total": 12
    }
   ...

@daffl
Copy link
Member

daffl commented Oct 4, 2016

Awesome, thank you for sharing!

Oh, btw, you can probably just return this.Model.aggregate(args) instead of having to create a wrapper promise.

@daffl daffl closed this as completed Oct 4, 2016
@mcnamee
Copy link
Author

mcnamee commented Oct 7, 2016

Thanks @daffl - that's true - although I have the wrapper promise so that I can alter the hook.result to include a total and if needed, later on extend to include $skip and $limit for example.

@daffl
Copy link
Member

daffl commented Oct 7, 2016

What I meant is that you can write the same thing like this:

/*
 * Distinct Search - can only be 1 distinct at a time
 * - allows you to URI query like: books?$distinct=author
 * ============================================================
 */
exports.searchDistinct = function () {
  return function (hook) {
    let query = hook.params.query;

    // Must be a before hook
    if (hook.type !== 'before') {
      throw new Error('The \'searchDistinct\' hook should only be used as a \'before\' hook (hooks.searchDistinct).');
    }

    // Throw error when no field is provided - eg. just users?$distinct
    if (query.$distinct === '') {
     throw new Error('Missing $distinct: Which field should be distinct? (hooks.searchDistinct)');
    }

    let distinctValue = query.$distinct || null;
    if (distinctValue == null) return hook;

    // Remove $distinct param from query (preventing errors)
    delete query.$distinct;

    var args = [
      { $match: query || {} },
      { $group: {
        _id: "$" + distinctValue,
        total: { $sum: 1 }
      }}
    ];

    return this.Model.aggregate(args)
      .then(data => {
        hook.result = {
          total: data.length || 0,
          data,
        };

        return hook;
      });
  }
}

I found that especially when dealing with other calls that return a promise there is usually no need to create a new promise with new Promise((resolve, reject) => {})

@mcnamee
Copy link
Author

mcnamee commented Oct 7, 2016

Ah hah! Very helpful, thank you!

@ekryski
Copy link
Member

ekryski commented Oct 16, 2016

I'm going to try and keep this in mind for reference in case we want to roll this into some common hooks. cc/ @feathersjs/core-team

@eddyystop
Copy link
Contributor

I've tagged this as a todo for myself.

Please comment on any enhancements, changes, etc you have in mind.

@vigalo
Copy link

vigalo commented Nov 25, 2017

Hello
I need to create a hook like this one to rethinkdb database adapter. How can I do it?
thanks.

@vigalo
Copy link

vigalo commented Nov 25, 2017

I solved my problem like this.

let query = hook.params.query;

// Must be a before hook
if (hook.type !== 'before') {
  throw new Error('El hook \'distinct\' sólo puede usarse como hook \'before\' (hooks.distinct).');
}

// Throw error when no field is provided - eg. just users?$distinct
if (query.$distinct === '') {
  throw new Error('$distinct no encontrado: ¿Qué campo debería ser distinto? (hooks.distinct)');
}

let distinctValue = query.$distinct || null;
console.log('distinctValue', distinctValue);
if (distinctValue == null) return hook;

// Remove $distinct param from query (preventing errors)
delete query.$distinct;

// Add only the field we are searching for
query = Object.assign({ $select: distinctValue },query)

const rethinkdbQuery = this.createQuery(query);
const r = this.options.r;

// In our case, disable pagination
hook.params.paginate = false;

// Update the query with an additional `distinct` condition
hook.params.rethinkdb = rethinkdbQuery.distinct();

return hook;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants