fixing up db's and data-service
This commit is contained in:
parent
aca98fdce4
commit
71f9b0a886
9 changed files with 211 additions and 19 deletions
50
libs/postgres-client/src/singleton.ts
Normal file
50
libs/postgres-client/src/singleton.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { PostgreSQLClient } from './client';
|
||||
import type { PostgreSQLClientConfig } from './types';
|
||||
|
||||
/**
|
||||
* Singleton PostgreSQL client instance
|
||||
* Provides global access to a single PostgreSQL connection pool
|
||||
*/
|
||||
let instance: PostgreSQLClient | null = null;
|
||||
|
||||
/**
|
||||
* Initialize the singleton PostgreSQL client
|
||||
*/
|
||||
export async function connectPostgreSQL(config?: PostgreSQLClientConfig): Promise<PostgreSQLClient> {
|
||||
if (!instance) {
|
||||
if (!config) {
|
||||
throw new Error('PostgreSQL client not initialized. Call connectPostgreSQL(config) first.');
|
||||
}
|
||||
instance = new PostgreSQLClient(config);
|
||||
await instance.connect();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton PostgreSQL client instance
|
||||
* @throws Error if not initialized
|
||||
*/
|
||||
export function getPostgreSQLClient(): PostgreSQLClient {
|
||||
if (!instance) {
|
||||
throw new Error('PostgreSQL client not initialized. Call connectPostgreSQL(config) first.');
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the PostgreSQL client is initialized
|
||||
*/
|
||||
export function isInitialized(): boolean {
|
||||
return instance !== null && instance.connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect and reset the singleton instance
|
||||
*/
|
||||
export async function disconnectPostgreSQL(): Promise<void> {
|
||||
if (instance) {
|
||||
await instance.disconnect();
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue