-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from yurenju/feat/user-mongo-model
feat: implemented user model with mongo integration
- Loading branch information
Showing
15 changed files
with
1,018 additions
and
189 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
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 @@ | ||
scripts/init-mongo.js |
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,11 @@ | ||
const MONGO_DATABASE = '<MONGO_DATABASE>'; | ||
const MONGO_DATABASE_USERNAME = '<MONGO_DATABASE_USERNAME>'; | ||
const MONGO_DATABASE_PASSWORD = '<MONGO_DATABASE_PASSWORD>'; | ||
|
||
db = db.getSiblingDB(MONGO_DATABASE); | ||
|
||
db.createUser({ | ||
user: MONGO_DATABASE_USERNAME, | ||
pwd: MONGO_DATABASE_PASSWORD, | ||
roles: [{ role: 'readWrite', db: MONGO_DATABASE }], | ||
}); |
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,31 @@ | ||
const { join } = require('path'); | ||
const shell = require('shelljs'); | ||
require('dotenv').config({ | ||
path: join(__dirname, '..', '.env.local'), | ||
}); | ||
|
||
let template = shell.cat(join(__dirname, 'init-mongo.js.template')); | ||
const replaceTerms = [ | ||
'MONGO_DATABASE', | ||
'MONGO_DATABASE_USERNAME', | ||
'MONGO_DATABASE_PASSWORD', | ||
]; | ||
|
||
replaceTerms.forEach((term) => { | ||
template = template.replaceAll(`<${term}>`, process.env[term]); | ||
}); | ||
|
||
const outputFile = join(__dirname, 'init-mongo.js'); | ||
shell.ShellString(template).to(outputFile); | ||
|
||
const command = `docker run \ | ||
--name ${process.env.MONGO_CONTAINER_NAME} \ | ||
-e MONGO_INITDB_ROOT_USERNAME=${process.env.MONGO_INITDB_ROOT_USERNAME} \ | ||
-e MONGO_INITDB_ROOT_PASSWORD=${process.env.MONGO_INITDB_ROOT_PASSWORD} \ | ||
-p 27017:27017 \ | ||
-v ${outputFile}:/docker-entrypoint-initdb.d/init-mongo.js:ro \ | ||
mongo`; | ||
|
||
shell.exec(`docker stop ${process.env.MONGO_CONTAINER_NAME}`); | ||
shell.exec(`docker rm ${process.env.MONGO_CONTAINER_NAME}`); | ||
shell.exec(command); |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
export interface Config { | ||
mongo: { | ||
username: string; | ||
password: string; | ||
host: string; | ||
database: string; | ||
}; | ||
} | ||
|
||
class PropertyNotFoundError extends Error { | ||
constructor(missingProperties: string[]) { | ||
const missings = missingProperties.join(', '); | ||
super(`The following properties could not be found: ${missings}`); | ||
this.name = 'PropertyNotFoundError'; | ||
Object.setPrototypeOf(this, PropertyNotFoundError.prototype); | ||
} | ||
} | ||
|
||
export function getConfig(): Config { | ||
const config = { | ||
mongo: { | ||
username: process.env.MONGO_DATABASE_USERNAME, | ||
password: process.env.MONGO_DATABASE_PASSWORD, | ||
host: process.env.MONGO_HOST, | ||
database: process.env.MONGO_DATABASE, | ||
}, | ||
}; | ||
|
||
const missingProperties = Object.keys(config.mongo).filter( | ||
(key) => !config.mongo[key] | ||
); | ||
|
||
if (missingProperties.length > 0) { | ||
throw new PropertyNotFoundError(missingProperties); | ||
} | ||
|
||
return config; | ||
} |
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,3 @@ | ||
export class CreateUserDto { | ||
nationalId: string; | ||
} |
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,12 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { HydratedDocument } from 'mongoose'; | ||
|
||
export type UserDocument = HydratedDocument<User>; | ||
|
||
@Schema() | ||
export class User { | ||
@Prop({ required: true }) | ||
nationalId: string; | ||
} | ||
|
||
export const UserSchema = SchemaFactory.createForClass(User); |
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,19 @@ | ||
import { Body, Controller, Get, Post } from '@nestjs/common'; | ||
import { User } from './schemas/user.schema'; | ||
import { UsersService } from './user.service'; | ||
import { CreateUserDto } from './create-user.dto'; | ||
|
||
@Controller('users') | ||
export class UsersController { | ||
constructor(private usersService: UsersService) {} | ||
|
||
@Post() | ||
create(@Body() createUserDto: CreateUserDto) { | ||
this.usersService.create(createUserDto); | ||
} | ||
|
||
@Get() | ||
findAll(): Promise<User[]> { | ||
return this.usersService.findAll(); | ||
} | ||
} |
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,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { User, UserSchema } from './schemas/user.schema'; | ||
import { UsersController } from './user.controller'; | ||
import { UsersService } from './user.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), | ||
], | ||
controllers: [UsersController], | ||
providers: [UsersService], | ||
}) | ||
export class UsersModule {} |
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,27 @@ | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { User } from './schemas/user.schema'; | ||
import { CreateUserDto } from './create-user.dto'; | ||
import { Model } from 'mongoose'; | ||
|
||
@Injectable() | ||
export class UsersService implements OnModuleInit { | ||
constructor(@InjectModel(User.name) private userModel: Model<User>) {} | ||
|
||
async create(createUserDto: CreateUserDto): Promise<User> { | ||
const createdUser = new this.userModel(createUserDto); | ||
return createdUser.save(); | ||
} | ||
|
||
async findAll(): Promise<User[]> { | ||
return this.userModel.find().exec(); | ||
} | ||
|
||
async onModuleInit() { | ||
const count = await this.userModel.count(); | ||
if (count === 0) { | ||
const dto: CreateUserDto = { nationalId: 'A123456789' }; | ||
return this.create(dto); | ||
} | ||
} | ||
} |
Oops, something went wrong.