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)

Hey 😍

Want to help the DEV Community feel more like a community?

Head over to the Welcome Thread and greet some new community members!

It only takes a minute of your time, and goes a long way!