JSPM

@fiveem/postgres-error-codes

0.0.5
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q50567F
  • License MIT

Postgres Error Codes

Package Exports

  • @fiveem/postgres-error-codes

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@fiveem/postgres-error-codes) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Postgres Error Codes

Postgres error codes mapping for NodeJS.

Install

npm i @fiveem/postgres-error-codes

Usage

Each condition from the PG documentation is available in the module. In order to obtain the status code use the prefixed uppercase condition name:

unique_violation => PG_UNIQUE_VIOLATION

not_null_violation => PG_NOT_NULL_VIOLATION

const { PG_UNIQUE_VIOLATION, PG_NOT_NULL_VIOLATION } = require('postgres-error-codes')

async function createUserMethod(req, res, next) {
    try {
        // Run insert user SQL
    } catch (err) {
        // If user with same email already exists
        if (err.code === PG_UNIQUE_VIOLATION) {
            return next('Email already exists!')
        }

        // Param should not be null
        if (err.code === PG_NOT_NULL_VIOLATION) {
            return next('Email required!')
        }

        next(err)
    }
}