SQL Worker
The SQL Worker is a powerful, multi-database worker that can execute SQL queries and stored procedures against PostgreSQL, SQL Server, and MySQL databases. It uses Dapper for high-performance database operations with full parameterized query support.
Features
- Multi-database support (PostgreSQL, SQL Server, MySQL)
- Parameterized queries (SQL injection protection)
- Stored procedure execution
- Transaction support with configurable isolation levels
- Three query types (NonQuery, Scalar, Reader)
- Connection pooling via named connections
- Result set limiting
- Automatic JSON result formatting
Use Cases
| Scenario | Example |
|---|---|
| Scheduled Reports | Generate daily/weekly reports from database |
| Data Cleanup | Periodic cleanup of old records |
| Data Aggregation | Calculate statistics and store results |
| Batch Processing | Process queued records in batches |
| Database Maintenance | Run maintenance scripts on schedule |
| ETL Jobs | Extract, transform, load operations |
Security Model
For security, database connection strings are never included in job data. Instead:
- Worker Configuration: Connection strings are configured in the worker's
appsettings.json - Job Data: Jobs reference connections by alias name only
- UI Integration: Available connection names appear as job data definition
┌─────────────────────────────────────────────────────────┐
│ Worker appsettings.json │
│ ┌───────────────────────────────────────────────────┐ │
│ │ "ExecutorConfig": { │ │
│ │ "Connections": { │ │
│ │ "MainDatabase": { │ │
│ │ "ConnectionString": "Server=...;Pass=...", │ │ ← Secrets stay here
│ │ "Provider": "PostgreSql" │ │
│ │ } │ │
│ │ } │ │
│ │ } │ │
│ └───────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
▲
│ Only alias is used
│
┌─────────────────────────────────────────────────────────┐
│ Job Data (from API/UI) │
│ ┌───────────────────────────────────────────────────┐ │
│ │ { │ │
│ │ "connectionName": "MainDatabase", ← Alias │ │
│ │ "query": "SELECT * FROM Users WHERE Id = @Id", │ │
│ │ "parameters": { "Id": 123 } │ │
│ │ } │ │
│ └───────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
Worker Configuration
Configure database connections in the worker's appsettings.json:
{
"ExecutorConfig": {
"Connections": {
"MainDatabase": {
"ConnectionString": "Host=localhost;Port=5432;Database=mydb;Username=user;Password=secret;",
"Provider": "PostgreSql",
"DefaultTimeoutSeconds": 30
},
"ReportingDatabase": {
"ConnectionString": "Host=reporting-db;Port=5432;Database=reports;Username=report_user;Password=report_pass;",
"Provider": "PostgreSql",
"DefaultTimeoutSeconds": 60
},
"LegacySqlServer": {
"ConnectionString": "Server=sql-server;Database=LegacyDb;User Id=sa;Password=pass;TrustServerCertificate=true;",
"Provider": "SqlServer",
"DefaultTimeoutSeconds": 30
},
"MySqlDatabase": {
"ConnectionString": "Server=mysql-host;Database=mydb;User=root;Password=pass;",
"Provider": "MySql",
"DefaultTimeoutSeconds": 30
}
}
}
}
Connection Properties
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
ConnectionString | string |