final config changes
This commit is contained in:
parent
718ace05b0
commit
19f05a2a69
7 changed files with 311 additions and 51 deletions
|
|
@ -15,15 +15,19 @@ import {
|
|||
} from './core';
|
||||
|
||||
import {
|
||||
// Database configuration
|
||||
// PostgreSQL configuration
|
||||
postgresConfig,
|
||||
PostgresConfig,
|
||||
POSTGRES_HOST,
|
||||
POSTGRES_PORT,
|
||||
POSTGRES_DATABASE,
|
||||
POSTGRES_USERNAME,
|
||||
POSTGRES_PASSWORD,
|
||||
// Backwards compatibility
|
||||
databaseConfig,
|
||||
DatabaseConfig,
|
||||
DB_HOST,
|
||||
DB_PORT,
|
||||
DB_NAME,
|
||||
DB_USER,
|
||||
DB_PASSWORD,
|
||||
} from './database';
|
||||
} from './postgres';
|
||||
|
||||
import {
|
||||
// QuestDB configuration
|
||||
|
|
@ -119,18 +123,17 @@ function basicUsageExample() {
|
|||
// Get the current environment
|
||||
const env = getEnvironment();
|
||||
console.log(`Current environment: ${env}`);
|
||||
|
||||
// Access individual configuration values
|
||||
console.log(`Database host: ${DB_HOST}`);
|
||||
console.log(`Database port: ${DB_PORT}`);
|
||||
// Access individual configuration values
|
||||
console.log(`Database host: ${POSTGRES_HOST}`);
|
||||
console.log(`Database port: ${POSTGRES_PORT}`);
|
||||
console.log(`Log level: ${LOG_LEVEL}`);
|
||||
|
||||
// Access full configuration objects
|
||||
// Access full database config objects
|
||||
console.log(`Full database config:`, {
|
||||
host: databaseConfig.DB_HOST,
|
||||
port: databaseConfig.DB_PORT,
|
||||
name: databaseConfig.DB_NAME,
|
||||
ssl: databaseConfig.DB_SSL,
|
||||
host: postgresConfig.POSTGRES_HOST,
|
||||
port: postgresConfig.POSTGRES_PORT,
|
||||
name: postgresConfig.POSTGRES_DATABASE,
|
||||
ssl: postgresConfig.POSTGRES_SSL,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -142,27 +145,27 @@ async function databaseConnectionExample() {
|
|||
|
||||
try {
|
||||
// Use the database configuration to create a connection string
|
||||
const connectionString = `postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`;
|
||||
const connectionString = `postgresql://${POSTGRES_USERNAME}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DATABASE}`;
|
||||
|
||||
console.log('Database connection settings:');
|
||||
console.log(`- Host: ${databaseConfig.DB_HOST}`);
|
||||
console.log(`- Port: ${databaseConfig.DB_PORT}`);
|
||||
console.log(`- Database: ${databaseConfig.DB_NAME}`);
|
||||
console.log(`- SSL enabled: ${databaseConfig.DB_SSL}`);
|
||||
console.log(`- Pool max connections: ${databaseConfig.DB_POOL_MAX}`);
|
||||
console.log(`- Query timeout: ${databaseConfig.DB_QUERY_TIMEOUT}ms`);
|
||||
console.log(`- Host: ${databaseConfig.POSTGRES_HOST}`);
|
||||
console.log(`- Port: ${databaseConfig.POSTGRES_PORT}`);
|
||||
console.log(`- Database: ${databaseConfig.POSTGRES_NAME}`);
|
||||
console.log(`- SSL enabled: ${databaseConfig.POSTGRES_SSL}`);
|
||||
console.log(`- Pool max connections: ${databaseConfig.POSTGRES_POOL_MAX}`);
|
||||
console.log(`- Query timeout: ${databaseConfig.POSTGRES_QUERY_TIMEOUT}ms`);
|
||||
|
||||
// Example pool configuration
|
||||
const poolConfig = {
|
||||
host: databaseConfig.DB_HOST,
|
||||
port: databaseConfig.DB_PORT,
|
||||
database: databaseConfig.DB_NAME,
|
||||
user: databaseConfig.DB_USER,
|
||||
password: databaseConfig.DB_PASSWORD,
|
||||
ssl: databaseConfig.DB_SSL,
|
||||
min: databaseConfig.DB_POOL_MIN,
|
||||
max: databaseConfig.DB_POOL_MAX,
|
||||
idleTimeoutMillis: databaseConfig.DB_POOL_IDLE_TIMEOUT,
|
||||
host: databaseConfig.POSTGRES_HOST,
|
||||
port: databaseConfig.POSTGRES_PORT,
|
||||
database: databaseConfig.POSTGRES_NAME,
|
||||
user: databaseConfig.POSTGRES_USER,
|
||||
password: databaseConfig.POSTGRES_PASSWORD,
|
||||
ssl: databaseConfig.POSTGRES_SSL,
|
||||
min: databaseConfig.POSTGRES_POOL_MIN,
|
||||
max: databaseConfig.POSTGRES_POOL_MAX,
|
||||
idleTimeoutMillis: databaseConfig.POSTGRES_POOL_IDLE_TIMEOUT,
|
||||
};
|
||||
|
||||
console.log('Pool configuration:', poolConfig);
|
||||
|
|
@ -375,7 +378,7 @@ function configurationValidationExample() {
|
|||
}
|
||||
|
||||
// Validate database connection settings
|
||||
if (databaseConfig.DB_POOL_MAX < databaseConfig.DB_POOL_MIN) {
|
||||
if (databaseConfig.POSTGRES_POOL_MAX < databaseConfig.POSTGRES_POOL_MIN) {
|
||||
throw new ConfigurationError('Database max pool size must be greater than min pool size');
|
||||
}
|
||||
|
||||
|
|
@ -626,16 +629,16 @@ function multiDatabaseServiceExample() {
|
|||
|
||||
// PostgreSQL for operational data
|
||||
postgresql: {
|
||||
host: databaseConfig.DB_HOST,
|
||||
port: databaseConfig.DB_PORT,
|
||||
database: databaseConfig.DB_NAME,
|
||||
username: databaseConfig.DB_USER,
|
||||
password: databaseConfig.DB_PASSWORD,
|
||||
ssl: databaseConfig.DB_SSL,
|
||||
host: databaseConfig.POSTGRES_HOST,
|
||||
port: databaseConfig.POSTGRES_PORT,
|
||||
database: databaseConfig.POSTGRES_NAME,
|
||||
username: databaseConfig.POSTGRES_USER,
|
||||
password: databaseConfig.POSTGRES_PASSWORD,
|
||||
ssl: databaseConfig.POSTGRES_SSL,
|
||||
pool: {
|
||||
min: databaseConfig.DB_POOL_MIN,
|
||||
max: databaseConfig.DB_POOL_MAX,
|
||||
idleTimeout: databaseConfig.DB_POOL_IDLE_TIMEOUT,
|
||||
min: databaseConfig.POSTGRES_POOL_MIN,
|
||||
max: databaseConfig.POSTGRES_POOL_MAX,
|
||||
idleTimeout: databaseConfig.POSTGRES_POOL_IDLE_TIMEOUT,
|
||||
},
|
||||
},
|
||||
|
||||
|
|
@ -712,10 +715,10 @@ function serviceConfigurationExample() {
|
|||
environment: getEnvironment(),
|
||||
},
|
||||
database: {
|
||||
host: databaseConfig.DB_HOST,
|
||||
port: databaseConfig.DB_PORT,
|
||||
name: databaseConfig.DB_NAME,
|
||||
ssl: databaseConfig.DB_SSL,
|
||||
host: databaseConfig.POSTGRES_HOST,
|
||||
port: databaseConfig.POSTGRES_PORT,
|
||||
name: databaseConfig.POSTGRES_NAME,
|
||||
ssl: databaseConfig.POSTGRES_SSL,
|
||||
}, logging: {
|
||||
level: loggingConfig.LOG_LEVEL,
|
||||
console: loggingConfig.LOG_CONSOLE,
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
export * from './core';
|
||||
|
||||
// Database configurations
|
||||
export * from './database';
|
||||
export * from './postgres';
|
||||
export * from './questdb';
|
||||
export * from './mongodb';
|
||||
export * from './dragonfly';
|
||||
|
|
|
|||
54
libs/config/src/postgres.ts
Normal file
54
libs/config/src/postgres.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* PostgreSQL configuration using envalid
|
||||
*/
|
||||
import { cleanEnv, str, port, bool, num } from 'envalid';
|
||||
|
||||
/**
|
||||
* PostgreSQL configuration with validation and defaults
|
||||
*/
|
||||
export const postgresConfig = cleanEnv(process.env, {
|
||||
// PostgreSQL Connection Settings
|
||||
POSTGRES_HOST: str({ default: 'localhost', desc: 'PostgreSQL host' }),
|
||||
POSTGRES_PORT: port({ default: 5432, desc: 'PostgreSQL port' }),
|
||||
POSTGRES_DATABASE: str({ default: 'stockbot', desc: 'PostgreSQL database name' }),
|
||||
POSTGRES_USERNAME: str({ default: 'stockbot', desc: 'PostgreSQL username' }),
|
||||
POSTGRES_PASSWORD: str({ default: '', desc: 'PostgreSQL password' }),
|
||||
|
||||
// Connection Pool Settings
|
||||
POSTGRES_POOL_MIN: num({ default: 2, desc: 'Minimum pool connections' }),
|
||||
POSTGRES_POOL_MAX: num({ default: 10, desc: 'Maximum pool connections' }),
|
||||
POSTGRES_POOL_IDLE_TIMEOUT: num({ default: 30000, desc: 'Pool idle timeout in ms' }),
|
||||
|
||||
// SSL Configuration
|
||||
POSTGRES_SSL: bool({ default: false, desc: 'Enable SSL for PostgreSQL connection' }),
|
||||
POSTGRES_SSL_REJECT_UNAUTHORIZED: bool({ default: true, desc: 'Reject unauthorized SSL certificates' }),
|
||||
|
||||
// Additional Settings
|
||||
POSTGRES_QUERY_TIMEOUT: num({ default: 30000, desc: 'Query timeout in ms' }),
|
||||
POSTGRES_CONNECTION_TIMEOUT: num({ default: 5000, desc: 'Connection timeout in ms' }),
|
||||
POSTGRES_STATEMENT_TIMEOUT: num({ default: 30000, desc: 'Statement timeout in ms' }),
|
||||
POSTGRES_LOCK_TIMEOUT: num({ default: 10000, desc: 'Lock timeout in ms' }),
|
||||
POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT: num({ default: 60000, desc: 'Idle in transaction timeout in ms' }),
|
||||
});
|
||||
|
||||
// Export typed configuration object
|
||||
export type PostgresConfig = typeof postgresConfig;
|
||||
|
||||
// Export individual config values for convenience
|
||||
export const {
|
||||
POSTGRES_HOST,
|
||||
POSTGRES_PORT,
|
||||
POSTGRES_DATABASE,
|
||||
POSTGRES_USERNAME,
|
||||
POSTGRES_PASSWORD,
|
||||
POSTGRES_POOL_MIN,
|
||||
POSTGRES_POOL_MAX,
|
||||
POSTGRES_POOL_IDLE_TIMEOUT,
|
||||
POSTGRES_SSL,
|
||||
POSTGRES_SSL_REJECT_UNAUTHORIZED,
|
||||
POSTGRES_QUERY_TIMEOUT,
|
||||
POSTGRES_CONNECTION_TIMEOUT,
|
||||
POSTGRES_STATEMENT_TIMEOUT,
|
||||
POSTGRES_LOCK_TIMEOUT,
|
||||
POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
|
||||
} = postgresConfig;
|
||||
Loading…
Add table
Add a link
Reference in a new issue