Giter Club home page Giter Club logo

adonis-rest-template's Introduction

Basestack for Adonis framework.

A basestack where lot of features will be handelled outof the box. So even if you don't know adonis and quickly want resource endpoints, you can use this template.

Adonis API application

This is the boilerplate for creating an API server in AdonisJs, it comes pre-configured with.

  1. Bodyparser
  2. Authentication
  3. CORS
  4. Lucid ORM
  5. Migrations and seeds
  6. Pagination
  7. Resource

Setup

Install Adonis

npm i -g @adonisjs/cli

Use the adonis command to install the blueprint

git clone https://github.com/hypermine-bc/adonis-rest-template.git

clone the repo and then run npm install.

adonis serve --dev

Migrations

Run the following command to run startup migrations.

adonis migration:run

Examples (for lazy developers)

Example User Controller

We are lazy developers Right! ๐Ÿ˜œ

So lets make the controller as lean as possible ๐Ÿ˜‰

Pay attention to these variables, that are defined

allowedWiths updatable, searchable

'use strict'
const BaseController = use('App/Controllers/Http/BaseController')
const UserValidations = use('App/Validators/AuthRegister')
const User = use('App/Models/User');

class UserController extends BaseController {

    constructor() {

        super();
        
        this.model = User

        this.allowedWiths = [
            'posts',
            'tokens'
        ]

        this.updatable = [
            'username',
            'email',
            'password'
        ]

        this.searchable = [
            'username',
            'email',
            'id'
        ]

        this.getValidators = function () {
            return new UserValidations()
            // return validations.rules
        }
    }
}

module.exports = UserController

Search

Yes! you got it right search is out of the box for every resource that extends baseController.

Ex 1 : Search for user contains vik

{{HYPERMINE_API_HOST}}/api/v1/users/?query=vik

Ex 2 : Search for user contains Vik with Posts and token

{{HYPERMINE_API_HOST}}/api/v1/users/?query=vik&attributes=posts,tokens

Look how easy it was right ๐Ÿ˜‰

Attributes

Lets say you want to get User with Posts and tokens

You can do request API like

{{HYPERMINE_API_HOST}}/api/v1/users/?attributes=posts,tokens

Filters

Since we are very lazy and generally fitrers are complicated so. This template add's it out-of-the-box.

Lets say you want to get users where username=vikram

{{HYPERMINE_API_HOST}}/api/v1/users/?filters=[{"username": "vikram"}]

Or perhaps you want to get vikram whoes id = 1

{{HYPERMINE_API_HOST}}/api/v1/users/?filters=[{"username":"vikram"},{"id": 1}]

Attribute + Filters

Lets get one step further, lets say you want to get Get user starts with vi and id=1 with posts and token

{{HYPERMINE_API_HOST}}/api/v1/users/?attributes=posts,tokens&filters=[{"username": [{"startsWith": "vi"}]},{"id": 1}]

Some more ;)

{{HYPERMINE_API_HOST}}/api/v1/users/?attributes=posts,tokens&filters=[{"username": [{"endsWith": "ide"}]}]

Logging

Finally we have logging Out-Of-The_box

As explained in Adonis framework the access log you can fine in your tmp/adonis.log file.

Ex: Loggin an update method

{"userid":2,"controller":"UserController","method":"update()","data":{},"time":"Mon Aug 19 2019 01:14:09 GMT+0530 (IST)","level":"info","message":"Resource-id : 5"}

adonis-rest-template's People

Contributors

vikramide avatar

Watchers

James Cloos 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.