autobackend

0.1.2 • Public • Published

AutoBackend

AutoBackend is batteries included backend configuration library for Express that automatically configure resources and API's with minimal effort. It supports multiple databases, and JSON Web Tokens by default. Using this library with a few lines of code, you will have user authentication and api endpoint protection with RESTful routes automatically configured, hence the name "AutoBackend".

Note... This library is ment for development project, or prototyping, and not for production, although it can be used in small environments. It's purpose is to automate the back-end logic, allowing the developer to focus on front-end implementation ie... (vue.js, angularjs, react).

Installation

npm install autobackend use the --save flag to include it in your package dependencies

Basic Usage

In your app.js file include the following code to setup AutoBackend

var autobackend = require("autobackend")()

Factory Generator

app.use('/', autobackend.factory());

This will load AutoBackend with the default configuration. Custom configuration can be loaded with a config object.
Below is the default configuration:

var config = {
    database:{
        type:"disk", // Options are disk, mongodb, mysql,sqlserver
        connection_string:"mongodb://localhost/test" // database connection string
    },
    jwt:{
        secret:"secret", // The secret used to create JSON Web Tokens
        header_key:"access_token" // Request header key for tokens to validate
    },
// This is the admin user that will be generated if the Factory Generator is called
    admin_user:{ 
        fname:"Website",
        lname:"Administrator",
        username:"admin",
        role:"admin",
        email:"admin@default.com",
        password:"password"
    }
    
}

Using app.use('/', autobackend.factory()) will create the following routes:

/users - protected with JSON Web Token user access_token key in the request header.

  • RESTful routes for user
    • "GET" /users/:_id? Gets all or one user with the optional _id key passed either in the parameter or query string
    • "POST" /users Inserts one user to database. Note... username, and email must be unique.
    • "PUT" /users/:_id? Updates one user in database by _id. Note... _id can be in the body, parameter, or query string of request.
    • "DELETE" /users/:_id? Deletes one user from database by _id. Note... _id can be in the body, parameter, or query string of request.

/auth - Used for authentication and registration routes.

  • Routes for /auth
    • "POST" /auth/login - Authenticate user and respond with JSON Web Token and user information. Note... Username or Email and password must be supplied in the body of this request. This will authenticate from the Users collection. You can use the default admin user to gain access to protected routes
    • "POST" /auth/logout Logs out user and places a record in attempts collection.
    • "POST" /auth/register Creates a new user, but does not log them in
    • "POST" /auth/validate Validates the JSON Web Token in the request headers
    • "GET" /auth/attempts - Protected This will retrieve a log of login and logout attempts. Each time a user attempts to login, a log record will be placed in the attempts collection. This is useful for debugging and access control
    • "GET" /auth/jwts- Protected This will retrieve all the tokens issued to the users. For debugging.

Collection Generator

As you can see, one line of code saves you a tremendous amount of time and effort. You can also create custom flexible collections with RESTful routes by using autobackend.collection("[name of collection]");

app.use('/', autobackend.factory());
app.use("/todos", autobackend.collection("todos"));

In addition to the routes created by factory, you now have a /todos collection. You can pass any JSON object to these routes and it will be stored.

  • RESTful routes for todos
    • "GET" /todos/:_id? Gets all or one todo with the optional _id key passed either in the parameter or query string
    • "POST" /todos Inserts one todo to database.
    • "PUT" /todos/:_id? Updates one todoin database by _id. Note... _id can be in the body, parameter, or query string of request.
    • "DELETE" /todos/:_id? Deletes one todo from database by _id. Note... _id can be in the body, parameter, or query string of request.

Route Protection

AutoBackend also provides you middleware functions the enable you to protect any route you have with the JSON Web Tokens issued by autobackend using autobackend.middleware.verify middleware.

app.use("/todos", autobackend.middleware.verify, autobackend.collection("todos"));

This will protect the /todos route with JSON Web Tokens issued by autobacked. The route will respond with a 401 Unauthorized status if the token is invalid. Using this you can protect any route you wish with one line of code.

Configuration

You can supply your own configuration object to autobackend, allowing you to use a different database or jwt secret, or admin user credentials Note... disk db is the default database, and mongodb is supported as an option. More database support will come in the furture.

Below is a sample configuration object:

// config.js
module.exports = {
    database:{
        type:"mongodb", // Options are disk, mongodb, "rethinkdb, mysql, sqlite, couchdb, sqlsvr will come in the future"
        connection_string:"mongodb://localhost/autobackend" // database connection string
    },
    jwt:{
        secret:"mysupersecret", // The secret used to create JSON Web Tokens
        header_key:"access_token" // Request header key for tokens to validate
    },
// This is the admin user that will be generated if the Factory Generator is called
    admin_user:{ 
        fname:"Imma",
        lname:"Boss",
        username:"admin",
        role:"admin",
        email:"admin@autobackend.com",
        password:"supersecretpassword"
    }
    
}

You can use this object when calling autobackend

var config = require("./config");
var autobackend = require("autobackend")(config);

or like this!

var config = require("./config");
var autobackend = require("../index");

var ab = autobackend(config);

or like this!

var autobackend = require("autobackend")
var config = {
    database:{
        type:"mongodb", 
        connection_string:"mongodb://localhost/autobackend" 
    },
    jwt:{
        secret:"mysupersecret", 
        header_key:"access_token"
    },
    admin_user:{ 
        fname:"Imma",
        lname:"Boss",
        username:"admin",
        role:"admin",
        email:"admin@autobackend.com",
        password:"supersecretpassword"
    }
    
}
var ab = autobackend(config)

You may supply only the parts of the configuration that you need like so:

var config = {
        database:{
            type:"mongodb", 
            connection_string:"mongodb://localhost/autobackend" 
        }
}

Contribution

I'm open to ways to make this library better. Please try this out in your own environments and let me know what works and what doesnt. Keep a look out for the future for more databases to be supported.

Package Sidebar

Install

npm i autobackend

Weekly Downloads

12

Version

0.1.2

License

ISC

Last publish

Collaborators

  • bcrowe306