Skip to content

Commit 1d7e5fa

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
rename directories
1 parent df738f5 commit 1d7e5fa

44 files changed

Lines changed: 18278 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# local installed dependenies
2+
node_modules/
3+
dist/
4+
5+
# local build
6+
wwwroot/
7+
8+
9+
# local development
10+
nodemon.json
11+
jest.config.js
12+
jest.e2e.config.js
13+
14+
# database development
15+
data/
16+
migrations/
17+
create_todos_db.sql
18+
knexfile.js
19+
20+
# environment variables
21+
.env
22+
.env.template
23+
.env.test
24+
25+
# tools configuration
26+
.prettierrc
27+
28+
# project info
29+
README.md
30+
31+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DB_HOST=
2+
DB_USER=
3+
DB_PASSWORD=
4+
DB_PORT=
5+
DB_NAME=
6+
DB_VERSION=
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
NODE_ENV=test
2+
PORT=3001
3+
DB_HOST=localhost
4+
DB_USER=postgres
5+
DB_PASSWORD=postgres
6+
DB_PORT=5432
7+
DB_NAME=todos_db
8+
DB_VERSION=10.4
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 2,
4+
"endOfLine": "lf",
5+
"trailingComma": "all",
6+
"singleQuote": true,
7+
"arrowParens": "always"
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM node:alpine3.12 as builder
2+
3+
WORKDIR /build
4+
5+
# Copy backend app files
6+
COPY ./src ./src
7+
8+
# Copy frontend app files
9+
COPY ./frontend ./frontend
10+
11+
# Copy dependencies manifest
12+
COPY package*.json ./
13+
14+
# Copy compile configuration
15+
COPY tsconfig.json ./
16+
17+
# Build apps
18+
RUN npm install
19+
20+
RUN cd ./frontend && npm install
21+
22+
RUN npm run build
23+
24+
# Packaging app
25+
FROM node:alpine3.12 as app
26+
27+
WORKDIR /app
28+
29+
COPY --from=builder ./build/wwwroot ./
30+
31+
# Install production dependencies
32+
COPY package*.json ./
33+
34+
RUN npm ci --only=production
35+
36+
CMD [ "node", "app.js" ]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM postgres:10.4
2+
3+
COPY ./todos_db.sql /docker-entrypoint-initdb.d
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
# Todo App React
2+
3+
## Environment Variables
4+
5+
```ini
6+
NODE_ENV=
7+
PORT=
8+
DB_HOST=
9+
DB_USER=
10+
DB_PASSWORD=
11+
DB_PORT=
12+
DB_NAME=
13+
DB_VERSION=
14+
```
15+
16+
## Start Database using Docker
17+
18+
```bash
19+
$ docker run -d -p 5432:5432 -v todos:/var/lib/postgresql/data --name postgres postgres:10.4
20+
```
21+
22+
If the database was not initialized, we can solve this by:
23+
24+
```bash
25+
$ docker exec -i postgres psql -U postgres < create_todos_db.sql
26+
```
27+
28+
Notice, that since we have a volume we will not need to run again the above code.
29+
30+
## Migrations
31+
32+
To create a new `knexfile.js` run:
33+
34+
```bash
35+
$ $(npm bin)/knex init
36+
```
37+
38+
### Creating a Table
39+
40+
```bash
41+
$ $(npm bin)/knex migrate:make create_todos_table
42+
Using environment: development
43+
Using environment: development
44+
Using environment: development
45+
Created Migration: /Users/jaimesalaszancada/Documents/paths/ci_cd-path/04_gitlab_or/todo-app-react/migrations/20201122205735_create_todos_table.js
46+
```
47+
48+
We can create the migration code as follows:
49+
50+
```js
51+
exports.up = function(knex) {
52+
return knex.schema.createTable('todos', function(table) {
53+
table.increments('id');
54+
table.string('title', 255).notNullable();
55+
table.boolean('completed').notNullable();
56+
});
57+
};
58+
59+
exports.down = function(knex) {
60+
return knex.schema.dropTable('users');
61+
};
62+
```
63+
64+
To see pending migrations
65+
66+
```bash
67+
$ $(npm bin)/knex migrate:list
68+
Using environment: development
69+
No Completed Migration files Found.
70+
Found 1 Pending Migration file/files.
71+
20201122205735_create_todos_table.js
72+
```
73+
74+
To run the migration
75+
76+
```bash
77+
$ $(npm bin)/knex migrate:up 20201122205735_create_todos_table.js
78+
Using environment: development
79+
Batch 1 ran the following migrations:
80+
20201122205735_create_todos_table.js
81+
```
82+
83+
### Updating table
84+
85+
* The previous table must be updated with two new columns:
86+
- due_date -> `datetime`
87+
- order -> `int`
88+
89+
As before we create the migration
90+
91+
```bash
92+
$ $(npm bin)/knex migrate:make update_todos_table
93+
Using environment: development
94+
Using environment: development
95+
Using environment: development
96+
Created Migration: /Users/jaimesalaszancada/Documents/paths/ci_cd-path/04_gitlab_or/todo-app-react/migrations/20201123104711_update_todos_table.js
97+
```
98+
99+
And the migration will look as follows:
100+
101+
```js
102+
exports.up = function(knex) {
103+
return knex.schema.table('todos', (t) => {
104+
t.datetime('due_date');
105+
t.integer('order');
106+
});
107+
};
108+
109+
exports.down = function(knex) {
110+
return knex.schema.table('todos', (t) => {
111+
t.dropColumn('due_date');
112+
t.dropColumn('order');
113+
});
114+
};
115+
```
116+
117+
To apply all pending migrations, we can run:
118+
119+
```bash
120+
$ $(npm bin)/knex migrate:list
121+
Using environment: development
122+
No Completed Migration files Found.
123+
Found 2 Pending Migration file/files.
124+
20201122205735_create_todos_table.js
125+
20201123104711_update_todos_table.js
126+
127+
$ $(npm bin)/knex migrate:latest
128+
Using environment: development
129+
Batch 1 run: 2 migrations
130+
```
131+
132+
### Seeding the database with Knex
133+
134+
Update `knexfile.js` as follows
135+
136+
```diff
137+
# .....
138+
development: {
139+
client: 'pg',
140+
connection: {
141+
host: process.env.DB_HOST || 'localhost',
142+
database: process.env.DB_NAME || 'todos',
143+
user: process.env.DB_USER || 'postgres',
144+
password: process.env.DB_PASSWORD || 'postgres',
145+
dbVersion: process.env.DB_VERSION || '10.4',
146+
port: +process.env.DB_PORT || 5432,
147+
},
148+
pool: {
149+
min: 2,
150+
max: 10,
151+
},
152+
migrations: {
153+
tableName: 'migrations',
154+
},
155+
+ seeds: {
156+
+ directory: './data/seeds',
157+
+ }
158+
},
159+
# .....
160+
```
161+
162+
Now to generate the seed file we must run `$(npm bin)/knex seed:make todos`
163+
164+
```bash
165+
$ $(npm bin)/knex seed:make todos
166+
Using environment: development
167+
Using environment: development
168+
Using environment: development
169+
Created seed file: /Users/jaimesalaszancada/Documents/paths/ci_cd-path/04_gitlab_or/todo-app-react/data/seeds/todos.js
170+
```
171+
172+
```js
173+
exports.seed = function(knex) {
174+
// Deletes ALL existing entries
175+
return knex('todos').truncate()
176+
.then(function () {
177+
// Inserts seed entries
178+
return knex('todos').insert([
179+
{ title: 'Learn GitHub Actions', completed: false },
180+
{ title: 'Learn Groovy', completed: false },
181+
{ title: 'Learn EKS', completed: false },
182+
]);
183+
});
184+
};
185+
186+
```
187+
188+
To run the seed now we can do
189+
190+
```bash
191+
$ $(npm bin)/knex seed:run
192+
```
193+
194+
#### Migration References
195+
196+
> Quick Start Tutorial: http://perkframework.com/v1/guides/database-migrations-knex.html
197+
> Seeding a database with Knex: https://dev.to/cesareferrari/database-seeding-with-knex-51gf
198+
199+
## Running the Application with Docker on Local
200+
201+
```bash
202+
$ docker build -t jaimesalas/lc-todo-monolith .
203+
```
204+
205+
Create network
206+
207+
```bash
208+
$ docker create network lemoncode
209+
```
210+
211+
Start database
212+
213+
```bash
214+
docker run -d --network lemoncode -v todos:/var/lib/postgresql/data --name postgres postgres:10.4
215+
```
216+
217+
Start app without database
218+
219+
```bash
220+
$ docker run -d -p 3000:3000 \
221+
-e NODE_ENV=production \
222+
-e PORT=3000 \
223+
jaimesalas/lc-todo-monolith
224+
```
225+
226+
Start up with database
227+
228+
```bash
229+
$ docker run -d --network lemoncode -p 3000:3000 \
230+
-e NODE_ENV=production \
231+
-e PORT=3000 \
232+
-e DB_HOST=postgres \
233+
-e DB_USER=postgres \
234+
-e DB_PASSWORD=postgres \
235+
-e DB_PORT=5432 \
236+
-e DB_NAME=todos_db \
237+
-e DB_VERSION=10.4 \
238+
--name monolith \
239+
jaimesalas/lc-todo-monolith
240+
```
241+
242+
## Create stand alone database
243+
244+
```bash
245+
$ docker build -t jaimesalas/lc-todo-db -f Dockerfile.todos_db .
246+
```
247+
248+
```bash
249+
$ docker run -d -p 5432:5432 -v other_todos:/var/lib/postgresql/data --name lc-todo-db jaimesalas/lc-todo-db
250+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
exports.seed = function(knex) {
3+
// Deletes ALL existing entries
4+
return knex('todos').delete()
5+
.then(function () {
6+
// Inserts seed entries
7+
return knex('todos').insert([
8+
{ title: 'Learn GitHub Actions', completed: false},
9+
{ title: 'Learn Groovy', completed: false},
10+
{ title: 'Learn EKS', completed: false}
11+
]);
12+
});
13+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-react",
5+
"@babel/preset-typescript"
6+
]
7+
}

0 commit comments

Comments
 (0)