Code generator that creates middleware functions for express JS that manage collections and persists them as json files.
Description of the domain model in
"$schema": "https://infinity.synoikos.de/schemata/server-generator.schema.json ",
Generate the code:
server-generator --typescript-output-folder src --openapi-output src/openapi.json characters.model.json
Resulting files in src:
The most reliable source of documentation of commandline options is to just run
--help Show help [boolean]
--version Show version number [boolean]
--typescript-output-folder Output folder for the generated server code.
--openapi-output File name of the generated OpenAPI description.
Prerequisites: node, npm, typescript
- clone this repo
npm run build-generator
chmod +x dist/server-generator.js
sudo npm install -g
In hobby projects, where I focus on the frontend of a cool litle web app, I run quite fast into the question of persistence. I could store everything in the browser. But that is not very safe, and I can't get the data onto other devices.
There has to be some kind of web service, that persists the data. Even if the app only ever runs locally. The pattern is always the same:
- create CRUD endpoints
- dump the data into files
- read the data from files
This projects attempts to collapse all the dull boilerplate into a concise description of the domain model.
Why generate explicit code?
Why not just write a super generic server, that reads the model descripion and does everything, that my generated code does as well? Maybe I will do that as well.
Generating explicit code leaves the option to turn the generator off and develop the code further.
It is also much easier to see, what is going on under the hood.
Dev notes for myself
To install this tool globally, run:
sudo npm install -g
This creates a just a link to the
dist/server-generator.js. The first line in that file, tells bash with which environment to execute this script.
That means, that no update or re-install is necessary, if this project gets developed further.
To use it, execute the following command in your workspace directory. That is the directory, in which you want to create the root folder for the new project.