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

Option to omit certain models #32

Open
daedalus28 opened this issue Mar 7, 2025 · 5 comments
Open

Option to omit certain models #32

daedalus28 opened this issue Mar 7, 2025 · 5 comments

Comments

@daedalus28
Copy link

daedalus28 commented Mar 7, 2025

Hi there! First off, thanks for this very useful library.

I'm looking to use this to automate the construction of Typebox types in the context of a feathers app and so I don't need anything but the plain models. I'm also unfortunately forced to use an older version of Typebox which doesn't support things like Composite (so I can't just ignore the other types and need to proactively edit the generated files). I see the input model option, but is there a way to only generate the plain models? From what I can tell, it looks like this would be an easy option to add here if not already supported:

https://github.com/m1212e/prismabox/blob/main/src/index.ts#L40-L53

@daedalus28
Copy link
Author

In the meantime, I've authored a bruteforce codemod that post-processes generated schemas:

export default function transformer(file, api) {
  const j = api.jscodeshift

  // Remove all but the first VariableDeclaration
  let source = j(file.source)
    .find(j.VariableDeclaration)
    .forEach((path, i) => {
      if (i) j(path).remove()
    })
    .toSource()

  return (
    source
      // Remove blank exports left behind by the above transformation
      .replace(/export {};/g, '')
      // Collapse multiple empty lines to one
      .replace(/\n\n+/g, '\n\n')
  )
}

Usage:

 npx jscodeshift -t path/to/this/file.js --extensions=ts --parser=ts  path/to/generated/schemas

@daedalus28
Copy link
Author

daedalus28 commented Mar 11, 2025

Are you open to an API option for this (or accepting a PR for it)? I think the simplest API here would be an array of strings representing the models you want to opt in, defaulting to everything being included. You could use that one api to then deprecate the inputModel property.

If not, given that I have a clear workaround, I'm happy to close the issue.

@m1212e
Copy link
Owner

m1212e commented Mar 11, 2025

Hi, sorry for the delayed response, got a lot to do right now. I'd be happy to review a PR. Could you give a quick example on how an example usage for various scenarios of your proposed solution would look like? Thanks for your effort!

@daedalus28
Copy link
Author

daedalus28 commented Mar 11, 2025

No worries - I'd suggest an array of string representing all of the options you support. I pulled this list from https://github.com/m1212e/prismabox/blob/main/src/index.ts#L40-L53

includeModels = [
  "enums",
  "plain",
  "relations",
  "where",
  "whereUnique",
  "plainInputCreate",
  "plainInputUpdate",
  "relationsInputCreate",
  "relationsInputUpdate",
  "select",
  "include",
  "orderBy",
]

Then in that index file, just check if filterModels is contains the relevant string (defaulting it to the full list).

Some ideas of additional things you could support:

  • Groups of models (like "inputModels") as an alias for a set of models (so ["enums", "inputModels"] maps to ["enums", "plainInputCreate", "plainInputUpdate", "relationsInputCreate", "relationsInputUpdate"])
  • An additional option called excludeModels which omits instead of includes. If you add support for this and default it to ["inputModels"], you have parity with the current default behavior.

@m1212e
Copy link
Owner

m1212e commented Mar 12, 2025

Thats nice. I like that idea!

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

2 participants