Skip to content

Commit 423cca4

Browse files
committed
docs: update readme for latest usage
1 parent 41dbc3d commit 423cca4

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

README.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,108 @@ The library also provides the ability to parse SQL Connection Strings.
1111

1212
## Parsing connection strings
1313

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

1717
```js
18-
const { parseConnectionString } = require('@tediousjs/connection-string');
18+
const { parse } = require('@tediousjs/connection-string');
1919

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

22-
const parsed = parseConnectionString(connectionString);
22+
const parsed = parse(connectionString);
2323

2424
console.log(parsed);
2525
```
2626

2727
Output to the console will be:
2828

29-
```json
30-
{
31-
"User id": "user",
32-
"password": "password",
33-
"initial catalog": "AdventureWorks",
34-
"server": "MySqlServer"
29+
```
30+
Map(4) {
31+
'user id' => 'user',
32+
'password' => 'password',
33+
'initial catalog' => 'AdventureWorks',
34+
'server' => 'MySqlServer'
3535
}
3636
```
3737

3838
## Parsing SQL connection strings
3939

40-
There is a specific helper for parsing SQL connection strings and this comes with a value normaliser and validation. It also has an
41-
option to "canonicalise" the properties. For many properties in an SQL connections string, there are aliases, when canonical properties
42-
are being used, these aliases will be returned as the canonical property.
40+
SQL connection strings can be parsed to a JSON object using the `toSchema()` method and the provided
41+
`MSSQL_SCHEMA`.
4342

4443
```js
45-
const { parseSqlConnectionString } = require('@tediousjs/connection-string');
44+
const { parse, MSSQL_SCHEMA } = require('@tediousjs/connection-string');
4645

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

49-
const parsed = parseSqlConnectionString(connectionString, true);
48+
const parsed = parse(connectionString);
5049

51-
console.log(parsed);
50+
console.log(parsed.toSchema(MSSQL_SCHEMA));
5251
```
5352

5453
Output to console will be:
5554

5655
```json
5756
{
58-
"user id": "user",
59-
"password": "password",
57+
"data source": "MySqlServer",
6058
"initial catalog": "AdventureWorks",
61-
"data source": "MySqlServer"
59+
"password": "password",
60+
"user id":"user"
6261
}
6362
```
6463

6564
NB: The `Server` property from the connection string has been re-written to the value `Data Source`
65+
66+
## Custom schemas
67+
68+
If you need to parse a connection string into a custom schema, the format is as follows:
69+
70+
```ts
71+
import { parse } from '@tediousjs/connection-string';
72+
73+
// a keyed map of name => config
74+
const schema = {
75+
'a string': {
76+
type: 'string',
77+
default: 'a default value',
78+
aliases: ['other', 'allowed', 'names'],
79+
},
80+
'a number': {
81+
type: 'number',
82+
default: 123,
83+
},
84+
'a boolean': {
85+
type: 'boolean',
86+
default: true,
87+
},
88+
};
89+
90+
const parsed = parse('a string=test;a number=987;a boolean=false;other value=missing');
91+
console.log(parsed.toSchema(schema));
92+
```
93+
94+
Output:
95+
96+
```json
97+
{
98+
"a string": "test",
99+
"a number": 987,
100+
"a boolean": false
101+
}
102+
```
103+
104+
## Accessing properties
105+
106+
The parsed connection string object is a readonly `Map` with an overloadded `get()` method allowing
107+
coercion of the value:
108+
109+
```ts
110+
import { parse } from '@tediousjs/connection-string';
111+
const parsed = parse('a string=test;a number=987;a boolean=false;other value=missing');
112+
// all values are strings by default
113+
console.log(parsed.get('a number')); // "987"
114+
// values can be coersed to an expected type
115+
console.log(parsed.get('a number', 'number')); // 987
116+
// coersion will be permissive in its type coersion
117+
console.log(parsed.get('a number', 'boolean')); // true
118+
```

0 commit comments

Comments
 (0)