{"id":43550,"library":"pg-error-enum","title":"pg-error-enum","description":"TypeScript Enum for Postgres Error Codes. Version 2.0.1 provides generated TypeScript enums mapping all PostgreSQL error codes to their SQLSTATE values, with zero runtime dependencies. Updated from the PostgreSQL errcodes.txt source. Supports ESM and CommonJS. Ideal for type-safe error handling in Node.js with the 'pg' library, eliminating magic strings.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/nihalgonsalves/pg-error-enum","tags":["javascript"],"install":[{"cmd":"npm install pg-error-enum","lang":"bash","label":"npm"},{"cmd":"yarn add pg-error-enum","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-error-enum","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Only named export available; default import not supported.","wrong":"import PostgresError from 'pg-error-enum'","symbol":"PostgresError","correct":"import { PostgresError } from 'pg-error-enum'"},{"note":"CommonJS destructure required, as the package exports an object.","wrong":"const PostgresError = require('pg-error-enum')","symbol":"PostgresError","correct":"const { PostgresError } = require('pg-error-enum')"},{"note":"Type-only import possible when only using enum as type.","wrong":null,"symbol":"type PostgresError","correct":"import type { PostgresError } from 'pg-error-enum'"}],"quickstart":{"code":"import { PostgresError } from 'pg-error-enum';\n\n// Example with node-postgres\nimport pg from 'pg';\nconst client = new pg.Client({ connectionString: process.env.DATABASE_URL ?? '' });\n\nasync function createUser(username: string) {\n  try {\n    await client.query('INSERT INTO users (username) VALUES ($1)', [username]);\n  } catch (err: any) {\n    if (err.code === PostgresError.UNIQUE_VIOLATION) {\n      throw new Error('That username is taken');\n    }\n    throw err;\n  }\n}","lang":"typescript","description":"Shows how to import and use the PostgresError enum to handle PostgreSQL unique constraint violations in a TypeScript project."},"warnings":[{"fix":"Upgrade Node.js to ^22.12.0 || >=24.0.0","message":"Version 2.0.0 dropped support for Node.js versions below 22.12.0","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"Check the generated enum for updated constant names; refer to the errcodes.txt mapping.","message":"Enum members renamed in v2; old names may be removed","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Compare with error.code from pg, which is a string.","message":"Enum values are SQLSTATE strings (e.g., '23505'), not numeric codes.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Upgrade to version 2.0.1 for latest codes.","message":"Not all PostgreSQL error codes may be present in older versions; ensure package is up-to-date.","severity":"gotcha","affected_versions":"<2.0.0"}],"env_vars":null,"search_vec":"'2.0.1':12 'code':10,21 'commonj':39 'depend':29 'elimin':53 'enum':4,6,16 'errcodes.txt':34 'error':3,9,20,45 'esm':37 'generat':14 'handl':46 'ideal':40 'javascript':56 'librari':52 'magic':54 'map':17 'node.js':48 'pg':2,51 'pg-error-enum':1 'postgr':8 'postgresql':19,33 'provid':13 'runtim':28 'safe':44 'sourc':35 'sqlstate':24 'string':55 'support':36 'type':43 'type-saf':42 'typescript':5,15 'updat':30 'valu':25 'version':11 'zero':27","created_at":"2026-06-05T17:00:20.227692+00:00","updated_at":"2026-06-05T17:00:20.227692+00:00","problems":[{"fix":"Add \"type\": \"module\" to package.json or use dynamic import.","cause":"Code compiled as CommonJS while package uses ESM.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Run: npm install pg-error-enum","cause":"Package not installed or not in node_modules.","error":"Module not found: Can't resolve 'pg-error-enum'"},{"fix":"Use import { PostgresError } from 'pg-error-enum' and refer to members directly.","cause":"Importing class incorrectly; PostgresError is an enum, instantiation not needed.","error":"TypeError: PostgresError is not a constructor"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/nihalgonsalves/pg-error-enum#readme","github":"https://github.com/nihalgonsalves/pg-error-enum","docs":null,"changelog":null,"pypi":null,"npm":"pg-error-enum","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}