23

GitHub - sugarkubes/generators: API Generator - instantly generate REST and Grap...

 5 years ago
source link: https://github.com/sugarkubes/generators
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

Instantly Generate Rest & GraphQL OAS 3.0 compliant APIs ?

Sugar Generator - API Edition

Install

npm i -g sugar-generate

Prereqs

Getting Started

  1. Create your schema. It could be as simple as:
{
  "schema": {
    "name": {
      "type": "String",
      "default": ""
    },
    "isDead": {
      "type": "Boolean",
      "default": false
    },
    "age": {
      "type": "Number",
      "default": false
    }
  },
  "statics": {}
}

save this to monkey.json

  1. generate your api
sugar-generate \
--type api \
--name monkey \
--schema ./samples/monkey.json \
--destination /private/var/andrew/generator-tests/3
  1. Run or build the docker container and visit http://localhost:3000
cd /wrannaman/generator
npm i
npm start
# Or build the docker container!
docker build -t myMonkeys:0.1.0 .

running oas docs

Features ?

  • Generates simple Nodejs code
  • Graphql and Rest out of the box
  • Uses Mongodb with Mongoose ORM
  • Easy to build / deploy
  • Dockerfile included
  • Generates CRUD APIs
    • create
    • get (many, with pagination; supports search, sort, filter, pagination out of the box)
    • getOne
    • update
    • delete

What it's good at ?

  • Generating an initial API
  • Microservice oriented
  • Ready to deploy (build with docker => deploy)

What it's not good at (yet) ?

  • idempotent changes (i.e. it doesn't know if you wrote code in there or changed things around)
  • working with modified code
  • populating table joins
  • custom actions inside controller functions

How It Works

  1. Feed it a json schema (see below's Example Schema)
  2. Name it
  3. Tell it where to put the code.
  4. Build your generated code with the docker file
  5. Deploy it and move on

TODO

  • basic generator tests
  • graphql tests
  • other databases?
  • your ideas?
  • react components for the api?!?!
  • middleware for auth, token validation, etc.

Example Schema

{
  "schema": {
    "first_name": {
      "type": "String",
      "default": ""
    },
    "last_name": {
      "type": "String",
      "default": ""
    },
    "email": {
      "type": "String",
      "trim": true,
      "required": true,
      "unique": true,
      "immutable": true, // Don't let this change from api on update
    },
    "password": {
      "type": "String",
      "trim": true,
      "select": false,
      "immutable": true, // Don't let this change from api on update
    },
    "intro": {
      "type": "Boolean",
      "default": false
    },
    "team": {
      "type": "ObjectId", // This references another collection
      "ref": "Team"
    },
    "sub": {
      "one": {
        "type": "String",
        "trim": true,
        "required": true
      },
      "two": {
        "type": "Number",
        "required": true
      }
    },
    "role": {
      "type": "String",
      "enum": ["user", "maker"],
      "default": "user"
    }
  },
  "statics": {
    "statuses": ["created", "under_review", "listed", "deleted"],
    "status": {
      "active": "active",
      "inactive": "inactive",
      "deleted": "deleted"
    }
  }
}

Graphql support

graphql mutation

graphql schema

graphql is supported and gets created by default so you can choose between rest and graphql

Graphql is on http://localhost:3000/graphql

Generated project structure

.
├── configs                 # Config File
├── connection              # DB Connections (mongo, redis)
├── controller              # Controllers
│   ├── <model name>        # Functions (one file, one function) create, delete, update, get, getOne
├── models                  # DB Models
├── router                  # Endpoint Routes
├── tests                   # Single Test File

Generated tests

** WARNING ** running the tests will pull the config file from configs/config.json and clear the DB

npm run test

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK