DEV Community

Adam Crockett šŸŒ€
Adam Crockett šŸŒ€

Posted on

The Art of Code Generator

Write your code by hand, use a an AI, compile it, whatever it was your keys bottom out as they slam ferociously to the switch, your sweaty now as 5ā€™ok approaches your energy is gone and so is your mindā€¦ tick tick tick your out of time, the release is out and your just hitting the push button.

If you had maybe saved 5 seconds you could have made the deadline! On top of this, how could you avoid this situation again?

Iā€™ve been here as many years ago, it was close but it was gutting, I have spent much time working out where I went wrong and the key way to overcome it. Automate everything that is worth investing the time in and does not take longer to produce than the task you aim to speed up. A time budget, also helps, triple it!

Now then we come to the crux of this, when you start a project, drop your ā€œIā€™m so smart prideā€ grab off the shelf tools and frameworks and create a lean but strong base to build off of.

But whatā€™s the development sinkhole for most applications? I think itā€™s sticking together the front end to the backend and getting the data!

We discussed in my recent post about using OpenAPI to generate the server mock, the client Rest client and the documentation! Thatā€™s a lot of time saved and value added from one single file!

We have our rest API mocked and connected to our application, thatā€™s now 2 days of development, yes just 2 days, now what about the database? For what Iā€™m doing I will use mongoose because a relational DB doesnā€™t fit. Mongoose uses schemas to connect to Mongo, although the schemas I already have are in a totally (sensible format) different format, we should have a look around on npm for ā€œopenapi-to-mongooseā€ sadly this didnā€™t turn up much, just older libs that either connected a long time ago but are now out of date, or the lib has been deletedā€¦
Option two, openAPI to JSON schema to mongoose code, it should be my plan B, itā€™s not as easy to control this code and the result still would not be generated JavaScript/ typescript files.

I have to write my own, so thatā€™s what I must do. I started wiring up npm script and added my dev-ops code gen script, always horrid to name this type of scriptā€¦ Anyway after Parsing the YAML I loop it, push a template literal with my generated mongoose Model into an array then write each to its own file ready for consumption.

I now have mongoose, documentation, client and mock generated from a single file, what else can I shortcut?

Top comments (0)