Skip to content

Commit e6b8c0a

Browse files
adicionando funcionalidade de validação em banco de dados
1 parent da678a1 commit e6b8c0a

14 files changed

+1463
-29
lines changed

.env

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--postgresql
2+
DATABASE_URL=postgresql://testuser:testpass@localhost:5432/testdb
3+
4+
-- sqlserver
5+
MSSQL_USER=sa
6+
MSSQL_PASSWORD=yourStrong(!)Password
7+
MSSQL_SERVER=localhost
8+
MSSQL_DATABASE=testdb
9+
MSSQL_PORT=1433
10+
11+
-- mysql
12+
MYSQL_HOST=localhost
13+
MYSQL_PORT=3306
14+
MYSQL_DATABASE=testdb
15+
MYSQL_USER=testuser
16+
MYSQL_PASSWORD=testpass
17+

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
# Project Resume
1+
# Jest-Supertest Api Tests
22

3-
In this project, I'll show how we can use an architecture to simplify building, updating and finding any errors
4-
easily with this pattern I've built!
3+
## Conteúdo
4+
- [Jest-Supertest Api Tests](#jest-supertest-api-tests)
5+
- [Conteúdo](#conteúdo)
6+
- [Instalação e Uso da arquitetura](#instalação-e-uso-da-arquitetura)
7+
- [Uso](#uso)
8+
- [Arquivo de Configuração:](#arquivo-de-configuração)
9+
- [Versões necessárias para a execução do projeto:](#versões-necessárias-para-a-execução-do-projeto)
510

6-
## Table of Contents
711

8-
- [Project Resume](#project-resume)
9-
- [Table of Contents](#table-of-contents)
10-
- [Installation](#installation)
11-
- [Usage](#usage)
12-
- [How to contribute](#how-to-contribute)
13-
- [Contribution guidelines](#contribution-guidelines)
14-
- [Contact](#contact)
12+
## Instalação e Uso da arquitetura
1513

16-
## Installation
17-
18-
Run to install and use the project:
14+
Execute para instalar e usar o projeto:
1915

2016
`npm install --save-dev @types/jest @types/node`
2117

22-
## Usage
18+
## Uso
2319

24-
After you install the depencencies you can use the command below to run the tests:
20+
Após a instalação das dependências, é possível executar o projeto execxutando o comando:
2521

2622
`npm test`
2723

28-
## How to contribute
29-
1. Fork the project
30-
2. Create a branch for your contribution: git checkout -b feature/your-feature
31-
3. Make your changes and commit: git commit -m 'Add amazing feature'
32-
4. Push to your fork: git push origin feature/your-feature
33-
5. Open a Pull Request to the main branch of the project
24+
## Arquivo de Configuração:
3425

35-
## Contribution guidelines
26+
`constants.ts`
3627

37-
- Add new tests
38-
- Follow the existing code style
28+
## Versões necessárias para a execução do projeto:
29+
- dependencies :
3930

40-
## Contact
31+
"@types/supertest": "^6.0.2",
32+
"jest": "^29.7.0",
33+
"jest-html-reporter": "^3.10.2",
34+
"ts-node": "^10.9.2"
35+
4136

42-
If you're interested in finding out more, don't hesitate to contact me via linkedin!
37+
- devDependencies:
4338

44-
www.linkedin.com/in/alexalexandrealves
39+
"@jest/globals": "^29.7.0",
40+
"@types/jest": "^29.5.14",
41+
"@types/node": "^22.10.6",
42+
"supertest": "^6.3.4",
43+
"ts-jest": "^29.1.2",
44+
"typescript": "^5.3.3"

db/mssql-connection.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as sql from 'mssql';
2+
import * as dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const config: sql.config = {
7+
user: process.env.MSSQL_USER,
8+
password: process.env.MSSQL_PASSWORD,
9+
server: process.env.MSSQL_SERVER || 'localhost',
10+
database: process.env.MSSQL_DATABASE,
11+
port: Number(process.env.MSSQL_PORT) || 1433,
12+
options: {
13+
encrypt: false, // true se estiver usando Azure
14+
trustServerCertificate: true, // necessário para conexões locais
15+
},
16+
};
17+
18+
let pool: sql.ConnectionPool;
19+
20+
export async function getMSSQLConnection(): Promise<sql.ConnectionPool> {
21+
if (!pool) {
22+
pool = await sql.connect(config);
23+
}
24+
return pool;
25+
}

db/mysql-connection.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as mysql from 'mysql2/promise';
2+
import * as dotenv from 'dotenv';
3+
4+
5+
dotenv.config();
6+
7+
export const mysqlPool = mysql.createPool({
8+
host: process.env.MYSQL_HOST,
9+
port: Number(process.env.MYSQL_PORT),
10+
database: process.env.MYSQL_DATABASE,
11+
user: process.env.MYSQL_USER,
12+
password: process.env.MYSQL_PASSWORD,
13+
});

db/postgres-connection.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Pool } from 'pg';
2+
import * as dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
export const pool = new Pool({
7+
connectionString: process.env.DATABASE_URL,
8+
});
9+
10+
export async function testConnection() {
11+
const client = await pool.connect();
12+
try {
13+
const res = await client.query('SELECT NOW()');
14+
console.log('Connected to DB at:', res.rows[0].now);
15+
} catch (err) {
16+
console.error('DB connection error:', err);
17+
} finally {
18+
client.release();
19+
}
20+
}

docker-compose.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: '3.8'
2+
3+
services:
4+
postgres:
5+
image: postgres:16
6+
container_name: postgres_test
7+
ports:
8+
- "5432:5432"
9+
environment:
10+
POSTGRES_USER: testuser
11+
POSTGRES_PASSWORD: testpass
12+
POSTGRES_DB: testdb
13+
volumes:
14+
- pgdata:/var/lib/postgresql/data
15+
- ./init-postgres.sql:/docker-entrypoint-initdb.d/init.sql
16+
17+
mssql:
18+
image: mcr.microsoft.com/mssql/server:2022-latest
19+
container_name: mssql_test
20+
ports:
21+
- "1433:1433"
22+
environment:
23+
ACCEPT_EULA: "Y"
24+
SA_PASSWORD: "YourStrong(!)Password"
25+
volumes:
26+
- mssql_data:/var/opt/mssql
27+
- ./init-mssql.sql:/init-mssql.sql
28+
command: /bin/bash -c "sleep 20 && /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'YourStrong(!)Password' -i /init-mssql.sql"
29+
30+
mysql:
31+
image: mysql:8.0
32+
container_name: mysql_test
33+
ports:
34+
- "3306:3306"
35+
environment:
36+
MYSQL_ROOT_PASSWORD: rootpass
37+
MYSQL_DATABASE: testdb
38+
MYSQL_USER: testuser
39+
MYSQL_PASSWORD: testpass
40+
volumes:
41+
- mysql_data:/var/lib/mysql
42+
- ./init-mysql.sql:/docker-entrypoint-initdb.d/init.sql
43+
44+
volumes:
45+
pgdata:
46+
mssql_data:
47+
mysql_data:

init-mssql.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE DATABASE testdb;
2+
GO
3+
4+
USE testdb;
5+
GO
6+
7+
CREATE TABLE users (
8+
id INT PRIMARY KEY IDENTITY(1,1),
9+
name NVARCHAR(100),
10+
age INT,
11+
city NVARCHAR(100)
12+
);
13+
GO
14+
15+
INSERT INTO users (name, age, city) VALUES
16+
('Ana Paula', 28, 'São Paulo'),
17+
('Carlos Mendes', 35, 'Rio de Janeiro'),
18+
('Mariana Lima', 22, 'Belo Horizonte');
19+
GO

init-mysql.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE IF NOT EXISTS users (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(100),
4+
age INT,
5+
city VARCHAR(100)
6+
);
7+
8+
INSERT INTO users (name, age, city) VALUES
9+
('Ana Paula', 28, 'São Paulo'),
10+
('Carlos Mendes', 35, 'Rio de Janeiro'),
11+
('Mariana Lima', 22, 'Belo Horizonte');

init.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
name VARCHAR(100),
4+
age INTEGER,
5+
city VARCHAR(100)
6+
);
7+
8+
INSERT INTO users (name, age, city) VALUES
9+
('Ana Souza', 28, 'São Paulo'),
10+
('Carlos Mendes', 35, 'Rio de Janeiro'),
11+
('Mariana Lima', 22, 'Belo Horizonte');

0 commit comments

Comments
 (0)