Postgres Config Source
A powerful plugin forTs.ED to manage your application configuration in PostgreSQL and sync changes in real time.
✨ Features
- ⚙️Configure your PostgreSQL connection via the
@Configurationdecorator. - 👀Watch PostgreSQL table changes and auto-update your app config in real time (leveraging PostgreSQL LISTEN/NOTIFY).
- 🔄Sync configuration values: Use PostgreSQL as a dynamic source of truth for your app settings.
- 🛠️Flexible options: Supports all PostgreSQL client options, custom tables, and validation schemas.
For more details on theConfigSource feature, go toConfiguration Source documentation page.
📦 Installation
Install the package and its peer dependencies:
sh
npm install --save @tsedio/config-postgresnpm install --save @tsed/config pgsh
yarn add @tsedio/config-postgresyarn add @tsed/config pgsh
pnpm add @tsedio/config-postgrespnpm add @tsed/config pgsh
bun add @tsedio/config-postgresbun add @tsed/config pgWARNING
See our documentation page for instructions oninstalling premium plugins.
⚙️ Configuration Example
Set up your PostgreSQL config source in your Ts.ED application:
typescript
import {withOptions}from "@tsed/config";import {PostgresConfigSource}from "@tsedio/config-postgres";import {Configuration, Constant}from "@tsed/di";@Configuration({ extends: [ withOptions(PostgresConfigSource, { name:"postgres", connectionString:"postgresql://postgres:postgres@localhost:5432/my_database",// PostgreSQL connection string table:"config" // Table used for config storage // Additional PostgreSQL client options can be provided here // ConfigSource options // validationSchema: object({}) // Optional: add a validation schema }) ]})export class Server { @Constant("configs.postgres") config: Record<string,any>;}👀 Watching PostgreSQL Table Changes
Enable real-time watching of your configuration table to auto-sync changes with your application.
This uses PostgreSQL's LISTEN/NOTIFY feature.
typescript
@Configuration({ extends: [ withOptions(PostgresConfigSource, { name:"postgres", connectionString:"postgresql://postgres:postgres@localhost:5432/my_database", table:"config", watch:true // 👈 Enable watch mode for real-time config updates! }) ]})export class Server { @Constant("configs.postgres") config: Record<string,any>;}✏️ Set Configuration Values Programmatically
You can update configuration values in PostgreSQL directly from your services, thanks to dependency injection:
typescript
import {PostgresConfigSource}from "@tsedio/config-postgres";import {InjectConfigSource}from "@tsed/config/decorators/injectConfigSource.js";import {Injectable}from "@tsed/di";@Injectable()class MyService { @InjectConfigSource("postgres") config: PostgresConfigSource; async setValue(key: string,value: any) { await this.config.set(key, value); }}💡 Tips
- 🐘LISTEN/NOTIFY: Make sure your PostgreSQL deployment supports LISTEN/NOTIFY (most do by default).
- 🏷️Custom Tables: Use the
tableoption to separate config from your main application data. - 📚Validation: Add a
validationSchemato enforce structure and types for your configuration. - 🔐Multiple Sources: Use different
namevalues to manage multiple PostgreSQL config sources in the same app.