0.1.8 • Published 3 years ago

nodejs-rest v0.1.8

Weekly downloads
26
License
MIT
Repository
github
Last release
3 years ago

nodejs-rest

Nodejs-rest is a quick and easy API builder framework, that lets you create powerful and professional API with most API features included including connection to multiple database connections.

  • The goal is to help you build your API by focussing more on your business logics, rather than tedious setups and redundant coding.

Installation

Installation is done using the npm install command:

$ npm install nodejs-rest

Features

  • Quick server configuration setup
  • Easy database connections.
  • API route setup with easy configurations
  • Route authentication
  • Hashing data to be stored on database or other persistance layer

Getting Start

The quickest way to get started with nodejs-rest is to start your node api project and install the nodejs-rest module

Creat your project:

  • *Make sure your project name is different from nodejs-rest
    // fill out the questionnaires
    $ npm init

    // for quick fill outs
    $ npm init -y

Install the package:

    $ npm install nodejs-rest --save

Create your index.js or server.js file in the root directory

  • A single file will sufice to build you API.
  • To build multiple apis with different database access point you can have multiple files (server1.js, server2.js,...) all created in the root directory.

Usage

The only import you are required to use the module is nodejs-rest.

const RestFactory = require("nodejs-rest");

const port = process.env.PORT || 3000;
const cors = ["https://localhost:3000"] // the default is *
const rest = RestFactory(port: number, cors?: string | string[]);

Database connection

rest.connect({
  port: 3306,
  database: "db_name",
  user: "user_name",
  password: "password",
  host: "host_address",
  database_type: "mysql",
});

Route configuration

You can set your routes prefixes, to give your different route projects senses. Like: https://mywebsite/api/v1/users/get/all

rest.landingRoute("/api/v1");

To add routes use the addRoute() function

Here is how route initialization and configuration structure looks like

rest.addRoute({
  method: "get" | "post" | "put" | "delete",
  url: string, // set your route url here `/user/get/all`
  action: {
    task:  "get" | "add" | "update" | "delete" , // tells the database what to do, in this case SELECT, UPDATE, INSERT, DELETE
    detail: {// detail has information about the retrieved data from connected database server
      table: string, // table name
      columns: string[], // array of columns from the respective database table, which are wanted for the SELECT query, default is *
      condition: string // sql conditions for query the WHERE query phrase,
    }
  },
  input_type: "params" | "query", // sets how you want your url to get parameters
  selectors: string[] // array of columns from the respective database table, where you are planning to pass as a where condition, which will be consumed from the url parameters
});

Examples

This route configuration generates the following api url https://mywebsite/user/get/all/:user_id

rest.addRoute({
  method: "get",
  url: "/user/get/all",
  action: {
    task: "get",
    detail: {
      table: "user",
      columns: ["user_first_name", "user_age", "user_status"],
      condition: "WHERE user_status=active LIMIT 100 ORDER ASC",
    },
  },
  input_type: "params", //(query, params) if input is expected to be passed in with GET request (optional)
  selectors: ["user_id"],
});

The same route configuration with input type query https://mywebsite/user/get/all/?user_id=1

{
  ...
  input_type: "query"
  ...
}

This is another route configuration example

rest.addRoute({
  method: "post",
  url: "/user/add",
  action: {
    task: "add",
    detail: {
      table: "user",
      columns: [
        "user_first_name",
        "user_last_name",
        "user_gender",
        "user_email",
        "user_password",
        "user_number",
      ],
    },
  },
});

The above route has the following features:

  • Accepts post request
  • Adds user's informations to user table
  • Operates INSERT database opertaion because of the following declaration
    {
        ...
        type: "add"
        ...
    }
  • And expects input arguments the same as columns array memebers
    {
        ...
        columns: [
        "user_first_name",
        "user_last_name",
        "user_gender",
        "user_email",
        "user_password",
        "user_number",]
        ...
    }

Build the routes' configurations

This command will inject all the routes' configurations listed above it, to the express router

rest.buildRoutes();

Start the server

This command starts your server

rest.init();

Todo

  • Route authorization and previlages
  • Middleware and modules injections

Contact

samuelgirmayesusleo0941@gmail.com

License

MIT