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

add query modifier function support. #409

Merged
merged 4 commits into from
Nov 16, 2021

Conversation

mrfrase3
Copy link
Contributor

Query Modifiers

Sometimes it's important to use an unusual Mongoose Query method, like specifying whether to read from a primary or secondary node, but maybe only for certain requests.

You can access the internal Mongoose Query object used for a find/get request by specifying the queryModifier function. It is also possible to override that global function by specifying the function in a requests params.

// Specify a global query modifier when creating the service
app.use('/messages', service({
  Model,
  queryModifier: (query, params) => {
    query.read('secondaryPreferred');
  },
  queryModifierKey: '__queryModifier__' // default is 'queryModifier'
}));

app.service('messages').find({
  query: { ... },
}).then((result) => {
  console.log('Result from secondary:', result)
});

// Override the modifier on a per-request basis
app.service('messages').find({
  query: { ... },
  __queryModifier__: (query, params) => {
    query.read('primaryPreferred');
  }
}).then((result) => {
  console.log('Result from primary:', result)
});

// Disable the global modifier on a per-request basis
app.service('messages').find({
  query: { ... },
  __queryModifier__: false
}).then((result) => {
  console.log('Result from default option:', result)
});

resolves #407

@daffl
Copy link
Member

daffl commented Nov 11, 2021

Sorry almost missed this. It looks good to mee. Can you resolve the conflict and I can get a new minor version out.

@mrfrase3
Copy link
Contributor Author

Hey @daffl,

I've fixed the merge conflict, I also updated the type definition.

@daffl
Copy link
Member

daffl commented Nov 15, 2021

Thank you! Just to clarify, __queryModifier__ is just an example for changing the key and it's params.queryModifier by default right?

@mrfrase3
Copy link
Contributor Author

Yes, correct. queryModifier is the default

@daffl daffl merged commit 8aa157f into feathersjs-ecosystem:master Nov 16, 2021
@daffl
Copy link
Member

daffl commented Nov 16, 2021

Released as v8.5.0, thank you!

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.

Support specifying which replica set to read from
2 participants