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

Support arrayFilter in patch #373

Closed
GaborTorma opened this issue Mar 4, 2020 · 7 comments
Closed

Support arrayFilter in patch #373

GaborTorma opened this issue Mar 4, 2020 · 7 comments

Comments

@GaborTorma
Copy link

Please support array filtering in patch method.
https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

@arfanliaqat
Copy link
Contributor

I think u can already use it just set it as mongoose option

context.params.mongoose = {
    arrayFilters: [{ key: value }],
};

@GaborTorma
Copy link
Author

Thanx! I found it, but it's hook.
Is it possible to define it on client side?

@arfanliaqat
Copy link
Contributor

arfanliaqat commented Mar 14, 2020

You can use a hack, recommended by maintainers for similar needs

// pass arrayFilters as normal query from client
app.service("comments").patch(_id, 
    { title: "updated title" }, 
    { query: { arrayFilters: [{ key: value }] } }
);


// on server side inside a before patch hook in comments service
context => {
    const { query, mongoose } = context.params;

    if (query.hasOwnProperty("arrayFilters")) {
        // merge existing mongoose options with array filters from client
        context.params.mongoose = { ...mongoose, arrayFilters: query.arrayFilters };
        
        // delete arrayFilters key from query because it is not supposed to be there
        delete context.params.query.arrayFilters;
    }

    return context;
}

You can use it for any other method i.e. create, get, find, or remove as well.

@GaborTorma
Copy link
Author

Ohh, thanx! Good solution.
Last question: why not pass it from patch method third parameter (params)?
Like these: patch(id, data, { mongoose: { arrayFilters: [ { key: value } ] } })

@arfanliaqat
Copy link
Contributor

arfanliaqat commented Mar 14, 2020

For security reasons maintainers decided not to allow passing all kinda params from client. A few things can only be done on server side. This is one of those cases. However i've seen the above pattern being recommended in several other discussions by maintainers when some param has to be passed from client.

@arfanliaqat
Copy link
Contributor

https://docs.feathersjs.com/api/services.html#params here at the end of params section u can read an important note

Important: For external calls only params.query will be sent 
between the client and server. If not passed, params.query 
will be undefined for internal calls.

So its by design... but u can bypass as per ur need with above pattern

@daffl
Copy link
Member

daffl commented Apr 29, 2020

Looks like this is solved. Thanks @arfanliaqat!

@daffl daffl closed this as completed Apr 29, 2020
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

No branches or pull requests

3 participants