Skip to content

Commit 72b6cbb

Browse files
committed
Initial commit - import from another repository
0 parents  commit 72b6cbb

File tree

100 files changed

+10691
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+10691
-0
lines changed

.dockerignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.env
2+
.git
3+
.gitignore
4+
coverage
5+
dist
6+
node_modules

.env.default

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
NODE_ENV=dev
2+
3+
# Database
4+
DB_TYPE=mariadb
5+
DB_HOST=localhost
6+
DB_PORT=3306
7+
DB_DATABASE=rams
8+
DB_USERNAME=rams-user
9+
# DB_PASSWORD
10+
# DB_ROOT_PASSWORD
11+
12+
# ORM
13+
ORM_SYNCHRONIZE=true
14+
ORM_LOGGING=error,warn

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# npm
2+
node_modules
3+
npm-debug.log
4+
5+
# IDE
6+
.vscode
7+
8+
# build
9+
dist
10+
11+
# test coverage
12+
coverage
13+
14+
# Configuration files
15+
.env

Dockerfile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM node:carbon
2+
3+
WORKDIR /usr/src/app
4+
5+
# Global Deps
6+
RUN npm install pm2 -g
7+
8+
# App deps
9+
COPY . /usr/src/app
10+
RUN npm install
11+
RUN npm run prestart:prod
12+
RUN npm prune --production
13+
14+
# Create an .env file
15+
COPY ./.env.default /usr/src/app/.env
16+
17+
# Symlink the public repo
18+
RUN ln -s /usr/src/app/src/public /usr/src/app/dist/src/public
19+
20+
# Overwrite env vars
21+
ENV DB_HOST maria
22+
23+
# Expose the default port
24+
EXPOSE 3000

README.md

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# NestJS Playground - API Movie Schedule (rams)
2+
3+
## Stack
4+
5+
**NodeJS**: https://nodejs.org/en/
6+
7+
**Nest**: https://nestjs.com/
8+
```
9+
A progressive Node.js framework for building efficient and scalable server-side applications on top of TypeScript & JavaScript (ES6 / ES7 / ES8) heavily inspired by Angular
10+
```
11+
12+
**TypeORM**: http://typeorm.io
13+
```
14+
TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap and Ionic platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7)
15+
```
16+
17+
**MariaDB**: https://mariadb.org/
18+
19+
**Docker**: https://www.docker.com/
20+
21+
## Requirements
22+
23+
### Install NodeJS and npm
24+
- [Download](https://nodejs.org/en/download/)
25+
- [Install via package manager](https://nodejs.org/en/download/package-manager/)
26+
27+
### Install Docker and docker-compose
28+
- https://docs.docker.com/installation
29+
- https://docs.docker.com/compose/install/
30+
- [Manage Docker as a non-root user](https://docs.docker.com/engine/installation/linux/linux-postinstall/#manage-docker-as-a-non-root-user)
31+
32+
## Configuration files
33+
34+
You have to create your own `.env` file, avoiding sharing password, and customizing depending on your own dev environment.
35+
36+
You can copy the default file, and have to update it. Or create your own.
37+
```
38+
cp .env.default .env
39+
```
40+
41+
Set your `DB_PASSWORD` and `DB_ROOT_PASSWORD` (for *dev* environment).
42+
43+
## Commands
44+
45+
### Dev
46+
47+
#### First time
48+
```
49+
./bin/dev-start.sh
50+
```
51+
It will starts 2 containers:
52+
- MariaDB server for the API data on `3306` port.
53+
- Adminer to manage MariaDB data while programming on http://localhost:8080.
54+
55+
#### Start the app
56+
```
57+
npm run start
58+
59+
# Watch for changes for automatic restart
60+
npm run start:watch
61+
```
62+
63+
The default URL is http://localhost:3000.
64+
65+
#### Clear all dev data and containers:
66+
```
67+
./bin/dev-clear.sh
68+
```
69+
70+
### Prod
71+
```
72+
# Build for production
73+
npm run prestart:prod
74+
75+
# Start the production app version
76+
npm run start:prod
77+
```
78+
79+
```
80+
# Start all stack
81+
DB_PASSWORD={{your_pwd}} DB_ROOT_PASSWORD={{your_pwd}} ./bin/prod-start.sh
82+
```
83+
84+
```
85+
# Inject fixtures
86+
docker exec rams-prod-app node dist/bin/fixtures-init.js
87+
```
88+
89+
```
90+
# Clear all prod data and containers
91+
./bin/prod-clear.sh
92+
```
93+
94+
### Data
95+
```
96+
# Insert fixtures data
97+
npm run fixtures:init
98+
99+
# Remove all fixtures from DB
100+
npm run fixtures:clear
101+
```
102+
103+
### Tests
104+
```
105+
# Run unit tests
106+
npm test
107+
108+
# Run and watch unit tests
109+
npm run test:watch
110+
111+
# Run unit tests coverage
112+
npm run test:coverage
113+
114+
# Opn coverage html report
115+
npm run open:coverage
116+
```
117+
118+
```
119+
# Run end-to-end tests
120+
npm run e2e
121+
122+
# Run and watch end-to-end tests
123+
npm run e2e:watch
124+
```
125+
126+
## URLs
127+
128+
- API: http://localhost:3000
129+
- Swagger definitions: http://localhost:3000/swagger
130+
- Adminer: http://localhost:8080
131+
132+
## TODO
133+
- Check the compatibility for `enum` in swagger nestjs library ([Github issue](https://github.com/nestjs/swagger/issues/19)).
134+
- Check the tests coverage for `constructor` with injection
135+
* [Github issue](https://github.com/istanbuljs/istanbuljs/issues/70)
136+
* [Angular Issue](https://github.com/angular/angular-cli/issues/5526)
137+
- Find a way to not expose URLs in Swagger (e.g. list of movies)
138+
139+
## Issues
140+
- The logger is logging duplication when a request is called.
141+
142+
## Resources
143+
144+
**NestJS**
145+
- [Documentation](https://docs.nestjs.com/)
146+
- [Typescript starter](https://github.com/nestjs/typescript-starter)
147+
- [Examples](https://github.com/nestjs/nest/tree/master/examples)
148+
149+
**Docker**
150+
- [MySQL container](https://hub.docker.com/_/mysql/)
151+
- [Adminer container](https://hub.docker.com/_/adminer/)
152+
- https://github.com/vishnubob/wait-for-it
153+
154+
**Misc**
155+
- [TypeORM](https://github.com/typeorm/typeorm)
156+
- TypeScript linter: [TSLint](https://palantir.github.io/tslint/)
157+
- Watch and reload while coding: [nodemon](https://nodemon.io/)
158+
159+
**Tips**
160+
- TypeORM repositories for unit testing: [here](https://github.com/nestjs/nest/issues/363#issuecomment-360105413)

bin/dev-clear.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
rm -rf node_modules/
4+
rm -rf coverage/
5+
rm -rf dist/
6+
7+
docker-compose -f docker-compose.dev.yml down --volumes

bin/dev-start.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
docker-compose -f docker-compose.dev.yml up -d
4+
5+
npm install
6+
npm run fixtures:init
7+
npm run start:watch

bin/fixtures-clear.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* tslint:disable:no-console */
2+
3+
import { NestFactory } from '@nestjs/core';
4+
5+
import { ApplicationModule } from '../src/app.module';
6+
import { FixturesModule } from '../src/fixtures/fixtures.module';
7+
import { FixturesService } from '../src/fixtures/fixtures.service';
8+
9+
// Create an execution context of the app
10+
(async () => {
11+
const context = await NestFactory.createApplicationContext(ApplicationModule);
12+
13+
const fixturesModule = await context.select<FixturesModule>(FixturesModule);
14+
const fixturesService = await fixturesModule.get<FixturesService>(FixturesService);
15+
16+
await fixturesService.clear();
17+
})()
18+
.then(() => {
19+
console.log(`All fixtures have been deleted from the database.`);
20+
21+
process.exit(0);
22+
})
23+
.catch(error => {
24+
console.log(error);
25+
});

bin/fixtures-init.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* tslint:disable:no-console */
2+
3+
import { NestFactory } from '@nestjs/core';
4+
5+
import { ApplicationModule } from '../src/app.module';
6+
import { FixturesModule } from '../src/fixtures/fixtures.module';
7+
import { FixturesService } from '../src/fixtures/fixtures.service';
8+
9+
// Create an execution context of the app
10+
(async () => {
11+
const context = await NestFactory.createApplicationContext(ApplicationModule);
12+
13+
const fixturesModule = await context.select<FixturesModule>(FixturesModule);
14+
const fixturesService = await fixturesModule.get<FixturesService>(FixturesService);
15+
16+
const locationList = await fixturesService.injectLocations();
17+
const movieList = await fixturesService.injectMovies();
18+
const scheduleList = await fixturesService.injectSchedules(locationList, movieList);
19+
20+
console.log(`- ${locationList.length} locations have been inserted in the database.`);
21+
console.log(`- ${movieList.length} movies have been inserted in the database.`);
22+
console.log(`- ${scheduleList.length} schedules have been inserted in the database.`);
23+
})()
24+
.then(() => {
25+
console.log(`Success!`);
26+
27+
process.exit(0);
28+
})
29+
.catch(error => {
30+
console.log(error);
31+
});

bin/prod-clear.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
docker-compose -f docker-compose.prod.yml down --volumes
4+
docker rmi rams-app

bin/prod-start.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
DB_PORT=3307 docker-compose -f docker-compose.prod.yml up --build -d

0 commit comments

Comments
 (0)