@@ -11,55 +11,108 @@ The library also provides the ability to parse SQL Connection Strings.
11
11
12
12
## Parsing connection strings
13
13
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.
16
16
17
17
``` js
18
- const { parseConnectionString } = require (' @tediousjs/connection-string' );
18
+ const { parse } = require (' @tediousjs/connection-string' );
19
19
20
20
const connectionString = ' User ID=user;Password=password;Initial Catalog=AdventureWorks;Server=MySqlServer' ;
21
21
22
- const parsed = parseConnectionString (connectionString);
22
+ const parsed = parse (connectionString);
23
23
24
24
console .log (parsed);
25
25
```
26
26
27
27
Output to the console will be:
28
28
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'
35
35
}
36
36
```
37
37
38
38
## Parsing SQL connection strings
39
39
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 ` .
43
42
44
43
``` js
45
- const { parseSqlConnectionString } = require (' @tediousjs/connection-string' );
44
+ const { parse , MSSQL_SCHEMA } = require (' @tediousjs/connection-string' );
46
45
47
46
const connectionString = ' User ID=user;Password=password;Initial Catalog=AdventureWorks;Server=MySqlServer' ;
48
47
49
- const parsed = parseSqlConnectionString (connectionString, true );
48
+ const parsed = parse (connectionString);
50
49
51
- console .log (parsed);
50
+ console .log (parsed . toSchema ( MSSQL_SCHEMA ) );
52
51
```
53
52
54
53
Output to console will be:
55
54
56
55
``` json
57
56
{
58
- "user id" : " user" ,
59
- "password" : " password" ,
57
+ "data source" : " MySqlServer" ,
60
58
"initial catalog" : " AdventureWorks" ,
61
- "data source" : " MySqlServer"
59
+ "password" : " password" ,
60
+ "user id" :" user"
62
61
}
63
62
```
64
63
65
64
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