Question: Would it be possible to generate models ...
# orm-help
m
Question: Would it be possible to generate models using the Prisma Generator Specification? Looking to implement versioning for each data model defined in schema, and the idea was to write a generator that writes a
Version
model for each model. E.g.
Copy code
model User {
  id: Int
  name: String
}
generated model:
Copy code
model UserVersion {
  id: Int
  data: Json 
  userId Int
  user User @relation(fields: [userId], references: [id])
}
j
It is very possible. I'm generating supplemental model data myself. Have a look at the source for the ERD mermaid diagram generator. It's fairly simple and what helped me get going. @ me if you'd like more tips and I can assist when I'm back at my desk.
m
Thanks for the direction @Jason Abbott! Is this the one you’re referencing: https://github.com/keonik/prisma-erd-generator?
Also, when generating supplemental model, would you write to the same output as the
prisma-client-js
generator? 🤔
j
You got on that fast. I was coming to add an important caveat I realized after posting. 🙂 Putting together a generator to create TypeScript
*Version
interfaces for all your models wouldn't be too hard (and yes, that's the repo I had in mind). But I realized what you probably want is not just the client typings but real Prisma models that the other Prisma tooling can utilize to create migrations and its nice client interactions. To my understanding, that would need a process before this generator stuff, to maybe produce an altered
.prisma
file that is then fed to the regular Prisma generator. You could leverage the Prisma SDK to get the
.prisma
AST to make that job easier but I think it might otherwise be a bit outside the scope of the usual Prisma generators.
m
Hmm, seems reasonable, going to try that approach, thanks again for the direction! Was hoping there was a generator way to do it, just so that it’s more plug and play