|
1 |
| -import { Connection } from 'mongoose' |
2 |
| -import * as mongoose from 'mongoose' |
| 1 | +import mongoose, { Connection } from 'mongoose' |
| 2 | + |
| 3 | +let cachedConnection: Connection | null = null |
| 4 | +let connectionPromise: Promise<Connection> | null = null |
3 | 5 |
|
4 | 6 | export class DalService {
|
5 |
| - private connection: Connection | undefined |
| 7 | + private static instance: DalService |
| 8 | + |
| 9 | + private constructor() {} |
| 10 | + |
| 11 | + static getInstance(): DalService { |
| 12 | + if (!DalService.instance) { |
| 13 | + DalService.instance = new DalService() |
| 14 | + } |
| 15 | + return DalService.instance |
| 16 | + } |
6 | 17 |
|
7 |
| - constructor() {} |
| 18 | + async connectDB(): Promise<Connection> { |
| 19 | + if (cachedConnection && this.isConnected()) { |
| 20 | + return cachedConnection |
| 21 | + } |
| 22 | + |
| 23 | + if (!process.env.EXERCISEDB_DATABASE) { |
| 24 | + throw new Error('EXERCISEDB_DATABASE environment variable is not set') |
| 25 | + } |
8 | 26 |
|
9 |
| - async connectDB(): Promise<Connection | undefined> { |
10 |
| - if (this.connection && this.isConnected()) { |
11 |
| - return this.connection |
| 27 | + if (!connectionPromise) { |
| 28 | + connectionPromise = mongoose |
| 29 | + .connect(process.env.EXERCISEDB_DATABASE, { |
| 30 | + serverSelectionTimeoutMS: 5000, |
| 31 | + socketTimeoutMS: 45000, |
| 32 | + maxPoolSize: 1, |
| 33 | + minPoolSize: 0, |
| 34 | + maxIdleTimeMS: 10000 |
| 35 | + }) |
| 36 | + .then((conn) => { |
| 37 | + console.log('Database connected successfully') |
| 38 | + return conn.connection |
| 39 | + }) |
| 40 | + .catch((error) => { |
| 41 | + console.error('Error connecting to the database:', error) |
| 42 | + connectionPromise = null |
| 43 | + throw error |
| 44 | + }) |
12 | 45 | }
|
13 | 46 |
|
14 | 47 | try {
|
15 |
| - if (process.env.EXERCISEDB_DATABASE !== undefined) { |
16 |
| - const instance = await mongoose.connect(process.env.EXERCISEDB_DATABASE) |
17 |
| - console.log('Database connected successfully') |
18 |
| - this.connection = instance.connection |
19 |
| - return this.connection |
20 |
| - } |
| 48 | + cachedConnection = await connectionPromise |
| 49 | + return cachedConnection |
21 | 50 | } catch (error) {
|
22 |
| - console.error('Error connecting to the database:', error) |
23 | 51 | throw new Error('Error connecting to the database')
|
24 | 52 | }
|
25 | 53 | }
|
26 | 54 |
|
27 | 55 | isConnected(): boolean {
|
28 |
| - return this.connection?.readyState === 1 |
| 56 | + return cachedConnection?.readyState === 1 |
29 | 57 | }
|
30 | 58 |
|
31 |
| - async disconnect() { |
32 |
| - try { |
33 |
| - await mongoose.disconnect() |
34 |
| - console.log('Database disconnected successfully') |
35 |
| - } catch (error) { |
36 |
| - console.error('Error disconnecting from the database:', error) |
37 |
| - throw error |
| 59 | + async disconnect(): Promise<void> { |
| 60 | + if (cachedConnection) { |
| 61 | + try { |
| 62 | + await mongoose.disconnect() |
| 63 | + cachedConnection = null |
| 64 | + connectionPromise = null |
| 65 | + console.log('Database disconnected successfully') |
| 66 | + } catch (error) { |
| 67 | + console.error('Error disconnecting from the database:', error) |
| 68 | + throw error |
| 69 | + } |
38 | 70 | }
|
39 | 71 | }
|
40 | 72 |
|
41 |
| - /** |
42 |
| - * The `destroy` function drops the database only in a test environment. |
43 |
| - */ |
44 |
| - async destroy() { |
45 |
| - if (process.env.NODE_ENV !== 'test') throw new Error('Allowed only in test environment') |
| 73 | + async destroy(): Promise<void> { |
| 74 | + if (process.env.NODE_ENV !== 'test') { |
| 75 | + throw new Error('Allowed only in test environment') |
| 76 | + } |
46 | 77 |
|
47 | 78 | try {
|
48 |
| - await mongoose.connection.dropDatabase() |
49 |
| - console.log('Database dropped successfully') |
| 79 | + if (cachedConnection) { |
| 80 | + await cachedConnection.dropDatabase() |
| 81 | + console.log('Database dropped successfully') |
| 82 | + } |
50 | 83 | } catch (error) {
|
51 | 84 | console.error('Error dropping the database:', error)
|
52 | 85 | throw error
|
| 86 | + } finally { |
| 87 | + await this.disconnect() |
53 | 88 | }
|
54 | 89 | }
|
| 90 | + async closeConnection(): Promise<void> { |
| 91 | + if (cachedConnection) { |
| 92 | + await cachedConnection.close() |
| 93 | + cachedConnection = null |
| 94 | + connectionPromise = null |
| 95 | + console.log('Database connection closed') |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + async cleanup(): Promise<void> { |
| 100 | + await this.closeConnection() |
| 101 | + } |
55 | 102 | }
|
0 commit comments