modeljs-api
Introduction
ModelJS API is a framework that make the ExpressJS developer life easier. When creating API you know how hard it was to create each route to each entity and validate them when creating, refine them when retrieving. Same code repeat every single time and the authentication is a headache.
But in the ModelJS API, we provide a simple configurable framework. Enable authentication to your server is just 5 lines of code in the config object.
Not like in LoopBackJS, the configuration is simple and easy to understand. The hooks feature make the api more stronger and customizable as the developer needs. By default this provides token base authentication.
Features
-
ExpressJS, MongoDB, mongoose, mongoose-auto-increment, ExpressJS cors.
-
Token based authentication with jwt token. Admin account can be created with adminKey
-
Models can be shaped as they way in mongoose schema
-
user model can be extend and by default username, password, userRole key is there
-
api path can be configurable.
-
Hooks is a strong feature. it can be used as middleware when request processing.
-
if auth enables, in hooks you can read the user and do the needful
-
Route can be protected by
allowedRoute
andnotAllowedRoutes
-
Actions can be protected by the authentication. Public and private routes.
-
#### Future goals
- move to MySQL and other DB as well
- role based action definitions.
- inbuilt password reset and email sending
How to integrate to your express server
- Create basic express app as bellow
const express = ;const logger = ; const modelJS = ; const config = ;const hooks = ;const app = ; // additional logs app; // Define the basic json parse middlewareapp;app; // initiate ModelJS application; // catch 404 and forward to error handlerapp; // error handlerapp; moduleexports = app;
Configuration
- Models Configurations
- Hooks Configurations
1. Models Configurations Object
Sample configuration object here and each key will be defined later.
moduleexports = dbName: 'test-db' // MongoDB collection name apiVersion: 'v1' // version tag of api, (api/v1/...) routePrefix: 'api' // first part of the url (<routePrefix>/<apiVersion/...) enableCors: true // cors enabled by `cors` plugin for ExpressJS auth: // auth related configuration enable: true // is auth enabled adminKey: 'shhh' // secret key when creating admin users shape: // extended user model shape. (username, password and userRole is there) telephone: type: String age: type: Number models: // this array contains the data of each entity name: 'Author' // Name of the Entity to show path: 'author' // url shows this eg: /api/v1/auth/ autoIncrement: // if you want to enable autoincrement id (by mongoose-auto-increment) enable: true // enable field: 'id' // filed name for auto increment id startAt: 100 // starting number shape: // shape of the model. (this is same as mongoose model) name: type: String required: true age: type: Number required: true allowedActions: CREATE // if allowed actions is given only create can do. blockedActions: GET_ALL // Author model getAll is not allowed privateActions: GET_ALL // to access user should be authenticated allPrivate: true // to ensure all the actions are under authenticated. no need to define private actions {} // manual validator function when creating the model
2. Hooks Configurations Object
Sample Hooks configuration object is here and each key will be defined later
NOTE: Every hook is ExpressJS middleware and you should call next() finally to execute further
moduleexports = generic: // execute for all requests { // execute before the handler. you can use params or req.body here // do something ; } { // execute after the handler. you can use mjs related object here. // do something ; } { // call before user models is being created (use this for manual validation) // do something ; } models: // hooks that are specified on models Book: // model name generic: { // execute before the handler for all the request to this model // do something ; } { // execute after all the handlers for all the requests to this model // do something ; } getAll: // define action specific hooks here { ; } { ; }