Détail du package

@tediousjs/connection-string

tediousjs4mMIT1.0.0

SQL ConnectionString parser

mssql, tsql, connectionstring

readme

Connection String Parser

npm version Lint, Test & Release

This node library is designed to allow the parsing of Connection Strings see https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring

The library also provides the ability to parse SQL Connection Strings.

Usage

Parsing connection strings

The library comes with a generic connection string parser that will parse through valid connection strings and produce a key-value readonly Map of the entries in that string. No additional validation is performed.

const { parse } = require('@tediousjs/connection-string');

const connectionString = 'User ID=user;Password=password;Initial Catalog=AdventureWorks;Server=MySqlServer';

const parsed = parse(connectionString);

console.log(parsed);

Output to the console will be:

Map(4) {
  'user id' => 'user',
  'password' => 'password',
  'initial catalog' => 'AdventureWorks',
  'server' => 'MySqlServer'
}

Parsing SQL connection strings

SQL connection strings can be parsed to a JSON object using the toSchema() method and the provided MSSQL_SCHEMA.

const { parse, MSSQL_SCHEMA } = require('@tediousjs/connection-string');

const connectionString = 'User ID=user;Password=password;Initial Catalog=AdventureWorks;Server=MySqlServer';

const parsed = parse(connectionString);

console.log(parsed.toSchema(MSSQL_SCHEMA));

Output to console will be:

{
  "data source": "MySqlServer",
  "initial catalog": "AdventureWorks",
  "password": "password",
  "user id":"user"
}

NB: The Server property from the connection string has been re-written to the value Data Source

Custom schemas

If you need to parse a connection string into a custom schema, the format is as follows:

import { parse } from '@tediousjs/connection-string';

// a keyed map of name => config
const schema = {
    'a string': {
        type: 'string',
        default: 'a default value',
        aliases: ['other', 'allowed', 'names'],
    },
    'a number': {
        type: 'number',
        default: 123,
    },
    'a boolean': {
        type: 'boolean',
        default: true,
    },
};

const parsed = parse('a string=test;a number=987;a boolean=false;other value=missing');
console.log(parsed.toSchema(schema));

Output:

{
  "a string": "test",
  "a number": 987,
  "a boolean": false
}

Accessing properties

The parsed connection string object is a readonly Map with an overloadded get() method allowing coercion of the value:

import { parse } from '@tediousjs/connection-string';
const parsed = parse('a string=test;a number=987;a boolean=false;other value=missing');
// all values are strings by default
console.log(parsed.get('a number')); // "987"
// values can be coersed to an expected type
console.log(parsed.get('a number', 'number')); // 987
// coersion will be permissive in its type coersion
console.log(parsed.get('a number', 'boolean')); // true

changelog

Changelog

1.0.0 (2025-06-08)

⚠ BREAKING CHANGES

  • Replace connectionStringParser() with parse() method. Rename buildConnectionString() to build(). Remove parseSqlConnectionString and replace with a way to transform parsed connection strings into a specific object schema (see ConnectionString.toSchema()`).
  • Drop support for non-LTS Node.js versions and upgrade the typescript version to support the newer JS features.

Features

  • bump minimum node support to v20 & update typescript (5d8e090)
  • create new parser to return readonly map when parsing connecton strings (b8ee936)
  • refactor parsing interface (41dbc3d)

0.6.0 (2024-09-11)

Features

  • add more authentication options (cae1d20)

0.5.0 (2023-08-09)

Features

  • add connection string builder (369a63f)

0.4.4 (2023-08-02)

Bug Fixes

0.4.3 (2023-08-01)

Bug Fixes

0.4.2 (2023-08-01)

Bug Fixes

  • update return types for parsers (bb6393a)

v0.4.2 (2023-01-19)

  • Fix bug with parsing string values that start with numbers

v0.4.1 (2022-06-07)

  • Add missing type declarations

v0.4.0 (2022-04-27)

  • Add LICENSE file (MIT) #17
  • Improve library code / TS definitions #18
  • Dependency updates

v0.3.0 (2021-01-21)

  • Allow parsed SQL connection strings to return unrecognised properties #4

v0.2.0 (2021-01-21)

  • Parsed query strings return lowercase keys #3

v0.1.0 (2021-01-21)

Initial release