ServerGenerator /
@peter peter on 20 Sep 2020 3 KB Created a readme.

Server Generator

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": "    ",
  "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

Commandline options

The most reliable source of documentation of commandline options is to just run server-generator --help.

  --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

  1. clone this repo
  2. npm run build-generator
  3. chmod +x dist/server-generator.js
  4. 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.

#!/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.