1.0.5 • Published 3 years ago

postgrexpress v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

PostgrExpress

Description

This package generates an Rest API for PostgreSQL.

Table of Contents

Required Dependencies

Usage

  // Import packages
  import * as PostgrExpress from "postgrexpress";
  import * as pg from "pg";
  import * as express from "express";
  
  const app = express();
  
  /*
   ... Your express server configurations ...
   */
  
  // Connect to your database via "pg" package
  const poolConfig = {
    host: '...',
    ...
  };
  
  const db = new pg.Pool(poolConfig);
  
  // Define your table's schema.
  const tables = [
      {
          tableName: "users",
          routeName: "users",
          primaryKey: "user_id",
          update: {
              fields: [
                  {
                      name: "username",
                      required: true,
                  },
              ],
          },
          insert: {
              fields: [
                  {
                    name: "username",
                    required: true,
                  }
             ],
          },
      },
  ];
  
  // You can define your views too. Reference API.
  
  // Define your PostgrExpress config.
  const conf = {
    database: {
        tables: schema,
        views: []
    }  
  };
  
  // Call PostgrExpress. 
  const pge = new PostgrExpress(db);
  app.use(`/api`, pge.PrepareRoute(conf));
  
  // Done.
  

API

Table Schema

PropertyTypeDescriptionRequired
tableNamestringPostgreSQL Table Name
routeNamestringRoute name for expressDefault: tableName
primaryKeystringPrimary key for table
updateUpdateCommandpost route/:id configuration
insertInsertCommandpost route/ configuration
selectSelectCommandget route & get route/:id configuration
deleteDeleteCommanddelete route/:id configuration

View Schema

PropertyTypeDescriptionRequired
routeNamestringRoute name for express
primaryKeystringPrimary key for table (must be with dot. "u.user_id")
querystringActual sql query for view
argsany[]Query args
selectSelectCommandget route & get route/:id configuration

Command Schemas (Select, Insert, Update, Delete)

Select

PropertyTypeDescriptionRequired
beforeasync (id?: number):voidBefore select request
afterasync (data?: any[]):voidAfter select request
columnsstring[]Select columns listDefault: "*"

Update

PropertyTypeDescriptionRequired
beforeasync (body: Object):ObjectIf you need to redefine body you can use this function.
afterasync (data: any[]):voidUpdated columns list
fieldsField[]Which columns will used for update
idColumnstringIf you dont want to use table.primaryKey

Insert

PropertyTypeDescriptionRequired
beforeasync (body: Object):ObjectIf you need to redefine body you can use this function.
afterasync (data: any[]):voidInserted columns list
fieldsField[]Which columns will used for insert

Delete

PropertyTypeDescriptionRequired
beforeasync (id: number):voidBefore delete request
afterasync (id: number):voidAfter deleted success
idColumnstringIf you dont want to use table.primaryKey

Field

PropertyTypeDescriptionRequired
namestringKey of object
requiredbooleanIf this field required for insert/updateDefault: false

Pagination

Select all route uses pagination default. Here are the querystring schema.

KeyTypeDescriptionDefault
limitnumberLimits result count100
offsetnumberStarts from0
orderbystringOrder columntable.primaryKey
ordertype'ASC' or 'DESC'Order type'DESC'

Example

Insert tables to database

    CREATE TABLE users(
       user_id integer SERIAL PRIMARY KEY,
       username varchar(100)
    );
    
    CREATE TABLE comments(
       comment_id integer SERIAL PRIMARY KEY,
       user_id integer,
       content varchar(500),
       data json
    );

Define schemas in typescript

  // Define fields for insert/update operations.
import PostgrExpress from "./PostgrExpress";

const userFields = [
    {
        name: 'username',
        required: true,
    }
];
const commentFields = [
    {
        name: 'content',
        required: true,
    },
    {
        name: 'user_id',
        required: true,
    }
];

// Define database config for PostgrExpress.
const schema = {
    tables: [
        {
            tableName: 'users',
            primaryKey: 'user_id',
            update: {
                fields: userFields,
                async after(updatedRows) {
                    // You can do any stuff with updatedRows.
                    console.log(`Updated row count: ${updatedRows}`);
                },
                async before(body) {
                    // You can do any editing on body.
                    // If you dont return edited body, this program will give error.
                    return body;
                }
            },
            insert: {
                fields: userFields
            }
        },
        {
            tableName: 'comments',
            routeName: 'custom_comments', // If you dont want to use /comments route.
            update: {
                fields: commentFields,
            },
            insert: {
                fields: commentFields
            }
        }
    ],
    views: [
        {
            routeName: "user_comments",
            primaryKey: "u.user_id",
            query: `SELECT * FROM users u INNER JOIN comments c ON u.user_id = c.user_id `,
            args: [],
        },
    ]
};

const pge = new PostgrExpress(db) // db is your Pg POOL!
const route = pge.PrepareRoute({
    database: schema,
}); // route is express.Route now.

// You can use route as any if you want.
server.use(`/api`, route);

// Done.

Result

MethodRouteDescription
GET/api/usersGets all users with pagination
GET/api/users/1Gets user where "user_id" = 1
POST/api/users (content-type: application/json)Inserts user with required key "username"
POST/api/users/1 (content-type: application/json)Updates user where "user_id" = 1
DELETE/api/users/1Deletes user where "user_id" = 1
GET/api/custom_commentsGets all comments with pagination
GET/api/custom_comments/1Gets comment where "comment_id" = 1
POST/api/custom_comments (content-type: application/json)Inserts comment with required keys "user_id", "content"
POST/api/custom_comments/1 (content-type: application/json)Updates comment where "comment_id" = 1
DELETE/api/custom_comments/1Deletes comment where "comment_id" = 1
GET/api/view/user_commentsGets user-comment join data
GET/api/view/user_comments/1Gets user-comment join data where "user_id" = 1

Testing

Writed tests for body handling and key checks.