Addis is a super easy and fast node express library that let's you create a professional API with sql databases.
$ npm install addis
- Super easy server configuration, by default basic server configurations are included.
- Quick and easy connection to database.
- Router configuration is simple and flexible
- Different accessories are present to ensure wide range of functionalities, like: - Making a route protected is just adding an object property - Token generation with key specified - Validating request bodies with JOI validator is made easy - Encrypting any data that is going to be passed to the database
Here we'll show you everything the package does.
Under route setting an object containing the route name as a key and express router as a value, this libarary handles the route creation too, which will be shown below.
const addis = require('addis');
//take a look at route configuration section below to understand how the routes files are created
const route1 = require('./route1');
const route2 = require('./route2');
addis.connection('localhost', 'userName', "password", 'databaseName', (err, con) => {
addis.server({
port: 3000,
token_key: "honkytonky", //if you need token generation in your app
connection: con, // callback result of abel.connection
app_level_middleware: [], //add app level middlewares in the array
route_setting: { "/route1": route1, "/route2": route2 } // add routes
});
})
For example let's there be a table called user, with entries userName, phone, email and Password. A general configuration looks like this:
const addis = require('addis');
const express = require('express')
const router = express()
var routingEngine = addis.routing(router)
var user = null;
//All routes here
user = routingEngine.routing({
router_type: 'get',
router_path: '/',
selectors: {
table_name: 'user'
},
accesseries: {
protected: false
}
})
module.exports = user;
Here are most common read operations in great detail
routingEngine.routing({
router_type: 'get',
router_path: '/',
selectors: {
selector: ['*'] // by default it is *, but you can add array of columns to be fetched like [userName, phone]. Then it will only fetch these
table_name: 'user' // add table name
},
accesseries: {
protected: false //determine if the route is protected or not
}
})
routingEngine.routing({
router_type: 'get',
router_path: '/:id_column', //The name of param passed must be as same as the column name on the database.
selectors: {
table_name: 'user'
},
})
// the equivalent statement would be SELECT * FROM table_name WHERE phone='phone' AND password='password'
routingEngine.routing({
router_type: 'get',
router_path: '/expression/:phone/:password', // the param name should be same as the corresponding table column name
selectors: {
table_name: 'student',
expression: ['AND'] // array of query statement binders between the params
}
})
This is the create operation on a database
routingEngine.routing({
router_type: 'post',
router_path: '/',
selectors: {
table_name: 'user',
},
accesseries: {
validator: validator, // add Joi validator object here
protected: false,
encryption: { // here is the encryption section
round: 1,
encrypt_column: 'userName' // specify which key in request.body to encrypt
}
}
})
Same as CREATE operation but pass id as a params in the route path, but as mentioned above the name of the param must be as same as the id column name.
...
route_path: '/:user_id'
...
routingEngine.routing({
router_type: 'delete',
router_path: '/:user_id', //again the name of param should be identical to the id column name.
selectors: {
table_name: 'student'
}
})
This library uses joi object for validation.
accesseries: {
...
validator: Joi_validation_object
...
}
To generate token, the first part of the path must be "login".
...
router_path: '/login/:userName/:password',
...
Addis uses bcryptjs for encryption.
accesseries: {
...
encryption: {
round: 1, // rounds of encryption
encrypt_column: 'password' // Column to be encrypted
}
...
}
If protected is true, addis checks token set in header based on the token key set on server configuration.
accesseries: {
...
protected: true,
...
}
Soon we'll be adding image and video support.
MIT