Code Monkey home page Code Monkey logo

Comments (13)

daffl avatar daffl commented on July 3, 2024

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;
      });
    }
  }
});

from feathers-rest.

mcnamee avatar mcnamee commented on July 3, 2024

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?

from feathers-rest.

daffl avatar daffl commented on July 3, 2024

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

from feathers-rest.

mcnamee avatar mcnamee commented on July 3, 2024

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

from feathers-rest.

mcnamee avatar mcnamee commented on July 3, 2024

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
    }
   ...

from feathers-rest.

daffl avatar daffl commented on July 3, 2024

Awesome, thank you for sharing!

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

from feathers-rest.

mcnamee avatar mcnamee commented on July 3, 2024

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.

from feathers-rest.

daffl avatar daffl commented on July 3, 2024

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) => {})

from feathers-rest.

mcnamee avatar mcnamee commented on July 3, 2024

Ah hah! Very helpful, thank you!

from feathers-rest.

ekryski avatar ekryski commented on July 3, 2024

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

from feathers-rest.

eddyystop avatar eddyystop commented on July 3, 2024

I've tagged this as a todo for myself.

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

from feathers-rest.

vigalo avatar vigalo commented on July 3, 2024

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

from feathers-rest.

vigalo avatar vigalo commented on July 3, 2024

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;

from feathers-rest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.