npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rest-docs

v1.2.0

Published

RESTful HTTP client library + Docs to test your API REST. Supports for PostgreSQL, MySQL, MariaDB and SQLite3.

Downloads

42

Readme

REST-Docs

RESTful HTTP client library + Docs to test your API REST. Supports for PostgreSQL, MySQL, MariaDB, MSSQL and SQLite3.

API

Table of contents

Install

npm i rest-docs --save

Install Database Library

  • MSSQL
npm i mssql --save
  • MySQL and MariaDB
npm i mysql --save
  • PostgreSQL
npm i pg --save
  • SQLite3
npm i sqlite3 --save

Usage

// server.js
const rest_docs = require('rest-docs');
var rest = new rest_docs();

rest.startServer({
  ip: '127.0.0.1', //<-- YOUR_SERVER_IP
  port: '8080', //<-- YOUR_SERVER_PORT
  compression: 'gzip' //<-- YOUR_COMPRESSION_STRATEGY
})

rest.startDBServer('mysql', {
  host: 'localhost', //<-- YOUR_DATABASE_HOST
  port: 3306, //<-- YOUR_DATABASE_PORT
  user: 'root', //<-- YOUR_DATABASE_USER
  password: '', //<-- YOUR_DATABASE_PASSWORD
  database: 'medic', //<-- YOUR_DATABASE_NAME
  timezone: '+00:00' //<-- YOUR_DATABASE_TIMEZONE
});

const api_config = {
  base: '/api',
  pages: {
    docs: true, //<-- Expose PAGE: /{{base}}/docs
    monitor: true //<-- Expose PAGE: /{{base}}/monitor
  },
  routes: {
    tb: [
      {      
        table: 'doctors', //<-- YOUR_TABLE_NAME
        event: 'DOCTOR', //<-- YOUR_EVENT_NAME 
        methods: ['GET', 'POST', 'PUT', 'DELETE'], //<-- YOUR_METHODS
        //Used only by methods 'POST' and 'PUT'
        columns: [
            {name: 'id', primary: true},
            {name: 'name'},
            {name: 'specialty'},
            {name: 'address'},
            {name: 'photo'}
        ]
      }
    ]  
  }
}

rest.buildRoutes(api_config)

Run

node server.js
# PAGES: {
#   api: 'http://127.0.0.1:8080/api',
#   docs: 'http://127.0.0.1:8080/api/docs',
#   monitor: 'http://127.0.0.1:8080/api/monitor'    
# }
# App listening at http://127.0.0.1:8080

Usage with .env file

npm i dotenv --save
// .env
NODE_ENV=development

IP=localhost
PORT=8000
COMPRESSION=gzip

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_DATABASE=medic

API_KEY=65856b68541470a4ad95e7fe8b6bbb40
// server.js
const rest_docs = require('rest-docs');
var rest = new rest_docs();

rest.startServer()
rest.startDBServer();

const api_config = {
  base: '/api',
  table: {
    created_date: 'created',
    modified_date: 'modified',
    active: 'deleted',
    active_operator: '=',
    active_value: 0,
    delete_value: 1
  },
  routes: {
    tb: [
      {      
        table: 'doctors', //<-- YOUR_TABLE_NAME
        event: 'DOCTOR', //<-- YOUR_EVENT_NAME 
        methods: ['GET', 'POST', 'PUT', 'DELETE'], //<-- YOUR_METHODS
        //Used only by methods 'POST' and 'PUT'
        columns: [
            {name: 'id', primary: true},
            {name: 'name'},
            {name: 'specialty'},
            {name: 'address'},
            {name: 'photo'}
        ]
      }
    ]
  }  
}

rest.buildRoutes(api_config)

Run

node server.js
# PAGES: {
#   api: 'http://localhost:8000/api',
#   docs: 'http://localhost:8000/api/docs',
#   monitor: 'http://localhost:8000/api/monitor'    
# }
# App listening at http://localhost:8000

Result

  • GET /api/docs

API

Test API

  • GET /api/doctors

API

  • GET /api/doctors/:id

API

  • POST /api/doctors

API

  • PUT /api/doctors/:id

API

  • DELETE /api/doctors/:id

API

Pages

  • Docs /api/docs

DOCS

  • Monitor /api/monitor

MONITOR

Methods

  • startServer(SERVER_CONFIG)

The SERVER_CONFIG represents the connection to the server.

|Constant |Default |Description | |-------------|-----------|--------------------| |ip |'localhost'|Server ip | |port |8000 |Server port | |compression|'' |Compression strategy|

Example:

// SERVER_CONFIG
{
  ip: {YOUR_SERVER_IP},
  port: {YOUR_SERVER_PORT},
  compression: {YOUR_COMPRESSION_STRATEGY}
}
  • startDBServer(CLIENT, CONNECTION_CONFIG)

The CLIENT parameter is required and determines which client adapter will be used with the library. By default: myslq.

|Database |CLIENT |Additional command to install the appropriate database library| |----------|-----------|--------------------------------------------------------------| |MariaDB |myslq |$ npm i mysql --save | |MSSQL |msslq |$ npm i mssql --save | |MySQL |myslq |$ npm i mysql --save | |PostgreSQL|pg |$ npm i pg --save | |SQLite3 |sqlite |$ npm i sqlite3 --save |

The CONNECTION_CONFIG represents the connection parameters to the database.

|Constant |Default |Description | |----------|-----------|------------------| |host |'localhost'|Database host name| |user |'root' |Database user name| |password|'' |Database password | |database|'database' |Database name | |timezone|'+00:00' |Database timezone |

Example:

// CONNECTION_CONFIG
{
  host: {YOUR_DATABASE_HOST},
  user: {YOUR_DATABASE_USER},
  password: {YOUR_DATABASE_PASSWORD},
  database: {YOUR_DATABASE_NAME},
  timezone: {YOUR_DATABASE_TIMEZONE}
}
  • buildRoutes(API_CONFIG)

The API_CONFIG represents the API configuration.

|Constant |Description | |---------|---------------------------------| |base |Main path of the API | |pages |Pages of the API | |table |Main configuration for all tables| |routes |All API routes |

Example:

// API_CONFIG
{
  base: '/api',
  pages: PAGE_CONFIG,
  table: TABLE_CONFIG,
  routes: ROUTE_CONFIG     
}

The PAGE_CONFIG represents the global configuration of the pages.

|Constant |Default |Description | |----------|--------|-----------------------------------------| |docs |true |Indicates if the docs page is visible | |monitor |true |Indicates if the monitor page is visible |

Example:

// PAGE_CONFIG
{      
  docs: true,  
  monitor: true
}

The TABLE_CONFIG represents the global configuration of table.

|Constant |Default |Description | |-----------------|-----------|---------------------------------------------------------| |created_date |'created' |Column name that indicates the update date | |modified_date |'modified' |Column name that indicates the update date | |active |'deleted' |Column name indicating active status | |active_operator|'=' |Operator that is applied on the active comparison query | |active_value |0 |Value that is applied on the active comparison query | |delete_value |1 |Value that is applied in the soft delete query |

Example:

// TABLE_CONFIG
{
  created_date: 'created',
  modified_date: 'modified',
  active: 'deleted',
  active_operator: '=',
  active_value: 0,
  delete_value: 1
}

The ROUTE_CONFIG represents the route groups.

|Constant |Default |Description | |----------|--------|-----------------------------| |tb |[] |Group for tables or views | |fn |[] |Group for functions | |sp |[] |Group for stored procedures |

Example:

// ROUTE_CONFIG
{      
  tb: [
    TB_CONFIG,
    ...
  ],  
  fn: [
    FN_CONFIG,
    ...
  ],
  sp: [
    SP_CONFIG,
    ...
  ]
}

The TB_CONFIG represents the table or view configuration.

|Constant |Default |Description | |----------|---------------------------------|------------------------------| |table |'table' |Table name | |view |null |View name | |event |'TABLE' |Event name(For socket.io event. => 'TABLE_INSERTED', 'TABLE_UPDATED', 'TABLE_DELETED') | |methods |['GET', 'POST', 'PUT', 'DELETE'] |List of methods to implement['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'SEARCH', 'SEARCH_COLUMN', 'POST_BATCH', 'PUT_BATCH', 'PATCH_BATCH', 'DELETE_BATCH'] | |columns |[] |List of columns(Used only by methods 'POST', 'PUT', 'PATCH', 'POST_BATCH', 'PUT_BATCH' and 'PATCH_BATCH') |

Example:

// TB_CONFIG
{      
  table: 'table',  
  view: null,
  event: 'TABLE',  
  methods: ['GET', 'POST', 'PUT', 'DELETE'],  
  columns: [
    COLUMN_CONFIG,
    ...
  ]
}

The COLUMN_CONFIG represents the column of table.

|Constant |Default |Description | |----------|-----------|--------------------------------------------------------| |name |'' |Column name | |primary |false |Defines if column is primary key | |hidden |false |Defines if the column data will be sent in the response |

Example:

// COLUMN_CONFIG
{
  name: 'id', 
  primary: true,
  hidden: true
}

The FN_CONFIG represents the function configuration.

|Constant |Default |Description | |-----------|-----------|---------------| |function |'function' |Function name | |params |[] |List of params |

Example:

// FN_CONFIG
{      
  function: 'function',  
  params: [
    PARAM_CONFIG,
    ...
  ]
}

The SP_CONFIG represents the stored procedure configuration.

|Constant |Default |Description | |------------|------------|----------------------| |procedure |'procedure' |Stored procedure name | |params |[] |List of params |

Example:

// SP_CONFIG
{      
  procedure: 'procedure',  
  params: [
    PARAM_CONFIG,
    ...
  ]
}

The PARAM_CONFIG represents the param of function or stored procedure.

|Constant |Default |Description | |----------|--------|------------| |name |'' |Param name |

Example:

// PARAM_CONFIG
{
  name: 'n'
}

Examples

Migrations

Author

@victor-valencia.

License

Licensed under the MIT license.