{"id":16343,"library":"dsn-parser","title":"DSN Parser","description":"The `dsn-parser` package (currently at version 1.0.3) provides a fluent API for parsing and constructing database connection strings (DSNs). It supports common DSN formats, extracting components such as driver, user, password, host, port, database name, and query parameters. A primary use case demonstrated is parsing Heroku-style PostgreSQL environment variables (`pgsql://user:pass@host:port/database`) into an object format compatible with popular database drivers like `node-postgres`. The library can also be used to programmatically build DSNs from individual components, offering a concise way to manage database connection configurations. It is a focused utility library solely for DSN manipulation.","status":"maintenance","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/fonini/dsn-parser","tags":["javascript","dsn","parse","jdbc","odbc","database"],"install":[{"cmd":"npm install dsn-parser","lang":"bash","label":"npm"},{"cmd":"yarn add dsn-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add dsn-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module. Direct ES module imports are not supported.","wrong":"import DSNParser from 'dsn-parser';","symbol":"DSNParser","correct":"const DSNParser = require('dsn-parser');"},{"note":"The library exports a class. Instantiation with `new` is required to parse or build a DSN.","wrong":"const dsn = DSNParser.parse('driver://...');","symbol":"DSNParser Class Instance","correct":"const dsn = new DSNParser('driver://user:pass@host:port/db?key=value');"},{"note":"While technically possible in an ESM file with `import DSNParser from 'dsn-parser';` due to Node.js's CJS interop, it is still a CJS module. Named imports like `{ DSNParser }` will fail.","wrong":"import { DSNParser } from 'dsn-parser';","symbol":"Module Import (ESM context)","correct":"import DSNParser from 'dsn-parser'; // Requires a CommonJS interop wrapper or bundler"}],"quickstart":{"code":"const DSNParser = require('dsn-parser');\nconst pg = require('pg'); // Example consumer, install 'pg' separately\n\n// Simulate Heroku environment variable\nprocess.env.HEROKU_POSTGRESQL_COPPER_URL = 'pgsql://user:pass@127.0.0.1:5432/my_db?sslmode=verify-full&application_name=myapp';\n\nconsole.log('--- Parsing DSN ---');\n// Parse the DSN string\nconst dsn = new DSNParser(process.env.HEROKU_POSTGRESQL_COPPER_URL);\nconst config = dsn.getParts();\n\nconsole.log('Parsed DSN configuration:', config);\n/*\nExample output:\n{\n  driver: 'pgsql',\n  user: 'user',\n  password: 'pass',\n  host: '127.0.0.1',\n  port: 5432,\n  database: 'my_db',\n  params: { sslmode: 'verify-full', application_name: 'myapp' }\n}\n*/\n\n// This config object can then be used with database clients, e.g., node-postgres\n// try {\n//   const pool = new pg.Pool(config);\n//   console.log('PG Pool config prepared. First client connect:', await pool.connect());\n//   await pool.end();\n// } catch (error) {\n//   console.error('Failed to connect with pg:', error.message);\n// }\n\nconsole.log('\\n--- Building DSN ---');\n// Modify and rebuild a DSN\nconst newDsn = new DSNParser();\nnewDsn.set('driver', 'mysql')\n    .set('user', 'root')\n    .set('password', 'mysecurepass')\n    .set('host', 'localhost')\n    .set('port', 3306)\n    .set('database', 'my_application_db')\n    .set('params', {\n        charset: 'utf8mb4',\n        timezone: 'Z'\n    });\n\nconsole.log('Built DSN string:', newDsn.getDSN());\n// Expected output: mysql://root:mysecurepass@localhost:3306/my_application_db?charset=utf8mb4&timezone=Z\n","lang":"javascript","description":"Demonstrates parsing a database connection string from an environment variable and extracting its components for use with a client like `node-postgres`, then building a new DSN programmatically."},"warnings":[{"fix":"Ensure you use `const DSNParser = require('dsn-parser');` when working in a CommonJS environment. In an ESM environment, you might need `import DSNParser from 'dsn-parser';` which relies on Node.js's CJS interop, but direct named imports will fail.","message":"The `dsn-parser` package is implemented as a CommonJS module. Attempting to use `import` syntax without proper tooling (like a bundler or specific Node.js configuration) can lead to import errors in ES module contexts.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate if the existing functionality meets your current and future needs. For actively evolving database ecosystems or stricter security requirements, consider a more actively maintained alternative if available.","message":"The package has not been updated in over six years (last commit as of late 2023). While functional for its intended purpose, it may lack support for newer DSN formats, advanced parsing requirements, or security patches for potential vulnerabilities in its parsing logic.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"If in a CommonJS file, use `const DSNParser = require('dsn-parser');`. If in an ES module file, use `import DSNParser from 'dsn-parser';` and verify your build setup correctly handles CommonJS interop.","cause":"Attempting to use `import { DSNParser } from 'dsn-parser';` or incorrectly accessing the default export of a CommonJS module in an ES module context.","error":"TypeError: DSNParser is not a constructor"},{"fix":"Change the import statement to `import DSNParser from 'dsn-parser';` to use ES module syntax. Ensure your environment or bundler is configured to correctly handle CommonJS modules within an ESM project.","cause":"Trying to use `require('dsn-parser')` directly within an ES module (`.mjs` file or `type: 'module'` in `package.json`).","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}