-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): Connection condition
- Loading branch information
1 parent
e7bfb6c
commit d9bbf47
Showing
1 changed file
with
11 additions
and
9 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,20 +1,22 @@ | ||
import * as mongoDB from "mongodb"; | ||
import * as dotenv from "dotenv"; | ||
import { Collection, Db, MongoClient } from "mongodb"; | ||
|
||
export const collections: { cards?: mongoDB.Collection } = {} | ||
export const collections: { cards?: Collection } = {} | ||
|
||
export async function connectToDatabase() { | ||
dotenv.config(); | ||
|
||
const client: mongoDB.MongoClient = new mongoDB.MongoClient(process.env.DB_CONN_STRING!); | ||
if (process.env.DB_CONN_STRING) { | ||
const client: MongoClient = new MongoClient(process.env.DB_CONN_STRING); | ||
|
||
await client.connect(); | ||
await client.connect(); | ||
|
||
const db: mongoDB.Db = client.db(process.env.DB_NAME); | ||
const db: Db = client.db(process.env.DB_NAME); | ||
|
||
const cardsCollection: mongoDB.Collection = db.collection(process.env.CARDS_COLLECTION_NAME!); | ||
const cardsCollection: Collection = db.collection(process.env.CARDS_COLLECTION_NAME!); | ||
|
||
collections.cards = cardsCollection; | ||
|
||
console.log(`Successfully connected to database: ${db.databaseName} and collection: ${cardsCollection.collectionName}`); | ||
collections.cards = cardsCollection; | ||
|
||
console.log(`Successfully connected to database: ${db.databaseName} and collection: ${cardsCollection.collectionName}`); | ||
} | ||
} |