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 queryTransform hook #280

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export interface FernsRouterOptions<T> {
user?: User,
query?: Record<string, any>
) => Record<string, any> | null | Promise<Record<string, any> | null>;
/** queryTransform is a function to transform the query params before they are used to query the database. This can be
* used to run intermediate queries to generate query fields for the main query. Runs before the queryFilter. */
queryTransform?: (query: Record<string, any>) => Record<string, any>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a test? And what would you do here that you wouldn't do in query filter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm, yes we could use the query filter that's true. the description just seemed to imply that it's only used to see if the query was allowed. I'll close this

/** Transformers allow data to be transformed before actions are executed, and serialized before being returned to
* the user.
*
Expand Down Expand Up @@ -430,6 +433,10 @@ export function fernsRouter<T>(
mongoose.connection.db.collection(model.collection.collectionName);
}

if (options.queryTransform) {
query = options.queryTransform(query);
}

// Check if any of the keys in the query are not allowed by options.queryFilter
if (options.queryFilter) {
let queryFilter;
Expand Down