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

pg-express

v0.0.22

Published

Lightweight automated CRUD generated public urls, with session based authentication checks for express based on table schemas, with automated migration.

Downloads

11

Readme

pg-express

Lightweight automated CRUD generated public urls, with session based authentication checks for express based on table schemas, with automated migration.

Installation

npm install pg-express

or

yarn install pg-express

The Goal:

Ever get tired of having to write migration, AND crud routes, AND do auth validation for all of your PostGres tables?

The goal is to create a much better workflow for:

  • Public CRUD Postgres operations
  • Authentication verification
  • Value parsing & string safety checks
  • Postgres migration between environments and computers

By defining a front-end framework and using express middleware, you can dynamically use the following urls publicly to use CRUD faster!

The output:

GET "/db/users/1"
  > (Auth check) If row authorized user === current user in session, return row(s) with id of 1
  > (No auth check) return row(s) with id of 1

GET "/db/users"
  > Will list all rows this authorized user is allowed to view

GET "/db/users?limit=10&offset=10"
  > Will list all rows this user is authorized to view, with pagination
  > Pagination defaults to limit 25 and offset 0 when unset.

POST "/db/users/1"
with BODY { "first":"Alex" }
  > If you are user with id of 1, you can update this row
  > Any parameter in JSON body request with key matching a column from your schema will be updated
  > In body the key "first" matches column "first" in table "users" so column "first" and row 1 will be updated to value "Alex"

PUT "/db/users"
with BODY {
    "first":    "Alex",
    "last":     "Navarro",
    "email":    "[email protected]",
    "password": "keyboard-cat"
}
  > Creates a new row using form body as columns that match column_names
  > In this case, it will match each body key (first, last, email, password) with the columns in our schema (first, last, email, password) and create a new row with those values.

DELETE "/db/users/1"
  > If you are authorized to modify this row, it will delete this row

The setup:

const express = require('express')

const PostgresExpress = require('../index.js')

const app = express()
const { port=3000 } = process.env

// Generate a schema, so 
const schema = {
    tables: [
        {
            name: 'users',
            security: {
                read: (req, res) => {
                    // Anyone can read any row in this table
                    return true 
                },
                write: (req, res) => {
                    // Write your own authorization rules! 
                    if (req.session && req.session.user && req.session.user.id) {
                        // Return a string that will compare a column to a value.
                        // In this case, we return 'id = x' which our route will automatically compare with a "WHERE" operator.
                        // The example below says the column "id" must match the user's session id in order to be editable
                        return 'id = ' + req.session.user.id
                    }
                    return false // Return false to deny any editing
                }
            },
            columns:[
                { column_name:'id',       data_type: 'BIGSERIAL', constraints: 'NOT NULL PRIMARY KEY', index:true },
                { column_name:'email',    data_type: 'character', constraints: 'varying(120) NOT NULL UNIQUE' },
                { column_name:'password', data_type: 'character', constraints: 'varying(60)', hidden:true, encryption: '12345678901234567890123456789012' },
                { column_name:'first',    data_type: 'character', constraints: 'varying(60)' },
                { column_name:'last',     data_type: 'character', constraints: 'varying(60)' }
            ]
        }
    ]
}

// Call body parsing middleware before this!
app.use(express.json())

app.use(
    PostgresExpress({
        // Pool is currently default and only supported, client mode coming soon
        mode: 'pool',
        // Add a connection string like 'postgres://user:pass:5432/database', or a PG connection object
        connection: '',
        // Any and all configurations in this object get passed if it generates it's own connection to pg object.
        connectionConfig:{
            connectionTimeoutMillis: 0,
            idleTimeoutMillis: 10000,
            max: 10
        },
        // Migration will ensure tables and columns exist whenever booted onto new server or local environments.
        migrate: true,
        // !important: Pass the schema to tell your middleware how to handle the table routes.
        schema
    })
)

This is just the beginning, there is much more to come!