Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
236 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,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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
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 |
---|---|---|
@@ -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 { } |
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 |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -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'); | ||
} | ||
} |
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