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

fix($getFilterMethod): prevent camelCase from stripping the leading $ of filter name #43

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

zaosoula
Copy link

@zaosoula zaosoula commented Jul 2, 2024

Hi,

I was trying to implement special filters starting with a $ chat ($select, $sort). And while testing i encountered an issue, $getFilterMethod strip the leading $ of the filter name while calling lodash/camelCase. This PR fixes it and allow the use of $filters

@zaosoula
Copy link
Author

zaosoula commented Jul 3, 2024

Our use-case as an example

import { BaseModelFilter } from 'adonis-lucid-filter'

export default class CommonFilter<T> extends BaseModelFilter {
  $sort(value: Record<keyof T, 'asc' | 'desc'>): void {
    if (typeof value === 'object') {
      Object.entries(value).forEach(([key, direction]) => {
        this.$query.orderBy(key, direction)
      })
    }
  }

  $select(value: string | string[]): void {
    this.$query.select(Array.from(new Set(['id', ...(Array.isArray(value) ? value : [value])])))
  }

  $populate(value: string | string[]): void {
    ;(Array.isArray(value) ? value : [value]).forEach((relation) => {
      this.$query.preload(relation)
    })
  }
}

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.

1 participant