Skip to content

Commit

Permalink
Merge branch 'main' into data-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriziopiu committed May 12, 2023
2 parents 5ed9ea3 + 1868391 commit a82c9b8
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 9 deletions.
3 changes: 2 additions & 1 deletion backend/.env
@@ -1,6 +1,7 @@
# Development config
PRODUCTION=false # Sets some prod specific things and makes seeding impossible
SEED_MONGO=false # Seeds the mongo database
MONGO_URL=/
MONGO_URL="mongodb://127.0.0.1:27017"
MONGO_DB="test"
#PORT=3000 # Optional
#API_PREFIX=api # Optional
153 changes: 150 additions & 3 deletions backend/package-lock.json

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

1 change: 1 addition & 0 deletions backend/package.json
Expand Up @@ -26,6 +26,7 @@
"@nestjs/platform-express": "^9.0.0",
"@nestjs/serve-static": "^3.0.1",
"joi": "^17.9.2",
"mongodb": "^5.5.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
Expand Down
5 changes: 3 additions & 2 deletions backend/src/app.controller.ts
Expand Up @@ -3,10 +3,11 @@ import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(private readonly appService: AppService) { }

@Get()
getHello(): string {
getHello(): any {
console.log("here");
return this.appService.getHello();
}
}
3 changes: 3 additions & 0 deletions backend/src/app.module.ts
Expand Up @@ -8,6 +8,7 @@ import { AppService } from './app.service';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { SeedModule } from './seed/seed.module';
import { MongoModule } from './mongo/mongo.module';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { SeedModule } from './seed/seed.module';
PRODUCTION: Joi.bool().required(),
SEED_MONGO: Joi.bool().required(),
MONGO_URL: Joi.string().required(),
MONGO_DB: Joi.string().required(),
PORT: Joi.number(),
API_PREFIX: Joi.string(),
}),
Expand All @@ -41,6 +43,7 @@ import { SeedModule } from './seed/seed.module';
inject: [ConfigService],
}),
SeedModule,
MongoModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
7 changes: 5 additions & 2 deletions backend/src/app.service.ts
@@ -1,8 +1,11 @@
import { Injectable } from '@nestjs/common';
import { MongoClient } from 'mongodb';
import { MongoService } from './mongo/mongo.service';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
constructor(private mongoService: MongoService) { }
async getHello(): Promise<any> {
return this.mongoService.getCollection('data').find({}).toArray()
}
}
10 changes: 10 additions & 0 deletions backend/src/mongo/mongo.module.ts
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { MongoService } from './mongo.service';

@Module({
imports: [ConfigModule],
providers: [MongoService],
exports: [MongoService],
})
export class MongoModule { }
18 changes: 18 additions & 0 deletions backend/src/mongo/mongo.service.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MongoService } from './mongo.service';

describe('MongoService', () => {
let service: MongoService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MongoService],
}).compile();

service = module.get<MongoService>(MongoService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
43 changes: 43 additions & 0 deletions backend/src/mongo/mongo.service.ts
@@ -0,0 +1,43 @@
import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Collection, Db, MongoClient } from 'mongodb';

@Injectable()
export class MongoService implements OnModuleInit, OnModuleDestroy {
private connection: MongoClient;
private db: Db;

constructor(private configService: ConfigService) { }

public getMongoDb(): Db {
return this.db;
}

public createCollection(name: string): Promise<Collection> {
return this.db.createCollection(name);
}

public getCollection(collection: string) {
return this.db.collection(collection);
}

async onModuleInit() {
Logger.debug('Initializing mongo connection');
const mongo_uri = this.configService.get("MONGO_URL");
const db = this.configService.get("MONGO_DB");

this.connection = new MongoClient(mongo_uri);
this.db = this.connection.db(db);
Logger.debug('Initialized mongo connection');
}
async onModuleDestroy() {
Logger.debug('Stopping mongo connection');
if (this.connection) await this.connection.close();
Logger.debug('Stopped mongo connection');
}
}
2 changes: 1 addition & 1 deletion backend/start.sh
Expand Up @@ -69,7 +69,7 @@ else
fi

# Start postgres container
DB_CONTAINER=$(sudo docker run --rm -p 127.0.0.1:27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=password mongo:6.0.5) && suc "Mongo started! $DB_CONTAINER" || exit 1
DB_CONTAINER=$(sudo docker run --rm -p 127.0.0.1:27017:27017 -d mongo:6.0.5) && suc "Mongo started! $DB_CONTAINER" || exit 1

# Create db schema
#npx prisma db push
Expand Down

0 comments on commit a82c9b8

Please sign in to comment.