Skip to content

Commit

Permalink
Merge pull request #128 from TogetherCrew/127-memory-heap-issue-due-t…
Browse files Browse the repository at this point in the history
…o-mongoose-connection

127 memory heap issue due to mongoose connection
  • Loading branch information
cyri113 authored Nov 15, 2023
2 parents 12b1e59 + 8e9aecd commit 94afd08
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@togethercrew.dev/db",
"version": "3.0.14",
"version": "3.0.15",
"description": "All interactions with DB",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
39 changes: 0 additions & 39 deletions src/service/database.service.ts

This file was deleted.

49 changes: 49 additions & 0 deletions src/service/databaseManager.ts
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;
}
}
}
4 changes: 2 additions & 2 deletions src/service/index.ts
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 };

0 comments on commit 94afd08

Please sign in to comment.