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

Is it possible to generate dynamic arguments? #347

Closed
jesster2k10 opened this issue May 29, 2019 · 3 comments
Closed

Is it possible to generate dynamic arguments? #347

jesster2k10 opened this issue May 29, 2019 · 3 comments
Labels
Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved

Comments

@jesster2k10
Copy link

I was wondering if it was possible to use some sort of function to generate a set of dynamic arguments on an abstract class decorated with "@ArgsType"

Take the code below for example:

export function WhereArguments(type: string) {
  const properties = getConnection()
    .getMetadata(type)
    .ownColumns.map(column => ({
      type: column.type,
      name: column.propertyName
    }))
  const strings = properties.filter(
    property => property.type === String || property.type === 'text'
  )
  const booleans = properties.filter(
    property =>
      property.type === Boolean ||
      property.type === 'bool' ||
      property.type === 'boolean'
  )

  const baseFilters = properties
    .filter(
      property => !strings.includes(property) && !booleans.includes(property)
    )
    .map(({ name }) => [
      `${name}`,
      `${name}_gt`,
      `${name}_lt`,
      `${name}_gte`,
      `${name}_lte`,
      `${name}_not`
    ])

  const stringFilters = strings.map(({ name }) => [
    name,
    `${name}_gt`,
    `${name}_lt`,
    `${name}_gte`,
    `${name}_lte`,
    `${name}_not`,
    `${name}_contains`,
    `${name}_not_contains`,
    `${name}_starts_with`,
    `${name}_not_starts_with`,
    `${name}_ends_with`,
    `${name}_not_ends_with`
  ])
  const booleanFilters = booleans.map(({ name }) => name)
  const filters = [].concat.apply(
    [],
    [...stringFilters, ...booleanFilters, ...baseFilters]
  )

  @ArgsType
  class Abstract {
     // is it possible to define properties on this class, based on the filters array?
  }
  return Abstract
}

My question, is it possible to apply properties to this class based on the 'filters' array so the end result would be like:

@ArgsType
class AbstractArgumentClass {
   @Field()
   public name_gte: string

  ...the other fields in the array
}

I'm not sure if this is possible, but it would save me a lot of time than having to define each of those for each of my entities/object types

@MichalLytek MichalLytek added the Question ❔ Not future request, proposal or bug issue label May 30, 2019
@MichalLytek
Copy link
Owner

MichalLytek commented May 30, 2019

Technically it's possible (call the decorator function manually, like in transpilled JS code) but you will loose type safety in your code as TypeScript is not capable of handling dynamic property values.

Maybe in your case, you should just use a more static-type friendly way for declaring arguments? There was a discussion about it: #134 (comment)

@jesster2k10
Copy link
Author

Wow, the method suggested there is great. Hadn't thought of that. Much better than what I was trying to implmement

@MichalLytek
Copy link
Owner

Closing for a housekeeping purposes 🔒

@MichalLytek MichalLytek added the Solved ✔️ The issue has been solved label Jun 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question ❔ Not future request, proposal or bug issue Solved ✔️ The issue has been solved
Projects
None yet
Development

No branches or pull requests

2 participants