Modular and extensible library for managing feature flags using Redis as storage. Based on Domain-Driven Design (DDD) principles and Hexagonal Architecture.
- ✅ Feature flag management: activate, deactivate, create, delete, retrieve.
- 🧱 Hexagonal architecture (ports and adapters).
- 🧠 Clear separation between domain, application, and infrastructure.
- 🔌 Redis storage (can be extended to others).
- 📦 Packaged as a library for reuse in multiple projects.
- 🧪 Tested with Vitest.
- ⚙️ Written in TypeScript and compatible with ESM/CJS.
npm install fflags-libWith the newly updated architecture, you can now instantiate the ManagerService by injecting the FeatureFlagRepository of your choice. You can use Redis, PostgreSQL, or MySQL.
First, choose the driver you want to use and instantiate its repository:
import { Redis } from 'ioredis';
import { ManagerService, RedisRepository } from "fflags-lib";
const redisClient = new Redis(); // defaults to localhost:6379
const repository = new RedisRepository(redisClient);
const managerService = ManagerService.getInstance(repository);Make sure you have created the required feature_flags table (check schema.sql) and that you have pg installed (npm i pg).
import { PostgresRepository, ManagerService } from "fflags-lib";
const repository = new PostgresRepository({
connectionString: "postgresql://user:password@localhost:5432/mydb"
});
const managerService = ManagerService.getInstance(repository);Make sure you have created the required feature_flags table (check schema.sql) and that you have mysql2 installed (npm i mysql2).
import { MysqlRepository, ManagerService } from "fflags-lib";
const repository = new MysqlRepository({
uri: "mysql://user:password@localhost:3306/mydb"
});
const managerService = ManagerService.getInstance(repository);Create a new feature flag with a key, an initial state (boolean), and a description.
// Creates a flag named 'new-feature', sets it to true, and adds a description
await managerService.createFlag('new-feature', true, 'Enables the new user dashboard');You can get a specific flag or list them based on their status.
// Get a specific flag by its key
const myFlag = await managerService.getFlag('new-feature');
// List all flags
const allFlags = await managerService.getAllFlags();
// List only active flags
const activeFlags = await managerService.getActivatedFlags();
// List only inactive flags
const inactiveFlags = await managerService.getInactiveFlags();You can toggle a flag's status between active and inactive.
// Deactivate the flag
await managerService.deactivateFlag('new-feature');
// Reactivate the flag
await managerService.activateFlag('new-feature');When a feature is fully rolled out or no longer needed, you can remove its flag.
// Delete the flag entirely
await managerService.deleteFlag('new-feature');Always remember to close the Redis connection safely when your application shuts down.
// Closes the Redis connection
await managerService.quit();npm run testTo use with your own paths or tools, check:
• tsconfig.json with configured baseUrl and paths.
• tsup.config.ts for bundling.
• vitest.config.ts for tests.
- Node.js 18+
- Redis 6+
- ioredis 5+