mssql: add all mssql options to Knex.ConnectionConfig (#4281)

Co-authored-by: Igor Savin <iselwin@gmail.com>
This commit is contained in:
Tyler Watson 2021-02-09 00:57:27 +10:00 committed by GitHub
parent 511ea2be0f
commit b530eacd61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 180 additions and 6 deletions

View File

@ -37,7 +37,7 @@ class Client_MSSQL extends Client {
authentication: {
type: settings.type || 'default',
options: {
userName: settings.user,
userName: settings.userName || settings.user,
password: settings.password,
domain: settings.domain,
token: settings.token,
@ -47,7 +47,7 @@ class Client_MSSQL extends Client {
msiEndpoint: settings.msiEndpoint,
},
},
server: settings.host || settings.server,
server: settings.server || settings.host,
options: {
database: settings.database,
encrypt: settings.encrypt || false,

View File

@ -0,0 +1,66 @@
import { expectAssignable, expectType } from 'tsd';
import { Knex } from '../types';
const azureActiveDirectoryConfig: Knex.MsSqlAzureActiveDirectoryAccessTokenAuthenticationConfig = {
type: 'azure-active-directory-access-token',
token: 'test',
server: 'test',
database: 'test'
}
const azureActiveDirectoryMsiConfig: Knex.MsSqlAzureActiveDirectoryMsiAppServiceAuthenticationConfig = {
type: 'azure-active-directory-msi-app-service',
database: '',
server: '',
clientId: '',
msiEndpoint: '',
msiSecret: ''
}
const azureActiveDirectoryMsiVmConfig: Knex.MsSqlAzureActiveDirectoryMsiVmAuthenticationConfig = {
type: 'azure-active-directory-msi-vm',
database: '',
server: '',
clientId: '',
msiEndpoint: 'test',
}
const azureActiveDirectoryPasswordConfig: Knex.MsSqlAzureActiveDirectoryPasswordAuthenticationConfig = {
type: 'azure-active-directory-password',
database: '',
server: '',
domain: '',
password: '',
userName: '',
}
const azureActiveDirectoryPrincipalConfig: Knex.MsSqlAzureActiveDirectoryServicePrincipalSecretConfig = {
clientId: '',
clientSecret: '',
database: '',
server: '',
tenantId: '',
type: 'azure-active-directory-service-principal-secret',
}
const defaultConfig: Knex.MsSqlDefaultAuthenticationConfig = {
type: 'default',
database: '',
server: '',
userName: '',
password: '',
}
// Assert that no type property works and assumes default
const connectionConfig: Knex.MsSqlConnectionConfig = {
server: '',
database: '',
userName: '',
password: ''
};
expectAssignable<Knex.MsSqlConnectionConfigBase>({
server: '',
database: '',
});

116
types/index.d.ts vendored
View File

@ -1927,12 +1927,120 @@ export declare namespace Knex {
requestTimeout?: number;
}
// Config object for mssql: see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mssql/index.d.ts
interface MsSqlConnectionConfig {
type MsSqlAuthenticationTypeOptions =
| 'default'
| 'ntlm'
| 'azure-active-directory-password'
| 'azure-active-directory-access-token'
| 'azure-active-directory-msi-vm'
| 'azure-active-directory-msi-app-service'
| 'azure-active-directory-service-principal-secret';
interface MsSqlDefaultAuthenticationConfig extends MsSqlConnectionConfigBase {
type?: 'default' | never;
}
interface MsSqlAzureActiveDirectoryMsiAppServiceAuthenticationConfig
extends MsSqlConnectionConfigBase {
type: 'azure-active-directory-msi-app-service';
/**
* If you user want to connect to an Azure app service using a specific client account
* they need to provide `clientId` asscoiate to their created idnetity.
*
* This is optional for retrieve token from azure web app service
*/
clientId?: string;
/**
* A msi app service environment need to provide `msiEndpoint` for retriving the accesstoken.
*/
msiEndpoint?: string;
/**
* A msi app service environment need to provide `msiSecret` for retriving the accesstoken.
*/
msiSecret?: string;
}
interface MsSqlAzureActiveDirectoryMsiVmAuthenticationConfig
extends MsSqlConnectionConfigBase {
type: 'azure-active-directory-msi-vm';
/**
* If you user want to connect to an Azure app service using a specific client account
* they need to provide `clientId` asscoiate to their created idnetity.
*
* This is optional for retrieve token from azure web app service
*/
clientId?: string;
/**
* A user need to provide `msiEndpoint` for retriving the accesstoken.
*/
msiEndpoint?: string;
}
interface MsSqlAzureActiveDirectoryAccessTokenAuthenticationConfig
extends MsSqlConnectionConfigBase {
type: 'azure-active-directory-access-token';
/**
* A user-provided access token
*/
token: string;
}
interface MsSqlAzureActiveDirectoryPasswordAuthenticationConfig
extends MsSqlConnectionConfigBase {
type: 'azure-active-directory-password';
/**
* Optional parameter for specific Azure tenant ID
*/
domain: string;
userName: string;
password: string;
}
interface MsSqlAzureActiveDirectoryServicePrincipalSecretConfig
extends MsSqlConnectionConfigBase {
type: 'azure-active-directory-service-principal-secret';
/**
* Application (`client`) ID from your registered Azure application
*/
clientId: string;
/**
* The created `client secret` for this registered Azure application
*/
clientSecret: string;
/**
* Directory (`tenant`) ID from your registered Azure application
*/
tenantId: string;
}
interface MsSqlNtlmAuthenticationConfig extends MsSqlConnectionConfigBase {
type: 'ntlm';
/**
* Once you set domain for ntlm authentication type, driver will connect to SQL Server using domain login.
*
* This is necessary for forming a connection using ntlm type
*/
domain: string;
userName: string;
password: string;
}
type MsSqlConnectionConfig =
| MsSqlDefaultAuthenticationConfig
| MsSqlNtlmAuthenticationConfig
| MsSqlAzureActiveDirectoryAccessTokenAuthenticationConfig
| MsSqlAzureActiveDirectoryMsiAppServiceAuthenticationConfig
| MsSqlAzureActiveDirectoryMsiVmAuthenticationConfig
| MsSqlAzureActiveDirectoryPasswordAuthenticationConfig
| MsSqlAzureActiveDirectoryServicePrincipalSecretConfig;
// Config object for tedious: see http://tediousjs.github.io/tedious/api-connection.html
interface MsSqlConnectionConfigBase {
type?: MsSqlAuthenticationTypeOptions;
driver?: string;
user?: string;
userName?: string; // equivalent to knex "user"
password?: string;
server: string;
server: string; // equivalent to knex "host"
port?: number;
domain?: string;
database: string;