1.8.50 • Published 2 years ago

drapi v1.8.50

Weekly downloads
11
License
ISC
Repository
bitbucket
Last release
2 years ago

Drapi

logo

Dynamic Rest Api (DRAPI) is a koaJs middleware, used to provide a REST Api in which you can dynamically define the controllers and its model's structure in a mongoDB database.

It also provides support for dynamic database connections based on a configurable request header param.

Overview

  • Dynamic database connections based on a header param (wp)
  • Dynamic rest apis (controllers) based on table on database (model)
  • Dynamic models definition based on table on database (structure)

Installation

	$ npm install drapi --save

Usage

App Skelleton

  1. Clone the drapi skelleton app
  2. Delete the Git folder (.git)
  3. In the app.js file it imports drapi and loads dynamic rest routes and custom routes
	const config = require('./config');	
	const Drapi = require('drapi');
	const path = require('path');
	
	const app = new Koa();
    const drapi = new Drapi(config);
    //load rest blueprints methods for custom rest controllers
	app.use(drapi.customRest(path.join(__dirname,'/api/controllers')));
    //load rest blueprints methods for dynamic rest controllers
    app.use(drapi.dynamicRest());
  1. Run the app
	$ npm run start

Configuration

The required configurations for drapi can be found in the /config/ folder.

You can set different configuration for each enviroment: development, testing, production.

The final json looks something like this:

	{ 
		"port": 3000,
		"db": { 
			"defaultDbName": "basehcm",
			"headerDbName": "wp",
			"host": "mongodb://localhost/"
		},
		"env": "development",
		"appBaseUrl": "/",
		"hostName": "http://localhost:3000"
	}
  • port: port number in which the api will be exposed
  • db.defaultDbName: Name of the default database to use when no header param recieved
  • db.headerDbName: Name of the header attribute in which the database name will be specified
  • db.host: Host of the mongoDB server
  • env: Current enviroment
  • appBaseUrl: Root path
  • hostName: App hostname to use

Database configuration

Drapi uses mongoDB as its default database engine

Static Collections

  • Model: collection in which all the desired controllers for the dynamic rest api should be inserted
	{
        "_id":{
        "type":"string"
        },
        "name":{
        "type":"string"
        }
    }

	//Eg.
	{
		"_id":"country",
	    "name":"Country"
	}
  • Structure: collection in whick the structure of the model for the controller should be inserted
	{
        "_id":{
        "type":"string"
        },
        "structure": {
        "type":"Mixed"
        }
    }

	//Eg.
	{
		"_id":"country",
	    "structure":{"_id":{"type":"ObjectId","primaryKey":true,"hidden":true,"auto":true},"name":{"type":"string","label":"Nombre"}}
	}

####Dynamic Collections

Every document in the "model" collection will become a collection in the database using its schema defined in the "structure" collection.

###Overwrite dynamic rest controllers

In the skelleton project, under api/controllers folder, create a file controller.js Create a class which extends the drapi rest controller

const RestController = require('drapi').restController;
 
    class CustomaController extends RestController {
        constructor() {
            super({
                name: "custom",
                routerPrefix: "/custom" 
            });
        }
 
        test(){
            ctx.body = "custom route"
        }
 
        registerRouter_() {
            //Add custom routes above super.registerRouter_()
            // this.router_.get('/test', (ctx) => this.test(ctx));
            super.registerRouter_();
        }
    }
 
module.exports = CustomaController;

###Overwrite rest methods

const RestController = require('drapi').restController;
 
    class CustomaController extends RestController {
        constructor() {
            super({
                name: "custom",
                routerPrefix: "/custom" 
            });
        }
 
        list(ctx){
            //list logic here
        }
 
        detail(ctx){
            //detail logic here
        }
 
        create(ctx){
            //create logic here
        }
 
        upd(ctx){
            //upd logic here
        }
 
        softdel(ctx){
            //softdel logic here
        }
 
        del(ctx){
            //del logic here
        }
 
        registerRouter_() {
            //Add custom routes above super.registerRouter_()
            // this.router_.get('/test', (ctx) => this.test(ctx));
            super.registerRouter_();
        }
    }
 
module.exports = CustomaController;

###Overwrte before and after hooks

const RestController = require('drapi').restController;
 
    class CustomaController extends RestController {
        constructor() {
            super({
                name: "custom",
                routerPrefix: "/custom" 
            });
        }
 
        /*
        * Hooks
        * Return true to continue
        * Return false to stop execution
        * to throw error use ctx.throw(<status>, "message")
        */
 
        beforeCreate(ctx){
            //logic here
            ctx.throw(400, "message");
            return false;
        }
 
        beforeUpdate(ctx){
            //logic here
            return true;
        }
 
        beforeDelete(ctx){
            //logic here
        }
 
        afterCreate(ctx){
            //logic here
            ctx.throw(400, "message");
            return false;
        }
 
        afterUpdate(ctx){
            //logic here
            return true;
        }
 
        afterDelete(ctx){
            //logic here
        }
    }
 
module.exports = CustomaController;

Contribution guidelines

  • Writing tests
  • Code review
  • Other guidelines

Who do I talk to?

  • Estratek: soporte@estratek.com
1.8.49

2 years ago

1.8.50

2 years ago

1.8.48

2 years ago

1.8.46

3 years ago

1.8.47

3 years ago

1.8.45

3 years ago

1.8.44

3 years ago

1.8.43

3 years ago

1.8.41

3 years ago

1.8.42

3 years ago

1.8.40

3 years ago

1.8.39

3 years ago

1.8.38

3 years ago

1.8.37

3 years ago

1.8.35

3 years ago

1.8.36

3 years ago

1.8.34

3 years ago

1.8.33

3 years ago

1.8.31

4 years ago

1.8.32

4 years ago

1.8.30

5 years ago

1.8.29

5 years ago

1.8.28

5 years ago

1.8.27

5 years ago

1.8.26

5 years ago

1.8.25

5 years ago

1.8.24

5 years ago

1.8.23

5 years ago

1.8.22

5 years ago

1.8.21

5 years ago

1.8.20

5 years ago

1.8.19

6 years ago

1.8.18

6 years ago

1.8.17

6 years ago

1.8.16

6 years ago

1.8.15

6 years ago

1.8.14

6 years ago

1.8.13

6 years ago

1.8.12

6 years ago

1.8.11

6 years ago

1.8.10

6 years ago

1.8.9

6 years ago

1.8.8

6 years ago

1.8.7

6 years ago

1.8.6

6 years ago

1.8.5

6 years ago

1.8.4

6 years ago

1.8.3

6 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.7

6 years ago

1.6.6

6 years ago

1.6.5

6 years ago

1.6.4

6 years ago

1.6.3

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.20

6 years ago

1.5.19

6 years ago

1.5.18

6 years ago

1.5.17

6 years ago

1.5.16

6 years ago

1.5.15

6 years ago

1.5.14

6 years ago

1.5.13

6 years ago

1.5.12

6 years ago

1.5.11

6 years ago

1.5.10

6 years ago

1.5.9

6 years ago

1.5.8

6 years ago

1.5.7

6 years ago

1.5.6

6 years ago

1.5.5

6 years ago

1.5.4

6 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

1.0.0

6 years ago