Skip to content

Commit

Permalink
Merge pull request #40 from UoaWDCC/DB_Connection
Browse files Browse the repository at this point in the history
Set up database connection
  • Loading branch information
GodYazza authored Jul 21, 2024
2 parents b336c60 + cf8f0c7 commit 9ddbcc0
Show file tree
Hide file tree
Showing 7 changed files with 1,766 additions and 1,291 deletions.
15 changes: 15 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Environment Variables for Database Connection

# Create a '.env' file under the api directory of the project and add the following variable:
# MONGODB_URI=mongodb+srv://fsae:cD5pAQGsceFvdle7@cluster0.rropzcv.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0

# Original config without using environment variables were:
# name: mongoDB
# connector: MongoDB
# url: mongodb+srv://fsae:cD5pAQGsceFvdle7@cluster0.rropzcv.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
# host: cluster0.rropzcv.mongodb.net
# port: 27017
# user: fsae
# password: ''
# database: ''
# useNewUrlParser: true
172 changes: 171 additions & 1 deletion api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
"@loopback/authorization": "^0.15.3",
"@loopback/boot": "^7.0.0",
"@loopback/core": "^6.0.0",
"@loopback/repository": "^7.0.0",
"@loopback/repository": "^7.0.4",
"@loopback/rest": "^14.0.0",
"@loopback/rest-explorer": "^7.0.0",
"@loopback/service-proxy": "^7.0.0",
"@types/jsonwebtoken": "^9.0.6",
"bcryptjs": "^2.4.3",
"dotenv": "^16.4.5",
"jsonwebtoken": "^9.0.2",
"loopback-connector-mongodb": "^6.2.0",
"tslib": "^2.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions api/src/datasources/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './dev-in-mem.datasource';
export * from './mongo-db.datasource';
28 changes: 28 additions & 0 deletions api/src/datasources/mongo-db.datasource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';

const config = {
name: 'mongoDB',
connector: 'mongodb',
url: process.env.MONGODB_URI, // Using environment variable
useNewUrlParser: true
};

// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html

@lifeCycleObserver('datasource')
export class MongoDbDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'mongoDB';
static readonly defaultConfig = config;

constructor(
@inject('datasources.config.mongoDB', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
3 changes: 3 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import dotenv from 'dotenv';
dotenv.config(); // Load environment variables from .env file

import {ApplicationConfig, FsaeApiApplication} from './application';

export * from './application';
Expand Down
Loading

0 comments on commit 9ddbcc0

Please sign in to comment.