@nowarajs/singleton-manager
v1.4.2
Published
Singleton Manager is a package that provides a simple way to manage singletons in your application. It allows you to create, retrieve, and manage singletons with ease and type safety.
Maintainers
Readme
🎯 NowaraJS - Singleton Manager
Managing singletons in TypeScript shouldn't require boilerplate everywhere. I built this package to have a single, centralized registry for all my singleton instances, no more scattered getInstance() patterns or global variables.
Why this package?
The goal is simple: One registry to rule them all.
Instead of implementing the singleton pattern in every class, you register instances once and retrieve them anywhere. Type-safe, predictable, and easy to test.
📌 Table of Contents
✨ Features
- 🔒 Type-Safe: Full TypeScript support with generics, no
anycasting. - 🎯 Centralized: One place to manage all your singletons.
- ⚡ Lightweight: Minimal overhead, zero dependencies.
🔧 Installation
bun add @nowarajs/singleton-manager @nowarajs/error⚙️ Usage
Registering Singletons
Register your instances once at startup. They'll be available everywhere.
import { SingletonManager } from '@nowarajs/singleton-manager';
class DatabaseConnection {
private _isConnected = false;
public constructor() {
console.log('Database connection created');
this._isConnected = true;
}
public query(sql: string): string[] {
return ['result1', 'result2'];
}
}
class ApiClient {
public constructor(
private readonly _baseUrl: string,
private readonly _apiKey: string
) {}
public get baseUrl(): string {
return this._baseUrl;
}
}
// Register with any constructor signature
SingletonManager.register('DatabaseConnection', new DatabaseConnection());
SingletonManager.register('ApiClient', new ApiClient('https://api.example.com', 'key'));Retrieving Instances
Same instance, every time. TypeScript knows the type.
const db1 = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
const db2 = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
console.log(db1 === db2); // true — same reference
db1.query('SELECT * FROM users'); // ✅ Type-safeChecking & Unregistering
if (SingletonManager.has('ApiClient')) {
const client = SingletonManager.get<ApiClient>('ApiClient');
console.log(client.baseUrl);
}
// Need to swap an instance? Unregister first.
SingletonManager.unregister('DatabaseConnection');
SingletonManager.register('DatabaseConnection', new DatabaseConnection());📚 API Reference
Full docs: nowarajs.github.io/singleton-manager
⚖️ License
MIT — Feel free to use it.
📧 Contact
- Mail: [email protected]
- GitHub: NowaraJS
