Skip to content
Snippets Groups Projects
Commit dec50e82 authored by Matt Kirby's avatar Matt Kirby
Browse files

Added mongodb provider

parent 386e6117
No related branches found
No related tags found
1 merge request!15Add Friend service
import mongoose from "mongoose";
/**
* Useful utilities for connection to a mongo database
*/
export class MongoConnectionProvider {
/**
* Mongo db name
*/
public DatabaseName: string;
/**
* Mongo db path
*/
public DatabasePath: string;
/**
* Mongo db port
*/
public DatabasePort: number;
/**
* Mongo db connection
*/
public Connection: mongoose.Connection = mongoose.connection;
constructor(name: string, path: string, port: number) {
this.DatabaseName = name;
this.DatabasePath = path;
this.DatabasePort = port;
}
/**
* Defines connection event functionality
* @param connectionUri
*/
private initializeConnectionEvents = (connectionUri: string) => {
this.Connection
.on('error', () => console.log(`Error connecting to database on: ${connectionUri}`))
.once('open', () => console.log(`Successful connection to database on: ${connectionUri}`))
}
/**
* Constructs the MongoDb connection URI
* @returns
*/
public ConnectionUri = (): string => `mongodb://${this.DatabasePath}:${this.DatabasePort}/${this.DatabaseName}`
/**
* Initalises a new mongo connection.
*/
public Connect = async (): Promise<void> => {
const connectionUri = this.ConnectionUri()
this.initializeConnectionEvents(connectionUri)
await mongoose.connect(connectionUri).catch((err) => {
console.log(err)
})
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment