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
|
|
@ -67,7 +67,7 @@
|
|||
"redis": {
|
||||
"host": "localhost",
|
||||
"port": 6379,
|
||||
"db": 1
|
||||
"db": 0
|
||||
},
|
||||
"defaultJobOptions": {
|
||||
"attempts": 3,
|
||||
|
|
|
|||
|
|
@ -27,3 +27,10 @@ export {
|
|||
createMongoDBClient,
|
||||
createAndConnectMongoDBClient,
|
||||
} from './factory';
|
||||
|
||||
// Singleton instance
|
||||
export {
|
||||
getMongoDBClient,
|
||||
connectMongoDB,
|
||||
getDatabase,
|
||||
} from './singleton';
|
||||
|
|
|
|||
62
libs/mongodb-client/src/singleton.ts
Normal file
62
libs/mongodb-client/src/singleton.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { MongoDBClient } from './client';
|
||||
import type { MongoDBClientConfig } from './types';
|
||||
import type { Db } from 'mongodb';
|
||||
|
||||
/**
|
||||
* Singleton MongoDB client instance
|
||||
* Provides global access to a single MongoDB connection
|
||||
*/
|
||||
let instance: MongoDBClient | null = null;
|
||||
|
||||
/**
|
||||
* Initialize the singleton MongoDB client
|
||||
*/
|
||||
export async function connectMongoDB(config?: MongoDBClientConfig): Promise<MongoDBClient> {
|
||||
if (!instance) {
|
||||
if (!config) {
|
||||
throw new Error('MongoDB client not initialized. Call connectMongoDB(config) first.');
|
||||
}
|
||||
instance = new MongoDBClient(config);
|
||||
await instance.connect();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton MongoDB client instance
|
||||
* @throws Error if not initialized
|
||||
*/
|
||||
export function getMongoDBClient(): MongoDBClient {
|
||||
if (!instance) {
|
||||
throw new Error('MongoDB client not initialized. Call connectMongoDB(config) first.');
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the MongoDB database instance
|
||||
* @throws Error if not initialized
|
||||
*/
|
||||
export function getDatabase(): Db {
|
||||
if (!instance) {
|
||||
throw new Error('MongoDB client not initialized. Call connectMongoDB(config) first.');
|
||||
}
|
||||
return instance.getDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the MongoDB client is initialized
|
||||
*/
|
||||
export function isInitialized(): boolean {
|
||||
return instance !== null && instance.connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect and reset the singleton instance
|
||||
*/
|
||||
export async function disconnectMongoDB(): Promise<void> {
|
||||
if (instance) {
|
||||
await instance.disconnect();
|
||||
instance = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -30,8 +30,14 @@ export type {
|
|||
AuditLog,
|
||||
} from './types';
|
||||
|
||||
// Utils
|
||||
// Factory functions
|
||||
export {
|
||||
createPostgreSQLClient,
|
||||
createAndConnectPostgreSQLClient,
|
||||
} from './factory';
|
||||
|
||||
// Singleton instance
|
||||
export {
|
||||
getPostgreSQLClient,
|
||||
connectPostgreSQL,
|
||||
} from './singleton';
|
||||
|
|
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -94,6 +94,34 @@ export class Queue {
|
|||
return await this.bullQueue.addBulk(jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a scheduled job with cron-like pattern
|
||||
*/
|
||||
async addScheduledJob(
|
||||
name: string,
|
||||
data: JobData,
|
||||
cronPattern: string,
|
||||
options: JobOptions = {}
|
||||
): Promise<Job> {
|
||||
const scheduledOptions: JobOptions = {
|
||||
...options,
|
||||
repeat: {
|
||||
pattern: cronPattern,
|
||||
// Use job name as repeat key to prevent duplicates
|
||||
key: `${this.queueName}:${name}`,
|
||||
},
|
||||
};
|
||||
|
||||
logger.info('Adding scheduled job', {
|
||||
queueName: this.queueName,
|
||||
jobName: name,
|
||||
cronPattern,
|
||||
repeatKey: scheduledOptions.repeat?.key
|
||||
});
|
||||
|
||||
return await this.bullQueue.add(name, data, scheduledOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get queue statistics
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -46,6 +46,13 @@ export interface JobOptions {
|
|||
type: 'exponential' | 'fixed';
|
||||
delay: number;
|
||||
};
|
||||
repeat?: {
|
||||
pattern?: string;
|
||||
key?: string;
|
||||
limit?: number;
|
||||
every?: number;
|
||||
immediately?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface QueueOptions {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue