Giter VIP home page Giter VIP logo

node-rest-api-generator's Introduction

Node REST API Generator

This is a beta version

Supposed project folder structure

Note: for the generator script to work, the following folder structure is needed

- app
    - controller
        - <Model_Name>Controller # example: UserController.js
    - model
        - <camel_Plueral_Model_Name> # example: users.js
-  database
    - config
        - config.json
    - migrations
    - models
    - seeders
- routes
    - api
        - <camel_Plueral_Model_Name> # example: users.js
    - index.js
    - urls.js
- app.js

How to use

1. I keep this repo inside the project root under a folder /automator

2. clone this repo under project-root/automator

$ git clone url-to-this-repo ./automator

3. Git ignore the automator folder

4. Create your Source Data Model like below inside the repo folder automator/model_source

4.1 Define the API urls:

  • In the ./routes/urls.js file
  • naming conventions
    • METHOD_MODEL(S), e.g: POST_USER, GET_USERS, GET_USER
    • API end point: '/api/models/:id'
exports.users = {
        POST_USER: "/api/users",
        GET_USERS: "/api/users",
        GET_USER: "/api/users/:id",
        PUT_USER: "/api/users/:id",
        PATCH_USER: "/api/users/:id",
        DELETE_USER: "/api/users/:id"
}

4.2 Model Generation Instructions:

  • names: {'name': ModelName, 'pleural_name': PleuralModelName}
  • attributes: only {"name": "field_name", "type": "sequelize_type"}
  • associations:
    • { "method": "belongsTo", "associated_model": "Model", "as": "model" }
    • { "method": "hasOne", "associated_model": "Model", "as": "model" }
    • { "method": "hasMany", "associated_model": "Model", "as": "model" }
  • routes[].method options: ['createModel', 'updateModel', 'patchModel', 'getModels', 'getModel', 'deleteModel']
  • For controller model and route (middleware file) you can choose your own folder name
{
    "models": [
        {
            "names": {
                "name": "User",
                "pleural_name": "Users"
            },
            "attributes": [
                {"name": "firstName", "type": "string"},
                {"name": "lastName", "type": "string"},
                {"name": "firstNameFurigana", "type": "string"},
                {"name": "lastNameFurigana", "type": "string"},
                {"name": "dob", "type": "date"},
                {"name": "email", "type": "string"},
                {"name": "isMember", "type": "boolean"}
            ],
            "associations": [
                {
                    "method": "belongsTo",
                    "associated_model": "Role",
                    "as": "role"
                }
            ],
            "routes": [
                {"url": "/api/activities", "method": "createModel"},
                {"url": "/api/activities/:id", "method": "updateModel"},
                {"url": "/api/activities/:id", "method": "patchModel"},
                {"url": "/api/activities/", "method": "getModels"},
                {"url": "/api/activities:id", "method": "getModel"},
                {"url": "/api/activities:id", "method": "deleteModel"}
            ],
            "controller_dir_name": "user",
            "model_dir_name": "users",
            "routes_dir_name": "users"
        },
        // More models ...
    ]
}

5. Run API generator script

$ node ./automator/restAPIGenerator.js ./automator/model_source/Mode.js ./automator/model_source/AnotherMode.js

6. Before running migration from ./database folder with sequelize cmd you may want to update your migration file for validation and etc.

$ sequelize db:migrate

7. In routes/index.js use the router middleware

const model = require("./api/models");
router.use(model);

That's it. Now you can try your new APIs with postman or similar.

node-rest-api-generator's People

Contributors

tmukammel avatar

Watchers

 avatar  avatar

node-rest-api-generator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.