0.0.7 • Published 3 years ago

@goodly/mongoframework v0.0.7

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
3 years ago

Mongo Express RESTful JSON API

This is basic mongo express framework that uses a configuration file\ to expose CRUD operations via a RESTful JSON API.

Quick Start

Install

npm i @goodly/mongoframework

Configure the API

Create a config.js file in the root directory of the project and \ add the code bellow.

module.exports = {
    database: {
        host: process.env.DB_HOST || '0.0.0.0',
        port: process.env.DB_PORT || '27017',
        name: process.env.DB_DATABASE || 'phonebook',
        user: process.env.DB_USER || 'root',
        password: process.env.DB_PASSWORD || 'password'
    },
    api: {
        baseURL: '/api/v1',
        port: process.env.API_PORT || '3000',
        maxResponseSize: 50,
        filters: {
            pageNo: 'pageNo',
            pageSize: 'pageSize',
            sortBy: 'sortBy',
            order: 'order'
        }
    },
    endpoints: {
        people: {
            schema: {
                table: 'people',
                isEmbedded: false,
                fields: {
                    name: {
                        type: 'string',
                        required: true
                    },
                    surname: {
                        type: 'string',
                        required: true
                    },
                    mobilenumber: {
                        type: 'mobile',
                        required: true
                    },
                    emailaddress: {
                        type: 'email',
                        required: true
                    }
                }
            },
            http: {
                get: ['', '/:id'],
                post: [''],
                patch: ['/:id'],
                delete: ['/:id']
            }
        }
    }
}

Create index.js

Crate a index.js file and copy and paste the code bellow \ to expose the api on the desired port.

const app = require('@goodly/mongoframework').app
const config  = require('@goodly/mongoframework').config
const http = require('http')


const port = Number.parseInt(config.api.port)
app.set('port', port)

const server = http.createServer(app)

const listen = () => {
  console.log('Listening on port: '.concat(port))
}

server.on('listening', listen)
server.listen(port)

Run API

To run the api simply run the code bellow in the terminal

node index

Detailed guide

Configuration process

All configuration is done in the config.js file. Create the file in\ the root directory of your project. The file shoudl contain, database,\ api and endpoint configuration. Paste the code bellow inside the your \ config.js file and paste all other configs inside it.

Entry point code

modules.exports = {

}

Database connectivity

Change the default values as required.

database: {
    host: process.env.DB_HOST || '0.0.0.0',
    port: process.env.DB_PORT || '27017',
    name: process.env.DB_DATABASE || 'petstore',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || 'password'
}

HTTP Server API configuration

Change the default values as required

api: {
    baseURL: '/api/v1',
    port: process.env.API_PORT || '3000',
    filters: {
      pageNo: 'pageNo',
      pageSize: 'pageSize',
      sortBy: 'sortBy',
      order: 'order'
    }
}
 

API Endpoint configuration

The endpoints property is used to describe the database tables and http\ endpoints that expose the database via REST API. Endpoints has two\ main properties shcema and http

endpoints: {
    schema: {

    },
    http: {

    }
}

schema

The schema property is used to describe how each table (collection) will\ store data.

schema: {
    table: 'people',
    isEmbedded: false,
    fields: {
        name: {
            type: 'string',
            required: true
        },
        surname: {
            type: 'string',
            required: true
        },
        mobilenumber: {
            type: 'mobile',
            required: true
        },
        emailaddress: {
            type: 'email',
            required: true
        }
    }
}

Table

The table property is used to define the table (collection) name.

isEmbedded

The isEmbedded property is used to define if a document is embedded in another. \ If it is an embedded document use the table (collection) name of the parent document.

Fields

The fields property is used to define all the document properties and thier respective\ types and if its required. Supported types are listed bellow.

typeDescriptionSupporting tags
stringall strings including an empty String
mobilephone numbers
emailemail address
alphaalphabet values only (a-z)
alphanumericalphabets and numbers (a-z) and (0-9)
datejavascript date values
intintegers
numericintegers, doubles, floats
booleanboolean values true and false
enumone of a list of predefined valuelist: array
customsupport for regex based validationregex: Regex

http

The http property is used to define the available http methods and paths\ relating to an entitiy. Supported methods are limited to GET, POST, PATCH, DELETE

http endpoint paths will append the endpoint name to the begining of the path,\ so no need to explicitly define the enpoint name. So the people paths will be\ configured as.

http: {
    get: ['', '/:id'],
    post: [''],
    patch: ['/:id'],
    delete: ['/:id']
}

Which will result in these endpoints

  • GET /people
  • GET /people/:id
  • POST /people
  • PATCH /people/:id
  • DELETE /people/:id

Adding Authentication and Authorisation

The framework uses express under the hood and exposes the express \ object as app. Which you can use to plugin standard express middlware \ to do things that the api does not provide pre and post request.

External dependencies

  • MongoDB database