Skip to content

Commit

Permalink
refactor(server): Connection condition
Browse files Browse the repository at this point in the history
  • Loading branch information
LeleDallas committed Jul 29, 2023
1 parent e7bfb6c commit d9bbf47
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions server/db/services/database.service.ts
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}`);
}
}

0 comments on commit d9bbf47

Please sign in to comment.