-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Removed `DatabaseHelper` in favor of `MongoClient` closes #35 * Added `DB_URI` env to allow for localhost databases * Added solution in database URI error message --------- Co-authored-by: Aydan Pirani <aydanpirani@gmail.com>
- Loading branch information
1 parent
9895c69
commit 7aad9e8
Showing
6 changed files
with
27 additions
and
64 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 |
---|---|---|
@@ -1,57 +1,20 @@ | ||
import "dotenv"; | ||
import { MongoClient, Collection, Db } from "mongodb"; | ||
import { MongoClient } from "mongodb"; | ||
|
||
// TODO: Add in documentation | ||
const username: string | undefined = process.env.DB_USERNAME; | ||
const password: string | undefined = process.env.DB_PASSWORD; | ||
const server: string | undefined = process.env.DB_SERVER; | ||
|
||
abstract class DatabaseHelper { | ||
private static databases: Map<string, Db> = new Map(); | ||
const credInfo: string = (username) ? `${encodeURIComponent(username)}:${encodeURIComponent(password || "")}@` : ""; | ||
|
||
/** | ||
* Get a particular collection from the MongoDB. If connection does not exist, instantiate it. | ||
* @param databaseName Name of the database to pull from | ||
* @param collectionName Name of the collection to pull from | ||
* @returns Promise for a collection from a database. | ||
*/ | ||
static async getCollection(databaseName: string, collectionName: string): Promise<Collection> { | ||
const database: Db = this.databases?.get(databaseName) ?? await this.getDatabase(databaseName); | ||
const targetCollection: Collection = database.collection(collectionName); | ||
|
||
console.log(`Successfully connected to collection: ${targetCollection.collectionName}`); | ||
return targetCollection; | ||
} | ||
|
||
/** | ||
* Connect to a particular database from Mongo, if not already connected. If already connected, return database. | ||
* @param databaseName Database to instantiate. | ||
* @returns Promise for generation of a database. | ||
*/ | ||
private static async getDatabase(databaseName: string): Promise<Db> { | ||
const connectionString: string = this.getConnectionString(); | ||
|
||
const client: MongoClient = new MongoClient(connectionString); | ||
await client.connect().catch((error: Error) => { | ||
console.error(error); | ||
}); | ||
|
||
const database: Db = client.db(databaseName); | ||
this.databases.set(databaseName, database); | ||
|
||
console.log(`Successfully connected to database: ${database.databaseName}`); | ||
return database; | ||
} | ||
|
||
private static getConnectionString():string { | ||
const user:string | undefined = process.env.DB_USERNAME; | ||
const pass:string | undefined = process.env.DB_PASSWORD; | ||
const server:string | undefined = process.env.DB_SERVER; | ||
|
||
if (!user || !pass || !server) { | ||
throw new Error("login values not provided in .env file!"); | ||
} | ||
|
||
const connectionString:string = `mongodb+srv://${user}:${pass}@${server}/?retryWrites=true&w=majority`; | ||
return connectionString; | ||
} | ||
const uri = process.env.DB_URI || (server) ? `mongodb+srv://${credInfo}${server}` : undefined; | ||
if (!uri) { | ||
throw new Error("No URI was able to be constructed or was provided in .env file! Please either set DB_URI or set DB_SERVER alongside DB_USERNAME and DB_PASSWORD if applicable."); | ||
} | ||
|
||
export default DatabaseHelper; | ||
const client = new MongoClient(uri, { | ||
retryWrites: true, | ||
w: "majority", | ||
}); | ||
|
||
export default client; |
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