-
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.
Merge pull request #136 from TogetherCrew/development
Memory Heap Issue due to mongoose connection
- Loading branch information
Showing
17 changed files
with
100 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ src/config/*.env | |
|
||
# compiled output | ||
/dist | ||
/lib | ||
/node_modules | ||
/lib | ||
|
||
|
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,19 +1,46 @@ | ||
import { Connection } from 'mongoose'; | ||
import parentLogger from '../config/logger'; | ||
import mongoose, { Connection } from 'mongoose'; | ||
import { | ||
heatMapSchema, | ||
rawInfoSchema, | ||
MemberActivitySchema, | ||
guildMemberSchema, | ||
channelSchema, | ||
roleSchema, | ||
type IHeatMap, | ||
type IRawInfo, | ||
type IMemberActivity, | ||
type IGuildMember, | ||
type IChannel, | ||
type IRole, | ||
} from '@togethercrew.dev/db'; | ||
import { type Snowflake } from 'discord.js'; | ||
|
||
const logger = parentLogger.child({ module: 'Connection' }); | ||
export default class DatabaseManager { | ||
private static instance: DatabaseManager; | ||
private modelCache: Record<string, boolean> = {}; | ||
public static getInstance(): DatabaseManager { | ||
if (typeof DatabaseManager.instance === 'undefined') { | ||
DatabaseManager.instance = new DatabaseManager(); | ||
} | ||
return DatabaseManager.instance; | ||
} | ||
|
||
public getTenantDb(tenantId: Snowflake): Connection { | ||
const dbName = tenantId; | ||
const db = mongoose.connection.useDb(dbName, { useCache: true }); | ||
this.setupModels(db); | ||
return db; | ||
} | ||
|
||
/** | ||
* Closes a given Mongoose connection. | ||
* @param {Connection} connection - The Mongoose connection object to be closed. | ||
* @returns {Promise<void>} - A promise that resolves when the connection has been successfully closed. | ||
* @throws {MongooseError} - If there is an error closing the connection, it is logged to the console and the error is thrown. | ||
*/ | ||
export async function closeConnection(connection: Connection) { | ||
try { | ||
await connection.close(); | ||
logger.info({ database: connection.name }, 'The connection to database has been successfully closed'); | ||
} catch (error) { | ||
logger.fatal({ database: connection.name, error }, 'Failed to close the connection to the database'); | ||
private setupModels(db: Connection): void { | ||
if (!this.modelCache[db.name]) { | ||
db.model<IHeatMap>('HeatMap', heatMapSchema); | ||
db.model<IRawInfo>('RawInfo', rawInfoSchema); | ||
db.model<IMemberActivity>('MemberActivity', MemberActivitySchema); | ||
db.model<IGuildMember>('GuildMember', guildMemberSchema); | ||
db.model<IChannel>('Channel', channelSchema); | ||
db.model<IRole>('Role', roleSchema); | ||
this.modelCache[db.name] = true; | ||
} | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.