Skip to content

Commit aab8f51

Browse files
authored
config: docker database (#3)
* feat: added docker script to create a postgres_db * update: variable environments * change: mysql for postgres * feat: added assets for readme * update: assets and commands to initialize project
1 parent 283172b commit aab8f51

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
lines changed

.env.example

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
DRIVER=mysql
1+
PROJECT_NAME=api_sequelize
2+
3+
DRIVER=postgres
24
HOST=localhost
3-
USER=root
4-
PASS=
5-
BASE=api
5+
UNAME=caioagiani
6+
PWORD=root
7+
DBASE=api
68

79
HASH=jwt_key

.github/logo.png

24.1 KB
Loading

README.MD

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<img width="115" height="100%" src="https://www.elemental.co.za/blog/wp-content/uploads/2020/04/api-2x.png" alt="API RESTful"></a>
2+
<img width="115" height="100%" src=".github/logo.png" alt="API RESTful"></a>
33
</p>
44

55
<h3 align="center">API RESTful - MySQL Sequelize :: Sucrase</h3>
@@ -20,11 +20,15 @@ Pacotes principais: **Express** responsável pela criação de rotas, Middleware
2020
## Instalação:
2121

2222
- Instalar dependência: `yarn install` ou `npm install`
23+
- Configurar variáveis ambiente: `cp .env.example .env`
24+
- Permissão de execução o script database: `sudo chmod +x src/scripts/database.sh`
25+
- Executar script database para configurar o banco: `yarn dev:db`
2326
- Iniciar banco de dados: `yarn sequelize db:migrate` ou import `api.sql`
2427
- Iniciar aplicação em desenvolvimento: `yarn dev`
28+
- Iniciar aplicação em produção: `yarn start`
29+
2530
- Format eslint: `yarn dev:fix`
2631
- Buildar sucrase: `yarn dev:build`
27-
- Iniciar aplicação em produção: `yarn start`
2832

2933
## Rotas
3034

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
"dev": "nodemon -r dotenv/config src/app.js",
2424
"dev:build": "sucrase ./src -d ./dist --transforms imports",
2525
"dev:fix": "eslint --fix src --ext .js",
26+
"dev:db": "./src/scripts/database.sh",
2627
"dev:jest": "jest --setupFiles dotenv/config --detectOpenHandles --forceExit"
2728
},
2829
"dependencies": {
2930
"bcryptjs": "^2.4.3",
3031
"cors": "^2.8.5",
3132
"express": "^4.17.1",
3233
"jsonwebtoken": "^8.5.1",
33-
"mysql": "^2.18.1",
34-
"mysql2": "^2.1.0",
34+
"pg": "^8.5.1",
35+
"pg-hstore": "^2.3.3",
3536
"sequelize": "^5.21.12",
3637
"yup": "^0.29.1"
3738
},

src/config/database.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = {
22
dialect: process.env.DRIVER,
33
host: process.env.HOST,
4-
username: process.env.USER,
5-
password: process.env.PASS,
6-
database: process.env.BASE,
4+
username: process.env.UNAME,
5+
password: process.env.PWORD,
6+
database: process.env.DBASE,
77
define: {
88
timestamps: true,
99
underscored: true,

src/scripts/database.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ -f .env ]; then
5+
export $(cat .env | grep -v '#' | awk '/=/ {print $1}')
6+
fi
7+
8+
echo "stop & remove old docker and starting new instance of [$PROJECT_NAME]"
9+
(docker kill $PROJECT_NAME || :) && \
10+
(docker rm $PROJECT_NAME || :) && \
11+
docker run --name $PROJECT_NAME \
12+
-e POSTGRES_USER=$UNAME \
13+
-e POSTGRES_PASSWORD=$PWORD \
14+
-e PGPASSWORD=$PWORD \
15+
-p 5432:5432 \
16+
-d postgres
17+
18+
echo "wait for pg-server [$PROJECT_NAME] to start";
19+
sleep 3;
20+
21+
echo "CREATE DATABASE $DBASE ENCODING 'UTF-8';" | docker exec -i $PROJECT_NAME psql -U $UNAME
22+
23+
echo "\l" | docker exec -i $PROJECT_NAME psql -U $UNAME

0 commit comments

Comments
 (0)