Skip to main content
Version: 0.17

Class: MSSQLConnector

Defined in: src/connectors/mssql.ts:45

Secure SQL Server connector

Features:

  • SQL injection prevention
  • Connection timeouts
  • SSL enforcement
  • Read-only query patterns
  • Secure error handling
  • Connection pooling via mssql

Example

import { MSSQLConnector } from '@freshguard/freshguard-core';

const connector = new MSSQLConnector({
host: 'sql-server.example.com', port: 1433,
database: 'analytics',
username: 'readonly',
password: process.env.MSSQL_PASSWORD!,
ssl: true,
});

Extends

  • BaseConnector

Constructors

Constructor

new MSSQLConnector(config, securityConfig?): MSSQLConnector

Defined in: src/connectors/mssql.ts:55

Parameters

config

ConnectorConfig

Database connection settings (host, port, database, credentials). Pass options.schema to target a specific schema (default: 'dbo').

securityConfig?

Partial<SecurityConfig>

Optional overrides for query timeouts, max rows, SSL, and blocked keywords

Returns

MSSQLConnector

Overrides

BaseConnector.constructor

Properties

config

protected config: ConnectorConfig

Defined in: src/connectors/base-connector.ts:63

Inherited from

BaseConnector.config


connectionTimeout

protected readonly connectionTimeout: number

Defined in: src/connectors/base-connector.ts:49

Inherited from

BaseConnector.connectionTimeout


enableDetailedLogging

protected readonly enableDetailedLogging: boolean

Defined in: src/connectors/base-connector.ts:57

Inherited from

BaseConnector.enableDetailedLogging


enableQueryAnalysis

protected readonly enableQueryAnalysis: boolean

Defined in: src/connectors/base-connector.ts:58

Inherited from

BaseConnector.enableQueryAnalysis


logger

protected readonly logger: StructuredLogger

Defined in: src/connectors/base-connector.ts:53

Inherited from

BaseConnector.logger


maxRows

protected readonly maxRows: number

Defined in: src/connectors/base-connector.ts:51

Inherited from

BaseConnector.maxRows


metrics

protected readonly metrics: MetricsCollector

Defined in: src/connectors/base-connector.ts:54

Inherited from

BaseConnector.metrics


queryAnalyzer

protected readonly queryAnalyzer: QueryComplexityAnalyzer

Defined in: src/connectors/base-connector.ts:55

Inherited from

BaseConnector.queryAnalyzer


queryTimeout

protected readonly queryTimeout: number

Defined in: src/connectors/base-connector.ts:50

Inherited from

BaseConnector.queryTimeout


requireSSL

protected readonly requireSSL: boolean

Defined in: src/connectors/base-connector.ts:52

Inherited from

BaseConnector.requireSSL


schemaCache

protected readonly schemaCache: SchemaCache

Defined in: src/connectors/base-connector.ts:56

Inherited from

BaseConnector.schemaCache

Methods

cleanup()

protected cleanup(): void

Defined in: src/connectors/base-connector.ts:806

Cleanup resources (should be called by subclasses in their close method)

Returns

void

Inherited from

BaseConnector.cleanup


clearSchemaCache()

protected clearSchemaCache(): void

Defined in: src/connectors/base-connector.ts:792

Clear schema cache for testing or maintenance

Returns

void

Inherited from

BaseConnector.clearSchemaCache


close()

close(): Promise<void>

Defined in: src/connectors/mssql.ts:414

Close the database connection

Returns

Promise<void>

Overrides

BaseConnector.close


connectLegacy()

connectLegacy(credentials): Promise<void>

Defined in: src/connectors/mssql.ts:508

Legacy connect method for backward compatibility

Parameters

credentials

SourceCredentials

Returns

Promise<void>

Deprecated

Use constructor with ConnectorConfig instead


createDebugErrorFactory()

protected createDebugErrorFactory(debugConfig?): DebugErrorFactory

Defined in: src/connectors/base-connector.ts:843

Helper method for subclasses to create debug-enabled error factory

Parameters

debugConfig?

DebugConfig

Returns

DebugErrorFactory

Inherited from

BaseConnector.createDebugErrorFactory


escapeIdentifier()

protected escapeIdentifier(identifier): string

Defined in: src/connectors/mssql.ts:485

Override escapeIdentifier for SQL Server bracket notation

Parameters

identifier

string

Returns

string

Overrides

BaseConnector.escapeIdentifier


executeParameterizedQuery()

protected executeParameterizedQuery(sql, parameters?): Promise<QueryResultRow[]>

Defined in: src/connectors/mssql.ts:118

Execute a parameterized SQL query using prepared statements

Parameters

sql

string

parameters?

unknown[] = []

Returns

Promise<QueryResultRow[]>

Overrides

BaseConnector.executeParameterizedQuery


executeQuery()

protected executeQuery(sql): Promise<QueryResultRow[]>

Defined in: src/connectors/mssql.ts:111

Execute a validated SQL query with security measures

Parameters

sql

string

Returns

Promise<QueryResultRow[]>

Overrides

BaseConnector.executeQuery


executeWithTimeout()

protected executeWithTimeout<T>(operation, timeoutMs): Promise<T>

Defined in: src/connectors/base-connector.ts:492

Execute function with timeout protection

Type Parameters

T

T

Parameters

operation

() => Promise<T>

timeoutMs

number

Returns

Promise<T>

Inherited from

BaseConnector.executeWithTimeout


getLastModified()

getLastModified(table): Promise<Date | null>

Defined in: src/connectors/mssql.ts:369

Get last modified timestamp using SQL Server-specific methods

Parameters

table

string

Returns

Promise<Date | null>

Overrides

BaseConnector.getLastModified


getMaxTimestamp()

getMaxTimestamp(table, column): Promise<Date | null>

Defined in: src/connectors/base-connector.ts:606

Get maximum timestamp value from a column

Parameters

table

string

column

string

Returns

Promise<Date | null>

Inherited from

BaseConnector.getMaxTimestamp


getMinTimestamp()

getMinTimestamp(table, column): Promise<Date | null>

Defined in: src/connectors/base-connector.ts:674

Get minimum timestamp value from a column

Parameters

table

string

column

string

Returns

Promise<Date | null>

Inherited from

BaseConnector.getMinTimestamp


getQueryAnalyzerConfig()

protected getQueryAnalyzerConfig(): QueryAnalyzerConfig

Defined in: src/connectors/base-connector.ts:799

Get query analyzer configuration

Returns

QueryAnalyzerConfig

Inherited from

BaseConnector.getQueryAnalyzerConfig


getRowCount()

getRowCount(table): Promise<number>

Defined in: src/connectors/base-connector.ts:548

Get row count for a table using parameterized query

Parameters

table

string

Returns

Promise<number>

Inherited from

BaseConnector.getRowCount


getSchemaCacheStats()

protected getSchemaCacheStats(): CacheStats

Defined in: src/connectors/base-connector.ts:785

Get schema cache statistics

Returns

CacheStats

Inherited from

BaseConnector.getSchemaCacheStats


getTableMetadata()

getTableMetadata(tableName, timestampColumn?): Promise<{ lastUpdate?: Date; rowCount: number; }>

Defined in: src/connectors/mssql.ts:562

Legacy get table metadata method for backward compatibility

Parameters

tableName

string

timestampColumn?

string = 'updated_at'

Returns

Promise<{ lastUpdate?: Date; rowCount: number; }>

Deprecated

Use getRowCount() and getMaxTimestamp() instead


getTableMetadataFresh()

protected getTableMetadataFresh(tableName): Promise<TableMetadata | null>

Defined in: src/connectors/base-connector.ts:330

Get fresh table metadata (to be overridden by specific connectors)

Parameters

tableName

string

Returns

Promise<TableMetadata | null>

Inherited from

BaseConnector.getTableMetadataFresh


getTableSchema()

getTableSchema(table): Promise<TableSchema>

Defined in: src/connectors/mssql.ts:319

Get table schema information securely

Parameters

table

string

Returns

Promise<TableSchema>

Overrides

BaseConnector.getTableSchema


listTables()

listTables(): Promise<string[]>

Defined in: src/connectors/mssql.ts:289

List all tables in the database

Returns

Promise<string[]>

Overrides

BaseConnector.listTables


logDebugError()

protected logDebugError(debugConfig, debugId, operation, context): void

Defined in: src/connectors/base-connector.ts:860

Helper method for subclasses to log debug errors

Parameters

debugConfig

DebugConfig | undefined

debugId

string

operation

string

context

Record<string, unknown>

Returns

void

Inherited from

BaseConnector.logDebugError


logDebugInfo()

protected logDebugInfo(debugConfig, debugId, operation, context): void

Defined in: src/connectors/base-connector.ts:851

Helper method for subclasses to log debug information

Parameters

debugConfig

DebugConfig | undefined

debugId

string

operation

string

context

Record<string, unknown>

Returns

void

Inherited from

BaseConnector.logDebugInfo


logError()

protected logError(operation, error, context?): void

Defined in: src/connectors/base-connector.ts:774

Helper method for specific connectors to log errors with consistent format

Parameters

operation

string

error

Error

context?

LogContext

Returns

void

Inherited from

BaseConnector.logError


logOperation()

protected logOperation(operation, context): void

Defined in: src/connectors/base-connector.ts:763

Helper method for specific connectors to log operations with consistent format

Parameters

operation

string

context

LogContext

Returns

void

Inherited from

BaseConnector.logOperation


query()

query<T>(_sql): Promise<T[]>

Defined in: src/connectors/mssql.ts:591

Legacy query method for backward compatibility

Type Parameters

T

T = unknown

Parameters

_sql

string

Returns

Promise<T[]>

Deprecated

Direct SQL queries are not allowed for security reasons


sanitizeError()

protected sanitizeError(error): string

Defined in: src/connectors/base-connector.ts:509

Sanitize error messages to prevent information leakage

Parameters

error

unknown

Returns

string

Inherited from

BaseConnector.sanitizeError


testConnection()

testConnection(debugConfig?): Promise<boolean>

Defined in: src/connectors/mssql.ts:173

Test database connection with security validation

Parameters

debugConfig?

DebugConfig

Returns

Promise<boolean>

Overrides

BaseConnector.testConnection


testConnectionLegacy()

testConnectionLegacy(): Promise<{ error?: string; success: boolean; tableCount?: number; }>

Defined in: src/connectors/mssql.ts:531

Legacy test connection method for backward compatibility

Returns

Promise<{ error?: string; success: boolean; tableCount?: number; }>

Deprecated

Use testConnection() instead


validateQuery()

protected validateQuery(sql, tableNames?): Promise<void>

Defined in: src/connectors/base-connector.ts:153

Validate SQL query against security rules with enhanced analysis

Combines traditional pattern matching with advanced query complexity analysis

Parameters

sql

string

tableNames?

string[] = []

Returns

Promise<void>

Inherited from

BaseConnector.validateQuery


validateResultSize()

protected validateResultSize(results): void

Defined in: src/connectors/base-connector.ts:742

Validate that query results don't exceed max rows limit

Parameters

results

QueryResultRow[]

Returns

void

Inherited from

BaseConnector.validateResultSize