@spidey52/config-manager
v1.0.1
Published
A TypeScript library for managing configuration data with pluggable storage backends, caching, and real-time watching.
Readme
@spidey52/config-manager
A TypeScript library for managing configuration data with pluggable storage backends, caching, and real-time watching.
Installation
npm install @spidey52/config-manager mongodbNote: mongodb is a peer dependency and must be installed separately.
Usage
File-based Storage
import { ConfigClient } from "@spidey52/config-manager";
const client = new ConfigClient({ store: "file", file: "config.json" });
// Set a config value
await client.set("database.url", "mongodb://localhost:27017");
// Get a config value
const url = await client.get("database.url");
console.log(url); // { key: 'database.url', value: 'mongodb://localhost:27017', timestamp: ..., version: 1 }
// List configs with a prefix
const configs = await client.list("database.");
console.log(configs); // Array of ConfigEntryMongoDB Storage
import { MongoClient } from "mongodb";
import { ConfigClient } from "@spidey52/config-manager";
const mongoClient = new MongoClient("mongodb://localhost:27017");
await mongoClient.connect();
const collection = mongoClient.db("config").collection("configs");
const client = new ConfigClient({ store: "mongodb", collection });
// Use similar to file example
await client.set("app.port", 3000);
const port = await client.get("app.port");With Caching
const client = new ConfigClient({
store: "file",
file: "config.json",
cache: "memory",
ttl: 60000, // 1 minute TTL
});With Key Prefix
const client = new ConfigClient({
store: "file",
file: "config.json",
prefix: "app.", // All keys will be prefixed with "app."
});
await client.set("port", 3000); // Stored as "app.port"
const port = await client.get("port"); // Retrieves "app.port", returns key as "port"
const allAppConfigs = await client.list(); // Lists all with "app." prefixWatching Changes
client.watch("url", (entry) => {
console.log("Config changed:", entry);
});
// Watch all changes
client.watchAll((entry) => {
console.log("Any config changed:", entry);
});API
get(key: string): Get a config entryset(key: string, value: any): Set a config valuelist(prefix?: string): List entries with optional prefixwatch(key, callback): Watch a specific keywatchPrefix(prefix, callback): Watch keys starting with prefixwatchMany(keys, callback): Watch multiple keyswatchAll(callback): Watch all changes
License
ISC
