@weaverslab/feature-flags
v1.0.1
Published
Feature flags for CodeWeavers
Maintainers
Readme
@weaverslab/feature-flags
A robust feature flag management service for CodeWeavers applications. This library provides a simple and efficient way to manage feature flags in your application with PostgreSQL support and real-time updates.
Features
- 🚀 Real-time feature flag updates using PostgreSQL notifications
- 💾 LRU caching for improved performance
- 🔄 Event-based updates for seamless flag changes
- 📦 TypeScript support
- 🔒 Type-safe API
Installation
npm install @weaverslab/feature-flags
# or
yarn add @weaverslab/feature-flags
# or
pnpm add @weaverslab/feature-flagsPrerequisites
- PostgreSQL database
- Node.js 14 or higher
- TypeScript (for TypeScript projects)
Configuration
Set up your environment variables:
DATABASE_URL=postgresql://user:password@localhost:5432/databaseCreate the required database table:
CREATE TABLE hub_feature_flag ( name VARCHAR(255) PRIMARY KEY, enabled BOOLEAN NOT NULL DEFAULT false );Set up the notification trigger in PostgreSQL:
CREATE OR REPLACE FUNCTION notify_feature_flag_changes() RETURNS trigger AS $$ BEGIN PERFORM pg_notify('feature_flag_changes', NEW.name); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER feature_flag_changes_trigger AFTER UPDATE ON hub_feature_flag FOR EACH ROW EXECUTE FUNCTION notify_feature_flag_changes();
Usage
import { FeatureFlagService } from '@weaverslab/feature-flags'
// Initialize the service
const featureFlags = new FeatureFlagService()
// Check if a feature is enabled
if (featureFlags.isEnabled('my-feature')) {
// Feature is enabled
console.log('Feature is active!')
}
// Listen for feature flag updates
featureFlags.onUpdate(() => {
console.log('Feature flags have been updated!')
})
// Set database URL programmatically (optional)
FeatureFlagService.setDatabaseUrl('postgresql://user:password@localhost:5432/database')API Reference
FeatureFlagService
The main class for managing feature flags.
Methods
isEnabled(flagName: string): boolean- Checks if a specific feature flag is enabled
- Returns
falseif the flag doesn't exist
onUpdate(callback: () => void): void- Registers a callback to be called when feature flags are updated
static setDatabaseUrl(url: string): void- Sets the database URL programmatically
Database Schema
The library expects a PostgreSQL table with the following structure:
CREATE TABLE hub_feature_flag (
name VARCHAR(255) PRIMARY KEY,
enabled BOOLEAN NOT NULL DEFAULT false
);Performance Considerations
- The library uses an LRU cache with a maximum of 100 entries and a TTL of 60 seconds
- Real-time updates are handled through PostgreSQL notifications
- Database connections are managed efficiently using connection pooling
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT © CodeWeavers
