Skip to content

Commit d224cf1

Browse files
committed
Add MSSQL cert support options for knex
1 parent 0dfe6f1 commit d224cf1

File tree

8 files changed

+77
-20
lines changed

8 files changed

+77
-20
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ The configuration file should contain a single array of database connection obje
188188
{
189189
"type": "sqlite",
190190
"path": "/path/to/database.sqlite"
191+
},
192+
{
193+
"name": "My MSSQL database",
194+
"type": "mssql",
195+
"host": "localhost",
196+
"port": "1433",
197+
"username": "sa",
198+
"password": "YourPassword123",
199+
"database": "master",
200+
"options": {
201+
"trustServerCertificate": true
202+
}
191203
}
192204
]
193205
```

schemas/devdbrc.json

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
"oneOf": [
99
{
1010
"title": "SQLite Configuration",
11-
"required": ["type", "path"],
11+
"required": [
12+
"type",
13+
"path"
14+
],
1215
"properties": {
1316
"type": {
1417
"type": "string",
15-
"enum": ["sqlite"],
18+
"enum": [
19+
"sqlite"
20+
],
1621
"description": "Database type - SQLite"
1722
},
1823
"path": {
@@ -23,15 +28,23 @@
2328
},
2429
{
2530
"title": "MySQL/MariaDB Configuration",
26-
"required": ["type", "username", "password", "database"],
31+
"required": [
32+
"type",
33+
"username",
34+
"password",
35+
"database"
36+
],
2737
"properties": {
2838
"name": {
2939
"type": "string",
3040
"description": "Optional name for this connection"
3141
},
3242
"type": {
3343
"type": "string",
34-
"enum": ["mysql", "mariadb"],
44+
"enum": [
45+
"mysql",
46+
"mariadb"
47+
],
3548
"description": "Database type - MySQL or MariaDB"
3649
},
3750
"host": {
@@ -55,20 +68,32 @@
5568
"database": {
5669
"type": "string",
5770
"description": "Database name"
71+
},
72+
"options": {
73+
"type": "object",
74+
"description": "Driver-specific options for MSSQL, e.g. trustServerCertificate",
75+
"additionalProperties": true
5876
}
5977
}
6078
},
6179
{
6280
"title": "PostgreSQL Configuration",
63-
"required": ["type", "username", "password", "database"],
81+
"required": [
82+
"type",
83+
"username",
84+
"password",
85+
"database"
86+
],
6487
"properties": {
6588
"name": {
6689
"type": "string",
6790
"description": "Optional name for this connection"
6891
},
6992
"type": {
7093
"type": "string",
71-
"enum": ["postgres"],
94+
"enum": [
95+
"postgres"
96+
],
7297
"description": "Database type - PostgreSQL"
7398
},
7499
"host": {
@@ -97,15 +122,22 @@
97122
},
98123
{
99124
"title": "Microsoft SQL Server Configuration",
100-
"required": ["type", "username", "password", "database"],
125+
"required": [
126+
"type",
127+
"username",
128+
"password",
129+
"database"
130+
],
101131
"properties": {
102132
"name": {
103133
"type": "string",
104134
"description": "Optional name for this connection"
105135
},
106136
"type": {
107137
"type": "string",
108-
"enum": ["mssql"],
138+
"enum": [
139+
"mssql"
140+
],
109141
"description": "Database type - Microsoft SQL Server"
110142
},
111143
"host": {
@@ -134,4 +166,4 @@
134166
}
135167
]
136168
}
137-
}
169+
}

snippets/devdbrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
"\t\"port\": ${3:1433},",
6565
"\t\"username\": \"${4:sa}\",",
6666
"\t\"password\": \"${5:password}\",",
67-
"\t\"database\": \"${6:database_name}\"",
67+
"\t\"database\": \"${6:database_name}\",",
68+
"\t\"options\": {",
69+
"\t\t\"trustServerCertificate\": ${7:true}",
70+
"\t}",
6871
"}"
6972
],
7073
"description": "Add a new Microsoft SQL Server database configuration"

src/providers/config-file-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async function reportNameError(config: MysqlConfig | PostgresConfig | MssqlConfi
116116
}
117117

118118
async function mssqlConfigResolver(mssqlConfig: MssqlConfig): Promise<EngineProviderCache | undefined> {
119-
const connection = await getConnectionFor('Config file provider', 'mssql', mssqlConfig.host, mssqlConfig.port, mssqlConfig.username, mssqlConfig.password, mssqlConfig.database, false)
119+
const connection = await getConnectionFor('Config file provider', 'mssql', mssqlConfig.host, mssqlConfig.port, mssqlConfig.username, mssqlConfig.password, mssqlConfig.database, false, mssqlConfig.options)
120120
if (!connection) return
121121

122122
const engine: MssqlEngine = new MssqlEngine(connection)
@@ -208,4 +208,4 @@ async function postgresConfigResolver(postgresConfig: PostgresConfig): Promise<E
208208
type: 'postgres',
209209
engine: engine
210210
}
211-
}
211+
}

src/services/connector.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ import { log } from './logging-service';
44
import { logToOutput } from './output-service';
55
import { KnexClientType } from '../types';
66

7-
export async function getConnectionFor(description: string, dialect: KnexClientType, host: string, port: number, username: string, password: string, database: string | undefined = undefined, notifyOnError = true): Promise<knexlib.Knex | undefined> {
7+
export async function getConnectionFor(description: string, dialect: KnexClientType, host: string, port: number, username: string, password: string, database: string | undefined = undefined, notifyOnError = true, options?: Record<string, unknown>): Promise<knexlib.Knex | undefined> {
88

99
log(`Connector - ${description}`, `Attempting to connect to database: dialect=${dialect}, host=${host}, port=${port}, username=${username}, database=${database ? String(database[0]) + '*****' : '<not-provided>'}`);
1010

1111
try {
12+
const connection: any = {
13+
host: host ? String(host) : host,
14+
port: port ? Number(port) : port,
15+
user: username ? String(username) : username,
16+
password: password ? String(password) : password,
17+
database: database ? String(database) : database,
18+
};
19+
20+
// Add options to connection config for MSSQL
21+
if (dialect === 'mssql' && options) {
22+
connection.options = options;
23+
}
24+
1225
const knex = knexlib.knex({
1326
client: dialect,
14-
connection: {
15-
host: host ? String(host) : host,
16-
port: port ? Number(port) : port,
17-
user: username ? String(username) : username,
18-
password: password ? String(password) : password,
19-
database: database ? String(database) : database,
20-
},
27+
connection,
2128
});
2229

2330
return knex

src/test/suite/engines/mssql-certificate.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('MSSQL Certificate Connection Tests', () => {
1919
.withName('devdb-test-container-mssql-cert')
2020
.acceptLicense()
2121
.withPassword('yourStrong(!)Password')
22+
.withReuse()
2223
.start();
2324
});
2425

src/test/suite/services/sql.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('SqliteService Tests', () => {
2020

2121
before(async function () {
2222
container = await new MySqlContainer(dockerImage)
23+
.withName('devdb-test-container-for-general-sql-tests')
2324
.withReuse()
2425
.start();
2526
})

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export interface PostgresConfig extends SqlConfig {
182182

183183
export interface MssqlConfig extends SqlConfig {
184184
type: 'mssql'
185+
options?: Record<string, unknown>
185186
}
186187

187188
export type LaravelConnection = 'pgsql' | 'mysql'

0 commit comments

Comments
 (0)