Database-JS Common Utilities
The `database-js-common` package provides essential utility functions designed to support the implementation of drivers within the broader `database-js` ecosystem. Its primary function is the robust parsing of database connection strings, converting complex URL-like formats (e.g., `key=value&nested[key]=value`) into structured JavaScript objects. It includes optional automatic type conversion for common values like booleans and numbers. Currently at version 1.0.1, this package serves as a foundational internal component for `database-js` drivers. However, it's crucial to note that the main `database-js` project itself, which this package supports, was last published in June 2021 (version 3.0.11). This indicates that the entire `database-js` ecosystem, including this common utility module, is likely in an abandoned or unmaintained state, with no active development, regular release cadence, or ongoing support. Its key differentiator is its specialized connection string parsing logic tailored for the `database-js` framework, rather than being a general-purpose, actively maintained parsing library.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module (ESM) context without proper setup, or when the file is treated as an ESM module (e.g., `.mjs` extension or `"type": "module"` in `package.json`).fixEnsure your environment correctly treats the file as CommonJS, or use dynamic `import()` if you must import a CJS module from an ESM file: `const Common = await import('database-js-common'); Common.default.parseConnectionParameters(...)`. -
TypeError: Common.parseConnectionParameters is not a function
cause Incorrectly accessing the `parseConnectionParameters` function. It is a method of the object returned by `require('database-js-common')`.fixEnsure you are calling it as `Common.parseConnectionParameters(...)` after assigning the `require` result to a variable like `Common`.
Warnings
- breaking The `database-js` ecosystem, including `database-js-common`, appears to be unmaintained. The main `database-js` package has not been updated in approximately five years (last published June 2021). This implies a lack of security updates, bug fixes, or compatibility improvements for modern Node.js versions or JavaScript features.
- gotcha This package is implemented as a CommonJS (CJS) module. Attempting to use `import` statements (ECMAScript Modules, ESM) directly will result in errors in a pure ESM environment.
Install
-
npm install database-js-common -
yarn add database-js-common -
pnpm add database-js-common
Imports
- Common
import Common from 'database-js-common';
const Common = require('database-js-common'); - parseConnectionParameters
import { parseConnectionParameters } from 'database-js-common';const Common = require('database-js-common'); const params = Common.parseConnectionParameters(paramString);
Quickstart
const Common = require('database-js-common');
// Example connection string mimicking URL parameters
const connectionString1 = "host=localhost&port=3306&user=root&password=secret&database=mydb";
const parsedParams1 = Common.parseConnectionParameters(connectionString1);
console.log('Parsed simple parameters:', parsedParams1);
/* Expected output:
{
host: 'localhost',
port: '3306',
user: 'root',
password: 'secret',
database: 'mydb'
}*/
// Example with nested parameters and type conversion
const connectionString2 = "key1=value1&key2[subkey1]=true&key2[subkey2]=123.45&key3[]=itemA&key3[]=itemB";
const parsedParams2 = Common.parseConnectionParameters(connectionString2, true);
console.log('Parsed nested with type conversion:', parsedParams2);
/* Expected output:
{
key1: 'value1',
key2: { subkey1: true, subkey2: 123.45 },
key3: [ 'itemA', 'itemB' ]
}*/
// Demonstrate how to use the parsed parameters in a driver-like context
function createMockConnection(options) {
console.log('Creating connection with options:', options);
return { status: 'connected', config: options };
}
const driverConnectionConfig = {
Hostname: 'my-db-server',
Port: '5432',
Username: 'dbuser',
Password: process.env.DB_PASSWORD ?? 'default_password',
Database: 'app_data',
Parameters: "timeout=3000&ssl=true&poolSize=10"
};
const driverParams = Common.parseConnectionParameters(driverConnectionConfig.Parameters, true);
const finalConnection = createMockConnection({
host: driverConnectionConfig.Hostname || 'localhost',
port: parseInt(driverConnectionConfig.Port) || 5432,
user: driverConnectionConfig.Username || 'root',
password: driverConnectionConfig.Password,
database: driverConnectionConfig.Database,
parameters: driverParams
});
console.log('Final connection object:', finalConnection);