Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keepInArray hook to keep fields from a nested array #420

Merged
merged 4 commits into from
Aug 4, 2018
Merged

Conversation

dekelev
Copy link
Member

@dekelev dekelev commented Jul 27, 2018

Problem

The keep hook does a great job for its intended purpose, but it won't work for nested arrays. for example, finding a record with one-to-many or many-to-many ORM relations.

Solution

This new keepInArray hook works like keep, but it accepts 2 arguments. the first is a dot notation string type field and the second is an array of field names to keep from it.

Usage

Data structures:

{ 
  users: [
    {
      name: 'John',
      secret: 'doNotKeep',
      dept: 'Accounting',
      address: {
        city: 'New York',
        secret: 'doNotKeep'
      }
    }
  ],
  owner: 'Jane Doe'
}

{
  account: {
    users: [
      {
        name: 'John',
        secret: 'doNotKeep',
        dept: 'Accounting',
        address: {
          city: 'New York',
          secret: 'doNotKeep'
        }
      }
    ],
    owner: 'Jane Doe'
  }
}

Using hook:

const { keepInArray } = require('feathers-hooks-common');
    
module.exports = { after: {
  create: keepInArray('users', ['name', 'dept', 'address.city']),
  find: keepInArray('account.users', ['name', 'dept', 'address.city']),
} };

Data structures after using hook:

{ 
  users: [
    {
      name: 'John',
      dept: 'Accounting',
      address: {
        city: 'New York'
      }
    }
  ],
  owner: 'Jane Doe'
}

{
  account: {
    users: [
      {
        name: 'John',
        dept: 'Accounting',
        address: {
          city: 'New York'
        }
      }
    ],
    owner: 'Jane Doe'
  }
}

Added tests and PR to the feathers-plus docs.

@eddyystop
Copy link
Collaborator

First of all, great job figuring out how to change the docs, and following the repo standards for the hook code.

Dot notation and arrays have been a quandry. In the general case of a.b.c.d.e both b and d may be arrays. I did not want to start dealing with multiple arrays.

Am I correct in thinking your hook will throw on line 22 if field is not an array? Would it be helpful if an explicit error identifying the problem is thrown instead?

Your comments are welcome, and let me think on this also.

@dekelev
Copy link
Member Author

dekelev commented Jul 27, 2018

@eddyystop the code will now throw a BadRequest error when the field param is leading to a non-empty & non-array object type.

@eddyystop
Copy link
Collaborator

I think the more clear a name is, the more likely a hook is to be found by users. Would you mind changing the name to keepInArray? I'll merge thereafter.

@dekelev
Copy link
Member Author

dekelev commented Jul 28, 2018

@eddyystop sure, name changed to keepInArray (also in the docs PR)

@dekelev dekelev changed the title Added keepIn hook to keep fields from a nested array Added keepInArray hook to keep fields from a nested array Jul 28, 2018
@eddyystop eddyystop merged commit 12e4d66 into feathersjs-ecosystem:master Aug 4, 2018
@eddyystop
Copy link
Collaborator

Published as 4.16.0

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

Successfully merging this pull request may close these issues.

2 participants