Class: PostgresConnector
Defined in: src/connectors/postgres.ts:47
Secure PostgreSQL connector
Features:
- SQL injection prevention
- Connection timeouts
- SSL enforcement
- Read-only query patterns
- Secure error handling
Example
import { PostgresConnector } from '@freshguard/freshguard-core';
const connector = new PostgresConnector({
host: 'localhost', port: 5432,
database: 'analytics',
username: 'readonly_user',
password: process.env.DB_PASSWORD!,
ssl: true,
});
const ok = await connector.testConnection();
const tables = await connector.listTables();
Extends
BaseConnector
Constructors
Constructor
new PostgresConnector(
config,securityConfig?):PostgresConnector
Defined in: src/connectors/postgres.ts:57
Parameters
config
ConnectorConfig
Database connection settings (host, port, database, credentials).
Pass options.schema to target a specific schema (default: 'public').
securityConfig?
Partial<SecurityConfig>
Optional overrides for query timeouts, max rows, SSL, and blocked keywords
Returns
PostgresConnector
Overrides
BaseConnector.constructor
Properties
config
protectedconfig:ConnectorConfig
Defined in: src/connectors/base-connector.ts:63
Inherited from
BaseConnector.config
connectionTimeout
protectedreadonlyconnectionTimeout:number
Defined in: src/connectors/base-connector.ts:49
Inherited from
BaseConnector.connectionTimeout
enableDetailedLogging
protectedreadonlyenableDetailedLogging:boolean
Defined in: src/connectors/base-connector.ts:57
Inherited from
BaseConnector.enableDetailedLogging
enableQueryAnalysis
protectedreadonlyenableQueryAnalysis:boolean
Defined in: src/connectors/base-connector.ts:58
Inherited from
BaseConnector.enableQueryAnalysis
logger
protectedreadonlylogger:StructuredLogger
Defined in: src/connectors/base-connector.ts:53
Inherited from
BaseConnector.logger
maxRows
protectedreadonlymaxRows:number
Defined in: src/connectors/base-connector.ts:51
Inherited from
BaseConnector.maxRows
metrics
protectedreadonlymetrics:MetricsCollector
Defined in: src/connectors/base-connector.ts:54
Inherited from
BaseConnector.metrics
queryAnalyzer
protectedreadonlyqueryAnalyzer:QueryComplexityAnalyzer
Defined in: src/connectors/base-connector.ts:55
Inherited from
BaseConnector.queryAnalyzer
queryTimeout
protectedreadonlyqueryTimeout:number
Defined in: src/connectors/base-connector.ts:50
Inherited from
BaseConnector.queryTimeout
requireSSL
protectedreadonlyrequireSSL:boolean
Defined in: src/connectors/base-connector.ts:52
Inherited from
BaseConnector.requireSSL
schemaCache
protectedreadonlyschemaCache:SchemaCache
Defined in: src/connectors/base-connector.ts:56
Inherited from
BaseConnector.schemaCache
Methods
cleanup()
protectedcleanup():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()
protectedclearSchemaCache():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/postgres.ts:396
Close the database connection
Returns
Promise<void>
Overrides
BaseConnector.close
connectLegacy()
connectLegacy(
credentials):Promise<void>
Defined in: src/connectors/postgres.ts:455
Legacy connect method for backward compatibility
Parameters
credentials
Returns
Promise<void>
Deprecated
Use constructor with ConnectorConfig instead
createDebugErrorFactory()
protectedcreateDebugErrorFactory(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()
protectedescapeIdentifier(identifier):string
Defined in: src/connectors/base-connector.ts:475
Escape SQL identifiers to prevent injection
Parameters
identifier
string
Returns
string
Inherited from
BaseConnector.escapeIdentifier
executeParameterizedQuery()
protectedexecuteParameterizedQuery(sql,parameters?):Promise<QueryResultRow[]>
Defined in: src/connectors/postgres.ts:121
Execute a parameterized SQL query using prepared statements
Parameters
sql
string
parameters?
unknown[] = []
Returns
Promise<QueryResultRow[]>
Overrides
BaseConnector.executeParameterizedQuery
executeQuery()
protectedexecuteQuery(sql):Promise<QueryResultRow[]>
Defined in: src/connectors/postgres.ts:114
Execute a validated SQL query with security measures
Parameters
sql
string
Returns
Promise<QueryResultRow[]>
Overrides
BaseConnector.executeQuery
executeWithTimeout()
protectedexecuteWithTimeout<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/postgres.ts:349
Get last modified timestamp using PostgreSQL-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()
protectedgetQueryAnalyzerConfig():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()
protectedgetSchemaCacheStats():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/postgres.ts:509
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()
protectedgetTableMetadataFresh(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/postgres.ts:299
Get table schema information securely
Parameters
table
string
Returns
Promise<TableSchema>
Overrides
BaseConnector.getTableSchema
listTables()
listTables():
Promise<string[]>
Defined in: src/connectors/postgres.ts:272
List all tables in the public schema
Returns
Promise<string[]>
Overrides
BaseConnector.listTables