-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47658da
commit bdda749
Showing
4 changed files
with
90 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,102 @@ | ||
import { Connection } from 'mongoose' | ||
import * as mongoose from 'mongoose' | ||
import mongoose, { Connection } from 'mongoose' | ||
|
||
let cachedConnection: Connection | null = null | ||
let connectionPromise: Promise<Connection> | null = null | ||
|
||
export class DalService { | ||
private connection: Connection | undefined | ||
private static instance: DalService | ||
|
||
private constructor() {} | ||
|
||
static getInstance(): DalService { | ||
if (!DalService.instance) { | ||
DalService.instance = new DalService() | ||
} | ||
return DalService.instance | ||
} | ||
|
||
constructor() {} | ||
async connectDB(): Promise<Connection> { | ||
if (cachedConnection && this.isConnected()) { | ||
return cachedConnection | ||
} | ||
|
||
if (!process.env.EXERCISEDB_DATABASE) { | ||
throw new Error('EXERCISEDB_DATABASE environment variable is not set') | ||
} | ||
|
||
async connectDB(): Promise<Connection | undefined> { | ||
if (this.connection && this.isConnected()) { | ||
return this.connection | ||
if (!connectionPromise) { | ||
connectionPromise = mongoose | ||
.connect(process.env.EXERCISEDB_DATABASE, { | ||
serverSelectionTimeoutMS: 5000, | ||
socketTimeoutMS: 45000, | ||
maxPoolSize: 1, | ||
minPoolSize: 0, | ||
maxIdleTimeMS: 10000 | ||
}) | ||
.then((conn) => { | ||
console.log('Database connected successfully') | ||
return conn.connection | ||
}) | ||
.catch((error) => { | ||
console.error('Error connecting to the database:', error) | ||
connectionPromise = null | ||
throw error | ||
}) | ||
} | ||
|
||
try { | ||
if (process.env.EXERCISEDB_DATABASE !== undefined) { | ||
const instance = await mongoose.connect(process.env.EXERCISEDB_DATABASE) | ||
console.log('Database connected successfully') | ||
this.connection = instance.connection | ||
return this.connection | ||
} | ||
cachedConnection = await connectionPromise | ||
return cachedConnection | ||
} catch (error) { | ||
console.error('Error connecting to the database:', error) | ||
throw new Error('Error connecting to the database') | ||
} | ||
} | ||
|
||
isConnected(): boolean { | ||
return this.connection?.readyState === 1 | ||
return cachedConnection?.readyState === 1 | ||
} | ||
|
||
async disconnect() { | ||
try { | ||
await mongoose.disconnect() | ||
console.log('Database disconnected successfully') | ||
} catch (error) { | ||
console.error('Error disconnecting from the database:', error) | ||
throw error | ||
async disconnect(): Promise<void> { | ||
if (cachedConnection) { | ||
try { | ||
await mongoose.disconnect() | ||
cachedConnection = null | ||
connectionPromise = null | ||
console.log('Database disconnected successfully') | ||
} catch (error) { | ||
console.error('Error disconnecting from the database:', error) | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* The `destroy` function drops the database only in a test environment. | ||
*/ | ||
async destroy() { | ||
if (process.env.NODE_ENV !== 'test') throw new Error('Allowed only in test environment') | ||
async destroy(): Promise<void> { | ||
if (process.env.NODE_ENV !== 'test') { | ||
throw new Error('Allowed only in test environment') | ||
} | ||
|
||
try { | ||
await mongoose.connection.dropDatabase() | ||
console.log('Database dropped successfully') | ||
if (cachedConnection) { | ||
await cachedConnection.dropDatabase() | ||
console.log('Database dropped successfully') | ||
} | ||
} catch (error) { | ||
console.error('Error dropping the database:', error) | ||
throw error | ||
} finally { | ||
await this.disconnect() | ||
} | ||
} | ||
async closeConnection(): Promise<void> { | ||
if (cachedConnection) { | ||
await cachedConnection.close() | ||
cachedConnection = null | ||
connectionPromise = null | ||
console.log('Database connection closed') | ||
} | ||
} | ||
|
||
async cleanup(): Promise<void> { | ||
await this.closeConnection() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters