-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.ts
34 lines (31 loc) · 1.04 KB
/
Database.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as mongoose from 'mongoose';
import Locals from './Locals';
import { BaseDatabase, BaseLogger } from '@/interfaces';
import { inject, injectable } from 'tsyringe';
import { TOKENS } from '@/di/Tokens';
@injectable()
export class Database implements BaseDatabase {
private instanceCreated = false;
constructor(@inject(TOKENS.logger) private logger: BaseLogger) {
this.logger = logger;
}
private createConnection(): any {
const connectURI = Locals.config().MONGO_URL;
mongoose.connect(connectURI, (error: mongoose.CallbackError) => {
if (error) {
this.logger.info('Failed to connect to the Mongo server!!');
throw error;
} else {
this.logger.info('connected to mongo server at: ' + connectURI);
}
});
};
public getConnection() {
if (!this.instanceCreated) {
this.createConnection();
this.instanceCreated = true;
}
return mongoose;
}
}
export default mongoose;