A powerful and type-safe singleton manager for TypeScript/JavaScript applications.
Singleton Manager provides a centralized way to manage singleton instances in your application. It ensures that only one instance of each registered class exists throughout the application lifecycle, with full TypeScript support and type safety.
bun add @nowarajs/singleton-manager @nowarajs/error
import { SingletonManager } from '@nowarajs/singleton-manager';
// Define your singleton classes
class DatabaseConnection {
private _isConnected = false;
public constructor() {
console.log('Database connection created');
this._isConnected = true;
}
public query(sql: string): string[] {
console.log(`Executing query: ${sql}`);
return ['result1', 'result2'];
}
}
class ApiClient {
public constructor(
private readonly _baseUrl: string,
private readonly _apiKey: string
) {}
public get baseUrl(): string {
return this._baseUrl;
}
}
// Register singletons (with or without constructor parameters)
SingletonManager.register('DatabaseConnection', new DatabaseConnection());
SingletonManager.register('ApiClient', new ApiClient('https://api.example.com', 'key'));
// Get singleton instances (same instance every time)
const db1 = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
const db2 = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
console.log(db1 === db2); // true
// TypeScript provides full type safety
db1.query('SELECT * FROM users'); // ✅ Works
// db1.nonExistentMethod(); // ❌ TypeScript error
// Check if registered
if (SingletonManager.has('ApiClient')) {
const client = SingletonManager.get<ApiClient>('ApiClient');
console.log(client.baseUrl); // https://api.example.com
}
// Unregister and re-register
SingletonManager.unregister('DatabaseConnection');
SingletonManager.register('DatabaseConnection', new DatabaseConnection());
You can find the complete API reference documentation for SingletonManager at:
Distributed under the MIT License. See LICENSE for more information.