If you've ever copy-pasted a getInstance() pattern into yet another class, this package is for you.
It gives you a single, centralized registry to store and retrieve instances — type-safe, predictable, and easy to test.
The classic singleton pattern works, but it scatters static instance fields across your codebase.
Every class re-implements the same boilerplate. With Registry, you register instances once and retrieve them anywhere by name.
One registry, no duplication.
any casting required.bun add @dws-std/registry
Peer dependency:
@dws-std/errormust be installed alongside.
Register your instances once at startup. They're available everywhere after that.
import { Registry } from '@dws-std/registry';
class DatabaseConnection {
private _isConnected: boolean = 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;
}
}
Registry.register('DatabaseConnection', new DatabaseConnection());
Registry.register('ApiClient', new ApiClient('https://api.example.com', 'key'));
Same instance, every time. You specify the type via the generic, and TypeScript takes care of the rest.
const db1 = Registry.get<DatabaseConnection>('DatabaseConnection');
const db2 = Registry.get<DatabaseConnection>('DatabaseConnection');
console.log(db1 === db2); // true — same reference
db1.query('SELECT * FROM users'); // type-safe
if (Registry.has('ApiClient')) {
const client = Registry.get<ApiClient>('ApiClient');
console.log(client.baseUrl);
}
// Need to replace an instance? Unregister first, then register the new one.
Registry.unregister('DatabaseConnection');
Registry.register('DatabaseConnection', new DatabaseConnection());
Full docs: Dominus-Web-Service.github.io/packages
MIT — Feel free to use it.