Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test backend yingjun #101

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2818a1b
init work for Wiredcraft back-end test
yingjun May 23, 2023
6ec1870
create user model
yingjun May 23, 2023
6cc142a
add prisma/client dependency
yingjun May 23, 2023
a721fcb
add swagger
yingjun May 23, 2023
32d0393
update launch script
yingjun May 23, 2023
55203c9
userdto validation, make sure api receiving correct data
yingjun May 23, 2023
55c6aa4
add db access layer
yingjun May 24, 2023
c669c0a
health check for app
yingjun May 24, 2023
281e32e
add dependencies
yingjun May 26, 2023
ca39841
update ignore
yingjun May 26, 2023
72b738f
use docker run mongodb for local dev and testing
yingjun May 28, 2023
d863988
handle env var use dontenv lib
yingjun May 28, 2023
58a5863
implmenet user CRUD rest api
yingjun May 28, 2023
4e6849e
update user model add updateAt field
yingjun May 28, 2023
0ad87c3
fix jest test config, prepare for unit test
yingjun May 28, 2023
f5fb4d3
secure header, prevent from xss
yingjun May 28, 2023
e196f96
unit test for user controller and service
yingjun May 28, 2023
49da10d
handle prisma error
yingjun May 28, 2023
38b3ed5
add logging middleware
yingjun May 28, 2023
96926dd
add tag for users api, add id for userdto
yingjun May 28, 2023
c4591ad
winston log config options
yingjun May 28, 2023
d533ff0
add tag for health api
yingjun May 28, 2023
adfb0db
add public folder prepare for serve document html
yingjun May 28, 2023
3c7a195
add dockerfile and jenkinsfile
yingjun May 28, 2023
07bbd6f
fix logging middleware
yingjun May 28, 2023
133e507
rewrite unit user service unit test, one bug found during unit test, …
yingjun May 29, 2023
b2f366d
add comments and cleanup code
yingjun May 29, 2023
2aa5c52
add request strip pipe, remove id, createAt, updateAt property from r…
yingjun May 29, 2023
fbd6ea8
clean up strip logic, make it resuable
yingjun May 29, 2023
8e0d9a9
cleanup code, add utils, fix unit test
yingjun May 30, 2023
c88b091
add pagniation support for get user api
yingjun May 30, 2023
3fee51b
remove unused code from util file
yingjun May 30, 2023
bfabc21
fix bug
yingjun May 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,vim,node,macos,windows
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,vim,node,macos,windows
/data/**/*
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

### Node Patch ###
# Serverless Webpack directories
.webpack/

# Optional stylelint cache

# SvelteKit build / generate output
.svelte-kit

### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim
Sessionx.vim

# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,vim,node,macos,windows
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.16.0
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"runtimeExecutable": "yarn",
"runtimeArgs": ["dev"],
"envFile": "${workspaceFolder}/.env",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"git.ignoreLimitWarning": true,
"dotenv.enableAutocloaking": false
}
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:18-alpine
LABEL author="Yingjun Cui"
LABEL version="1.0"
LABEL description="Wiredcraft backend test"

WORKDIR /usr/src/app


COPY ./dist ./dist
COPY ./prisma ./prisma
COPY ./public ./public
COPY package.json .
COPY yarn.lock .

RUN yarn install --production=true
RUN yarn prisma generate

ENV PORT=3000
EXPOSE 3000

CMD ["node", "./dist/app.js"]
Empty file added Jenkinsfile
Empty file.
33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "3.8"

services:
mongo1:
image: mongo:5
container_name: mongo1
command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30001"]
Copy link
Member

@MiffyLiye MiffyLiye May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does default configuration support replica set?
where to config the database if we want to use single node mongo db in local, while use replica set in QA / Staging / Production?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the story.
this is my first time use Prisma orm, I know it is bit risk use new lib/framework which I don't familiar during interview project. But I always want to learn something new from each interview. I spent few hours read the doc and I found it is only supports replica. Also I think it is generally considered a good practice using MongoDB in a replica set configuration.
and if we really need use single node mongodb, we can easily replaced it with other orm framework, that is why I use interface injected to the userService instead of inject concrete prismaService class. the code can be much more simplified if I directly inject prismaService to userService but I think it lots the flexibility and maintainability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok to use replica set, does the default configuration support replica set?
where to configure it if another environment uses more nodes or other ports?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok to use replica set, does the default configuration support replica set?
Yes, prisma ONLY supports replica.

where to configure it if another environment uses more nodes or other ports?

sorry I forgot push example.env file. We can define DATABASE_URL in .env or pass as ENV var.
Screenshot 2023-06-02 at 16 21 02

volumes:
- ./data/mongo-1:/data/db
ports:
- 30001:30001
healthcheck:
test: test $$(echo "rs.initiate({_id:'my-replica-set',members:[{_id:0,host:\"mongo1:30001\"},{_id:1,host:\"mongo2:30002\"},{_id:2,host:\"mongo3:30003\"}]}).ok || rs.status().ok" | mongo --port 30001 --quiet) -eq 1
interval: 10s
start_period: 30s

mongo2:
image: mongo:5
container_name: mongo2
command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30002"]
volumes:
- ./data/mongo-2:/data/db
ports:
- 30002:30002

mongo3:
image: mongo:5
container_name: mongo3
command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30003"]
volumes:
- ./data/mongo-3:/data/db
ports:
- 30003:30003
14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const tsconfig = require("./tsconfig.json");
const moduleNameMapper = require("tsconfig-paths-jest")(tsconfig);

module.exports = {
moduleFileExtensions: ["js", "json", "ts"],
rootDir: ".",
testEnvironment: "node",
testRegex: "spec.ts$",
transform: {
"^.+\\.(t|j)s$": "ts-jest",
},
preset: "ts-jest",
moduleNameMapper,
};
9 changes: 9 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"webpack": true
}
}
8 changes: 8 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts", ".git", "node_modules/**/*"],
"execMap": {
"ts": "node -r dotenv/config -r tsconfig-paths/register -r ts-node/register"
}
}
Loading