-
Notifications
You must be signed in to change notification settings - Fork 4
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 #128 from TogetherCrew/127-memory-heap-issue-due-t…
…o-mongoose-connection 127 memory heap issue due to mongoose connection
- Loading branch information
Showing
4 changed files
with
52 additions
and
42 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import mongoose, { type Connection } from 'mongoose'; | ||
import { | ||
heatMapSchema, | ||
rawInfoSchema, | ||
MemberActivitySchema, | ||
guildMemberSchema, | ||
channelSchema, | ||
roleSchema, | ||
} from '../models/schemas'; | ||
import { | ||
type IHeatMap, | ||
type IRawInfo, | ||
type IMemberActivity, | ||
type IGuildMember, | ||
type IChannel, | ||
type IRole, | ||
} from '../interfaces'; | ||
|
||
import { type Snowflake } from 'discord.js'; | ||
|
||
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; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import databaseService from './database.service'; | ||
import DatabaseManager from './databaseManager'; | ||
|
||
export { databaseService }; | ||
export { DatabaseManager }; |