Code generator that creates middleware functions for express JS that manage collections and persists them as json files.
.vscode | 4 years ago | ||
src | 4 years ago | ||
.eslintignore | 4 years ago | ||
.eslintrc.js | 4 years ago | ||
.gitignore | 4 years ago | ||
package-lock.json | 4 years ago | ||
package.json | 4 years ago | ||
readme.md | 4 years ago | ||
tsconfig.json | 4 years ago |
Code generator that creates middleware functions for express JS that manage collections and persists them as json files.
Description of the domain model in characters.model.json
:
{ "$schema": "https://infinity.synoikos.de/schemata/server-generator.schema.json ", "collections": [ { "name": "Characters", "entities": { "name": "Character", "properties": [ { "key": "name", "type": "string", "isNullable": false }, { "key": "backstory", "type": "string", "isNullable": false } ] } } ] }
Generate the code:
server-generator --typescript-output-folder src --openapi-output src/openapi.json characters.model.json
Resulting files in src:
CharactersDataAccess.ts
CharactersMiddleware.ts
CharactersModel.ts
openapi.json
util.ts
The most reliable source of documentation of commandline options is to just run server-generator --help
.
Options: --help Show help [boolean] --version Show version number [boolean] --typescript-output-folder Output folder for the generated server code. [string] [required] --openapi-output File name of the generated OpenAPI description. [string] [required]
Prerequisites: node, npm, typescript
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:
This projects attempts to collapse all the dull boilerplate into a concise description of the domain model.
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.
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.
#!/usr/bin/env node
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.
server-generator