-
Notifications
You must be signed in to change notification settings - Fork 26
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
Dynamically Generate TypeScript DB Models, Transcript Model, and other Constants #167
Comments
In addition to creating the models themselves that have some interaction with the database, it would be great to generate the constants. class FileFields:
name = "name"
uri = "uri" So that we can use these constants during filtering and querying |
This is where the Python database models live: https://github.com/CouncilDataProject/cdp-backend/blob/main/cdp_backend/database/models.py#L26 This is where the TypeScript database models (and their unpackers) live: https://github.com/CouncilDataProject/cdp-frontend/tree/main/src/models I think the best way todo this would be to use a jinja template potentially. But really this is all about code generation so whatever method works best. |
I've done a bit of initial research on parsing the Python models. It's going to take a bit of puzzle-solving / trial and error, but I'm inclined to go with Python's Another approach is creating a set of regex's or substring conditionals to replicate the grammar of what goes into a Python class and any expressions / statements within it, but that seems a bit more hacky and error prone. Or perhaps another approach that doesn't involve such heavy parsing I'm not thinking of...? Not sure what that would be though. |
Will give a more thorough look at your comment tomorrow but you may be interested to see how we are parsing this info in the database diagram generator: https://github.com/CouncilDataProject/cdp-backend/blob/main/cdp_backend/bin/create_cdp_database_uml.py |
And here is how that DATABASE_MODELS list is generated: https://github.com/CouncilDataProject/cdp-backend/blob/main/cdp_backend/database/__init__.py Using the |
Learning many things about Python's built in modules... ty for the link.
|
Hey @liamphmurphy wanted to bump and see how you were doing on this? I understand if you are busy and haven't worked on it haha just trying to track various projects states is all |
Hey @JacksonMaxfield, thanks for reaching out! My bad for not communicating. I've been moving the last few weeks and most of my communication went out the wayside. As a general update, I explored the inspect lib and think that's a great way to go. I'm planning on using a Python class to represent each TS model, just to make things tidy / hopefully more readable even if it's a bit overkill. Needless to say if it needs to be done quicker than I am able to commit feel free to let me know and someone else can jump in, but if not, I'm looking forward to continuing on it. |
No worries! Good luck with the move.
Hmmmm does that mean we will need to update both the Python model and the TS model when we make changes? I don't really care the structure of the class the goal is really just "get database changes and constant additions or changes down to a single point of truth"
No timeline crunch! Please keep working on it whenever move is wrapped up ❤️ |
Good clarifying question, I just misspoke; I'm making a generic "Generator" class that'll have separate attributes, methods etc. to hopefully make it a bit easier to read what this new script is parsing in the Python models, to be used in the TS models. It won't make any assumptions about the models that currently exist. e.g.:
The actual logic for this isn't super complex, just fiddlying with the |
Ahhhhh cool! Thanks! I am already learning :) |
Just a quick update. still chipping away at this! I've been dealing with COVID over the last week. I've gotten some basic TypeScript models generated, I'm now working on a lot of those edge cases that require extra logic in the TS constructors, such as ReferenceField's. |
Ahhhh sorry to hear COVID hit you!! Hopefully you are resting well! Yea, ReferenceFields are always great hahaha. Please let me know if you need anything or any input or anything! Thanks!!! |
At long last (and after a lot of trial and error), I have MatterFile outputting:
I'm going to prepare a PR soon, just want to check some of the other models and check for any touchups first. The thing I know I'll change is that the |
I think it's because This looks great though! |
Idea / Feature
Dynamically generate TypeScript object definitions from
cdp-backend
Python DB models.Use Case / User Story
As a developer I want to be able to add, remove, or update a database model in a single location rather than multiple.
Solution
Add a script to
cdp-backend
that when ran will generate a TypeScript package of the database models + extras that we can push to npm on new version push.Alternatives
Stakeholders
Backend maintainers
Frontend maintainers
Major Components
cdp-backend
Dependencies
Other Notes
The text was updated successfully, but these errors were encountered: