Giter Site home page Giter Site logo

generic-rest-api's Introduction

Build Status

generic-rest-api

Express JS generic REST API based on your sequelize models

The goal is to make something that we can use to build APIs really fast, instead of using a full framework like sailsjs or loopback

Requirements

  • expressjs
  • sequelize

Getting Started

First you add generic-rest-api to your project:

npm install --save generic-rest-api

Then you add it as a middleware to your express app, informing the path where your sequelize models are:

var genericRestApi = require('generic-rest-api');
...
app.use(genericRestApi(__dirname+'/models'));

Let's assume you have just one model named user. The first example will add your application the following routes:

GET /user GET /user/:id GET /user/:id/:relation POST /user PUT /user/:id DELETE /user/:id

If you want to add a prefix to your API you just need to declare it in express use:

app.use('/api/v2', genericRestApi(__dirname+'/models'));

This way your API will be:

GET /api/v2/user [...]

You can also add some middlewares to all generic rest routes using an options argument:

var options = {
    middlewares: [
        authentication
    ]
};
app.use(genericRestApi(__dirname+'/models', options));

Aditional Parameters

In way to perform more specific queries, you can pass some parameters while accessing your API route.

Filter results by specif value (works as sql 'where [name] = [value]') all parameters passed as get method will be treated as a 'WHERE' rule

GET /api/v2/user/?firstName=Luiz

Show only specific fields To return only specif fields, you can pass a parameter called 'fields' separeted by comma.

GET /api/v2/user/?firstName=Luiz&fields=user_id,age,fullName

Making field relations If a foreign key is set to a field, you can pass with a parameter called 'populate' in order to have it retrieve within your API response. If you have more than one relation, you can pass it also separated by comma

GET /api/v2/user/?firstName=Luiz&fields=user_id,age,fullName&populate=company,...

Advanced where queries In order to have advanced queries with 'LIKE' on 'WHERE', you can specify an JSON just like sequelize accepts

GET /api/v1/user/?where={"name":{"$like":"%25Luiz%25"}}

Important notes:

Authentication

There is no authentication in this module. I know I'm forcing your hand here, but you can use a middleware for authentication and hooks on models for authorization.

Obs

If you want to override any method, you just need to add your own custom route BEFORE the middleware.

Issues

If you find any kind of issue, please make this world more beautiful and report this issue so I can correct it. And, of course, if you want you can always make a pull request.

Next Steps

  • support Restify
  • support other ORMs

generic-rest-api's People

Contributors

luizguilhermesj avatar

Watchers

James Cloos avatar  avatar

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.